ori_app/command.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
use ori_core::{
view::{BoxedView, View},
window::{Window, WindowId},
};
/// Commands that can be sent to the application.
///
/// # Example
/// ```no_run
/// # use ori_core::{view::View, views::*, context::*};
/// # use ori_app::AppCommand;
/// fn ui() -> impl View {
/// // Here we create a button that quits the application when clicked.
/// on_click(
/// button(text("Quit")),
/// |cx, _| cx.cmd(AppCommand::Quit),
/// )
/// }
/// ```
pub enum AppCommand {
/// Open a new window.
OpenWindow(Window, Box<dyn FnMut() -> BoxedView<()> + Send>),
/// Close a window.
CloseWindow(WindowId),
/// Drag a window.
DragWindow(WindowId),
/// Quit the application.
Quit,
}
impl AppCommand {
/// Convenience method to open a window with a view.
///
/// Note that `V` must implement `View<()>`, and therefore cannot access the data of the
/// application. If you need to access the data, you can implement an [`AppDelegate`](crate::AppDelegate) instead.
///
/// # Example
/// ```no_run
/// # use ori_core::{view::View, views::*, context::*, window::Window};
/// # use ori_app::AppCommand;
/// fn ui() -> impl View {
/// // Here we create a button that opens a new window when clicked.
/// on_click(
/// button(text("Open new window")),
/// |cx, _| {
/// let window = Window::new()
/// .title("New window");
///
/// cx.cmd(AppCommand::open_window(window, popup));
/// },
/// )
/// }
///
/// fn popup() -> impl View {
/// text("Hello, world!")
/// }
/// ```
pub fn open_window<V: View + 'static>(
window: Window,
mut view: impl FnMut() -> V + Send + 'static,
) -> Self {
Self::OpenWindow(window, Box::new(move || Box::new(view())))
}
}