From c86b0222ce49e3cb33f41d63834b0c1ad6177f59 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Thu, 14 Aug 2025 11:15:55 +0800 Subject: [PATCH] example: Add a HelloWorld example. (#1133) Ref #1132 to make a minimized app --- crates/story/examples/hello_world.rs | 78 ++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 crates/story/examples/hello_world.rs diff --git a/crates/story/examples/hello_world.rs b/crates/story/examples/hello_world.rs new file mode 100644 index 00000000..30821917 --- /dev/null +++ b/crates/story/examples/hello_world.rs @@ -0,0 +1,78 @@ +use gpui::*; +use gpui_component::{ + button::{Button, ButtonVariants}, + Root, StyledExt, +}; + +pub struct HelloWorld; + +impl Render for HelloWorld { + fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { + div() + .v_flex() + .gap_2() + .size_full() + .items_center() + .justify_center() + .text_center() + .child("Hello, World!") + .child( + Button::new("ok") + .primary() + .label("Let's Go!") + .on_click(|_, _, _| println!("Clicked!")), + ) + } +} + +fn main() { + let app = Application::new(); + + app.run(move |cx| { + gpui_component::init(cx); + + cx.activate(true); + + let mut window_size = size(px(640.), px(480.)); + if let Some(display) = cx.primary_display() { + let display_size = display.bounds().size; + window_size.width = window_size.width.min(display_size.width * 0.85); + window_size.height = window_size.height.min(display_size.height * 0.85); + } + let window_bounds = Bounds::centered(None, window_size, cx); + + cx.spawn(async move |cx| { + let options = WindowOptions { + window_bounds: Some(WindowBounds::Windowed(window_bounds)), + titlebar: None, + window_min_size: Some(gpui::Size { + width: px(640.), + height: px(480.), + }), + kind: WindowKind::Normal, + #[cfg(target_os = "linux")] + window_background: gpui::WindowBackgroundAppearance::Transparent, + #[cfg(target_os = "linux")] + window_decorations: Some(gpui::WindowDecorations::Client), + ..Default::default() + }; + + let window = cx + .open_window(options, |window, cx| { + let view = cx.new(|_| HelloWorld); + cx.new(|cx| Root::new(view.into(), window, cx)) + }) + .expect("failed to open window"); + + window + .update(cx, |_, window, _| { + window.activate_window(); + window.set_window_title("Example"); + }) + .expect("failed to update window"); + + Ok::<_, anyhow::Error>(()) + }) + .detach(); + }); +}