cc-xcap 0.1.5

Forked from xcap, CC-XCap is a cross-platform screen capture library written in Rust, forked from xcap. It supports Linux (X11, Wayland), MacOS, and Windows. CC-XCap supports screenshot and video recording (WIP).
use image::RgbaImage;

use crate::{Monitor, error::XCapResult, platform::impl_window::ImplWindow};

#[derive(Debug, Clone)]
pub struct Window {
    pub(crate) impl_window: ImplWindow,
}

impl Window {
    pub(crate) fn new(impl_window: ImplWindow) -> Window {
        Window { impl_window }
    }
}

impl Window {
    /// List all windows, sorted by z coordinate.
    pub fn all() -> XCapResult<Vec<Window>> {
        let windows = ImplWindow::all()?
            .iter()
            .map(|impl_window| Window::new(impl_window.clone()))
            .collect();

        Ok(windows)
    }
    pub fn list_all() -> XCapResult<Vec<Window>> {
        let windows = ImplWindow::list_all()?
            .iter()
            .filter_map(|impl_window| {
                let window = Window::new(impl_window.clone());
                // 在一个 match 中同时检查 y 坐标和 title
                match (window.y(), window.title()) {
                    // 如果 y 坐标为 0 或 title 为空,过滤掉
                    (Ok(y), Ok(title)) if y == 0 || title.is_empty() => None,
                    // 如果获取 y 坐标失败,保留窗口
                    (Err(_), _) => Some(window),
                    // 如果获取 title 失败,保留窗口
                    (_, Err(_)) => Some(window),
                    // 其他情况保留窗口
                    _ => Some(window),
                }
            })
            .collect();

        Ok(windows)
    }
}

impl Window {
    /// The window id
    pub fn id(&self) -> XCapResult<u32> {
        self.impl_window.id()
    }
    /// The window process id
    pub fn pid(&self) -> XCapResult<u32> {
        self.impl_window.pid()
    }
    /// The window app name
    pub fn app_name(&self) -> XCapResult<String> {
        self.impl_window.app_name()
    }
    /// The window title
    pub fn title(&self) -> XCapResult<String> {
        self.impl_window.title()
    }
    /// The window current monitor
    pub fn current_monitor(&self) -> XCapResult<Monitor> {
        Ok(Monitor::new(self.impl_window.current_monitor()?))
    }
    /// The window x coordinate.
    pub fn x(&self) -> XCapResult<i32> {
        self.impl_window.x()
    }
    /// The window y coordinate.
    pub fn y(&self) -> XCapResult<i32> {
        self.impl_window.y()
    }
    /// The window z coordinate.
    pub fn z(&self) -> XCapResult<i32> {
        self.impl_window.z()
    }
    /// The window pixel width.
    pub fn width(&self) -> XCapResult<u32> {
        self.impl_window.width()
    }
    /// The window pixel height.
    pub fn height(&self) -> XCapResult<u32> {
        self.impl_window.height()
    }
    /// The window is minimized.
    pub fn is_minimized(&self) -> XCapResult<bool> {
        self.impl_window.is_minimized()
    }
    /// The window is maximized.
    pub fn is_maximized(&self) -> XCapResult<bool> {
        self.impl_window.is_maximized()
    }
    /// The window is focused.
    pub fn is_focused(&self) -> XCapResult<bool> {
        self.impl_window.is_focused()
    }
}

impl Window {
    pub fn capture_image(&self) -> XCapResult<RgbaImage> {
        self.impl_window.capture_image()
    }

    pub fn capture_thumbnail(&self) -> XCapResult<RgbaImage> {
        self.impl_window.capture_thumbnail()
    }
}