use super::*;
impl ImageBuffer {
#[must_use]
pub fn solid(width: u32, height: u32, color: Color) -> Self {
let count: usize = (width as usize).saturating_mul(height as usize);
let rgba: [u8; 4] = color.to_rgba_bytes();
let mut pixels: Vec<u8> = Vec::with_capacity(count.saturating_mul(4));
for _ in 0..count {
pixels.extend_from_slice(&rgba);
}
Self::new(width, height, pixels)
}
}
impl InputNodeState {
#[must_use]
pub fn text<S>(value: S) -> Self
where
S: Into<String>,
{
let s: String = value.into();
let caret: usize = s.len();
Self::new(s, String::new(), caret, false, false, false)
}
#[must_use]
pub fn with_placeholder<S>(mut self, placeholder: S) -> Self
where
S: Into<String>,
{
self.set_placeholder(placeholder.into());
self
}
#[must_use]
pub fn with_password(mut self, password: bool) -> Self {
self.set_password(password);
self
}
#[must_use]
pub fn with_multiline(mut self, multiline: bool) -> Self {
self.set_multiline(multiline);
self
}
#[must_use]
pub fn with_disabled(mut self, disabled: bool) -> Self {
self.set_disabled(disabled);
self
}
pub fn insert_str(&mut self, text: &str) {
if self.get_disabled() || text.is_empty() {
return;
}
let pos: usize = self.get_caret().min(self.get_value().len());
self.get_mut_value().insert_str(pos, text);
self.set_caret(pos + text.len());
}
pub fn backspace(&mut self) {
if self.get_disabled() || self.get_caret() == 0 {
return;
}
let pos: usize = self.get_caret().min(self.get_value().len());
let prev_char_start: usize = self.get_value()[..pos]
.char_indices()
.next_back()
.map(|(idx, _): (usize, char)| idx)
.unwrap_or(0);
self.get_mut_value().replace_range(prev_char_start..pos, "");
self.set_caret(prev_char_start);
}
}