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