1use std::path::PathBuf;
10
11use mgui_core::element::ElementTree;
12use mgui_core::{FramePaint, Size};
13
14use crate::backend_info::ImageTextureStats;
15use crate::renderer::RenderStats;
16use crate::task::TaskExecutor;
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}
185
186pub struct AppContext<'a> {
190 pub shell: &'a dyn WindowShell,
192 backend: &'a mut dyn RunnerBackend,
193 pub tasks: &'a TaskExecutor,
195}
196
197impl<'a> AppContext<'a> {
198 #[doc(hidden)]
200 pub fn new(shell: &'a dyn WindowShell, backend: &'a mut dyn RunnerBackend, tasks: &'a TaskExecutor) -> Self {
201 Self { shell, backend, tasks }
202 }
203}
204
205impl AppContext<'_> {
206 pub fn is_software(&self) -> bool {
207 self.backend.is_software()
208 }
209
210 pub fn metrics(&self) -> ShellMetrics {
211 self.shell.metrics()
212 }
213
214 pub fn scale_factor(&self) -> f32 {
216 self.metrics().scale_factor
217 }
218
219 pub fn logical_size(&self) -> Size {
221 self.metrics().logical_size
222 }
223
224 pub fn request_redraw(&self) {
226 self.shell.request_redraw();
227 }
228
229 pub fn image_texture_stats(&self) -> ImageTextureStats {
231 self.backend.image_texture_stats()
232 }
233
234 pub fn resolve_gpu_profile(&mut self) -> Option<u32> {
236 self.backend.resolve_gpu_profile()
237 }
238
239 pub fn render_stats(&self) -> RenderStats {
241 self.backend.render_stats()
242 }
243}
244
245pub trait RunnerApp {
254 fn tree_mut(&mut self) -> &mut ElementTree;
256
257 fn mark_redraw(&mut self);
259
260 fn mark_rebuild(&mut self) {
263 self.mark_redraw();
264 }
265
266 fn frame(&mut self, ctx: &mut AppContext<'_>) -> Option<FramePaint>;
275
276 fn after_present(&mut self, _ctx: &mut AppContext<'_>) {}
278
279 fn on_close_requested(&mut self, _ctx: &mut AppContext<'_>) -> bool {
283 true
284 }
285
286 fn on_event(&mut self, _ctx: &mut AppContext<'_>, _event: &ShellEvent) {}
290
291 fn on_tray_event(&mut self, _ctx: &mut AppContext<'_>, _event: &TrayEvent) {}
295
296 fn on_native_menu_command(&mut self, _ctx: &mut AppContext<'_>, _id: u64) {}
300
301 fn on_second_instance(&mut self, _ctx: &mut AppContext<'_>, _args: &[String]) {}
306
307 fn on_global_shortcut(&mut self, _ctx: &mut AppContext<'_>, _id: u64) {}
311
312 fn on_global_shortcut_registered(&mut self, _ctx: &mut AppContext<'_>, _id: u64, _result: Result<(), String>) {}
316
317 fn on_resized(&mut self, _ctx: &mut AppContext<'_>, _logical_size: Size) {}
319
320 fn on_scale_factor_changed(&mut self, _ctx: &mut AppContext<'_>) {}
322
323 fn on_about_to_wait(&mut self, _ctx: &mut AppContext<'_>) -> bool {
325 false
326 }
327
328 fn next_wake(&mut self, _ctx: &mut AppContext<'_>) -> Option<std::time::Duration> {
333 None
334 }
335}
336
337#[cfg(test)]
338mod tests {
339 use super::*;
340
341 #[test]
342 fn tray_is_opt_in_and_preserves_config() {
343 let config = RunnerConfig::new("test", 320.0, 200.0);
344 assert!(config.decorations, "默认使用系统标题栏");
345 assert!(config.single_instance.is_none(), "默认允许多实例");
346 assert!(config.tray.is_none(), "默认不创建托盘");
347 assert!(config.native_menu.is_none(), "默认不安装原生菜单");
348 assert!(config.global_shortcuts.is_empty(), "默认无全局快捷键");
349
350 let tray = TrayConfig::new("mgui")
351 .with_menu_item(TrayMenuItem::item(1, "Show"))
352 .with_menu_item(TrayMenuItem::separator());
353 let menu = NativeMenuBar::new().with_menu(
354 NativeMenu::new("File")
355 .with_item(NativeMenuCommand::new(1, "Quit").with_shortcut(Shortcut::command(ShortcutKey::Char('q')))),
356 );
357 let global = GlobalShortcut::new(7, Shortcut::ctrl_alt(ShortcutKey::Char('m')));
358 let config = config
359 .with_decorations(false)
360 .with_single_instance("studio.test.single")
361 .with_tray(tray.clone())
362 .with_native_menu(menu.clone())
363 .with_global_shortcut(global.clone());
364 assert!(!config.decorations);
365 assert_eq!(config.single_instance.as_ref().map(|instance| instance.id.as_str()), Some("studio.test.single"));
366 assert_eq!(config.tray, Some(tray));
367 assert_eq!(config.native_menu, Some(menu));
368 assert_eq!(config.global_shortcuts, vec![global]);
369 }
370}