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

+46 -2
View File
@@ -4,15 +4,17 @@ use crate::events::{
TransitionData, WheelData,
};
use crate::file_data::HasFileData;
use crate::geometry::PixelsSize;
use crate::geometry::{ClientPoint, ElementPoint, PagePoint, ScreenPoint};
use crate::input_data::{decode_key_location, decode_mouse_button_set, MouseButton};
use crate::prelude::*;
use keyboard_types::{Code, Key, Modifiers};
use std::str::FromStr;
use wasm_bindgen::JsCast;
use web_sys::{js_sys, ResizeObserverEntry};
use web_sys::{
AnimationEvent, CompositionEvent, Event, KeyboardEvent, MouseEvent, PointerEvent, Touch,
TouchEvent, TransitionEvent, WheelEvent,
AnimationEvent, CompositionEvent, CustomEvent, Event, KeyboardEvent, MouseEvent, PointerEvent,
Touch, TouchEvent, TransitionEvent, WheelEvent,
};
macro_rules! uncheck_convert {
@@ -51,6 +53,22 @@ uncheck_convert![
web_sys::FocusEvent => FocusData,
];
impl From<Event> for ResizeData {
#[inline]
fn from(e: Event) -> Self {
<ResizeData as From<&Event>>::from(&e)
}
}
impl From<&Event> for ResizeData {
#[inline]
fn from(e: &Event) -> Self {
let e: &CustomEvent = e.unchecked_ref();
let value = e.detail();
Self::from(value.unchecked_into::<ResizeObserverEntry>())
}
}
impl HasCompositionData for CompositionEvent {
fn data(&self) -> std::string::String {
self.data().unwrap_or_default()
@@ -571,6 +589,32 @@ impl crate::RenderedElementBacking for web_sys::Element {
}
}
fn extract_first_size(resize_observer_output: js_sys::Array) -> ResizeResult<PixelsSize> {
let first = resize_observer_output.get(0);
let size = first.unchecked_into::<web_sys::ResizeObserverSize>();
// inline_size matches the width of the element if its writing-mode is horizontal, the height otherwise
let inline_size = size.inline_size();
// block_size matches the height of the element if its writing-mode is horizontal, the width otherwise
let block_size = size.block_size();
Ok(PixelsSize::new(inline_size, block_size))
}
impl HasResizeData for ResizeObserverEntry {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn get_border_box_size(&self) -> ResizeResult<PixelsSize> {
extract_first_size(self.border_box_size())
}
fn get_content_box_size(&self) -> ResizeResult<PixelsSize> {
extract_first_size(self.content_box_size())
}
}
impl HasScrollData for Event {
fn as_any(&self) -> &dyn std::any::Any {
self