#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "macos")]
pub use macos::CaptureMonitorImpl;
pub struct CaptureMonitor {
#[cfg(target_os = "macos")]
inner: Option<macos::CaptureMonitorImpl>,
}
#[derive(Debug)]
pub enum CaptureError {
PermissionDenied,
Os(String),
}
impl std::fmt::Display for CaptureError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::PermissionDenied => {
f.write_str("macOS Accessibility/Input Monitoring permission denied")
}
Self::Os(s) => write!(f, "capture OS error: {s}"),
}
}
}
impl std::error::Error for CaptureError {}
impl CaptureMonitor {
#[cfg(target_os = "macos")]
pub fn start(
threshold_ms: u32,
on_trigger: Box<dyn Fn() + Send + 'static>,
) -> Result<Self, CaptureError> {
let inner = macos::CaptureMonitorImpl::start(threshold_ms, on_trigger)?;
Ok(Self { inner: Some(inner) })
}
#[cfg(not(target_os = "macos"))]
pub fn start(
_threshold_ms: u32,
_on_trigger: Box<dyn Fn() + Send + 'static>,
) -> Result<Self, CaptureError> {
Err(CaptureError::Os("capture only supported on macOS".into()))
}
}
impl Drop for CaptureMonitor {
fn drop(&mut self) {
#[cfg(target_os = "macos")]
{
self.inner.take();
}
}
}