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 startup_full: bool,
48 pub decorations: bool,
50 pub window_icon: Option<WindowIcon>,
52 pub app_id: Option<String>,
54 pub window_state_path: Option<PathBuf>,
57 pub single_instance: Option<SingleInstanceConfig>,
59 pub tray: Option<TrayConfig>,
61 pub native_menu: Option<NativeMenuBar>,
64 pub global_shortcuts: Vec<GlobalShortcut>,
66}
67
68impl RunnerConfig {
69 pub fn new(title: impl Into<String>, width: f32, height: f32) -> Self {
70 Self {
71 title: title.into(),
72 inner_size: Size::new(width, height),
73 physical_size: false,
74 prefer_software: false,
75 startup_smoke: false,
76 startup_full: false,
77 decorations: true,
78 window_icon: None,
79 app_id: None,
80 window_state_path: None,
81 single_instance: None,
82 tray: None,
83 native_menu: None,
84 global_shortcuts: Vec::new(),
85 }
86 }
87
88 pub fn with_physical_size(mut self, physical: bool) -> Self {
90 self.physical_size = physical;
91 self
92 }
93
94 pub fn with_prefer_software(mut self, prefer: bool) -> Self {
96 self.prefer_software = prefer;
97 self
98 }
99
100 pub fn with_startup_smoke(mut self, smoke: bool) -> Self {
102 self.startup_smoke = smoke;
103 if !smoke {
104 self.startup_full = false;
105 }
106 self
107 }
108
109 pub fn with_startup_full(mut self, full: bool) -> Self {
112 self.startup_full = full;
113 if full {
114 self.startup_smoke = true;
115 }
116 self
117 }
118
119 pub fn with_decorations(mut self, decorations: bool) -> Self {
122 self.decorations = decorations;
123 self
124 }
125
126 pub fn with_window_icon(mut self, icon: WindowIcon) -> Self {
128 self.window_icon = Some(icon);
129 self
130 }
131
132 pub fn with_app_id(mut self, id: impl Into<String>) -> Self {
134 self.app_id = Some(id.into());
135 self
136 }
137
138 pub fn with_window_state_path(mut self, path: impl Into<PathBuf>) -> Self {
140 self.window_state_path = Some(path.into());
141 self
142 }
143
144 pub fn with_single_instance(mut self, id: impl Into<String>) -> Self {
147 self.single_instance = Some(SingleInstanceConfig::new(id));
148 self
149 }
150
151 pub fn with_single_instance_config(mut self, config: SingleInstanceConfig) -> Self {
153 self.single_instance = Some(config);
154 self
155 }
156
157 pub fn with_tray(mut self, tray: TrayConfig) -> Self {
162 self.tray = Some(tray);
163 self
164 }
165
166 pub fn with_native_menu(mut self, menu: NativeMenuBar) -> Self {
169 self.native_menu = Some(menu);
170 self
171 }
172
173 pub fn with_global_shortcuts(mut self, shortcuts: Vec<GlobalShortcut>) -> Self {
175 self.global_shortcuts = shortcuts;
176 self
177 }
178
179 pub fn with_global_shortcut(mut self, shortcut: GlobalShortcut) -> Self {
181 self.global_shortcuts.push(shortcut);
182 self
183 }
184}
185
186pub trait SoftwareBackend {
191 fn present(&mut self, frame: &FramePaint) -> Result<(), String>;
193 fn set_scale_factor(&mut self, scale_factor: f32);
194 fn set_logical_size(&mut self, size: Size);
195}
196
197#[doc(hidden)]
199pub trait RunnerBackend {
200 fn is_software(&self) -> bool;
201 fn image_texture_stats(&self) -> ImageTextureStats;
202 fn resolve_gpu_profile(&mut self) -> Option<u32>;
203 fn render_stats(&self) -> RenderStats;
205 fn present_stats(&self) -> PresentStats {
211 PresentStats::default()
212 }
213}
214
215pub struct AppContext<'a> {
219 pub shell: &'a dyn WindowShell,
221 backend: &'a mut dyn RunnerBackend,
222 pub tasks: &'a TaskExecutor,
224}
225
226impl<'a> AppContext<'a> {
227 #[doc(hidden)]
229 pub fn new(shell: &'a dyn WindowShell, backend: &'a mut dyn RunnerBackend, tasks: &'a TaskExecutor) -> Self {
230 Self { shell, backend, tasks }
231 }
232}
233
234impl AppContext<'_> {
235 pub fn is_software(&self) -> bool {
236 self.backend.is_software()
237 }
238
239 pub fn metrics(&self) -> ShellMetrics {
240 self.shell.metrics()
241 }
242
243 pub fn scale_factor(&self) -> f32 {
245 self.metrics().scale_factor
246 }
247
248 pub fn logical_size(&self) -> Size {
250 self.metrics().logical_size
251 }
252
253 pub fn request_redraw(&self) {
255 self.shell.request_redraw();
256 }
257
258 pub fn image_texture_stats(&self) -> ImageTextureStats {
260 self.backend.image_texture_stats()
261 }
262
263 pub fn resolve_gpu_profile(&mut self) -> Option<u32> {
265 self.backend.resolve_gpu_profile()
266 }
267
268 pub fn render_stats(&self) -> RenderStats {
270 self.backend.render_stats()
271 }
272
273 pub fn present_stats(&self) -> PresentStats {
279 self.backend.present_stats()
280 }
281}
282
283pub trait RunnerApp {
292 fn tree_mut(&mut self) -> &mut ElementTree;
294
295 fn mark_redraw(&mut self);
297
298 fn mark_rebuild(&mut self) {
301 self.mark_redraw();
302 }
303
304 fn frame(&mut self, ctx: &mut AppContext<'_>) -> Option<FramePaint>;
313
314 fn after_present(&mut self, _ctx: &mut AppContext<'_>) {}
316
317 fn on_close_requested(&mut self, _ctx: &mut AppContext<'_>) -> bool {
321 true
322 }
323
324 fn on_event(&mut self, _ctx: &mut AppContext<'_>, _event: &ShellEvent) {}
328
329 fn on_tray_event(&mut self, _ctx: &mut AppContext<'_>, _event: &TrayEvent) {}
333
334 fn on_native_menu_command(&mut self, _ctx: &mut AppContext<'_>, _id: u64) {}
338
339 fn on_second_instance(&mut self, _ctx: &mut AppContext<'_>, _args: &[String]) {}
344
345 fn on_global_shortcut(&mut self, _ctx: &mut AppContext<'_>, _id: u64) {}
349
350 fn on_global_shortcut_registered(&mut self, _ctx: &mut AppContext<'_>, _id: u64, _result: Result<(), String>) {}
354
355 fn on_resized(&mut self, _ctx: &mut AppContext<'_>, _logical_size: Size) {}
357
358 fn on_scale_factor_changed(&mut self, _ctx: &mut AppContext<'_>) {}
360
361 fn on_about_to_wait(&mut self, _ctx: &mut AppContext<'_>) -> bool {
363 false
364 }
365
366 fn next_wake(&mut self, _ctx: &mut AppContext<'_>) -> Option<std::time::Duration> {
371 None
372 }
373}
374
375#[cfg(test)]
376mod tests {
377 use super::*;
378
379 #[test]
380 fn tray_is_opt_in_and_preserves_config() {
381 let config = RunnerConfig::new("test", 320.0, 200.0);
382 assert!(config.decorations, "默认使用系统标题栏");
383 assert!(config.single_instance.is_none(), "默认允许多实例");
384 assert!(config.tray.is_none(), "默认不创建托盘");
385 assert!(config.native_menu.is_none(), "默认不安装原生菜单");
386 assert!(config.global_shortcuts.is_empty(), "默认无全局快捷键");
387
388 let tray = TrayConfig::new("mgui")
389 .with_menu_item(TrayMenuItem::item(1, "Show"))
390 .with_menu_item(TrayMenuItem::separator());
391 let menu = NativeMenuBar::new().with_menu(
392 NativeMenu::new("File")
393 .with_item(NativeMenuCommand::new(1, "Quit").with_shortcut(Shortcut::command(ShortcutKey::Char('q')))),
394 );
395 let global = GlobalShortcut::new(7, Shortcut::ctrl_alt(ShortcutKey::Char('m')));
396 let config = config
397 .with_decorations(false)
398 .with_single_instance("studio.test.single")
399 .with_tray(tray.clone())
400 .with_native_menu(menu.clone())
401 .with_global_shortcut(global.clone());
402 assert!(!config.decorations);
403 assert_eq!(config.single_instance.as_ref().map(|instance| instance.id.as_str()), Some("studio.test.single"));
404 assert_eq!(config.tray, Some(tray));
405 assert_eq!(config.native_menu, Some(menu));
406 assert_eq!(config.global_shortcuts, vec![global]);
407 }
408}