|
1 | | -use floating_ui_utils::{Rect, Strategy, dom::get_document_element}; |
| 1 | +use floating_ui_utils::{ |
| 2 | + Rect, Strategy, |
| 3 | + dom::{get_computed_style, get_document_element, get_window, is_web_kit}, |
| 4 | +}; |
2 | 5 | use web_sys::Element; |
3 | 6 |
|
4 | | -pub fn get_viewport_rect(element: &Element, _strategy: Strategy) -> Rect { |
5 | | - // let window = get_window(Some(element)); |
| 7 | +use crate::utils::get_window_scroll_bar_x::get_window_scroll_bar_x; |
| 8 | + |
| 9 | +// Safety check: ensure the scrollbar space is reasonable in case this calculation is affected by unusual styles. |
| 10 | +// Most scrollbars leave 15-18px of space. |
| 11 | +const SCROLLBAR_MAX: f64 = 25.0; |
| 12 | + |
| 13 | +pub fn get_viewport_rect(element: &Element, strategy: Strategy) -> Rect { |
| 14 | + let window = get_window(Some(element)); |
6 | 15 | let html = get_document_element(Some(element.into())); |
7 | | - // TODO |
8 | | - // let visual_viewport = window.visual_viewport; |
| 16 | + let visual_viewport = window.visual_viewport(); |
9 | 17 |
|
10 | | - let x = 0.0; |
11 | | - let y = 0.0; |
12 | | - let width = html.client_width() as f64; |
13 | | - let height = html.client_height() as f64; |
| 18 | + let mut x = 0.0; |
| 19 | + let mut y = 0.0; |
| 20 | + let mut width = html.client_width() as f64; |
| 21 | + let mut height = html.client_height() as f64; |
14 | 22 |
|
15 | | - // TODO: visual viewport |
| 23 | + if let Some(visual_viewport) = visual_viewport { |
| 24 | + width = visual_viewport.width(); |
| 25 | + height = visual_viewport.height(); |
| 26 | + |
| 27 | + let visual_viewport_based = is_web_kit(); |
| 28 | + if !visual_viewport_based || strategy == Strategy::Fixed { |
| 29 | + x = visual_viewport.offset_left(); |
| 30 | + y = visual_viewport.offset_top(); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + let window_scrollbar_x = get_window_scroll_bar_x(&html, None); |
| 35 | + // <html> `overflow: hidden` + `scrollbar-gutter: stable` reduces the visual width of the <html>, |
| 36 | + // but this is not considered in the size of `html.client_width`. |
| 37 | + if window_scrollbar_x <= 0.0 { |
| 38 | + let doc = html |
| 39 | + .owner_document() |
| 40 | + .expect("Element should have owner document."); |
| 41 | + let body = doc.body().expect("Document should have body."); |
| 42 | + let body_styles = get_computed_style(&body); |
| 43 | + let body_margin_inline = if doc.compat_mode() == "CSS1Compat" { |
| 44 | + body_styles |
| 45 | + .get_property_value("margin-left") |
| 46 | + .expect("Computed style should have margin left.") |
| 47 | + .parse::<f64>() |
| 48 | + .unwrap_or(0.0) |
| 49 | + + body_styles |
| 50 | + .get_property_value("margin-right") |
| 51 | + .expect("Computed style should have margin right.") |
| 52 | + .parse::<f64>() |
| 53 | + .unwrap_or(0.0) |
| 54 | + } else { |
| 55 | + 0.0 |
| 56 | + }; |
| 57 | + let clipping_stable_scrollbar_width = |
| 58 | + ((html.client_width() as f64) - (body.client_width() as f64) - body_margin_inline) |
| 59 | + .abs(); |
| 60 | + |
| 61 | + if clipping_stable_scrollbar_width <= SCROLLBAR_MAX { |
| 62 | + width -= clipping_stable_scrollbar_width; |
| 63 | + } |
| 64 | + } else if window_scrollbar_x <= SCROLLBAR_MAX { |
| 65 | + // If the <body> scrollbar is on the left, the width needs to be extended |
| 66 | + // by the scrollbar amount so there isn't extra space on the right. |
| 67 | + width += window_scrollbar_x; |
| 68 | + } |
16 | 69 |
|
17 | 70 | Rect { |
18 | 71 | x, |
|
0 commit comments