provide nicer error types
This commit is contained in:
1 parent
920fcf728c
commit
1aad285853
2 files changed
+72
-29
No files matched your search
@@ -4,11 +4,13 @@ use crate::events::{
|
||||
};
|
||||
use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint};
|
||||
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 std::convert::TryInto;
|
||||
use std::str::FromStr;
|
||||
use wasm_bindgen::JsCast;
|
||||
use wasm_bindgen::{JsCast, JsValue};
|
||||
use web_sys::{
|
||||
AnimationEvent, CompositionEvent, Event, KeyboardEvent, MouseEvent, PointerEvent,
|
||||
ScrollIntoViewOptions, TouchEvent, TransitionEvent, WheelEvent,
|
||||
@@ -201,19 +203,19 @@ impl From<&web_sys::Element> for MountedData {
|
||||
}
|
||||
|
||||
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();
|
||||
Some(euclid::Rect::new(
|
||||
Ok(euclid::Rect::new(
|
||||
euclid::Point2D::new(rect.left(), rect.top()),
|
||||
euclid::Size2D::new(rect.width(), rect.height()),
|
||||
))
|
||||
}
|
||||
|
||||
fn get_raw_element(&self) -> Option<&dyn std::any::Any> {
|
||||
Some(self)
|
||||
fn get_raw_element(&self) -> MountedResult<&dyn std::any::Any> {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn scroll_to(&self, behavior: ScrollBehavior) -> Option<()> {
|
||||
fn scroll_to(&self, behavior: ScrollBehavior) -> MountedResult<()> {
|
||||
match behavior {
|
||||
ScrollBehavior::Instant => self.scroll_into_view_with_scroll_into_view_options(
|
||||
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<()> {
|
||||
self.dyn_ref::<web_sys::HtmlElement>().and_then(|e| {
|
||||
if focus {
|
||||
e.focus().ok()
|
||||
} else {
|
||||
e.blur().ok()
|
||||
}
|
||||
})
|
||||
fn set_focus(&self, focus: bool) -> MountedResult<()> {
|
||||
self.dyn_ref::<web_sys::HtmlElement>()
|
||||
.ok_or_else(|| MountedError::OperationFailed(Box::new(FocusError(self.into()))))
|
||||
.and_then(|e| {
|
||||
(if focus { e.focus() } else { e.blur() })
|
||||
.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 {}
|
||||
Reference in new issue
Block a user