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