provide nicer error types

This commit is contained in:
Evan Almloff 2023-03-19 17:02:12 -05:00
parent 920fcf728c
commit 1aad285853
2 changed files with 72 additions and 29 deletions

View file

@ -2,30 +2,34 @@
use euclid::Rect; use euclid::Rect;
use std::{any::Any, rc::Rc}; use std::{
any::Any,
fmt::{Display, Formatter},
rc::Rc,
};
/// An Element that has been rendered and allows reading and modifying information about it. /// An Element that has been rendered and allows reading and modifying information about it.
/// ///
/// Different platforms will have different implementations and different levels of support for this trait. Renderers that do not support specific features will return `None` for those queries. /// Different platforms will have different implementations and different levels of support for this trait. Renderers that do not support specific features will return `None` for those queries.
pub trait RenderedElementBacking { pub trait RenderedElementBacking {
/// Get the renderer specific element for the given id /// Get the renderer specific element for the given id
fn get_raw_element(&self) -> Option<&dyn Any> { fn get_raw_element(&self) -> MountedResult<&dyn Any> {
None Err(MountedError::NotSupported)
} }
/// Get the bounding rectangle of the element relative to the viewport (this does not include the scroll position) /// Get the bounding rectangle of the element relative to the viewport (this does not include the scroll position)
fn get_client_rect(&self) -> Option<Rect<f64, f64>> { fn get_client_rect(&self) -> MountedResult<Rect<f64, f64>> {
None Err(MountedError::NotSupported)
} }
/// Scroll to make the element visible /// Scroll to make the element visible
fn scroll_to(&self, _behavior: ScrollBehavior) -> Option<()> { fn scroll_to(&self, _behavior: ScrollBehavior) -> MountedResult<()> {
None Err(MountedError::NotSupported)
} }
/// Set the focus on the element /// Set the focus on the element
fn set_focus(&self, _focus: bool) -> Option<()> { fn set_focus(&self, _focus: bool) -> MountedResult<()> {
None Err(MountedError::NotSupported)
} }
} }
@ -53,22 +57,22 @@ impl MountedData {
} }
/// Get the renderer specific element for the given id /// Get the renderer specific element for the given id
pub fn get_raw_element(&self) -> Option<&dyn Any> { pub fn get_raw_element(&self) -> MountedResult<&dyn Any> {
self.inner.get_raw_element() self.inner.get_raw_element()
} }
/// Get the bounding rectangle of the element relative to the viewport (this does not include the scroll position) /// Get the bounding rectangle of the element relative to the viewport (this does not include the scroll position)
pub fn get_client_rect(&self) -> Option<Rect<f64, f64>> { pub fn get_client_rect(&self) -> MountedResult<Rect<f64, f64>> {
self.inner.get_client_rect() self.inner.get_client_rect()
} }
/// Scroll to make the element visible /// Scroll to make the element visible
pub fn scroll_to(&self, behavior: ScrollBehavior) -> Option<()> { pub fn scroll_to(&self, behavior: ScrollBehavior) -> MountedResult<()> {
self.inner.scroll_to(behavior) self.inner.scroll_to(behavior)
} }
/// Set the focus on the element /// Set the focus on the element
pub fn set_focus(&self, focus: bool) -> Option<()> { pub fn set_focus(&self, focus: bool) -> MountedResult<()> {
self.inner.set_focus(focus) self.inner.set_focus(focus)
} }
} }
@ -83,3 +87,30 @@ impl_event! [
/// mounted /// mounted
onmounted onmounted
]; ];
/// The MountedResult type for the MountedData
pub type MountedResult<T> = Result<T, MountedError>;
#[derive(Debug)]
/// The error type for the MountedData
pub enum MountedError {
/// The renderer does not support the requested operation
NotSupported,
/// The element was not found
OperationFailed(Box<dyn std::error::Error>),
}
impl Display for MountedError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
MountedError::NotSupported => {
write!(f, "The renderer does not support the requested operation")
}
MountedError::OperationFailed(e) => {
write!(f, "The operation failed: {}", e)
}
}
}
}
impl std::error::Error for MountedError {}

View file

@ -4,11 +4,13 @@ use crate::events::{
}; };
use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint}; use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint};
use crate::input_data::{decode_key_location, decode_mouse_button_set, MouseButton}; use crate::input_data::{decode_key_location, decode_mouse_button_set, MouseButton};
use crate::{DragData, MountedData, RenderedElementBacking, ScrollBehavior}; use crate::{
DragData, MountedData, MountedError, MountedResult, RenderedElementBacking, ScrollBehavior,
};
use keyboard_types::{Code, Key, Modifiers}; use keyboard_types::{Code, Key, Modifiers};
use std::convert::TryInto; use std::convert::TryInto;
use std::str::FromStr; use std::str::FromStr;
use wasm_bindgen::JsCast; use wasm_bindgen::{JsCast, JsValue};
use web_sys::{ use web_sys::{
AnimationEvent, CompositionEvent, Event, KeyboardEvent, MouseEvent, PointerEvent, AnimationEvent, CompositionEvent, Event, KeyboardEvent, MouseEvent, PointerEvent,
ScrollIntoViewOptions, TouchEvent, TransitionEvent, WheelEvent, ScrollIntoViewOptions, TouchEvent, TransitionEvent, WheelEvent,
@ -201,19 +203,19 @@ impl From<&web_sys::Element> for MountedData {
} }
impl RenderedElementBacking for web_sys::Element { impl RenderedElementBacking for web_sys::Element {
fn get_client_rect(&self) -> Option<euclid::Rect<f64, f64>> { fn get_client_rect(&self) -> MountedResult<euclid::Rect<f64, f64>> {
let rect = self.get_bounding_client_rect(); let rect = self.get_bounding_client_rect();
Some(euclid::Rect::new( Ok(euclid::Rect::new(
euclid::Point2D::new(rect.left(), rect.top()), euclid::Point2D::new(rect.left(), rect.top()),
euclid::Size2D::new(rect.width(), rect.height()), euclid::Size2D::new(rect.width(), rect.height()),
)) ))
} }
fn get_raw_element(&self) -> Option<&dyn std::any::Any> { fn get_raw_element(&self) -> MountedResult<&dyn std::any::Any> {
Some(self) Ok(self)
} }
fn scroll_to(&self, behavior: ScrollBehavior) -> Option<()> { fn scroll_to(&self, behavior: ScrollBehavior) -> MountedResult<()> {
match behavior { match behavior {
ScrollBehavior::Instant => self.scroll_into_view_with_scroll_into_view_options( ScrollBehavior::Instant => self.scroll_into_view_with_scroll_into_view_options(
ScrollIntoViewOptions::new().behavior(web_sys::ScrollBehavior::Instant), ScrollIntoViewOptions::new().behavior(web_sys::ScrollBehavior::Instant),
@ -223,16 +225,26 @@ impl RenderedElementBacking for web_sys::Element {
), ),
} }
Some(()) Ok(())
} }
fn set_focus(&self, focus: bool) -> Option<()> { fn set_focus(&self, focus: bool) -> MountedResult<()> {
self.dyn_ref::<web_sys::HtmlElement>().and_then(|e| { self.dyn_ref::<web_sys::HtmlElement>()
if focus { .ok_or_else(|| MountedError::OperationFailed(Box::new(FocusError(self.into()))))
e.focus().ok() .and_then(|e| {
} else { (if focus { e.focus() } else { e.blur() })
e.blur().ok() .map_err(|err| MountedError::OperationFailed(Box::new(FocusError(err))))
} })
})
} }
} }
#[derive(Debug)]
struct FocusError(JsValue);
impl std::fmt::Display for FocusError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "failed to focus element {:?}", self.0)
}
}
impl std::error::Error for FocusError {}