Skip to main content

mgui_shell_winit/
window.rs

1use std::sync::Arc;
2
3use mgui_core::Rect;
4use mgui_shell::{ShellMetrics, WindowShell};
5use winit::dpi::{LogicalPosition, LogicalSize};
6use winit::window::Window;
7
8/// 桌面窗口句柄(可克隆)。
9#[derive(Clone)]
10pub struct DesktopWindow {
11    window: Arc<Window>,
12}
13
14impl DesktopWindow {
15    pub fn new(window: Arc<Window>) -> Self {
16        Self { window }
17    }
18
19    /// 底层 winit 窗口——仅供 desktop host 的 GPU/AccessKit/事件循环使用。
20    pub fn as_winit(&self) -> &Arc<Window> {
21        &self.window
22    }
23}
24
25impl WindowShell for DesktopWindow {
26    fn metrics(&self) -> ShellMetrics {
27        let sf = (self.window.scale_factor() as f32).max(0.01);
28        let phys = self.window.inner_size();
29        ShellMetrics::from_physical(phys.width, phys.height, sf)
30    }
31
32    fn request_redraw(&self) {
33        self.window.request_redraw();
34    }
35
36    fn set_title(&self, title: &str) {
37        self.window.set_title(title);
38    }
39
40    fn set_visible(&self, visible: bool) {
41        self.window.set_visible(visible);
42    }
43
44    fn set_ime_allowed(&self, allowed: bool) {
45        self.window.set_ime_allowed(allowed);
46    }
47
48    fn set_ime_cursor_area(&self, rect: Rect) {
49        self.window.set_ime_cursor_area(
50            LogicalPosition::new(rect.x as f64, rect.y as f64),
51            LogicalSize::new(rect.width.max(0.0) as f64, rect.height.max(0.0) as f64),
52        );
53    }
54}