versoview/webview/
history_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use super::webview_menu::WebViewMenu;
use crate::webview::WebView;
use crate::{verso::send_to_constellation, window::Window};
use base::id::WebViewId;
use constellation_traits::{EmbedderToConstellationMessage, TraversalDirection};
use crossbeam_channel::Sender;
use embedder_traits::ViewportDetails;
use serde::{Deserialize, Serialize};
use servo_url::ServoUrl;
use std::fmt;
use url::Url;
use webrender_api::units::DeviceRect;
use winit::dpi::LogicalPosition;

/// The Previous/Next History Menu of the Window. It will be opened when users left long click on panel's previous/next button.
#[derive(Clone)]
pub struct HistoryMenu {
    /// The action to perform
    action: HistoryMenuAction,
    /// The menu items
    menu_items: Vec<HistoryMenuItem>,
    /// The webview that the menu is attached to
    pub(crate) webview: WebView,
    /// Menu position, used for positioning the menu by CSS
    position: LogicalPosition<f64>,
}

impl HistoryMenu {
    /// Create history menu with custom items
    pub fn new_with_menu(action: HistoryMenuAction, menu_items: Vec<HistoryMenuItem>) -> Self {
        let webview_id = WebViewId::new();
        let webview = WebView::new(webview_id, ViewportDetails::default());

        Self {
            action,
            menu_items,
            webview,
            position: LogicalPosition::new(0.0, 0.0),
        }
    }
}

impl WebViewMenu for HistoryMenu {
    fn webview(&self) -> &WebView {
        &self.webview
    }

    fn resource_url(&self) -> ServoUrl {
        let mut url = Url::parse("verso://resources/components/history_menu.html").unwrap();
        url.query_pairs_mut()
            .append_pair("action", &self.action.to_string());
        url.query_pairs_mut()
            .append_pair("items", &self.serialize_items());
        url.query_pairs_mut()
            .append_pair("pos_x", &self.position.x.to_string());
        url.query_pairs_mut()
            .append_pair("pos_y", &self.position.y.to_string());
        ServoUrl::from_url(url)
    }

    fn set_webview_rect(&mut self, rect: DeviceRect) {
        self.webview.set_size(rect);
    }

    fn position(&self) -> LogicalPosition<f64> {
        self.position
    }

    fn set_position(&mut self, position: LogicalPosition<f64>) {
        self.position = position;
    }

    fn close(&mut self, sender: &Sender<EmbedderToConstellationMessage>) {
        send_to_constellation(
            sender,
            EmbedderToConstellationMessage::CloseWebView(self.webview().webview_id),
        );
    }
}

impl HistoryMenu {
    /// Convert the menu items to JSON string
    fn serialize_items(&self) -> String {
        serde_json::to_string(&self.menu_items).unwrap()
    }
}

/// Menu Item
#[derive(Debug, Clone, Serialize)]
pub struct HistoryMenuItem {
    /// index of the menu item
    pub index: usize,
    /// title of the menu item
    pub title: String,
    /// url of the menu item
    pub url: String,
}

impl HistoryMenuItem {
    /// Create a new menu item
    pub fn new(index: usize, title: Option<&str>, url: &str) -> Self {
        let title = title.unwrap_or(url);
        Self {
            index,
            title: title.to_string(),
            url: url.to_string(),
        }
    }
}

/// History Menu Click Result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistoryMenuUIResponse {
    /// index of the menu item
    pub index: Option<usize>,
    /// action of the menu item
    pub action: HistoryMenuAction,
}

/// Open History Menu Request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpenHistoryMenuRequest {
    /// The action to perform
    pub action: HistoryMenuAction,
    /// The position of the menu
    pub position: LogicalPosition<f64>,
}

/// Action of the history menu
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum HistoryMenuAction {
    /// Previous item
    Prev,
    /// Next item
    Next,
}

impl fmt::Display for HistoryMenuAction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            HistoryMenuAction::Prev => write!(f, "Prev"),
            HistoryMenuAction::Next => write!(f, "Next"),
        }
    }
}

impl Window {
    pub(crate) fn show_history_menu(
        &mut self,
        sender: &Sender<EmbedderToConstellationMessage>,
        request: OpenHistoryMenuRequest,
    ) -> Option<HistoryMenu> {
        let tab = self.tab_manager.current_tab().unwrap();
        let history = tab.history();
        let current_index = history.current_idx;
        let items: Vec<HistoryMenuItem> = match request.action {
            HistoryMenuAction::Prev => {
                // For prev, we want items from current_index-1 to 0
                history.list[..current_index]
                    .iter()
                    .rev()
                    .enumerate()
                    .map(|(index, url)| HistoryMenuItem::new(index, None, url.as_str()))
                    .collect()
            }
            HistoryMenuAction::Next => {
                // For next, we want items from current_index+1 to end
                history.list[current_index + 1..]
                    .iter()
                    .enumerate()
                    .map(|(index, url)| HistoryMenuItem::new(index, None, url.as_str()))
                    .collect()
            }
        };

        if items.is_empty() {
            return None;
        }

        let mut menu = HistoryMenu::new_with_menu(request.action, items);
        menu.show(
            sender,
            self,
            request.position.to_physical(self.scale_factor()),
        );
        Some(menu)
    }

    pub(crate) fn handle_history_menu_event(
        &mut self,
        sender: &Sender<EmbedderToConstellationMessage>,
        event: crate::webview::history_menu::HistoryMenuUIResponse,
    ) {
        self.close_webview_menu(sender);

        if let Some(tab_id) = self.tab_manager.current_tab_id() {
            if let Some(index) = event.index {
                match event.action {
                    HistoryMenuAction::Prev => {
                        send_to_constellation(
                            sender,
                            EmbedderToConstellationMessage::TraverseHistory(
                                tab_id,
                                TraversalDirection::Back(index + 1),
                            ),
                        );
                    }
                    HistoryMenuAction::Next => {
                        send_to_constellation(
                            sender,
                            EmbedderToConstellationMessage::TraverseHistory(
                                tab_id,
                                TraversalDirection::Forward(index + 1),
                            ),
                        );
                    }
                }
            }
        } else {
            log::error!("No active webview to handle history menu event");
        }
    }
}