layer_shell.rs

  1#![cfg_attr(target_family = "wasm", no_main)]
  2
  3fn run_example() {
  4    #[cfg(all(target_os = "linux", feature = "wayland"))]
  5    example::main();
  6
  7    #[cfg(not(all(target_os = "linux", feature = "wayland")))]
  8    panic!("This example requires the `wayland` feature and a linux system.");
  9}
 10
 11#[cfg(not(target_family = "wasm"))]
 12fn main() {
 13    run_example();
 14}
 15
 16#[cfg(target_family = "wasm")]
 17#[wasm_bindgen::prelude::wasm_bindgen(start)]
 18pub fn start() {
 19    gpui_platform::web_init();
 20    run_example();
 21}
 22
 23#[cfg(all(target_os = "linux", feature = "wayland"))]
 24mod example {
 25    use std::time::{Duration, SystemTime, UNIX_EPOCH};
 26
 27    use gpui::{
 28        App, Bounds, Context, FontWeight, Size, Window, WindowBackgroundAppearance, WindowBounds,
 29        WindowKind, WindowOptions, div, layer_shell::*, point, prelude::*, px, rems, rgba, white,
 30    };
 31    use gpui_platform::application;
 32
 33    struct LayerShellExample;
 34
 35    impl LayerShellExample {
 36        fn new(cx: &mut Context<Self>) -> Self {
 37            cx.spawn(async move |this, cx| {
 38                loop {
 39                    let _ = this.update(cx, |_, cx| cx.notify());
 40                    cx.background_executor()
 41                        .timer(Duration::from_millis(500))
 42                        .await;
 43                }
 44            })
 45            .detach();
 46
 47            LayerShellExample
 48        }
 49    }
 50
 51    impl Render for LayerShellExample {
 52        fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
 53            let now = SystemTime::now()
 54                .duration_since(UNIX_EPOCH)
 55                .unwrap()
 56                .as_secs();
 57
 58            let hours = (now / 3600) % 24;
 59            let minutes = (now / 60) % 60;
 60            let seconds = now % 60;
 61
 62            div()
 63                .size_full()
 64                .flex()
 65                .items_center()
 66                .justify_center()
 67                .text_size(rems(4.5))
 68                .font_weight(FontWeight::EXTRA_BOLD)
 69                .text_color(white())
 70                .bg(rgba(0x0000044))
 71                .rounded_xl()
 72                .child(format!("{:02}:{:02}:{:02}", hours, minutes, seconds))
 73        }
 74    }
 75
 76    pub fn main() {
 77        application().run(|cx: &mut App| {
 78            cx.open_window(
 79                WindowOptions {
 80                    titlebar: None,
 81                    window_bounds: Some(WindowBounds::Windowed(Bounds {
 82                        origin: point(px(0.), px(0.)),
 83                        size: Size::new(px(500.), px(200.)),
 84                    })),
 85                    app_id: Some("gpui-layer-shell-example".to_string()),
 86                    window_background: WindowBackgroundAppearance::Transparent,
 87                    kind: WindowKind::LayerShell(LayerShellOptions {
 88                        namespace: "gpui".to_string(),
 89                        anchor: Anchor::LEFT | Anchor::RIGHT | Anchor::BOTTOM,
 90                        margin: Some((px(0.), px(0.), px(40.), px(0.))),
 91                        keyboard_interactivity: KeyboardInteractivity::None,
 92                        ..Default::default()
 93                    }),
 94                    ..Default::default()
 95                },
 96                |_, cx| cx.new(LayerShellExample::new),
 97            )
 98            .unwrap();
 99        });
100    }
101}