use crate::error::PlatformError;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WindowInfo {
pub id: String,
pub title: String,
pub focused: bool,
pub main: bool,
pub visible: bool,
pub width: u32,
pub height: u32,
}
#[async_trait]
pub trait AppScreenshot: Send + Sync {
async fn list_app_windows(&self) -> Result<Vec<WindowInfo>, PlatformError> {
Err(PlatformError::NotSupported(
"list_app_windows is not implemented for this platform".to_string(),
))
}
async fn resolve_app_window(
&self,
window_id: Option<&str>,
) -> Result<WindowInfo, PlatformError> {
let windows = self.list_app_windows().await?;
if let Some(window_id) = window_id {
return windows
.into_iter()
.find(|window| window.id == window_id)
.ok_or_else(|| {
PlatformError::InvalidParameter(format!(
"window id does not belong to this app: {window_id}"
))
});
}
windows
.iter()
.find(|window| window.focused && window.visible)
.or_else(|| windows.iter().find(|window| window.main && window.visible))
.or_else(|| windows.iter().find(|window| window.visible))
.or_else(|| windows.first())
.cloned()
.ok_or_else(|| PlatformError::Platform("no app window is available".to_string()))
}
async fn take_app_screenshot(&self, window_id: Option<&str>) -> Result<Vec<u8>, PlatformError> {
let _ = window_id;
Err(PlatformError::NotSupported(
"app screenshot is not implemented for this platform".to_string(),
))
}
}