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, FileDragOptions, FileDragOutcome, GlobalShortcut, IconImage, ImeEvent, NativeMenu,
19 NativeMenuBar, NativeMenuCommand, NativeMenuController, NativeMenuItem, Notification, NotificationLevel,
20 NotificationTimeout, ResizeDirection, ShellEvent, ShellMetrics, Shortcut, ShortcutKey, SingleInstanceConfig,
21 TrayConfig, TrayEvent, TrayMenuItem, WindowCorners, WindowIcon, WindowShell, WindowState,
22};
23
24#[derive(Clone, Debug)]
30pub struct RunnerConfig {
31 pub title: String,
32 pub inner_size: Size,
34 pub physical_size: bool,
36 pub prefer_software: bool,
38 pub startup_smoke: bool,
40 pub decorations: bool,
42 pub window_icon: Option<WindowIcon>,
44 pub app_id: Option<String>,
46 pub window_state_path: Option<PathBuf>,
49 pub single_instance: Option<SingleInstanceConfig>,
51 pub tray: Option<TrayConfig>,
53 pub native_menu: Option<NativeMenuBar>,
56 pub global_shortcuts: Vec<GlobalShortcut>,
58}
59
60impl RunnerConfig {
61 pub fn new(title: impl Into<String>, width: f32, height: f32) -> Self {
62 Self {
63 title: title.into(),
64 inner_size: Size::new(width, height),
65 physical_size: false,
66 prefer_software: false,
67 startup_smoke: false,
68 decorations: true,
69 window_icon: None,
70 app_id: None,
71 window_state_path: None,
72 single_instance: None,
73 tray: None,
74 native_menu: None,
75 global_shortcuts: Vec::new(),
76 }
77 }
78
79 pub fn with_physical_size(mut self, physical: bool) -> Self {
81 self.physical_size = physical;
82 self
83 }
84
85 pub fn with_prefer_software(mut self, prefer: bool) -> Self {
87 self.prefer_software = prefer;
88 self
89 }
90
91 pub fn with_startup_smoke(mut self, smoke: bool) -> Self {
93 self.startup_smoke = smoke;
94 self
95 }
96
97 pub fn with_decorations(mut self, decorations: bool) -> Self {
100 self.decorations = decorations;
101 self
102 }
103
104 pub fn with_window_icon(mut self, icon: WindowIcon) -> Self {
106 self.window_icon = Some(icon);
107 self
108 }
109
110 pub fn with_app_id(mut self, id: impl Into<String>) -> Self {
112 self.app_id = Some(id.into());
113 self
114 }
115
116 pub fn with_window_state_path(mut self, path: impl Into<PathBuf>) -> Self {
118 self.window_state_path = Some(path.into());
119 self
120 }
121
122 pub fn with_single_instance(mut self, id: impl Into<String>) -> Self {
125 self.single_instance = Some(SingleInstanceConfig::new(id));
126 self
127 }
128
129 pub fn with_single_instance_config(mut self, config: SingleInstanceConfig) -> Self {
131 self.single_instance = Some(config);
132 self
133 }
134
135 pub fn with_tray(mut self, tray: TrayConfig) -> Self {
140 self.tray = Some(tray);
141 self
142 }
143
144 pub fn with_native_menu(mut self, menu: NativeMenuBar) -> Self {
147 self.native_menu = Some(menu);
148 self
149 }
150
151 pub fn with_global_shortcuts(mut self, shortcuts: Vec<GlobalShortcut>) -> Self {
153 self.global_shortcuts = shortcuts;
154 self
155 }
156
157 pub fn with_global_shortcut(mut self, shortcut: GlobalShortcut) -> Self {
159 self.global_shortcuts.push(shortcut);
160 self
161 }
162}
163
164pub trait SoftwareBackend {
169 fn present(&mut self, frame: &FramePaint) -> Result<(), String>;
171 fn set_scale_factor(&mut self, scale_factor: f32);
172 fn set_logical_size(&mut self, size: Size);
173}
174
175#[doc(hidden)]
177pub trait RunnerBackend {
178 fn is_software(&self) -> bool;
179 fn image_texture_stats(&self) -> ImageTextureStats;
180 fn resolve_gpu_profile(&mut self) -> Option<u32>;
181 fn render_stats(&self) -> RenderStats;
183}
184
185pub struct AppContext<'a> {
189 pub shell: &'a dyn WindowShell,
191 backend: &'a mut dyn RunnerBackend,
192 pub tasks: &'a TaskExecutor,
194}
195
196impl<'a> AppContext<'a> {
197 #[doc(hidden)]
199 pub fn new(shell: &'a dyn WindowShell, backend: &'a mut dyn RunnerBackend, tasks: &'a TaskExecutor) -> Self {
200 Self { shell, backend, tasks }
201 }
202}
203
204impl AppContext<'_> {
205 pub fn is_software(&self) -> bool {
206 self.backend.is_software()
207 }
208
209 pub fn metrics(&self) -> ShellMetrics {
210 self.shell.metrics()
211 }
212
213 pub fn scale_factor(&self) -> f32 {
215 self.metrics().scale_factor
216 }
217
218 pub fn logical_size(&self) -> Size {
220 self.metrics().logical_size
221 }
222
223 pub fn request_redraw(&self) {
225 self.shell.request_redraw();
226 }
227
228 pub fn image_texture_stats(&self) -> ImageTextureStats {
230 self.backend.image_texture_stats()
231 }
232
233 pub fn resolve_gpu_profile(&mut self) -> Option<u32> {
235 self.backend.resolve_gpu_profile()
236 }
237
238 pub fn render_stats(&self) -> RenderStats {
240 self.backend.render_stats()
241 }
242}
243
244pub trait RunnerApp {
253 fn tree_mut(&mut self) -> &mut ElementTree;
255
256 fn mark_redraw(&mut self);
258
259 fn mark_rebuild(&mut self) {
262 self.mark_redraw();
263 }
264
265 fn frame(&mut self, ctx: &mut AppContext<'_>) -> Option<FramePaint>;
274
275 fn after_present(&mut self, _ctx: &mut AppContext<'_>) {}
277
278 fn on_close_requested(&mut self, _ctx: &mut AppContext<'_>) -> bool {
282 true
283 }
284
285 fn on_event(&mut self, _ctx: &mut AppContext<'_>, _event: &ShellEvent) {}
289
290 fn on_tray_event(&mut self, _ctx: &mut AppContext<'_>, _event: &TrayEvent) {}
294
295 fn on_native_menu_command(&mut self, _ctx: &mut AppContext<'_>, _id: u64) {}
299
300 fn on_second_instance(&mut self, _ctx: &mut AppContext<'_>, _args: &[String]) {}
305
306 fn on_global_shortcut(&mut self, _ctx: &mut AppContext<'_>, _id: u64) {}
310
311 fn on_global_shortcut_registered(&mut self, _ctx: &mut AppContext<'_>, _id: u64, _result: Result<(), String>) {}
315
316 fn on_resized(&mut self, _ctx: &mut AppContext<'_>, _logical_size: Size) {}
318
319 fn on_scale_factor_changed(&mut self, _ctx: &mut AppContext<'_>) {}
321
322 fn on_about_to_wait(&mut self, _ctx: &mut AppContext<'_>) -> bool {
324 false
325 }
326
327 fn next_wake(&mut self, _ctx: &mut AppContext<'_>) -> Option<std::time::Duration> {
332 None
333 }
334}
335
336#[cfg(test)]
337mod tests {
338 use super::*;
339
340 #[test]
341 fn tray_is_opt_in_and_preserves_config() {
342 let config = RunnerConfig::new("test", 320.0, 200.0);
343 assert!(config.decorations, "默认使用系统标题栏");
344 assert!(config.single_instance.is_none(), "默认允许多实例");
345 assert!(config.tray.is_none(), "默认不创建托盘");
346 assert!(config.native_menu.is_none(), "默认不安装原生菜单");
347 assert!(config.global_shortcuts.is_empty(), "默认无全局快捷键");
348
349 let tray = TrayConfig::new("mgui")
350 .with_menu_item(TrayMenuItem::item(1, "Show"))
351 .with_menu_item(TrayMenuItem::separator());
352 let menu = NativeMenuBar::new().with_menu(
353 NativeMenu::new("File")
354 .with_item(NativeMenuCommand::new(1, "Quit").with_shortcut(Shortcut::command(ShortcutKey::Char('q')))),
355 );
356 let global = GlobalShortcut::new(7, Shortcut::ctrl_alt(ShortcutKey::Char('m')));
357 let config = config
358 .with_decorations(false)
359 .with_single_instance("studio.test.single")
360 .with_tray(tray.clone())
361 .with_native_menu(menu.clone())
362 .with_global_shortcut(global.clone());
363 assert!(!config.decorations);
364 assert_eq!(config.single_instance.as_ref().map(|instance| instance.id.as_str()), Some("studio.test.single"));
365 assert_eq!(config.tray, Some(tray));
366 assert_eq!(config.native_menu, Some(menu));
367 assert_eq!(config.global_shortcuts, vec![global]);
368 }
369}