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;
17pub use mgui_shell::{
18 ClipboardHandle, ClipboardImage, FileDialogOptions, FileDragOptions, FileDragOutcome, GlobalShortcut, IconImage,
19 ImeEvent, NativeMenu, NativeMenuBar, NativeMenuCommand, NativeMenuController, NativeMenuItem, Notification,
20 NotificationLevel, NotificationTimeout, ResizeDirection, ShellEvent, ShellMetrics, Shortcut, ShortcutKey,
21 SingleInstanceConfig, TrayConfig, TrayEvent, TrayMenuItem, WindowCommand, WindowCommandBus, WindowCorners,
22 WindowIcon, WindowShell, WindowState,
23};
24
25#[derive(Clone, Debug)]
31pub struct RunnerConfig {
32 pub title: String,
33 pub inner_size: Size,
35 pub physical_size: bool,
37 pub prefer_software: bool,
39 pub startup_smoke: bool,
41 pub decorations: bool,
43 pub window_icon: Option<WindowIcon>,
45 pub app_id: Option<String>,
47 pub window_state_path: Option<PathBuf>,
50 pub single_instance: Option<SingleInstanceConfig>,
52 pub tray: Option<TrayConfig>,
54 pub native_menu: Option<NativeMenuBar>,
57 pub global_shortcuts: Vec<GlobalShortcut>,
59}
60
61impl RunnerConfig {
62 pub fn new(title: impl Into<String>, width: f32, height: f32) -> Self {
63 Self {
64 title: title.into(),
65 inner_size: Size::new(width, height),
66 physical_size: false,
67 prefer_software: false,
68 startup_smoke: false,
69 decorations: true,
70 window_icon: None,
71 app_id: None,
72 window_state_path: None,
73 single_instance: None,
74 tray: None,
75 native_menu: None,
76 global_shortcuts: Vec::new(),
77 }
78 }
79
80 pub fn with_physical_size(mut self, physical: bool) -> Self {
82 self.physical_size = physical;
83 self
84 }
85
86 pub fn with_prefer_software(mut self, prefer: bool) -> Self {
88 self.prefer_software = prefer;
89 self
90 }
91
92 pub fn with_startup_smoke(mut self, smoke: bool) -> Self {
94 self.startup_smoke = smoke;
95 self
96 }
97
98 pub fn with_decorations(mut self, decorations: bool) -> Self {
101 self.decorations = decorations;
102 self
103 }
104
105 pub fn with_window_icon(mut self, icon: WindowIcon) -> Self {
107 self.window_icon = Some(icon);
108 self
109 }
110
111 pub fn with_app_id(mut self, id: impl Into<String>) -> Self {
113 self.app_id = Some(id.into());
114 self
115 }
116
117 pub fn with_window_state_path(mut self, path: impl Into<PathBuf>) -> Self {
119 self.window_state_path = Some(path.into());
120 self
121 }
122
123 pub fn with_single_instance(mut self, id: impl Into<String>) -> Self {
126 self.single_instance = Some(SingleInstanceConfig::new(id));
127 self
128 }
129
130 pub fn with_single_instance_config(mut self, config: SingleInstanceConfig) -> Self {
132 self.single_instance = Some(config);
133 self
134 }
135
136 pub fn with_tray(mut self, tray: TrayConfig) -> Self {
141 self.tray = Some(tray);
142 self
143 }
144
145 pub fn with_native_menu(mut self, menu: NativeMenuBar) -> Self {
148 self.native_menu = Some(menu);
149 self
150 }
151
152 pub fn with_global_shortcuts(mut self, shortcuts: Vec<GlobalShortcut>) -> Self {
154 self.global_shortcuts = shortcuts;
155 self
156 }
157
158 pub fn with_global_shortcut(mut self, shortcut: GlobalShortcut) -> Self {
160 self.global_shortcuts.push(shortcut);
161 self
162 }
163}
164
165pub trait SoftwareBackend {
170 fn present(&mut self, frame: &FramePaint) -> Result<(), String>;
172 fn set_scale_factor(&mut self, scale_factor: f32);
173 fn set_logical_size(&mut self, size: Size);
174}
175
176#[doc(hidden)]
178pub trait RunnerBackend {
179 fn is_software(&self) -> bool;
180 fn image_texture_stats(&self) -> ImageTextureStats;
181 fn resolve_gpu_profile(&mut self) -> Option<u32>;
182 fn render_stats(&self) -> RenderStats;
184 fn present_stats(&self) -> PresentStats {
190 PresentStats::default()
191 }
192}
193
194pub struct AppContext<'a> {
198 pub shell: &'a dyn WindowShell,
200 backend: &'a mut dyn RunnerBackend,
201 pub tasks: &'a TaskExecutor,
203}
204
205impl<'a> AppContext<'a> {
206 #[doc(hidden)]
208 pub fn new(shell: &'a dyn WindowShell, backend: &'a mut dyn RunnerBackend, tasks: &'a TaskExecutor) -> Self {
209 Self { shell, backend, tasks }
210 }
211}
212
213impl AppContext<'_> {
214 pub fn is_software(&self) -> bool {
215 self.backend.is_software()
216 }
217
218 pub fn metrics(&self) -> ShellMetrics {
219 self.shell.metrics()
220 }
221
222 pub fn scale_factor(&self) -> f32 {
224 self.metrics().scale_factor
225 }
226
227 pub fn logical_size(&self) -> Size {
229 self.metrics().logical_size
230 }
231
232 pub fn request_redraw(&self) {
234 self.shell.request_redraw();
235 }
236
237 pub fn image_texture_stats(&self) -> ImageTextureStats {
239 self.backend.image_texture_stats()
240 }
241
242 pub fn resolve_gpu_profile(&mut self) -> Option<u32> {
244 self.backend.resolve_gpu_profile()
245 }
246
247 pub fn render_stats(&self) -> RenderStats {
249 self.backend.render_stats()
250 }
251
252 pub fn present_stats(&self) -> PresentStats {
258 self.backend.present_stats()
259 }
260}
261
262pub trait RunnerApp {
271 fn tree_mut(&mut self) -> &mut ElementTree;
273
274 fn mark_redraw(&mut self);
276
277 fn mark_rebuild(&mut self) {
280 self.mark_redraw();
281 }
282
283 fn frame(&mut self, ctx: &mut AppContext<'_>) -> Option<FramePaint>;
292
293 fn after_present(&mut self, _ctx: &mut AppContext<'_>) {}
295
296 fn on_close_requested(&mut self, _ctx: &mut AppContext<'_>) -> bool {
300 true
301 }
302
303 fn on_event(&mut self, _ctx: &mut AppContext<'_>, _event: &ShellEvent) {}
307
308 fn on_tray_event(&mut self, _ctx: &mut AppContext<'_>, _event: &TrayEvent) {}
312
313 fn on_native_menu_command(&mut self, _ctx: &mut AppContext<'_>, _id: u64) {}
317
318 fn on_second_instance(&mut self, _ctx: &mut AppContext<'_>, _args: &[String]) {}
323
324 fn on_global_shortcut(&mut self, _ctx: &mut AppContext<'_>, _id: u64) {}
328
329 fn on_global_shortcut_registered(&mut self, _ctx: &mut AppContext<'_>, _id: u64, _result: Result<(), String>) {}
333
334 fn on_resized(&mut self, _ctx: &mut AppContext<'_>, _logical_size: Size) {}
336
337 fn on_scale_factor_changed(&mut self, _ctx: &mut AppContext<'_>) {}
339
340 fn on_about_to_wait(&mut self, _ctx: &mut AppContext<'_>) -> bool {
342 false
343 }
344
345 fn next_wake(&mut self, _ctx: &mut AppContext<'_>) -> Option<std::time::Duration> {
350 None
351 }
352}
353
354#[cfg(test)]
355mod tests {
356 use super::*;
357
358 #[test]
359 fn tray_is_opt_in_and_preserves_config() {
360 let config = RunnerConfig::new("test", 320.0, 200.0);
361 assert!(config.decorations, "默认使用系统标题栏");
362 assert!(config.single_instance.is_none(), "默认允许多实例");
363 assert!(config.tray.is_none(), "默认不创建托盘");
364 assert!(config.native_menu.is_none(), "默认不安装原生菜单");
365 assert!(config.global_shortcuts.is_empty(), "默认无全局快捷键");
366
367 let tray = TrayConfig::new("mgui")
368 .with_menu_item(TrayMenuItem::item(1, "Show"))
369 .with_menu_item(TrayMenuItem::separator());
370 let menu = NativeMenuBar::new().with_menu(
371 NativeMenu::new("File")
372 .with_item(NativeMenuCommand::new(1, "Quit").with_shortcut(Shortcut::command(ShortcutKey::Char('q')))),
373 );
374 let global = GlobalShortcut::new(7, Shortcut::ctrl_alt(ShortcutKey::Char('m')));
375 let config = config
376 .with_decorations(false)
377 .with_single_instance("studio.test.single")
378 .with_tray(tray.clone())
379 .with_native_menu(menu.clone())
380 .with_global_shortcut(global.clone());
381 assert!(!config.decorations);
382 assert_eq!(config.single_instance.as_ref().map(|instance| instance.id.as_str()), Some("studio.test.single"));
383 assert_eq!(config.tray, Some(tray));
384 assert_eq!(config.native_menu, Some(menu));
385 assert_eq!(config.global_shortcuts, vec![global]);
386 }
387}