Skip to main content

euv_core/scene/kind/
impl.rs

1use super::*;
2
3/// Helper operations for `ImageBuffer` and `InputNodeState`.
4impl ImageBuffer {
5    /// Creates a solid-color RGBA8 image buffer for icons, placeholders, or tests.
6    ///
7    /// # Arguments
8    ///
9    /// - `u32` - Width in pixels.
10    /// - `u32` - Height in pixels.
11    /// - `Color` - Fill color.
12    ///
13    /// # Returns
14    ///
15    /// - `Self` - Populated `ImageBuffer`.
16    #[must_use]
17    pub fn solid(width: u32, height: u32, color: Color) -> Self {
18        let count: usize = (width as usize).saturating_mul(height as usize);
19        let rgba: [u8; 4] = color.to_rgba_bytes();
20        let mut pixels: Vec<u8> = Vec::with_capacity(count.saturating_mul(4));
21        for _ in 0..count {
22            pixels.extend_from_slice(&rgba);
23        }
24        Self::new(width, height, pixels)
25    }
26}
27
28/// Text editing and fluent construction helpers for `InputNodeState`.
29impl InputNodeState {
30    /// Creates an enabled single-line `InputNodeState` initialized with `value` and caret at the end.
31    ///
32    /// # Arguments
33    ///
34    /// - `S` - Initial input text value.
35    ///
36    /// # Returns
37    ///
38    /// - `Self` - Ready `InputNodeState`.
39    #[must_use]
40    pub fn text<S>(value: S) -> Self
41    where
42        S: Into<String>,
43    {
44        let s: String = value.into();
45        let caret: usize = s.len();
46        Self::new(s, String::new(), caret, false, false, false)
47    }
48
49    /// Sets placeholder text displayed when the input value is empty.
50    ///
51    /// # Arguments
52    ///
53    /// - `Self` - Source input state.
54    /// - `S` - Placeholder text.
55    ///
56    /// # Returns
57    ///
58    /// - `Self` - Updated input state.
59    #[must_use]
60    pub fn with_placeholder<S>(mut self, placeholder: S) -> Self
61    where
62        S: Into<String>,
63    {
64        self.set_placeholder(placeholder.into());
65        self
66    }
67
68    /// Toggles masked password rendering.
69    ///
70    /// # Arguments
71    ///
72    /// - `Self` - Source input state.
73    /// - `bool` - Password mode flag.
74    ///
75    /// # Returns
76    ///
77    /// - `Self` - Updated input state.
78    #[must_use]
79    pub fn with_password(mut self, password: bool) -> Self {
80        self.set_password(password);
81        self
82    }
83
84    /// Toggles multi-line text editing.
85    ///
86    /// # Arguments
87    ///
88    /// - `Self` - Source input state.
89    /// - `bool` - Multiline flag.
90    ///
91    /// # Returns
92    ///
93    /// - `Self` - Updated input state.
94    #[must_use]
95    pub fn with_multiline(mut self, multiline: bool) -> Self {
96        self.set_multiline(multiline);
97        self
98    }
99
100    /// Toggles disabled state preventing user edits.
101    ///
102    /// # Arguments
103    ///
104    /// - `Self` - Source input state.
105    /// - `bool` - Disabled flag.
106    ///
107    /// # Returns
108    ///
109    /// - `Self` - Updated input state.
110    #[must_use]
111    pub fn with_disabled(mut self, disabled: bool) -> Self {
112        self.set_disabled(disabled);
113        self
114    }
115
116    /// Inserts `text` at the current caret position and advances the caret.
117    ///
118    /// # Arguments
119    ///
120    /// - `&Self` - Mutable input state.
121    /// - `&str` - UTF-8 text to insert.
122    pub fn insert_str(&mut self, text: &str) {
123        if self.get_disabled() || text.is_empty() {
124            return;
125        }
126        let pos: usize = self.get_caret().min(self.get_value().len());
127        self.get_mut_value().insert_str(pos, text);
128        self.set_caret(pos + text.len());
129    }
130
131    /// Removes the character immediately preceding the caret if any.
132    ///
133    /// # Arguments
134    ///
135    /// - `&Self` - Mutable input state.
136    pub fn backspace(&mut self) {
137        if self.get_disabled() || self.get_caret() == 0 {
138            return;
139        }
140        let pos: usize = self.get_caret().min(self.get_value().len());
141        let prev_char_start: usize = self.get_value()[..pos]
142            .char_indices()
143            .next_back()
144            .map(|(idx, _): (usize, char)| idx)
145            .unwrap_or(0);
146        self.get_mut_value().replace_range(prev_char_start..pos, "");
147        self.set_caret(prev_char_start);
148    }
149}