Increase accuracy of various Web APIs (#2946)
This commit is contained in:
parent
db8de03142
commit
c4d70d75c1
9 changed files with 73 additions and 38 deletions
|
|
@ -28,6 +28,7 @@ pub struct Canvas {
|
|||
common: Common,
|
||||
id: WindowId,
|
||||
pub has_focus: Arc<AtomicBool>,
|
||||
pub is_intersecting: Option<bool>,
|
||||
on_touch_start: Option<EventListenerHandle<dyn FnMut(Event)>>,
|
||||
on_touch_end: Option<EventListenerHandle<dyn FnMut(Event)>>,
|
||||
on_focus: Option<EventListenerHandle<dyn FnMut(FocusEvent)>>,
|
||||
|
|
@ -97,6 +98,7 @@ impl Canvas {
|
|||
},
|
||||
id,
|
||||
has_focus: Arc::new(AtomicBool::new(false)),
|
||||
is_intersecting: None,
|
||||
on_touch_start: None,
|
||||
on_touch_end: None,
|
||||
on_blur: None,
|
||||
|
|
@ -134,11 +136,29 @@ impl Canvas {
|
|||
|
||||
pub fn position(&self) -> LogicalPosition<f64> {
|
||||
let bounds = self.common.raw.get_bounding_client_rect();
|
||||
|
||||
LogicalPosition {
|
||||
let mut position = LogicalPosition {
|
||||
x: bounds.x(),
|
||||
y: bounds.y(),
|
||||
};
|
||||
|
||||
let document = self.window().document().expect("Failed to obtain document");
|
||||
|
||||
if document.contains(Some(self.raw())) {
|
||||
let style = self
|
||||
.window()
|
||||
.get_computed_style(self.raw())
|
||||
.expect("Failed to obtain computed style")
|
||||
// this can't fail: we aren't using a pseudo-element
|
||||
.expect("Invalid pseudo-element");
|
||||
if style.get_property_value("display").unwrap() != "none" {
|
||||
position.x += super::style_size_property(&style, "border-left-width")
|
||||
+ super::style_size_property(&style, "padding-left");
|
||||
position.y += super::style_size_property(&style, "border-top-width")
|
||||
+ super::style_size_property(&style, "padding-top");
|
||||
}
|
||||
}
|
||||
|
||||
position
|
||||
}
|
||||
|
||||
pub fn old_size(&self) -> PhysicalSize<u32> {
|
||||
|
|
|
|||
|
|
@ -12,19 +12,9 @@ impl IntersectionObserverHandle {
|
|||
where
|
||||
F: 'static + FnMut(bool),
|
||||
{
|
||||
let mut skip = true;
|
||||
let closure = Closure::new(move |entries: Array| {
|
||||
let entry: IntersectionObserverEntry = entries.get(0).unchecked_into();
|
||||
|
||||
let is_intersecting = entry.is_intersecting();
|
||||
|
||||
// skip first intersection
|
||||
if skip && is_intersecting {
|
||||
skip = false;
|
||||
return;
|
||||
}
|
||||
|
||||
callback(is_intersecting);
|
||||
callback(entry.is_intersecting());
|
||||
});
|
||||
let observer = IntersectionObserver::new(closure.as_ref().unchecked_ref())
|
||||
// we don't provide any `options`
|
||||
|
|
|
|||
|
|
@ -75,13 +75,17 @@ pub fn set_canvas_size(
|
|||
) {
|
||||
let document = window.document().expect("Failed to obtain document");
|
||||
|
||||
if !document.contains(Some(raw)) {
|
||||
return;
|
||||
}
|
||||
|
||||
let style = window
|
||||
.get_computed_style(raw)
|
||||
.expect("Failed to obtain computed style")
|
||||
// this can't fail: we aren't using a pseudo-element
|
||||
.expect("Invalid pseudo-element");
|
||||
|
||||
if !document.contains(Some(raw)) || style.get_property_value("display").unwrap() == "none" {
|
||||
if style.get_property_value("display").unwrap() == "none" {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -144,20 +148,4 @@ pub fn is_visible(window: &web_sys::Window) -> bool {
|
|||
document.visibility_state() == VisibilityState::Visible
|
||||
}
|
||||
|
||||
pub fn is_intersecting(window: &web_sys::Window, canvas: &HtmlCanvasElement) -> bool {
|
||||
let rect = canvas.get_bounding_client_rect();
|
||||
// This should never panic.
|
||||
let window_width = window.inner_width().unwrap().as_f64().unwrap() as i32;
|
||||
let window_height = window.inner_height().unwrap().as_f64().unwrap() as i32;
|
||||
let left = rect.left() as i32;
|
||||
let width = rect.width() as i32;
|
||||
let top = rect.top() as i32;
|
||||
let height = rect.height() as i32;
|
||||
|
||||
let horizontal = left <= window_width && left + width >= 0;
|
||||
let vertical = top <= window_height && top + height >= 0;
|
||||
|
||||
horizontal && vertical
|
||||
}
|
||||
|
||||
pub type RawCanvasType = HtmlCanvasElement;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue