mgui-shell-winit 0.5.0

winit 适配层:Window → WindowShell / ShellEvent,以及软件回退窗口句柄
Documentation
//! arboard 剪贴板后端:文本 / HTML / RGBA 图片 / 文件列表。

use std::borrow::Cow;
use std::path::PathBuf;

use mgui_core::{ClipboardImage, ClipboardProvider};

pub(crate) struct DesktopClipboard {
    inner: Option<arboard::Clipboard>,
}

impl DesktopClipboard {
    pub(crate) fn new() -> Self {
        Self { inner: arboard::Clipboard::new().ok() }
    }
}

impl ClipboardProvider for DesktopClipboard {
    fn get_text(&mut self) -> Option<String> {
        self.inner.as_mut()?.get_text().ok()
    }

    fn set_text(&mut self, text: &str) -> bool {
        self.inner.as_mut().and_then(|clipboard| clipboard.set_text(text).ok()).is_some()
    }

    fn get_html(&mut self) -> Option<String> {
        self.inner.as_mut()?.get().html().ok()
    }

    fn set_html(&mut self, html: &str, alt_text: Option<&str>) -> bool {
        self.inner.as_mut().and_then(|clipboard| clipboard.set().html(html, alt_text).ok()).is_some()
    }

    fn get_image(&mut self) -> Option<ClipboardImage> {
        let clipboard = self.inner.as_mut()?;
        let image = clipboard.get_image().ok()?;
        ClipboardImage::from_rgba(image.width as u32, image.height as u32, image.bytes.into_owned())
    }

    fn set_image(&mut self, image: &ClipboardImage) -> bool {
        let data = arboard::ImageData {
            width: image.width() as usize,
            height: image.height() as usize,
            bytes: Cow::Borrowed(image.rgba()),
        };
        self.inner.as_mut().and_then(|clipboard| clipboard.set_image(data).ok()).is_some()
    }

    fn get_file_list(&mut self) -> Option<Vec<PathBuf>> {
        self.inner.as_mut()?.get().file_list().ok()
    }

    fn set_file_list(&mut self, paths: &[PathBuf]) -> bool {
        self.inner.as_mut().and_then(|clipboard| clipboard.set().file_list(paths).ok()).is_some()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// 手动验收:会读写真实系统剪贴板,默认 ignore。
    ///
    /// `cargo test -p mgui-shell-winit -- --ignored manual_rich_clipboard`
    #[test]
    #[ignore = "manual: reads/writes the real system clipboard"]
    fn manual_rich_clipboard() {
        let mut cb = DesktopClipboard::new();
        assert!(cb.set_text("mgui clipboard"), "text write");
        assert_eq!(cb.get_text().as_deref(), Some("mgui clipboard"));

        assert!(cb.set_html("<b>mgui</b>", Some("mgui")), "html write");
        assert_eq!(cb.get_html().as_deref(), Some("<b>mgui</b>"));

        let image =
            ClipboardImage::from_rgba(2, 2, vec![255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255, 255, 255, 255, 255])
                .expect("image");
        assert!(cb.set_image(&image), "image write");
        let got = cb.get_image().expect("image read");
        assert_eq!((got.width(), got.height()), (2, 2));

        let path = std::env::temp_dir().join(format!("mgui-clipboard-{}.txt", std::process::id()));
        std::fs::write(&path, b"mgui").expect("temp file");
        let files = vec![path.clone()];
        assert!(cb.set_file_list(&files), "file list write");
        let got = cb.get_file_list().expect("file list read");
        assert!(got.iter().any(|entry| entry.ends_with(path.file_name().unwrap())), "file list: {got:?}");
        let _ = std::fs::remove_file(path);
    }
}