mgui-shell-winit 0.1.0

winit 适配层:Window → WindowShell / ShellEvent,以及软件回退窗口句柄
Documentation
use std::sync::Arc;

use mgui_core::Rect;
use mgui_shell::{ShellMetrics, WindowShell};
use winit::dpi::{LogicalPosition, LogicalSize};
use winit::window::Window;

/// 桌面窗口句柄(可克隆)。
#[derive(Clone)]
pub struct DesktopWindow {
    window: Arc<Window>,
}

impl DesktopWindow {
    pub fn new(window: Arc<Window>) -> Self {
        Self { window }
    }

    /// 底层 winit 窗口——仅供 desktop host 的 GPU/AccessKit/事件循环使用。
    pub fn as_winit(&self) -> &Arc<Window> {
        &self.window
    }
}

impl WindowShell for DesktopWindow {
    fn metrics(&self) -> ShellMetrics {
        let sf = (self.window.scale_factor() as f32).max(0.01);
        let phys = self.window.inner_size();
        ShellMetrics::from_physical(phys.width, phys.height, sf)
    }

    fn request_redraw(&self) {
        self.window.request_redraw();
    }

    fn set_title(&self, title: &str) {
        self.window.set_title(title);
    }

    fn set_visible(&self, visible: bool) {
        self.window.set_visible(visible);
    }

    fn set_ime_allowed(&self, allowed: bool) {
        self.window.set_ime_allowed(allowed);
    }

    fn set_ime_cursor_area(&self, rect: Rect) {
        self.window.set_ime_cursor_area(
            LogicalPosition::new(rect.x as f64, rect.y as f64),
            LogicalSize::new(rect.width.max(0.0) as f64, rect.height.max(0.0) as f64),
        );
    }
}