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

Code before:
    eventtarget: EventTarget,

    /// The associated [`Window`] for this visual viewport.
    window: Dom<Window>,

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

    /// The scale factor of visual viewport, which is also commonly known as pinch-zoom.
    /// <https://drafts.csswg.org/cssom-view/#scale-factor>
    scale: Cell<f32>,
}

impl VisualViewport {
    fn new_inherited(window: &Window, viewport_rect: Rect<f32, CSSPixel>, scale: f32) -> 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 it's default value.
    pub(crate) fn new_from_layout_viewport(
        window: &Window,
        viewport_rect: Rect<f32, CSSPixel>,
        can_gc: CanGc,
    ) -> DomRoot<Self> {
        reflect_dom_object(
            Box::new(Self::new_inherited(window, viewport_rect, 1.)),
            window,
            can_gc,
        )
    }
}

impl VisualViewportMethods<crate::DomTypeHolder> for VisualViewport {
    /// <https://drafts.csswg.org/cssom-view/#dom-visualviewport-offsetleft>
    fn OffsetLeft(&self) -> Finite<f64> {
        // > 1. If the visual viewport’s associated document is not fully active, return 0.
        if !self.window.has_fully_active_document() {
            return Finite::wrap(0.);
        }

        // > 2. Otherwise, return the offset of the left edge of the visual viewport from the left edge of the
        // >    layout viewport.
        Finite::wrap(self.viewport_rect.get().min_x() as f64)
    }

Review comment (suggestion):
```suggestion
    /// 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.
```

Code after:
#[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(
                window,
                Rect::from_size(viewport_size),
                Scale::identity(),
            )),
            window,
            can_gc,
        )
    }
}

impl VisualViewportMethods<crate::DomTypeHolder> for VisualViewport {