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