versoview/webview/
webview_menu.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use constellation_traits::EmbedderToConstellationMessage;
use crossbeam_channel::Sender;
use dpi::{LogicalPosition, PhysicalPosition};
use embedder_traits::ViewportDetails;
use euclid::Scale;
use servo_url::ServoUrl;
use webrender_api::units::DeviceRect;

use crate::{verso::send_to_constellation, window::Window};

use super::WebView;

/// Trait for webview menus
pub trait WebViewMenu {
    /// Get the webview of the menu
    fn webview(&self) -> &WebView;
    /// Set the webview rect of the menu
    fn set_webview_rect(&mut self, rect: DeviceRect);
    /// Get the position of the menu
    fn position(&self) -> LogicalPosition<f64>;
    /// Set the position of the menu
    fn set_position(&mut self, position: LogicalPosition<f64>);
    /// Get the resource url of the menu which is used to present the menu
    fn resource_url(&self) -> ServoUrl;
    /// Show the menu
    fn show(
        &mut self,
        sender: &Sender<EmbedderToConstellationMessage>,
        window: &Window,
        position: PhysicalPosition<f64>,
    ) {
        self.set_position(position.to_logical(window.scale_factor()));
        self.set_webview_rect(DeviceRect::from_size(window.outer_size()));

        let hidpi_scale_factor = Scale::new(window.scale_factor() as f32);
        let size = window.outer_size().to_f32() / hidpi_scale_factor;
        send_to_constellation(
            sender,
            EmbedderToConstellationMessage::NewWebView(
                self.resource_url(),
                self.webview().webview_id,
                ViewportDetails {
                    size,
                    hidpi_scale_factor,
                },
            ),
        );
    }
    /// Close the menu
    fn close(&mut self, sender: &Sender<EmbedderToConstellationMessage>) {
        send_to_constellation(
            sender,
            EmbedderToConstellationMessage::CloseWebView(self.webview().webview_id),
        );
    }
}