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}
140
141/// Filter for file selection;
142/// the `String` content is expected to be extension (e.g, "doc", without the prefixing ".")
143#[derive(Clone, Debug, Deserialize, Serialize)]
144pub struct FilterPattern(pub String);
145
146#[derive(Debug, Deserialize, Serialize)]
147pub struct FilePickerRequest {
148    pub origin: ImmutableOrigin,
149    pub current_paths: Vec<PathBuf>,
150    pub filter_patterns: Vec<FilterPattern>,
151    pub allow_select_multiple: bool,
152    pub accept_current_paths_for_testing: bool,
153}
154
155#[derive(Debug, Deserialize, Serialize)]
156pub enum EmbedderControlResponse {
157    SelectElement(Option<usize>),
158    ColorPicker(Option<RgbColor>),
159    FilePicker(Option<Vec<SelectedFile>>),
160    ContextMenu(Option<ContextMenuAction>),
161}
162
163/// Response to file selection request
164#[derive(Debug, Deserialize, Serialize)]
165pub struct SelectedFile {
166    pub id: Uuid,
167    pub filename: PathBuf,
168    pub modified: SystemTime,
169    pub size: u64,
170    // https://w3c.github.io/FileAPI/#dfn-type
171    pub type_string: String,
172}
173
174#[derive(Deserialize, Serialize)]
175pub enum SimpleDialogRequest {
176    Alert {
177        id: EmbedderControlId,
178        message: String,
179        response_sender: GenericSender<AlertResponse>,
180    },
181    Confirm {
182        id: EmbedderControlId,
183        message: String,
184        response_sender: GenericSender<ConfirmResponse>,
185    },
186    Prompt {
187        id: EmbedderControlId,
188        message: String,
189        default: String,
190        response_sender: GenericSender<PromptResponse>,
191    },
192}
193
194#[derive(Deserialize, PartialEq, Serialize)]
195pub enum AlertResponse {
196    Ok,
197}
198
199#[derive(Deserialize, PartialEq, Serialize)]
200pub enum ConfirmResponse {
201    Ok,
202    Cancel,
203}
204
205#[derive(Deserialize, PartialEq, Serialize)]
206pub enum PromptResponse {
207    Ok(String),
208    Cancel,
209}