euv-core 0.3.9

A declarative, cross-platform UI framework for Rust with virtual DOM, reactive signals, and HTML macros for WebAssembly.
Documentation
use crate::*;

/// Maps each `NativeEventName` variant to its corresponding DOM event string.
impl NativeEventName {
    /// Returns the string representation of this event name for DOM binding.
    pub fn as_str(&self) -> String {
        match self {
            NativeEventName::Click => "click".to_string(),
            NativeEventName::DblClick => "dblclick".to_string(),
            NativeEventName::MouseDown => "mousedown".to_string(),
            NativeEventName::MouseUp => "mouseup".to_string(),
            NativeEventName::MouseMove => "mousemove".to_string(),
            NativeEventName::MouseEnter => "mouseenter".to_string(),
            NativeEventName::MouseLeave => "mouseleave".to_string(),
            NativeEventName::MouseOver => "mouseover".to_string(),
            NativeEventName::MouseOut => "mouseout".to_string(),
            NativeEventName::ContextMenu => "contextmenu".to_string(),
            NativeEventName::Input => "input".to_string(),
            NativeEventName::KeyDown => "keydown".to_string(),
            NativeEventName::KeyUp => "keyup".to_string(),
            NativeEventName::KeyPress => "keypress".to_string(),
            NativeEventName::Focus => "focus".to_string(),
            NativeEventName::Blur => "blur".to_string(),
            NativeEventName::FocusIn => "focusin".to_string(),
            NativeEventName::FocusOut => "focusout".to_string(),
            NativeEventName::Submit => "submit".to_string(),
            NativeEventName::Change => "change".to_string(),
            NativeEventName::Drag => "drag".to_string(),
            NativeEventName::DragStart => "dragstart".to_string(),
            NativeEventName::DragEnd => "dragend".to_string(),
            NativeEventName::DragOver => "dragover".to_string(),
            NativeEventName::DragEnter => "dragenter".to_string(),
            NativeEventName::DragLeave => "dragleave".to_string(),
            NativeEventName::Drop => "drop".to_string(),
            NativeEventName::TouchStart => "touchstart".to_string(),
            NativeEventName::TouchEnd => "touchend".to_string(),
            NativeEventName::TouchMove => "touchmove".to_string(),
            NativeEventName::TouchCancel => "touchcancel".to_string(),
            NativeEventName::Wheel => "wheel".to_string(),
            NativeEventName::Copy => "copy".to_string(),
            NativeEventName::Cut => "cut".to_string(),
            NativeEventName::Paste => "paste".to_string(),
            NativeEventName::Play => "play".to_string(),
            NativeEventName::Pause => "pause".to_string(),
            NativeEventName::Ended => "ended".to_string(),
            NativeEventName::LoadedData => "loadeddata".to_string(),
            NativeEventName::CanPlay => "canplay".to_string(),
            NativeEventName::VolumeChange => "volumechange".to_string(),
            NativeEventName::TimeUpdate => "timeupdate".to_string(),
            NativeEventName::HashChange => "hashchange".to_string(),
            NativeEventName::EuvSignalUpdate => "__euv_signal_update__".to_string(),
            NativeEventName::Other(name) => name.clone(),
        }
    }
}

/// Implements `Display` for `NativeEventName` by delegating to `as_str`.
///
/// This automatically provides the `ToString` trait via blanket implementation.
impl std::fmt::Display for NativeEventName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Implementation of event handler construction, cloning, and invocation.
impl NativeEventHandler {
    /// Creates a new event handler from an `NativeEventName` enum and callback.
    pub fn new<F>(event_name: NativeEventName, callback: F) -> Self
    where
        F: FnMut(NativeEvent) + 'static,
    {
        NativeEventHandler {
            event_name: event_name.as_str(),
            callback: Rc::new(RefCell::new(callback)),
        }
    }

    /// Invokes the underlying callback with the given event.
    pub fn handle(&self, event: NativeEvent) {
        let mut cb: RefMut<dyn FnMut(NativeEvent)> = self.get_callback().borrow_mut();
        cb(event);
    }
}

/// Clones the event handler, sharing the underlying callback reference.
impl Clone for NativeEventHandler {
    fn clone(&self) -> Self {
        NativeEventHandler {
            event_name: self.get_event_name().clone(),
            callback: Rc::clone(self.get_callback()),
        }
    }
}

/// Converts an owned event handler into an attribute value.
impl IntoEventAttribute for NativeEventHandler {
    fn into_event_attribute(self) -> AttributeValue {
        AttributeValue::Event(self)
    }
}

/// Converts an optional event handler into an attribute value.
impl IntoEventAttribute for Option<NativeEventHandler> {
    fn into_event_attribute(self) -> AttributeValue {
        match self {
            Some(handler) => AttributeValue::Event(handler),
            None => AttributeValue::Text(String::new()),
        }
    }
}