1use std::path::PathBuf;
10
11use mgui_core::element::ElementTree;
12use mgui_core::{FramePaint, Size};
13
14use crate::renderer::{PresentStats, RenderStats};
15use crate::task::TaskExecutor;
16use crate::ImageTextureStats;
17
18pub mod commands;
19pub use commands::{CommandId, CommandOutcome, CommandRouter, CommandScope, CommandSource, CommandStats};
20
21pub use mgui_shell::{
22 ClipboardHandle, ClipboardImage, FileDialogOptions, FileDragOptions, FileDragOutcome, GlobalShortcut, IconImage,
23 ImeEvent, NativeMenu, NativeMenuBar, NativeMenuCommand, NativeMenuController, NativeMenuItem, Notification,
24 NotificationLevel, NotificationTimeout, ResizeDirection, ShellEvent, ShellMetrics, Shortcut, ShortcutKey,
25 SingleInstanceConfig, TrayConfig, TrayEvent, TrayMenuItem, WindowCommand, WindowCommandBus, WindowCorners,
26 WindowIcon, WindowShell, WindowState,
27};
28
29#[derive(Clone, Debug)]
35pub struct RunnerConfig {
36 pub title: String,
37 pub inner_size: Size,
39 pub physical_size: bool,
41 pub prefer_software: bool,
43 pub startup_smoke: bool,
45 pub decorations: bool,
47 pub window_icon: Option<WindowIcon>,
49 pub app_id: Option<String>,
51 pub window_state_path: Option<PathBuf>,
54 pub single_instance: Option<SingleInstanceConfig>,
56 pub tray: Option<TrayConfig>,
58 pub native_menu: Option<NativeMenuBar>,
61 pub global_shortcuts: Vec<GlobalShortcut>,
63}
64
65impl RunnerConfig {
66 pub fn new(title: impl Into<String>, width: f32, height: f32) -> Self {
67 Self {
68 title: title.into(),
69 inner_size: Size::new(width, height),
70 physical_size: false,
71 prefer_software: false,
72 startup_smoke: false,
73 decorations: true,
74 window_icon: None,
75 app_id: None,
76 window_state_path: None,
77 single_instance: None,
78 tray: None,
79 native_menu: None,
80 global_shortcuts: Vec::new(),
81 }
82 }
83
84 pub fn with_physical_size(mut self, physical: bool) -> Self {
86 self.physical_size = physical;
87 self
88 }
89
90 pub fn with_prefer_software(mut self, prefer: bool) -> Self {
92 self.prefer_software = prefer;
93 self
94 }
95
96 pub fn with_startup_smoke(mut self, smoke: bool) -> Self {
98 self.startup_smoke = smoke;
99 self
100 }
101
102 pub fn with_decorations(mut self, decorations: bool) -> Self {
105 self.decorations = decorations;
106 self
107 }
108
109 pub fn with_window_icon(mut self, icon: WindowIcon) -> Self {
111 self.window_icon = Some(icon);
112 self
113 }
114
115 pub fn with_app_id(mut self, id: impl Into<String>) -> Self {
117 self.app_id = Some(id.into());
118 self
119 }
120
121 pub fn with_window_state_path(mut self, path: impl Into<PathBuf>) -> Self {
123 self.window_state_path = Some(path.into());
124 self
125 }
126
127 pub fn with_single_instance(mut self, id: impl Into<String>) -> Self {
130 self.single_instance = Some(SingleInstanceConfig::new(id));
131 self
132 }
133
134 pub fn with_single_instance_config(mut self, config: SingleInstanceConfig) -> Self {
136 self.single_instance = Some(config);
137 self
138 }
139
140 pub fn with_tray(mut self, tray: TrayConfig) -> Self {
145 self.tray = Some(tray);
146 self
147 }
148
149 pub fn with_native_menu(mut self, menu: NativeMenuBar) -> Self {
152 self.native_menu = Some(menu);
153 self
154 }
155
156 pub fn with_global_shortcuts(mut self, shortcuts: Vec<GlobalShortcut>) -> Self {
158 self.global_shortcuts = shortcuts;
159 self
160 }
161
162 pub fn with_global_shortcut(mut self, shortcut: GlobalShortcut) -> Self {
164 self.global_shortcuts.push(shortcut);
165 self
166 }
167}
168
169pub trait SoftwareBackend {
174 fn present(&mut self, frame: &FramePaint) -> Result<(), String>;
176 fn set_scale_factor(&mut self, scale_factor: f32);
177 fn set_logical_size(&mut self, size: Size);
178}
179
180#[doc(hidden)]
182pub trait RunnerBackend {
183 fn is_software(&self) -> bool;
184 fn image_texture_stats(&self) -> ImageTextureStats;
185 fn resolve_gpu_profile(&mut self) -> Option<u32>;
186 fn render_stats(&self) -> RenderStats;
188 fn present_stats(&self) -> PresentStats {
194 PresentStats::default()
195 }
196}
197
198pub struct AppContext<'a> {
202 pub shell: &'a dyn WindowShell,
204 backend: &'a mut dyn RunnerBackend,
205 pub tasks: &'a TaskExecutor,
207}
208
209impl<'a> AppContext<'a> {
210 #[doc(hidden)]
212 pub fn new(shell: &'a dyn WindowShell, backend: &'a mut dyn RunnerBackend, tasks: &'a TaskExecutor) -> Self {
213 Self { shell, backend, tasks }
214 }
215}
216
217impl AppContext<'_> {
218 pub fn is_software(&self) -> bool {
219 self.backend.is_software()
220 }
221
222 pub fn metrics(&self) -> ShellMetrics {
223 self.shell.metrics()
224 }
225
226 pub fn scale_factor(&self) -> f32 {
228 self.metrics().scale_factor
229 }
230
231 pub fn logical_size(&self) -> Size {
233 self.metrics().logical_size
234 }
235
236 pub fn request_redraw(&self) {
238 self.shell.request_redraw();
239 }
240
241 pub fn image_texture_stats(&self) -> ImageTextureStats {
243 self.backend.image_texture_stats()
244 }
245
246 pub fn resolve_gpu_profile(&mut self) -> Option<u32> {
248 self.backend.resolve_gpu_profile()
249 }
250
251 pub fn render_stats(&self) -> RenderStats {
253 self.backend.render_stats()
254 }
255
256 pub fn present_stats(&self) -> PresentStats {
262 self.backend.present_stats()
263 }
264}
265
266pub trait RunnerApp {
275 fn tree_mut(&mut self) -> &mut ElementTree;
277
278 fn mark_redraw(&mut self);
280
281 fn mark_rebuild(&mut self) {
284 self.mark_redraw();
285 }
286
287 fn frame(&mut self, ctx: &mut AppContext<'_>) -> Option<FramePaint>;
296
297 fn after_present(&mut self, _ctx: &mut AppContext<'_>) {}
299
300 fn on_close_requested(&mut self, _ctx: &mut AppContext<'_>) -> bool {
304 true
305 }
306
307 fn on_event(&mut self, _ctx: &mut AppContext<'_>, _event: &ShellEvent) {}
311
312 fn on_tray_event(&mut self, _ctx: &mut AppContext<'_>, _event: &TrayEvent) {}
316
317 fn on_native_menu_command(&mut self, _ctx: &mut AppContext<'_>, _id: u64) {}
321
322 fn on_second_instance(&mut self, _ctx: &mut AppContext<'_>, _args: &[String]) {}
327
328 fn on_global_shortcut(&mut self, _ctx: &mut AppContext<'_>, _id: u64) {}
332
333 fn on_global_shortcut_registered(&mut self, _ctx: &mut AppContext<'_>, _id: u64, _result: Result<(), String>) {}
337
338 fn on_resized(&mut self, _ctx: &mut AppContext<'_>, _logical_size: Size) {}
340
341 fn on_scale_factor_changed(&mut self, _ctx: &mut AppContext<'_>) {}
343
344 fn on_about_to_wait(&mut self, _ctx: &mut AppContext<'_>) -> bool {
346 false
347 }
348
349 fn next_wake(&mut self, _ctx: &mut AppContext<'_>) -> Option<std::time::Duration> {
354 None
355 }
356}
357
358#[cfg(test)]
359mod tests {
360 use super::*;
361
362 #[test]
363 fn tray_is_opt_in_and_preserves_config() {
364 let config = RunnerConfig::new("test", 320.0, 200.0);
365 assert!(config.decorations, "默认使用系统标题栏");
366 assert!(config.single_instance.is_none(), "默认允许多实例");
367 assert!(config.tray.is_none(), "默认不创建托盘");
368 assert!(config.native_menu.is_none(), "默认不安装原生菜单");
369 assert!(config.global_shortcuts.is_empty(), "默认无全局快捷键");
370
371 let tray = TrayConfig::new("mgui")
372 .with_menu_item(TrayMenuItem::item(1, "Show"))
373 .with_menu_item(TrayMenuItem::separator());
374 let menu = NativeMenuBar::new().with_menu(
375 NativeMenu::new("File")
376 .with_item(NativeMenuCommand::new(1, "Quit").with_shortcut(Shortcut::command(ShortcutKey::Char('q')))),
377 );
378 let global = GlobalShortcut::new(7, Shortcut::ctrl_alt(ShortcutKey::Char('m')));
379 let config = config
380 .with_decorations(false)
381 .with_single_instance("studio.test.single")
382 .with_tray(tray.clone())
383 .with_native_menu(menu.clone())
384 .with_global_shortcut(global.clone());
385 assert!(!config.decorations);
386 assert_eq!(config.single_instance.as_ref().map(|instance| instance.id.as_str()), Some("studio.test.single"));
387 assert_eq!(config.tray, Some(tray));
388 assert_eq!(config.native_menu, Some(menu));
389 assert_eq!(config.global_shortcuts, vec![global]);
390 }
391}