Skip to main content

euv_ui/hook/counter/
impl.rs

1use super::*;
2
3/// Inherent implementation of [`Counter`].
4impl Counter {
5    /// Adds `step` to the value, clamping into `[min, max]`
6    /// afterwards. If the counter is already at `max`, the
7    /// value is left at `max`.
8    pub fn increment(&self) {
9        let current: i32 = self.get_value().get();
10        let mut next: i32 = current.saturating_add(self.step);
11        if let Some(max) = self.max {
12            next = next.min(max);
13        }
14        if let Some(min) = self.min {
15            next = next.max(min);
16        }
17        self.get_value().set(next);
18    }
19
20    /// Subtracts `step` from the value, clamping into
21    /// `[min, max]` afterwards. If the counter is already
22    /// at `min`, the value is left at `min`.
23    pub fn decrement(&self) {
24        let current: i32 = self.get_value().get();
25        let mut next: i32 = current.saturating_sub(self.step);
26        if let Some(min) = self.min {
27            next = next.max(min);
28        }
29        if let Some(max) = self.max {
30            next = next.min(max);
31        }
32        self.get_value().set(next);
33    }
34
35    /// Replaces the value with `next`, clamping into
36    /// `[min, max]` if bounds are set.
37    ///
38    /// # Arguments
39    ///
40    /// - `i32` - A 32-bit signed integer (`i32`).
41    pub fn set(&self, next: i32) {
42        let clamped: i32 = match (self.min, self.max) {
43            (Some(min), Some(max)) => next.clamp(min, max),
44            (Some(min), None) => next.max(min),
45            (None, Some(max)) => next.min(max),
46            (None, None) => next,
47        };
48        self.get_value().set(clamped);
49    }
50
51    /// Replaces the value with `next` without clamping.
52    /// Bypasses both the `min` and `max` bounds. Use
53    /// when you genuinely want to push the counter outside
54    /// its configured range (e.g., to "reset to a
55    /// deliberately out-of-range sentinel" or to recover
56    /// from an invalid configuration).
57    ///
58    /// # Arguments
59    ///
60    /// - `i32` - A 32-bit signed integer (`i32`).
61    pub fn set_unchecked(&self, next: i32) {
62        self.get_value().set(next);
63    }
64
65    /// Returns `true` when the value is at the configured
66    /// `max` (or always `false` if unbounded above).
67    ///
68    /// # Returns
69    ///
70    /// - `bool` - `true` when the value is at the configured maximum.
71    pub fn is_at_max(&self) -> bool {
72        match self.max {
73            Some(max) => self.get_value().get() >= max,
74            None => false,
75        }
76    }
77
78    /// Returns `true` when the value is at the configured
79    /// `min` (or always `false` if unbounded below).
80    ///
81    /// # Returns
82    ///
83    /// - `bool` - `true` when the value is at the configured minimum.
84    pub fn is_at_min(&self) -> bool {
85        match self.min {
86            Some(min) => self.get_value().get() <= min,
87            None => false,
88        }
89    }
90
91    /// Returns the current value as a snapshot.
92    ///
93    /// # Returns
94    ///
95    /// - `i32` - The current value (or a snapshot thereof).
96    pub fn get(&self) -> i32 {
97        self.get_value().get()
98    }
99}
100
101/// Formatting / debug-printing for [`Counter`].
102impl Display for Counter {
103    /// Formats the [`Counter`] via the supplied formatter.
104    ///
105    /// # Arguments
106    ///
107    /// - `&mut Formatter<'_>` - The formatter receiving the formatted output.
108    ///
109    /// # Returns
110    ///
111    /// - `FmtResult` - Result of the formatting operation.
112    fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
113        write!(formatter, "Counter({})", self.get_value().get())
114    }
115}