zng_wgt_input/
cmd.rs

1//! Common commands.
2//!
3
4use zng_app::event::CommandArgs;
5use zng_ext_clipboard::{COPY_CMD, CUT_CMD, PASTE_CMD};
6use zng_wgt::{ICONS, prelude::*};
7
8command! {
9    /// Represents the **new** action.
10    ///
11    /// The command parameter can identify the new item type, otherwise the default (or single) type
12    /// must be used.
13    pub static NEW_CMD {
14        l10n!: true,
15        name: "New",
16        shortcut: [shortcut!(CTRL + 'N'), shortcut!(New)],
17    };
18
19    /// Represents the **open** action.
20    ///
21    /// The command parameter can be an item path to open (like a `PathBuf`), otherwise the
22    /// command implementer must identify the item, either by context or by prompting the user.
23    pub static OPEN_CMD {
24        l10n!: true,
25        name: "Open…",
26        shortcut: [shortcut!(CTRL + 'O'), shortcut!(Open)],
27        icon: wgt_fn!(|_| ICONS.get("file-open")),
28    };
29
30    /// Represents the **save** action.
31    ///
32    /// Usually this saves to the already defined item path (open or previous save path),
33    /// otherwise the user is prompted like [`SAVE_AS_CMD`].
34    pub static SAVE_CMD {
35        l10n!: true,
36        name: "Save",
37        shortcut: [shortcut!(CTRL + 'S'), shortcut!(Save)],
38        icon: wgt_fn!(|_| ICONS.get("save")),
39    };
40
41    /// Represents the **save-as** action.
42    ///
43    /// Usually this prompts the user for a save path, even if a previous path is already known.
44    pub static SAVE_AS_CMD {
45        l10n!: true,
46        name: "Save As…",
47        shortcut: [shortcut!(CTRL | SHIFT + 'S')],
48    };
49
50    /// Represents the **context menu open** action.
51    pub static CONTEXT_MENU_CMD {
52        shortcut: [shortcut!(SHIFT + F10), shortcut!(ContextMenu)],
53        icon: wgt_fn!(|_| ICONS.get(["context-menu", "menu-open"])),
54    };
55
56    /// Represents the **open settings** action.
57    ///
58    /// Settings is an editor for a selection of app configs.
59    ///
60    /// # Parameter
61    ///
62    /// The parameter can be a `Txt` that can match a `ConfigKey` or config metadata
63    /// such as the display name or description.
64    pub static SETTINGS_CMD {
65        l10n!: true,
66        name: "Settings",
67        shortcut: [shortcut!(CTRL + ',')],
68        icon: wgt_fn!(|_| ICONS.get("settings")),
69    };
70}
71
72command_property! {
73    /// On new command.
74    ///
75    /// Receives [`NEW_CMD`] command events scoped on the widget. The command parameter can be
76    /// the new item type identifier.
77    #[property(EVENT)]
78    pub fn on_new<on_pre_new, can_new>(child: impl IntoUiNode, handler: Handler<CommandArgs>) -> UiNode {
79        NEW_CMD
80    }
81
82    /// On open command.
83    ///
84    /// Receives [`OPEN_CMD`] command events scoped on the widget. The command parameter can be
85    /// a path to open, otherwise the path must be derived from context or the user prompted.
86    #[property(EVENT)]
87    pub fn on_open<on_pre_open, can_open>(child: impl IntoUiNode, handler: Handler<CommandArgs>) -> UiNode {
88        OPEN_CMD
89    }
90
91    /// On save command.
92    ///
93    /// Receives [`SAVE_CMD`] command events scoped on the widget. Usually saves to the last
94    /// open or save path, otherwise prompt the user like [`on_save_as`].
95    ///
96    /// [`on_save_as`]: fn@on_save_as
97    #[property(EVENT)]
98    pub fn on_save<on_pre_save, can_save>(child: impl IntoUiNode, handler: Handler<CommandArgs>) -> UiNode {
99        SAVE_CMD
100    }
101
102    /// On save-as command.
103    ///
104    /// Receives [`SAVE_AS_CMD`] command events scoped on the widget. Usually prompts the user for
105    /// a new save path.
106    #[property(EVENT)]
107    pub fn on_save_as<on_pre_save_as, can_save_as>(child: impl IntoUiNode, handler: Handler<CommandArgs>) -> UiNode {
108        SAVE_AS_CMD
109    }
110
111    /// On cut command.
112    ///
113    /// Receives [`CUT_CMD`] command events scoped on the widget. You can use the `CLIPBOARD` service
114    /// to send data to the clipboard.
115    ///
116    /// [`CUT_CMD`]: zng_ext_clipboard::CUT_CMD
117    #[property(EVENT)]
118    pub fn on_cut<on_pre_cut, can_cut>(child: impl IntoUiNode, handler: Handler<CommandArgs>) -> UiNode {
119        CUT_CMD
120    }
121
122    /// On copy command.
123    ///
124    /// Receives [`COPY_CMD`] command events scoped on the widget. You can use the `CLIPBOARD` service
125    /// to send data to the clipboard.
126    ///
127    /// [`COPY_CMD`]: zng_ext_clipboard::COPY_CMD
128    #[property(EVENT)]
129    pub fn on_copy<on_pre_copy, can_copy>(child: impl IntoUiNode, handler: Handler<CommandArgs>) -> UiNode {
130        COPY_CMD
131    }
132
133    /// On paste command.
134    ///
135    /// Receives [`PASTE_CMD`] command events scoped on the widget. You can use the `CLIPBOARD` service
136    /// to receive data from the clipboard.
137    ///
138    /// [`PASTE_CMD`]: zng_ext_clipboard::PASTE_CMD
139    #[property(EVENT)]
140    pub fn on_paste<on_pre_paste, can_paste>(child: impl IntoUiNode, handler: Handler<CommandArgs>) -> UiNode {
141        PASTE_CMD
142    }
143
144    /// On settings command.
145    ///
146    /// Receives [`SETTINGS_CMD`] command events scoped on the widget.
147    #[property(EVENT)]
148    pub fn on_settings<on_pre_settings, can_settings>(child: impl IntoUiNode, handler: Handler<CommandArgs>) -> UiNode {
149        SETTINGS_CMD
150    }
151}