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

Code before:
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::cell::Cell;

use dom_struct::dom_struct;
use euclid::Rect;
use script_bindings::codegen::GenericBindings::VisualViewportBinding::VisualViewportMethods;
use script_bindings::num::Finite;
use script_bindings::root::{Dom, DomRoot};
use script_bindings::script_runtime::CanGc;
use style_traits::CSSPixel;

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 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> {

Review comment (suggestion):
```suggestion
    /// The rectangle bound of the [`VisualViewport`], relative to the layout viewport.
```

Code after:
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::cell::Cell;

use dom_struct::dom_struct;
use euclid::{Rect, Scale, Size2D};
use script_bindings::codegen::GenericBindings::VisualViewportBinding::VisualViewportMethods;
use script_bindings::codegen::GenericBindings::WindowBinding::WindowMethods;
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),
        }
    }