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

Code before:
    }

    // https://html.spec.whatwg.org/multipage/#globaleventhandlers
    global_event_handlers!();

    // https://html.spec.whatwg.org/multipage/#windoweventhandlers
    window_event_handlers!();

    /// <https://developer.mozilla.org/en-US/docs/Web/API/Window/screen>
    fn Screen(&self, can_gc: CanGc) -> DomRoot<Screen> {
        self.screen.or_init(|| Screen::new(self, can_gc))
    }

    /// <https://drafts.csswg.org/cssom-view/#visualviewport>
    fn GetVisualViewport(&self, can_gc: CanGc) -> Option<DomRoot<VisualViewport>> {
        // Visual viewport is only relevant when the document is fully active.
        if !self.document.get()?.is_fully_active() {
            return None;
        }

        // TODO(#41341): we are only initializing the visual viewport here, but it is never updated.
        Some(self.visual_viewport.or_init(|| {
            VisualViewport::new_from_layout_viewport(
                self,
                Rect::new(
                    self.scroll_offset().to_point().cast_unit(),
                    self.viewport_details().size,
                ),
                can_gc,
            )
        }))
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-windowbase64-btoa>
    fn Btoa(&self, btoa: DOMString) -> Fallible<DOMString> {
        base64_btoa(btoa)
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-windowbase64-atob>
    fn Atob(&self, atob: DOMString) -> Fallible<DOMString> {
        base64_atob(atob)
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-window-requestanimationframe>
    fn RequestAnimationFrame(&self, callback: Rc<FrameRequestCallback>) -> u32 {
        self.Document()
            .request_animation_frame(AnimationFrameCallback::FrameRequestCallback { callback })
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-window-cancelanimationframe>
    fn CancelAnimationFrame(&self, ident: u32) {

Review comment (suggestion):
Please don't use `cast_unit()` if you can help it. Instead actually use units.

Code after:
    }

    // https://html.spec.whatwg.org/multipage/#globaleventhandlers
    global_event_handlers!();

    // https://html.spec.whatwg.org/multipage/#windoweventhandlers
    window_event_handlers!();

    /// <https://developer.mozilla.org/en-US/docs/Web/API/Window/screen>
    fn Screen(&self, can_gc: CanGc) -> DomRoot<Screen> {
        self.screen.or_init(|| Screen::new(self, can_gc))
    }

    /// <https://drafts.csswg.org/cssom-view/#dom-window-visualviewport>
    fn GetVisualViewport(&self, can_gc: CanGc) -> Option<DomRoot<VisualViewport>> {
        // > If the associated document is fully active, the visualViewport attribute must return the
        // > VisualViewport object associated with the Window object’s associated document. Otherwise,
        // > it must return null.
        if !self.Document().is_fully_active() {
            return None;
        }

        // TODO(#41341): we are only initializing the visual viewport here, but it is never updated.
        Some(self.visual_viewport.or_init(|| {
            VisualViewport::new_from_layout_viewport(self, self.viewport_details().size, can_gc)
        }))
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-windowbase64-btoa>
    fn Btoa(&self, btoa: DOMString) -> Fallible<DOMString> {
        base64_btoa(btoa)
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-windowbase64-atob>
    fn Atob(&self, atob: DOMString) -> Fallible<DOMString> {
        base64_atob(atob)
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-window-requestanimationframe>
    fn RequestAnimationFrame(&self, callback: Rc<FrameRequestCallback>) -> u32 {
        self.Document()
            .request_animation_frame(AnimationFrameCallback::FrameRequestCallback { callback })
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-window-cancelanimationframe>
    fn CancelAnimationFrame(&self, ident: u32) {
        let doc = self.Document();
        doc.cancel_animation_frame(ident);
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-window-postmessage>