Skip to main content

xrack_layout/
delay_button.rs

1use crate::{ActivationKey, Point, Target, pointer_events::PRESS_AND_HOLD_SLOP};
2use std::time::{Duration, Instant};
3
4#[derive(Clone, Copy, Debug)]
5pub(crate) enum DelaySource {
6    Pointer,
7    Key(ActivationKey),
8    Touch(Point),
9    Accessibility,
10}
11
12#[derive(Clone, Debug)]
13pub(crate) struct DelayHold {
14    pub target: Target,
15    pub source: DelaySource,
16    pub scope: Vec<usize>,
17    pub duration: Duration,
18    start: Instant,
19    pub fraction: f32,
20    ready: bool,
21}
22
23impl DelayHold {
24    pub fn new(
25        target: Target,
26        source: DelaySource,
27        scope: Vec<usize>,
28        duration: Duration,
29        start: Instant,
30    ) -> Self {
31        Self {
32            target,
33            source,
34            scope,
35            duration,
36            start,
37            fraction: 0.0,
38            ready: false,
39        }
40    }
41
42    pub fn deadline(&self) -> Instant {
43        self.start + self.duration
44    }
45
46    pub fn sample(&mut self, now: Instant) -> bool {
47        if now >= self.deadline() {
48            self.fraction = 1.0;
49            self.ready = true;
50            true
51        } else {
52            self.fraction = (now.saturating_duration_since(self.start).as_secs_f64()
53                / self.duration.as_secs_f64()) as f32;
54            false
55        }
56    }
57
58    pub fn pending(&self) -> bool {
59        !self.ready
60    }
61}
62
63#[derive(Clone, Copy, Debug, PartialEq, Eq)]
64pub(crate) struct ButtonHoldTiming {
65    delay: Duration,
66    interval: Option<Duration>,
67}
68
69impl ButtonHoldTiming {
70    pub fn from_node(node: &xrack_runtime::Node) -> Option<Self> {
71        if !matches!(node.kind, "Button" | "ToolButton" | "RoundButton") {
72            return None;
73        }
74        if node.boolean("auto-repeat", false) {
75            let interval = node.int("auto-repeat-interval").unwrap_or(100);
76            return (interval > 0).then(|| Self {
77                delay: Duration::from_millis(
78                    node.int("auto-repeat-delay")
79                        .unwrap_or(300)
80                        .clamp(0, 60_000) as u64,
81                ),
82                interval: Some(Duration::from_millis(interval.min(60_000) as u64)),
83            });
84        }
85        let delay = node.int("press-and-hold-interval").unwrap_or(800);
86        (node.press_and_hold_handler && delay > 0).then(|| Self {
87            delay: Duration::from_millis(delay.min(60_000) as u64),
88            interval: None,
89        })
90    }
91}
92
93#[derive(Clone, Copy, Debug, PartialEq, Eq)]
94pub enum ButtonHoldEvent {
95    Repeat,
96    PressAndHold,
97}
98
99impl ButtonHoldEvent {
100    pub fn handler(self) -> &'static str {
101        match self {
102            Self::Repeat => "clicked",
103            Self::PressAndHold => "press-and-hold",
104        }
105    }
106}
107
108#[derive(Clone, Debug)]
109pub(crate) struct ButtonHold {
110    pub target: Target,
111    pub source: DelaySource,
112    pub scope: Vec<usize>,
113    pub timing: ButtonHoldTiming,
114    pub deadline: Option<Instant>,
115    origin: Option<Point>,
116}
117
118impl ButtonHold {
119    pub fn new(
120        target: Target,
121        source: DelaySource,
122        scope: Vec<usize>,
123        timing: ButtonHoldTiming,
124        origin: Option<Point>,
125        now: Instant,
126    ) -> Self {
127        Self {
128            target,
129            source,
130            scope,
131            timing,
132            deadline: Some(now + timing.delay),
133            origin,
134        }
135    }
136
137    pub fn moved_too_far(&self, point: Point) -> bool {
138        self.timing.interval.is_none()
139            && self.origin.is_some_and(|origin| {
140                (point.x - origin.x).hypot(point.y - origin.y) > PRESS_AND_HOLD_SLOP
141            })
142    }
143
144    pub fn sample(&mut self, now: Instant) -> Option<ButtonHoldEvent> {
145        if now < self.deadline? {
146            return None;
147        }
148        // Skip missed activations instead of delivering a burst after a stall.
149        self.deadline = self.timing.interval.map(|interval| now + interval);
150        Some(if self.deadline.is_some() {
151            ButtonHoldEvent::Repeat
152        } else {
153            ButtonHoldEvent::PressAndHold
154        })
155    }
156}