euv-core 0.47.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Value equality for `ImageBuffer` with O(1) identity and shared-storage fast paths.
impl PartialEq for ImageBuffer {
    /// Compares two image buffers by dimensions and RGBA8 pixel content, short-circuiting on matching image ID or shared `Arc` storage.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Left-hand image buffer.
    /// - `&Self` - Right-hand image buffer.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when both buffers have identical dimensions and pixel bytes.
    fn eq(&self, other: &Self) -> bool {
        if self.get_width() != other.get_width() || self.get_height() != other.get_height() {
            return false;
        }
        if self.get_id() != 0 && self.get_id() == other.get_id() {
            return true;
        }
        Arc::ptr_eq(self.pixels_arc(), other.pixels_arc())
            || self.pixels_slice() == other.pixels_slice()
    }
}

/// Reflexive equality marker for `ImageBuffer`.
impl Eq for ImageBuffer {}

/// Helper operations for `ImageBuffer` and `InputNodeState`.
impl ImageBuffer {
    /// Constructs a zero-copy shared RGBA8 `ImageBuffer` and assigns a unique monotonic identifier.
    ///
    /// # Arguments
    ///
    /// - `u32` - Width in pixels.
    /// - `u32` - Height in pixels.
    /// - `P` - Packed RGBA8 pixel storage convertible into `Arc<[u8]>`.
    ///
    /// # Returns
    ///
    /// - `Self` - Populated zero-copy `ImageBuffer`.
    #[must_use]
    pub fn new<P>(width: u32, height: u32, pixels: P) -> Self
    where
        P: Into<Arc<[u8]>>,
    {
        let id: u64 = NEXT_IMAGE_ID.fetch_add(1, Ordering::Relaxed);
        let shared: Arc<[u8]> = pixels.into();
        Self {
            id,
            width,
            height,
            pixels: shared,
        }
    }

    /// Returns a borrowed slice over the packed RGBA8 pixel bytes.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Source image buffer.
    ///
    /// # Returns
    ///
    /// - `&[u8]` - Packed RGBA8 pixel slice.
    #[must_use]
    pub fn get_pixels(&self) -> &[u8] {
        self.pixels.as_ref()
    }

    /// Returns a borrowed slice over the packed RGBA8 pixel bytes.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Source image buffer.
    ///
    /// # Returns
    ///
    /// - `&[u8]` - Packed RGBA8 pixel slice.
    #[must_use]
    pub fn pixels_slice(&self) -> &[u8] {
        self.get_pixels()
    }

    /// Returns a borrowed reference to the underlying shared `Arc<[u8]>` pixel storage.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Source image buffer.
    ///
    /// # Returns
    ///
    /// - `&Arc<[u8]>` - Shared atomic reference-counted pixel slice.
    #[must_use]
    pub fn pixels_arc(&self) -> &Arc<[u8]> {
        let Self { pixels, .. } = self;
        pixels
    }

    /// Creates a solid-color RGBA8 image buffer for icons, placeholders, or tests.
    ///
    /// # Arguments
    ///
    /// - `u32` - Width in pixels.
    /// - `u32` - Height in pixels.
    /// - `Color` - Fill color.
    ///
    /// # Returns
    ///
    /// - `Self` - Populated `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)
    }
}

/// Text editing and fluent construction helpers for `InputNodeState`.
impl InputNodeState {
    /// Creates an enabled single-line `InputNodeState` initialized with `value` and caret at the end.
    ///
    /// # Arguments
    ///
    /// - `S` - Initial input text value.
    ///
    /// # Returns
    ///
    /// - `Self` - Ready `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)
    }

    /// Sets placeholder text displayed when the input value is empty.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source input state.
    /// - `S` - Placeholder text.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated input state.
    #[must_use]
    pub fn with_placeholder<S>(mut self, placeholder: S) -> Self
    where
        S: Into<String>,
    {
        self.set_placeholder(placeholder.into());
        self
    }

    /// Toggles masked password rendering.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source input state.
    /// - `bool` - Password mode flag.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated input state.
    #[must_use]
    pub fn with_password(mut self, password: bool) -> Self {
        self.set_password(password);
        self
    }

    /// Toggles multi-line text editing.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source input state.
    /// - `bool` - Multiline flag.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated input state.
    #[must_use]
    pub fn with_multiline(mut self, multiline: bool) -> Self {
        self.set_multiline(multiline);
        self
    }

    /// Toggles disabled state preventing user edits.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source input state.
    /// - `bool` - Disabled flag.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated input state.
    #[must_use]
    pub fn with_disabled(mut self, disabled: bool) -> Self {
        self.set_disabled(disabled);
        self
    }

    /// Inserts `text` at the current caret position and advances the caret.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable input state.
    /// - `&str` - UTF-8 text to insert.
    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());
    }

    /// Removes the character immediately preceding the caret if any.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable input state.
    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);
    }
}