window_observer/platform_impl/windows/
observer.rs

1use wineventhook::WindowEventHook;
2
3use crate::{Error, EventTx};
4
5use super::hook_task::make_wineventhook_task;
6
7/// Observes window events on the Windows platform by using [wineventhook].
8pub struct WindowsWindowObserver {
9    hook: WindowEventHook,
10}
11
12impl WindowsWindowObserver {
13    /// Starts observing window events for a specific process ID.
14    pub async fn start(
15        pid: u32,
16        event_tx: EventTx,
17        event_filter: crate::EventFilter,
18    ) -> Result<Self, Error> {
19        if pid == 0 {
20            return Err(Error::InvalidProcessId(pid));
21        }
22
23        let hook = make_wineventhook_task(pid, event_tx, event_filter).await?;
24
25        Ok(Self { hook })
26    }
27
28    /// Stops observing window events.
29    pub async fn stop(self) -> Result<(), Error> {
30        self.hook
31            .unhook()
32            .await
33            .map_err(super::error::WindowsError::from)?;
34
35        Ok(())
36    }
37
38    /// Retrieves the underlying [`WindowEventHook`].
39    pub fn hook(&self) -> &WindowEventHook {
40        &self.hook
41    }
42}