embedder_traits/
embedder_controls.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 std::fmt::Debug;
6use std::hash::Hash;
7use std::path::PathBuf;
8use std::time::SystemTime;
9
10use bitflags::bitflags;
11use serde::{Deserialize, Serialize};
12use servo_base::Epoch;
13use servo_base::generic_channel::GenericSender;
14use servo_base::id::{PipelineId, WebViewId};
15use servo_url::ImmutableOrigin;
16use url::Url;
17use uuid::Uuid;
18
19use crate::{InputMethodType, RgbColor};
20
21/// The id of a user interface control that the engine requests that the
22/// embedder show.
23#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
24pub struct EmbedderControlId {
25    #[doc(hidden)]
26    pub webview_id: WebViewId,
27    #[doc(hidden)]
28    pub pipeline_id: PipelineId,
29    #[doc(hidden)]
30    pub index: Epoch,
31}
32
33/// A request from the engine to the embedder to display a user interface control.
34#[derive(Debug, Deserialize, Serialize)]
35pub enum EmbedderControlRequest {
36    /// Indicates that the user has activated a `<select>` element.
37    SelectElement(Vec<SelectElementOptionOrOptgroup>, Option<usize>),
38    /// Indicates that the user has activated a `<input type=color>` element.
39    ColorPicker(RgbColor),
40    /// Indicates that the user has activated a `<input type=file>` element.
41    FilePicker(FilePickerRequest),
42    /// Indicates that the the user has activated a text or input control that should show
43    /// an IME.
44    InputMethod(InputMethodRequest),
45    /// Indicates that the the user has triggered the display of a context menu.
46    ContextMenu(ContextMenuRequest),
47}
48
49#[derive(Clone, Debug, Deserialize, Serialize)]
50pub struct SelectElementOption {
51    /// A unique identifier for the option that can be used to select it.
52    pub id: usize,
53    /// The label that should be used to display the option to the user.
54    pub label: String,
55    /// Whether or not the option is selectable
56    pub is_disabled: bool,
57}
58
59/// Represents the contents of either an `<option>` or an `<optgroup>` element
60#[derive(Clone, Debug, Deserialize, Serialize)]
61pub enum SelectElementOptionOrOptgroup {
62    Option(SelectElementOption),
63    Optgroup {
64        label: String,
65        options: Vec<SelectElementOption>,
66    },
67}
68
69/// Request to present a context menu to the user. This is triggered by things like
70/// right-clicking on web content.
71#[derive(Debug, Deserialize, Serialize)]
72pub struct ContextMenuRequest {
73    pub element_info: ContextMenuElementInformation,
74    pub items: Vec<ContextMenuItem>,
75}
76
77/// An item in a context menu.
78#[derive(Clone, Debug, Deserialize, Serialize)]
79pub enum ContextMenuItem {
80    Item {
81        label: String,
82        action: ContextMenuAction,
83        enabled: bool,
84    },
85    Separator,
86}
87
88/// A particular action associated with a [`ContextMenuItem`]. These actions are
89/// context-sensitive, which means that some of them are available only for some
90/// page elements.
91#[derive(Copy, Clone, Debug, Deserialize, PartialEq, Serialize)]
92pub enum ContextMenuAction {
93    GoBack,
94    GoForward,
95    Reload,
96
97    CopyLink,
98    OpenLinkInNewWebView,
99
100    CopyImageLink,
101    OpenImageInNewView,
102
103    Cut,
104    Copy,
105    Paste,
106    SelectAll,
107}
108
109bitflags! {
110    #[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, Serialize)]
111    pub struct ContextMenuElementInformationFlags: u8 {
112        /// Whether or not the element this context menu was activated for was a link.
113        const Link = 1 << 1;
114        /// Whether or not the element this context menu was activated for was an image.
115        const Image = 1 << 2;
116        /// Whether or not the element this context menu was activated for was editable
117        /// text.
118        const EditableText = 1 << 3;
119        /// Whether or not the element this context menu was activated for was covered by
120        /// a selection.
121        const Selection = 1 << 4;
122    }
123}
124
125/// Information about the element that a context menu was activated for. values which
126/// do not apply to this element will be `None`.
127///
128/// Note that an element might be both an image and a link, if the element is an `<img>`
129/// tag nested inside of a `<a>` tag.
130#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
131pub struct ContextMenuElementInformation {
132    pub flags: ContextMenuElementInformationFlags,
133    pub link_url: Option<Url>,
134    pub image_url: Option<Url>,
135}
136
137/// Request to present an IME to the user when an editable element is focused. If `type` is
138/// [`InputMethodType::Text`], then the `text` parameter specifies the pre-existing text content and
139/// `insertion_point` the zero-based index into the string of the insertion point.
140#[derive(Debug, Deserialize, Serialize)]
141pub struct InputMethodRequest {
142    pub input_method_type: InputMethodType,
143    pub text: String,
144    pub insertion_point: Option<u32>,
145    pub multiline: bool,
146    pub allow_virtual_keyboard: bool,
147}
148
149/// Filter for file selection;
150/// the `String` content is expected to be extension (e.g, "doc", without the prefixing ".")
151#[derive(Clone, Debug, Deserialize, Serialize)]
152pub struct FilterPattern(pub String);
153
154#[derive(Debug, Deserialize, Serialize)]
155pub struct FilePickerRequest {
156    pub origin: ImmutableOrigin,
157    pub current_paths: Vec<PathBuf>,
158    pub filter_patterns: Vec<FilterPattern>,
159    pub allow_select_multiple: bool,
160    pub accept_current_paths_for_testing: bool,
161}
162
163/// Response from the embedder to an [`EmbedderControlRequest`].
164#[derive(Debug, Deserialize, Serialize)]
165pub enum EmbedderControlResponse {
166    SelectElement(Option<usize>),
167    ColorPicker(Option<RgbColor>),
168    FilePicker(Option<Vec<SelectedFile>>),
169    ContextMenu(Option<ContextMenuAction>),
170}
171
172/// Response to file selection request
173#[derive(Debug, Deserialize, Serialize)]
174pub struct SelectedFile {
175    pub id: Uuid,
176    pub filename: PathBuf,
177    pub modified: SystemTime,
178    pub size: u64,
179    // https://w3c.github.io/FileAPI/#dfn-type
180    pub type_string: String,
181}
182
183/// Request from Servo to the embedder with the details of the simple dialog to be displayed.
184#[derive(Deserialize, Serialize)]
185pub enum SimpleDialogRequest {
186    Alert {
187        id: EmbedderControlId,
188        message: String,
189        response_sender: GenericSender<AlertResponse>,
190    },
191    Confirm {
192        id: EmbedderControlId,
193        message: String,
194        response_sender: GenericSender<ConfirmResponse>,
195    },
196    Prompt {
197        id: EmbedderControlId,
198        message: String,
199        default: String,
200        response_sender: GenericSender<PromptResponse>,
201    },
202}
203
204/// The action selected by the user in the alert dialog.
205#[derive(Deserialize, PartialEq, Serialize)]
206pub enum AlertResponse {
207    Ok,
208}
209
210/// The action selected by the user in the confirm dialog.
211#[derive(Deserialize, PartialEq, Serialize)]
212pub enum ConfirmResponse {
213    Ok,
214    Cancel,
215}
216
217/// The action selected by the user in the prompt dialog.
218#[derive(Deserialize, PartialEq, Serialize)]
219pub enum PromptResponse {
220    Ok(String),
221    Cancel,
222}