Add the onresize event handler to Element (#2479)

* Add the capability to handle resize event for web target

* Add the capability to handle resize event for desktop target

* Return all the sizes, not just the first one

* Fix conversion from platform to generic ResizedData for liveview

* Update the generated interpreter js code base

* Fix clippy warnings

* Fix inconsistent use of block_size and inline_size

* Rename `onresized` event to `onresize`

* Remove the the special-casing logic from the binding logic

* Propagating the resize events using CustomEvent

* Fix case convention in core ts

* revert changes to unified bindings

* Cleanup as suggested

* add a resize example

* Fix desktop resize events

* remove tracing from resize example

* use the raw resize entry so we can downcast on web

* remove unused ResizeEventDetail

---------

Co-authored-by: Evan Almloff <evanalmloff@gmail.com>
This commit is contained in:
ASR-ASUandEvan Almloff authored and GitHub committed 2024-08-15 01:23:49 +00:00
1 parent 4d6fb74e87
commit 2f49a89638
19 files changed
+442 -50

No files matched your search

+11
View File
@@ -134,6 +134,8 @@ pub trait HtmlEventConverter: Send + Sync {
fn convert_mouse_data(&self, event: &PlatformEventData) -> MouseData;
/// Convert a general event to a pointer data event
fn convert_pointer_data(&self, event: &PlatformEventData) -> PointerData;
/// Convert a general event to a resize data event
fn convert_resize_data(&self, event: &PlatformEventData) -> ResizeData;
/// Convert a general event to a scroll data event
fn convert_scroll_data(&self, event: &PlatformEventData) -> ScrollData;
/// Convert a general event to a selection data event
@@ -220,6 +222,12 @@ impl From<&PlatformEventData> for PointerData {
}
}
impl From<&PlatformEventData> for ResizeData {
fn from(val: &PlatformEventData) -> Self {
with_event_converter(|c| c.convert_resize_data(val))
}
}
impl From<&PlatformEventData> for ScrollData {
fn from(val: &PlatformEventData) -> Self {
with_event_converter(|c| c.convert_scroll_data(val))
@@ -268,6 +276,7 @@ mod media;
mod mounted;
mod mouse;
mod pointer;
mod resize;
mod scroll;
mod selection;
mod toggle;
@@ -287,6 +296,7 @@ pub use media::*;
pub use mounted::*;
pub use mouse::*;
pub use pointer::*;
pub use resize::*;
pub use scroll::*;
pub use selection::*;
pub use toggle::*;
@@ -367,6 +377,7 @@ pub fn event_bubbles(evt: &str) -> bool {
"playing" => false,
"progress" => false,
"ratechange" => false,
"resize" => false,
"seeked" => false,
"seeking" => false,
"stalled" => false,
+168
View File
@@ -0,0 +1,168 @@
use std::fmt::{Display, Formatter};
pub struct ResizeData {
inner: Box<dyn HasResizeData>,
}
impl<E: HasResizeData> From<E> for ResizeData {
fn from(e: E) -> Self {
Self { inner: Box::new(e) }
}
}
impl ResizeData {
/// Create a new ResizeData
pub fn new(inner: impl HasResizeData + 'static) -> Self {
Self {
inner: Box::new(inner),
}
}
/// Get the border box size of the observed element
pub fn get_border_box_size(&self) -> ResizeResult<PixelsSize> {
self.inner.get_border_box_size()
}
/// Get the content box size of the observed element
pub fn get_content_box_size(&self) -> ResizeResult<PixelsSize> {
self.inner.get_content_box_size()
}
/// Downcast this event to a concrete event type
pub fn downcast<T: 'static>(&self) -> Option<&T> {
self.inner.as_any().downcast_ref::<T>()
}
}
impl std::fmt::Debug for ResizeData {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ResizeData")
.field("border_box_size", &self.inner.get_border_box_size())
.field("content_box_size", &self.inner.get_content_box_size())
.finish()
}
}
impl PartialEq for ResizeData {
fn eq(&self, _: &Self) -> bool {
true
}
}
#[cfg(feature = "serialize")]
/// A serialized version of ResizeData
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
pub struct SerializedResizeData {
pub border_box_size: PixelsSize,
pub content_box_size: PixelsSize,
}
#[cfg(feature = "serialize")]
impl SerializedResizeData {
/// Create a new SerializedResizeData
pub fn new(border_box_size: PixelsSize, content_box_size: PixelsSize) -> Self {
Self {
border_box_size,
content_box_size,
}
}
}
#[cfg(feature = "serialize")]
impl From<&ResizeData> for SerializedResizeData {
fn from(data: &ResizeData) -> Self {
Self::new(
data.get_border_box_size().unwrap(),
data.get_content_box_size().unwrap(),
)
}
}
#[cfg(feature = "serialize")]
impl HasResizeData for SerializedResizeData {
/// Get the border box size of the observed element
fn get_border_box_size(&self) -> ResizeResult<PixelsSize> {
Ok(self.border_box_size)
}
/// Get the content box size of the observed element
fn get_content_box_size(&self) -> ResizeResult<PixelsSize> {
Ok(self.content_box_size)
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
#[cfg(feature = "serialize")]
impl serde::Serialize for ResizeData {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
SerializedResizeData::from(self).serialize(serializer)
}
}
#[cfg(feature = "serialize")]
impl<'de> serde::Deserialize<'de> for ResizeData {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let data = SerializedResizeData::deserialize(deserializer)?;
Ok(Self {
inner: Box::new(data),
})
}
}
pub trait HasResizeData: std::any::Any {
/// Get the border box size of the observed element
fn get_border_box_size(&self) -> ResizeResult<PixelsSize> {
Err(ResizeError::NotSupported)
}
/// Get the content box size of the observed element
fn get_content_box_size(&self) -> ResizeResult<PixelsSize> {
Err(ResizeError::NotSupported)
}
/// return self as Any
fn as_any(&self) -> &dyn std::any::Any;
}
use dioxus_core::Event;
use crate::geometry::PixelsSize;
pub type ResizeEvent = Event<ResizeData>;
impl_event! {
ResizeData;
/// onresize
onresize
}
/// The ResizeResult type for the ResizeData
pub type ResizeResult<T> = Result<T, ResizeError>;
#[derive(Debug)]
/// The error type for the MountedData
#[non_exhaustive]
pub enum ResizeError {
/// The renderer does not support the requested operation
NotSupported,
/// The element was not found
OperationFailed(Box<dyn std::error::Error>),
}
impl Display for ResizeError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ResizeError::NotSupported => {
write!(f, "The renderer does not support the requested operation")
}
ResizeError::OperationFailed(e) => {
write!(f, "The operation failed: {}", e)
}
}
}
}
impl std::error::Error for ResizeError {}