Skip to main content

euv_core/scene/kind/
impl.rs

1use super::*;
2
3/// Value equality for `ImageBuffer` with O(1) identity and shared-storage fast paths.
4impl PartialEq for ImageBuffer {
5    /// Compares two image buffers by dimensions and RGBA8 pixel content, short-circuiting on matching image ID or shared `Arc` storage.
6    ///
7    /// # Arguments
8    ///
9    /// - `&Self` - Left-hand image buffer.
10    /// - `&Self` - Right-hand image buffer.
11    ///
12    /// # Returns
13    ///
14    /// - `bool` - `true` when both buffers have identical dimensions and pixel bytes.
15    fn eq(&self, other: &Self) -> bool {
16        if self.get_width() != other.get_width() || self.get_height() != other.get_height() {
17            return false;
18        }
19        if self.get_id() != 0 && self.get_id() == other.get_id() {
20            return true;
21        }
22        Arc::ptr_eq(self.pixels_arc(), other.pixels_arc())
23            || self.pixels_slice() == other.pixels_slice()
24    }
25}
26
27/// Reflexive equality marker for `ImageBuffer`.
28impl Eq for ImageBuffer {}
29
30/// Helper operations for `ImageBuffer` and `InputNodeState`.
31impl ImageBuffer {
32    /// Constructs a zero-copy shared RGBA8 `ImageBuffer` and assigns a unique monotonic identifier.
33    ///
34    /// # Arguments
35    ///
36    /// - `u32` - Width in pixels.
37    /// - `u32` - Height in pixels.
38    /// - `P` - Packed RGBA8 pixel storage convertible into `Arc<[u8]>`.
39    ///
40    /// # Returns
41    ///
42    /// - `Self` - Populated zero-copy `ImageBuffer`.
43    #[must_use]
44    pub fn new<P>(width: u32, height: u32, pixels: P) -> Self
45    where
46        P: Into<Arc<[u8]>>,
47    {
48        let id: u64 = NEXT_IMAGE_ID.fetch_add(1, Ordering::Relaxed);
49        let shared: Arc<[u8]> = pixels.into();
50        Self {
51            id,
52            width,
53            height,
54            pixels: shared,
55        }
56    }
57
58    /// Returns a borrowed slice over the packed RGBA8 pixel bytes.
59    ///
60    /// # Arguments
61    ///
62    /// - `&Self` - Source image buffer.
63    ///
64    /// # Returns
65    ///
66    /// - `&[u8]` - Packed RGBA8 pixel slice.
67    #[must_use]
68    pub fn get_pixels(&self) -> &[u8] {
69        self.pixels.as_ref()
70    }
71
72    /// Returns a borrowed slice over the packed RGBA8 pixel bytes.
73    ///
74    /// # Arguments
75    ///
76    /// - `&Self` - Source image buffer.
77    ///
78    /// # Returns
79    ///
80    /// - `&[u8]` - Packed RGBA8 pixel slice.
81    #[must_use]
82    pub fn pixels_slice(&self) -> &[u8] {
83        self.get_pixels()
84    }
85
86    /// Returns a borrowed reference to the underlying shared `Arc<[u8]>` pixel storage.
87    ///
88    /// # Arguments
89    ///
90    /// - `&Self` - Source image buffer.
91    ///
92    /// # Returns
93    ///
94    /// - `&Arc<[u8]>` - Shared atomic reference-counted pixel slice.
95    #[must_use]
96    pub fn pixels_arc(&self) -> &Arc<[u8]> {
97        let Self { pixels, .. } = self;
98        pixels
99    }
100
101    /// Creates a solid-color RGBA8 image buffer for icons, placeholders, or tests.
102    ///
103    /// # Arguments
104    ///
105    /// - `u32` - Width in pixels.
106    /// - `u32` - Height in pixels.
107    /// - `Color` - Fill color.
108    ///
109    /// # Returns
110    ///
111    /// - `Self` - Populated `ImageBuffer`.
112    #[must_use]
113    pub fn solid(width: u32, height: u32, color: Color) -> Self {
114        let count: usize = (width as usize).saturating_mul(height as usize);
115        let rgba: [u8; 4] = color.to_rgba_bytes();
116        let mut pixels: Vec<u8> = Vec::with_capacity(count.saturating_mul(4));
117        for _ in 0..count {
118            pixels.extend_from_slice(&rgba);
119        }
120        Self::new(width, height, pixels)
121    }
122}
123
124/// Text editing and fluent construction helpers for `InputNodeState`.
125impl InputNodeState {
126    /// Creates an enabled single-line `InputNodeState` initialized with `value` and caret at the end.
127    ///
128    /// # Arguments
129    ///
130    /// - `S` - Initial input text value.
131    ///
132    /// # Returns
133    ///
134    /// - `Self` - Ready `InputNodeState`.
135    #[must_use]
136    pub fn text<S>(value: S) -> Self
137    where
138        S: Into<String>,
139    {
140        let s: String = value.into();
141        let caret: usize = s.len();
142        Self::new(s, String::new(), caret, false, false, false)
143    }
144
145    /// Sets placeholder text displayed when the input value is empty.
146    ///
147    /// # Arguments
148    ///
149    /// - `Self` - Source input state.
150    /// - `S` - Placeholder text.
151    ///
152    /// # Returns
153    ///
154    /// - `Self` - Updated input state.
155    #[must_use]
156    pub fn with_placeholder<S>(mut self, placeholder: S) -> Self
157    where
158        S: Into<String>,
159    {
160        self.set_placeholder(placeholder.into());
161        self
162    }
163
164    /// Toggles masked password rendering.
165    ///
166    /// # Arguments
167    ///
168    /// - `Self` - Source input state.
169    /// - `bool` - Password mode flag.
170    ///
171    /// # Returns
172    ///
173    /// - `Self` - Updated input state.
174    #[must_use]
175    pub fn with_password(mut self, password: bool) -> Self {
176        self.set_password(password);
177        self
178    }
179
180    /// Toggles multi-line text editing.
181    ///
182    /// # Arguments
183    ///
184    /// - `Self` - Source input state.
185    /// - `bool` - Multiline flag.
186    ///
187    /// # Returns
188    ///
189    /// - `Self` - Updated input state.
190    #[must_use]
191    pub fn with_multiline(mut self, multiline: bool) -> Self {
192        self.set_multiline(multiline);
193        self
194    }
195
196    /// Toggles disabled state preventing user edits.
197    ///
198    /// # Arguments
199    ///
200    /// - `Self` - Source input state.
201    /// - `bool` - Disabled flag.
202    ///
203    /// # Returns
204    ///
205    /// - `Self` - Updated input state.
206    #[must_use]
207    pub fn with_disabled(mut self, disabled: bool) -> Self {
208        self.set_disabled(disabled);
209        self
210    }
211
212    /// Inserts `text` at the current caret position and advances the caret.
213    ///
214    /// # Arguments
215    ///
216    /// - `&Self` - Mutable input state.
217    /// - `&str` - UTF-8 text to insert.
218    pub fn insert_str(&mut self, text: &str) {
219        if self.get_disabled() || text.is_empty() {
220            return;
221        }
222        let pos: usize = self.get_caret().min(self.get_value().len());
223        self.get_mut_value().insert_str(pos, text);
224        self.set_caret(pos + text.len());
225    }
226
227    /// Removes the character immediately preceding the caret if any.
228    ///
229    /// # Arguments
230    ///
231    /// - `&Self` - Mutable input state.
232    pub fn backspace(&mut self) {
233        if self.get_disabled() || self.get_caret() == 0 {
234            return;
235        }
236        let pos: usize = self.get_caret().min(self.get_value().len());
237        let prev_char_start: usize = self.get_value()[..pos]
238            .char_indices()
239            .next_back()
240            .map(|(idx, _): (usize, char)| idx)
241            .unwrap_or(0);
242        self.get_mut_value().replace_range(prev_char_start..pos, "");
243        self.set_caret(prev_char_start);
244    }
245}