Files
dioxus/packages/html/src/events/image.rs
T
Evan Almloff d8026f8824 Fix autofill event downcast - ignore events of the wrong type (#5379)
* fix autofill events

* ignore the wrong type of event like we do in desktop

* check the event type before dispatch instead of panicing on downcast

* clean up conversion

* remove example

* pull out shared event list

* pull in deserialize_raw

* unified event list

* prettier comment

* restore test

* restore alias

* normal doc comments

* move over comments

* minimize diff

* remove alias, was moved into the macro
2026-04-07 13:18:24 -07:00

97 lines
2.4 KiB
Rust

use dioxus_core::Event;
pub type ImageEvent = Event<ImageData>;
pub struct ImageData {
inner: Box<dyn HasImageData>,
}
impl<E: HasImageData> From<E> for ImageData {
fn from(e: E) -> Self {
Self { inner: Box::new(e) }
}
}
impl std::fmt::Debug for ImageData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ImageData")
.field("load_error", &self.load_error())
.finish()
}
}
impl PartialEq for ImageData {
fn eq(&self, other: &Self) -> bool {
self.load_error() == other.load_error()
}
}
impl ImageData {
/// Create a new ImageData
pub fn new(e: impl HasImageData) -> Self {
Self { inner: Box::new(e) }
}
/// If the renderer encountered an error while loading the image
pub fn load_error(&self) -> bool {
self.inner.load_error()
}
pub fn downcast<T: 'static>(&self) -> Option<&T> {
self.inner.as_any().downcast_ref::<T>()
}
}
#[cfg(feature = "serialize")]
/// A serialized version of ImageData
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
pub struct SerializedImageData {
#[cfg_attr(feature = "serialize", serde(default))]
load_error: bool,
}
#[cfg(feature = "serialize")]
impl From<&ImageData> for SerializedImageData {
fn from(data: &ImageData) -> Self {
Self {
load_error: data.load_error(),
}
}
}
#[cfg(feature = "serialize")]
impl HasImageData for SerializedImageData {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn load_error(&self) -> bool {
self.load_error
}
}
#[cfg(feature = "serialize")]
impl serde::Serialize for ImageData {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
SerializedImageData::from(self).serialize(serializer)
}
}
#[cfg(feature = "serialize")]
impl<'de> serde::Deserialize<'de> for ImageData {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let data = SerializedImageData::deserialize(deserializer)?;
Ok(Self {
inner: Box::new(data),
})
}
}
/// A trait for any object that has the data for an image event
pub trait HasImageData: std::any::Any {
/// If the renderer encountered an error while loading the image
fn load_error(&self) -> bool;
/// return self as Any
fn as_any(&self) -> &dyn std::any::Any;
}