App
App contains the state of your whole application, it allows you to control many aspects of the application's functionality such as managing the state of Entity's.
Opening a Window
Using App you can access the open_window function which takes a WindowOptions and a callback which supplies mutable references to a Window and App that is used to build the root view. To learn more on how views are created, you can read the Render section.
use gpui::{Application, WindowOptions};
fn main() {
Application::new().run(|app| {
app.open_window(WindowOptions::default(), |window, app| {
// Return root view
})
.unwrap();
});
}
On Action
Using App you can use on_action function to bind a callback to the firing of a action globally throughout your application.
use gpui::{
AppContext, Application, Context, Empty, IntoElement, Render, Window, WindowOptions, actions,
};
actions!(actions_namespace, [Enter]);
struct RootView;
impl Render for RootView {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
Empty
}
}
fn main() {
Application::new().run(|app| {
app.open_window(WindowOptions::default(), |_window, app| {
app.bind_keys([gpui::KeyBinding::new("enter", Enter, None)]);
app.on_action(|&Enter, _app| println!("Enter key hit!"));
app.new(|_cx| RootView)
})
.unwrap();
});
}
Spawn
Using App you can use the spawn function to enqueue a future on the main thread, it takes a AsyncFnOnce which will provide AsyncApp when the async closure is invoked. The AsyncApp allows you to access application state.
use gpui::Application;
fn main() {
Application::new().run(|app| {
app.spawn(async |app| {
// Some asynchronous work
})
.detach();
});
}