visual_cortex/event.rs
1use std::sync::Arc;
2use std::time::SystemTime;
3
4use visual_cortex_vision::DetectorOutput;
5
6/// An event emitted by a watcher.
7#[derive(Debug, Clone)]
8pub struct Event {
9 pub watcher: Arc<str>,
10 pub timestamp: SystemTime,
11 pub kind: EventKind,
12}
13
14#[derive(Debug, Clone)]
15#[non_exhaustive]
16pub enum EventKind {
17 /// Every-match mode: the detector matched this tick.
18 Matched { output: DetectorOutput },
19 /// Change mode (default): the detector's output transitioned.
20 /// `old` is `None` on the first evaluation.
21 Changed {
22 old: Option<DetectorOutput>,
23 new: DetectorOutput,
24 },
25 /// Threshold mode: the predicate flipped.
26 ThresholdCrossed {
27 value: f64,
28 direction: ThresholdDirection,
29 },
30 /// Template mode: the best match score rose to or above the threshold.
31 TemplateAppeared { score: f32 },
32 /// Template mode: the best match score fell below the threshold.
33 TemplateVanished,
34 /// The detector failed repeatedly; the watcher keeps running.
35 WatcherDegraded { consecutive_errors: u32 },
36 /// The capture target disappeared (e.g. the watched window closed).
37 /// Session-wide: delivered to every subscriber. Watchers stay registered;
38 /// a `TargetReattached` follows if the target comes back.
39 TargetLost,
40 /// A previously lost capture target is available again. Session-wide.
41 TargetReattached,
42}
43
44impl EventKind {
45 /// Session-wide events that every subscriber should see, regardless of the
46 /// watcher names their stream filters on.
47 pub fn is_session_level(&self) -> bool {
48 matches!(self, EventKind::TargetLost | EventKind::TargetReattached)
49 }
50}
51
52#[derive(Debug, Clone, Copy, PartialEq, Eq)]
53pub enum ThresholdDirection {
54 /// The predicate became true.
55 Entered,
56 /// The predicate became false.
57 Exited,
58}