use std::fmt;
#[derive(Debug, Clone)]
pub struct WindowInfo {
pub pid: u32,
pub title: Option<String>,
pub(crate) handle: WindowHandle,
}
#[derive(Debug, Clone)]
pub(crate) enum WindowHandle {
#[cfg(windows)]
Win32(*mut std::ffi::c_void),
#[cfg(target_os = "linux")]
X11(u64),
#[cfg(target_os = "macos")]
CoreGraphics(u32),
}
impl WindowInfo {
#[cfg(windows)]
pub fn new_windows(pid: u32, title: Option<String>, hwnd: *mut std::ffi::c_void) -> Self {
Self { pid, title, handle: WindowHandle::Win32(hwnd) }
}
#[cfg(target_os = "linux")]
pub fn new_linux(pid: u32, title: Option<String>, xid: u64) -> Self {
Self { pid, title, handle: WindowHandle::X11(xid) }
}
#[cfg(target_os = "macos")]
pub fn new_macos(pid: u32, title: Option<String>, window_id: u32) -> Self {
Self { pid, title, handle: WindowHandle::CoreGraphics(window_id) }
}
pub fn title_or_unknown(&self) -> &str {
self.title.as_deref().unwrap_or("Unknown")
}
}
impl fmt::Display for WindowInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.title {
Some(title) => write!(f, "Window(PID={}, '{}')", self.pid, title),
None => write!(f, "Window(PID={})", self.pid),
}
}
}