Repository: servo/servo
File: components/script/dom/visualviewport.rs (Rust)

Code before:
use crate::dom::bindings::reflector::reflect_dom_object;
use crate::dom::eventtarget::EventTarget;
use crate::dom::window::Window;

/// <https://drafts.csswg.org/cssom-view/#the-visualviewport-interface>
#[dom_struct]
pub(crate) struct VisualViewport {
    eventtarget: EventTarget,

    /// <https://drafts.csswg.org/cssom-view/#dom-visualviewport-offsetleft>
    offset_left: Cell<f64>,

    /// <https://drafts.csswg.org/cssom-view/#dom-visualviewport-offsettop>
    offset_top: Cell<f64>,

    /// <https://drafts.csswg.org/cssom-view/#dom-visualviewport-pageleft>
    page_left: Cell<f64>,

    /// <https://drafts.csswg.org/cssom-view/#dom-visualviewport-pagetop>
    page_top: Cell<f64>,

    /// <https://drafts.csswg.org/cssom-view/#dom-visualviewport-width>
    width: Cell<f64>,

    /// <https://drafts.csswg.org/cssom-view/#dom-visualviewport-height>
    height: Cell<f64>,

    /// <https://drafts.csswg.org/cssom-view/#dom-visualviewport-scale>
    scale: Cell<f64>,
}

impl VisualViewport {
    fn new_inherited(
        offset_left: f64,
        offset_top: f64,
        page_left: f64,
        page_top: f64,
        width: f64,
        height: f64,
        scale: f64,
    ) -> Self {
        Self {
            eventtarget: EventTarget::new_inherited(),
            offset_left: Cell::new(offset_left),
            offset_top: Cell::new(offset_top),
            page_left: Cell::new(page_left),
            page_top: Cell::new(page_top),
            width: Cell::new(width),
            height: Cell::new(height),
            scale: Cell::new(scale),
        }

Review comment (suggestion):
Please just store the rect of the pinch zoom viewport in relation to the layout viewport for now. The rest will be calculated dynamically.

```suggestion
    viewport_rect: Cell<Rect<f64, CSSPixel>>,
```

Code after:
use script_bindings::num::Finite;
use script_bindings::root::{Dom, DomRoot};
use script_bindings::script_runtime::CanGc;
use style_traits::CSSPixel;
use webrender_api::units::DevicePixel;

use crate::dom::bindings::reflector::reflect_dom_object;
use crate::dom::eventtarget::EventTarget;
use crate::dom::window::Window;

/// <https://drafts.csswg.org/cssom-view/#the-visualviewport-interface>
#[dom_struct]
pub(crate) struct VisualViewport {
    eventtarget: EventTarget,

    /// The associated [`Window`] for this [`VisualViewport`],
    window: Dom<Window>,

    /// The rectangle bound of the [`VisualViewport`], relative to the layout viewport.
    #[no_trace]
    viewport_rect: Cell<Rect<f32, CSSPixel>>,

    /// The scale factor of [`VisualViewport`], which is also commonly known as pinch-zoom.
    /// <https://drafts.csswg.org/cssom-view/#scale-factor>
    #[no_trace]
    scale: Cell<Scale<f32, DevicePixel, DevicePixel>>,
}

impl VisualViewport {
    fn new_inherited(
        window: &Window,
        viewport_rect: Rect<f32, CSSPixel>,
        scale: Scale<f32, DevicePixel, DevicePixel>,
    ) -> Self {
        Self {
            eventtarget: EventTarget::new_inherited(),
            window: Dom::from_ref(window),
            viewport_rect: Cell::new(viewport_rect),
            scale: Cell::new(scale),
        }
    }

    /// The initial visual viewport based on a layout viewport relative to the initial containing block, where
    /// the dimension would be the same as layout viewport leaving the offset and the scale to its default value.
    pub(crate) fn new_from_layout_viewport(
        window: &Window,
        viewport_size: Size2D<f32, CSSPixel>,
        can_gc: CanGc,
    ) -> DomRoot<Self> {
        reflect_dom_object(
            Box::new(Self::new_inherited(