Files
oak-gpui/examples/prelude.rs
T
Nate Butler 18aeb44a54 Start reorganizing examples (#10)
* Update crate name

* wip

* reduce to just the gpui crate

* Fix misisng example images

* Delete ownership_post.rs

* Update Cargo.toml

* Update README.md

* bump to 0.3.3

* organize

* organize

* Add more learn examples

* Delete _ownership_and_data_flow.rs

* wip learn examples

* Fix colors

* tidy

* Move _template up

* add animation example

* Update examples

* remove legacy examples from toml

* Delete examples.toml
2025-12-28 23:14:33 -05:00

42 lines
1.2 KiB
Rust

//! Example Prelude
//!
//! Common helpers for GPUI examples. Import with:
//!
//! ```ignore
//! #[path = "../prelude.rs"]
//! mod example_prelude;
//! use example_prelude::init_example;
//! ```
use gpui::{App, KeyBinding, Menu, MenuItem, SharedString, actions};
actions!(example, [Quit, CloseWindow]);
/// Sets up common example boilerplate:
/// - Activates the application
/// - Sets up an app menu with the example name and a Quit action (cmd-q)
/// - Configures the app to quit when all windows are closed
pub fn init_example(cx: &mut App, name: impl Into<SharedString>) {
// Bring the example window to the front
cx.activate(true);
// Define the quit action...
cx.on_action(|_: &Quit, cx| cx.quit());
// ...then bind it to cmd+q
cx.bind_keys([KeyBinding::new("cmd-q", Quit, None)]);
// Set up an app menu with the example name and a Quit action (cmd-q)
cx.set_menus(vec![Menu {
name: name.into(),
items: vec![MenuItem::action("Quit", Quit)],
}]);
// Quit the app when all windows are closed
cx.on_window_closed(|cx| {
if cx.windows().is_empty() {
cx.quit();
}
})
.detach();
}