Skip to main content

servoshell/desktop/
webview_widget.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use accesskit::Affine;
6use egui::{Rect, Sense, Widget};
7use euclid::Scale;
8use servo::{DeviceIndependentPixel, DevicePixel, WebView};
9
10pub(crate) struct WebViewWidget {
11    id: egui::Id,
12    webview: WebView,
13    scale_factor: Scale<f32, DeviceIndependentPixel, DevicePixel>,
14    available_rect: Rect,
15    /// Whether servoshell-global focus is inside this widget's webview, regardless of whether the
16    /// application itself has system focus.
17    contains_focus: bool,
18    /// Whether the webview is ready to take egui focus yet: it's window has system focus and,
19    /// if accessibilty has been activated, it's AccessKit tree has been sent.
20    can_take_focus: bool,
21}
22
23impl WebViewWidget {
24    pub fn new(
25        webview: WebView,
26        scale_factor: Scale<f32, DeviceIndependentPixel, DevicePixel>,
27        available_rect: Rect,
28        contains_focus: bool,
29        can_take_focus: bool,
30    ) -> Self {
31        Self {
32            id: egui::Id::new(webview.id()),
33            webview,
34            scale_factor,
35            available_rect,
36            contains_focus,
37            can_take_focus,
38        }
39    }
40}
41
42impl Widget for WebViewWidget {
43    fn ui(self, ui: &mut egui::Ui) -> egui::Response {
44        let tree_id = self.webview.accesskit_tree_id();
45
46        // Always build an AccessKit graft node, even if the webview isn't currently showing, so
47        // that any tree updates from the webview won't cause a panic.
48        if let Some(tree_id) = tree_id {
49            ui.accesskit_node_builder(self.id, |node| {
50                let affine = {
51                    // The grafted WebView tree reports bounds in device pixels relative to the
52                    // WebView's own origin, so this node supplies the offset of the WebView within
53                    // the window, and scales it to the same scale as the rest of the nodes in egui's
54                    // AccessKit tree.
55                    let scale = (1.0 / self.scale_factor.get()) as f64;
56                    let x = self.available_rect.min.x as f64;
57                    let y = self.available_rect.min.y as f64;
58                    Affine::new([scale, 0.0, 0.0, scale, x, y])
59                };
60                node.set_tree_id(tree_id);
61                // Only the transform is set: AccessKit consumers exclude graft nodes from
62                // the presented tree, so bounds on this node would never be read.
63                node.set_transform(affine);
64            });
65        }
66
67        // If:
68        // - this is not the active webview, or
69        // - the application doesn't have system focus, or
70        // - accessibility is active, but the initial AccessKit update for the webview hasn't been
71        //   sent yet;
72        // then don't move egui focus to this widget on this tick.
73        if !self.can_take_focus {
74            return ui.interact(self.available_rect, self.id, Sense::hover());
75        }
76
77        let response = ui.interact(self.available_rect, self.id, Sense::FOCUSABLE);
78
79        // If the application lost and regained system focus since last time this widget was drawn,
80        // we need to request focus; otherwise, focus will reset to the first UI element.
81        if !response.has_focus() && self.contains_focus {
82            response.request_focus();
83        }
84
85        // Note: we don't need to use `Memory::set_focus_lock_filter()` here because `HeadedWindow`
86        // already grabs keyboard events if `Gui::webview_widget_focused` is true.
87        // See `HeadedWindow::handle_winit_window_event()`.
88
89        response
90    }
91}