servo/components/script/dom/screen.rs
Martin Robinson b925c31424
libservo: Start moving WindowMethods to WebViewDelegate (#36223)
`WindowMethods` is used by the embedding layer to get information from
the embedder. This change moves the functionality for getting screen
size and `WebView` offsets to `WebViewDelegate`.

This is important because `WebView`s might be on different screens or
have different offsets on the screen itself, so it makes sense for this
to be per-`WebView` and not global to the embedder. HiDPI and animation
state functionality will move to the embedder in subsequent changes.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>

<!-- Please describe your changes on the following line: -->


---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by
`[X]` when the step is complete, and replace `___` with appropriate
data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes do not require tests because they just modify the
`WebView` API surface a bit.

<!-- Also, please make sure that "Allow edits from maintainers" checkbox
is checked, so that we can help you if you get stuck somewhere along the
way.-->

<!-- Pull requests that do not address these steps are welcome, but they
will require additional verification as part of the review process. -->

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
2025-04-02 11:17:24 +00:00

100 lines
3.2 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 dom_struct::dom_struct;
use euclid::Size2D;
use profile_traits::ipc;
use servo_geometry::DeviceIndependentIntSize;
use style_traits::CSSPixel;
use webrender_traits::CrossProcessCompositorMessage;
use crate::dom::bindings::codegen::Bindings::ScreenBinding::ScreenMethods;
use crate::dom::bindings::num::Finite;
use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::window::Window;
use crate::script_runtime::CanGc;
#[dom_struct]
pub(crate) struct Screen {
reflector_: Reflector,
window: Dom<Window>,
}
impl Screen {
fn new_inherited(window: &Window) -> Screen {
Screen {
reflector_: Reflector::new(),
window: Dom::from_ref(window),
}
}
pub(crate) fn new(window: &Window, can_gc: CanGc) -> DomRoot<Screen> {
reflect_dom_object(Box::new(Screen::new_inherited(window)), window, can_gc)
}
fn screen_size(&self) -> Size2D<u32, CSSPixel> {
let (sender, receiver) =
ipc::channel::<DeviceIndependentIntSize>(self.global().time_profiler_chan().clone())
.unwrap();
self.window
.compositor_api()
.sender()
.send(CrossProcessCompositorMessage::GetScreenSize(
self.window.webview_id(),
sender,
))
.unwrap();
let size = receiver.recv().unwrap_or(Size2D::zero()).to_u32();
Size2D::new(size.width, size.height)
}
fn screen_avail_size(&self) -> Size2D<u32, CSSPixel> {
let (sender, receiver) =
ipc::channel::<DeviceIndependentIntSize>(self.global().time_profiler_chan().clone())
.unwrap();
self.window
.compositor_api()
.sender()
.send(CrossProcessCompositorMessage::GetAvailableScreenSize(
self.window.webview_id(),
sender,
))
.unwrap();
let size = receiver.recv().unwrap_or(Size2D::zero()).to_u32();
Size2D::new(size.width, size.height)
}
}
impl ScreenMethods<crate::DomTypeHolder> for Screen {
// https://drafts.csswg.org/cssom-view/#dom-screen-availwidth
fn AvailWidth(&self) -> Finite<f64> {
Finite::wrap(self.screen_avail_size().width as f64)
}
// https://drafts.csswg.org/cssom-view/#dom-screen-availheight
fn AvailHeight(&self) -> Finite<f64> {
Finite::wrap(self.screen_avail_size().height as f64)
}
// https://drafts.csswg.org/cssom-view/#dom-screen-width
fn Width(&self) -> Finite<f64> {
Finite::wrap(self.screen_size().width as f64)
}
// https://drafts.csswg.org/cssom-view/#dom-screen-height
fn Height(&self) -> Finite<f64> {
Finite::wrap(self.screen_size().height as f64)
}
// https://drafts.csswg.org/cssom-view/#dom-screen-colordepth
fn ColorDepth(&self) -> u32 {
24
}
// https://drafts.csswg.org/cssom-view/#dom-screen-pixeldepth
fn PixelDepth(&self) -> u32 {
24
}
}