example: Add a HelloWorld example. (#1133)

Ref #1132 to make a minimized app
This commit is contained in:
Jason Lee 2025-08-14 11:15:55 +08:00 committed by GitHub
parent 37de698a61
commit c86b0222ce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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<Self>) -> 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();
});
}