Skip to main content

lingxia_platform/traits/
screenshot.rs

1use crate::error::PlatformError;
2use async_trait::async_trait;
3use serde::{Deserialize, Serialize};
4
5/// Description of a top-level window belonging to the host app.
6///
7/// Returned by [`AppScreenshot::list_app_windows`]. The `id` is opaque
8/// platform-specific (macOS NSWindow.windowNumber, Windows HWND, etc.) and
9/// is the value to pass back to [`AppScreenshot::take_app_screenshot`] to
10/// target that specific window.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct WindowInfo {
13    /// Opaque platform-specific identifier (stringified for portability).
14    pub id: String,
15    /// Window title (may be empty if the platform / app does not set one).
16    pub title: String,
17    /// `true` if this window currently has keyboard focus / is "key".
18    pub focused: bool,
19    /// `true` if this window is the app's main window (macOS concept).
20    pub main: bool,
21    /// `true` if the window is currently on-screen / not minimized.
22    pub visible: bool,
23    /// Width in points (logical, not pixels).
24    pub width: u32,
25    /// Height in points (logical, not pixels).
26    pub height: u32,
27}
28
29/// Capture a PNG snapshot of the host app's window(s).
30///
31/// Conceptually one level above `WebViewController::take_screenshot`: the
32/// WebView API only sees web content, while this captures the entire window
33/// the host renders — host-drawn navigation bars, native overlays, multiple
34/// WebViews composited together, etc.
35#[async_trait]
36pub trait AppScreenshot: Send + Sync {
37    /// Enumerate the app's top-level windows.
38    ///
39    /// Mobile platforms typically return a single "main" entry. Desktop
40    /// platforms return one entry per open NSWindow / HWND.
41    async fn list_app_windows(&self) -> Result<Vec<WindowInfo>, PlatformError> {
42        Err(PlatformError::NotSupported(
43            "list_app_windows is not implemented for this platform".to_string(),
44        ))
45    }
46
47    /// Capture and return PNG-encoded bytes of the app's window.
48    ///
49    /// When `window_id` is `None`, the platform picks a sensible default
50    /// (key/focused window on desktop; the sole window on mobile).
51    async fn take_app_screenshot(&self, window_id: Option<&str>) -> Result<Vec<u8>, PlatformError> {
52        let _ = window_id;
53        Err(PlatformError::NotSupported(
54            "app screenshot is not implemented for this platform".to_string(),
55        ))
56    }
57}