mgui_runtime/app/
commands.rs1use std::collections::HashMap;
11
12use mgui_app_lifecycle::FeatureLifecycle;
13use mgui_core::element::ElementTree;
14use mgui_shell::WindowShell;
15
16#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
18pub struct CommandId(pub u64);
19
20impl CommandId {
21 pub const fn new(id: u64) -> Self {
22 Self(id)
23 }
24}
25
26#[derive(Clone, Copy, Debug, PartialEq, Eq)]
28pub enum CommandSource {
29 Menu,
30 TrayMenu,
31 TrayClick,
32 GlobalShortcut,
33 SecondInstance,
34}
35
36impl CommandSource {
37 pub fn as_str(self) -> &'static str {
38 match self {
39 Self::Menu => "menu",
40 Self::TrayMenu => "tray_menu",
41 Self::TrayClick => "tray_click",
42 Self::GlobalShortcut => "global_shortcut",
43 Self::SecondInstance => "second_instance",
44 }
45 }
46}
47
48#[derive(Clone, Copy, Debug, PartialEq, Eq)]
50pub enum CommandOutcome {
51 Consumed,
52 PassThrough,
53}
54
55pub struct CommandScope<'a> {
57 shell: &'a dyn WindowShell,
58 tree: &'a mut ElementTree,
59 lifecycle: Option<&'a FeatureLifecycle>,
60 args: &'a [String],
61 source: CommandSource,
62 needs_redraw: Option<&'a mut bool>,
63 needs_rebuild: Option<&'a mut bool>,
64}
65
66impl<'a> CommandScope<'a> {
67 pub fn new(
70 shell: &'a dyn WindowShell,
71 tree: &'a mut ElementTree,
72 lifecycle: Option<&'a FeatureLifecycle>,
73 args: &'a [String],
74 source: CommandSource,
75 needs_redraw: Option<&'a mut bool>,
76 needs_rebuild: Option<&'a mut bool>,
77 ) -> Self {
78 Self { shell, tree, lifecycle, args, source, needs_redraw, needs_rebuild }
79 }
80
81 pub fn shell(&self) -> &dyn WindowShell {
82 self.shell
83 }
84
85 pub fn tree_mut(&mut self) -> &mut ElementTree {
86 self.tree
87 }
88
89 pub fn lifecycle(&self) -> Option<&FeatureLifecycle> {
90 self.lifecycle
91 }
92
93 pub fn source(&self) -> CommandSource {
94 self.source
95 }
96
97 pub fn args(&self) -> &[String] {
99 self.args
100 }
101
102 pub fn request_redraw(&mut self) {
103 if let Some(flag) = self.needs_redraw.as_deref_mut() {
104 *flag = true;
105 }
106 }
107
108 pub fn request_rebuild(&mut self) {
109 if let Some(flag) = self.needs_rebuild.as_deref_mut() {
110 *flag = true;
111 }
112 self.request_redraw();
113 }
114}
115
116#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
118pub struct CommandStats {
119 pub delivered: u64,
120 pub consumed: u64,
121 pub passthrough: u64,
122 pub menu: u64,
123 pub tray_menu: u64,
124 pub tray_click: u64,
125 pub global_shortcut: u64,
126 pub second_instance: u64,
127}
128
129type CommandHandler = Box<dyn FnMut(&mut CommandScope<'_>) -> CommandOutcome>;
130
131#[derive(Default)]
133pub struct CommandRouter {
134 handlers: HashMap<CommandId, CommandHandler>,
135 stats: CommandStats,
136}
137
138impl std::fmt::Debug for CommandRouter {
139 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
140 f.debug_struct("CommandRouter").field("registered", &self.handlers.len()).field("stats", &self.stats).finish()
141 }
142}
143
144impl CommandRouter {
145 pub fn new() -> Self {
146 Self::default()
147 }
148
149 pub fn register<F>(&mut self, id: CommandId, handler: F) -> &mut Self
151 where
152 F: FnMut(&mut CommandScope<'_>) -> CommandOutcome + 'static,
153 {
154 self.handlers.insert(id, Box::new(handler));
155 self
156 }
157
158 pub fn registered(&self) -> usize {
159 self.handlers.len()
160 }
161
162 pub fn stats(&self) -> CommandStats {
163 self.stats
164 }
165
166 pub fn dispatch(&mut self, scope: &mut CommandScope<'_>, id: CommandId) -> CommandOutcome {
168 let source = scope.source;
169 self.stats.delivered += 1;
170 match source {
171 CommandSource::Menu => self.stats.menu += 1,
172 CommandSource::TrayMenu => self.stats.tray_menu += 1,
173 CommandSource::TrayClick => self.stats.tray_click += 1,
174 CommandSource::GlobalShortcut => self.stats.global_shortcut += 1,
175 CommandSource::SecondInstance => self.stats.second_instance += 1,
176 }
177 match self.handlers.get_mut(&id) {
178 Some(handler) => {
179 let outcome = handler(scope);
180 if outcome == CommandOutcome::Consumed {
181 self.stats.consumed += 1;
182 } else {
183 self.stats.passthrough += 1;
184 }
185 outcome
186 }
187 None => {
188 self.stats.passthrough += 1;
189 CommandOutcome::PassThrough
190 }
191 }
192 }
193}