Files
dioxus/packages/html/src/events/composition.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.5 KiB
Rust

use dioxus_core::Event;
pub type CompositionEvent = Event<CompositionData>;
pub struct CompositionData {
inner: Box<dyn HasCompositionData>,
}
impl std::fmt::Debug for CompositionData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CompositionData")
.field("data", &self.data())
.finish()
}
}
impl<E: HasCompositionData> From<E> for CompositionData {
fn from(e: E) -> Self {
Self { inner: Box::new(e) }
}
}
impl PartialEq for CompositionData {
fn eq(&self, other: &Self) -> bool {
self.data() == other.data()
}
}
impl CompositionData {
/// Create a new CompositionData
pub fn new(inner: impl HasCompositionData + 'static) -> Self {
Self {
inner: Box::new(inner),
}
}
/// The characters generated by the input method that raised the event
pub fn data(&self) -> String {
self.inner.data()
}
pub fn downcast<T: 'static>(&self) -> Option<&T> {
self.inner.as_any().downcast_ref()
}
}
#[cfg(feature = "serialize")]
/// A serialized version of CompositionData
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
pub struct SerializedCompositionData {
data: String,
}
#[cfg(feature = "serialize")]
impl From<&CompositionData> for SerializedCompositionData {
fn from(data: &CompositionData) -> Self {
Self { data: data.data() }
}
}
#[cfg(feature = "serialize")]
impl HasCompositionData for SerializedCompositionData {
fn data(&self) -> String {
self.data.clone()
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
#[cfg(feature = "serialize")]
impl serde::Serialize for CompositionData {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
SerializedCompositionData::from(self).serialize(serializer)
}
}
#[cfg(feature = "serialize")]
impl<'de> serde::Deserialize<'de> for CompositionData {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let data = SerializedCompositionData::deserialize(deserializer)?;
Ok(Self {
inner: Box::new(data),
})
}
}
/// A trait for any object that has the data for a composition event
pub trait HasCompositionData: std::any::Any {
/// The characters generated by the input method that raised the event
fn data(&self) -> String;
/// return self as Any
fn as_any(&self) -> &dyn std::any::Any;
}