1use 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#[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#[derive(Debug, Deserialize, Serialize)]
35pub enum EmbedderControlRequest {
36 SelectElement(SelectElementRequest),
38 ColorPicker(RgbColor),
40 FilePicker(FilePickerRequest),
42 InputMethod(InputMethodRequest),
45 ContextMenu(ContextMenuRequest),
47}
48
49#[derive(Clone, Debug, Deserialize, Serialize)]
50pub struct SelectElementOption {
51 pub id: usize,
53 pub label: String,
55 pub is_disabled: bool,
57}
58
59#[derive(Clone, Debug, Deserialize, Serialize)]
61pub enum SelectElementOptionOrOptgroup {
62 Option(SelectElementOption),
63 Optgroup {
64 label: String,
65 options: Vec<SelectElementOption>,
66 },
67}
68
69#[derive(Debug, Deserialize, Serialize)]
72pub struct ContextMenuRequest {
73 pub element_info: ContextMenuElementInformation,
74 pub items: Vec<ContextMenuItem>,
75}
76
77#[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#[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 const Link = 1 << 1;
114 const Image = 1 << 2;
116 const EditableText = 1 << 3;
119 const Selection = 1 << 4;
122 }
123}
124
125#[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#[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#[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 pub select_directory: bool,
164}
165
166#[derive(Debug, Deserialize, Serialize)]
168pub struct SelectElementRequest {
169 pub options: Vec<SelectElementOptionOrOptgroup>,
170 pub selected_options: Vec<usize>,
171 pub allow_select_multiple: bool,
172}
173
174#[derive(Debug, Deserialize, Serialize)]
176pub enum EmbedderControlResponse {
177 SelectElement(Vec<usize>),
178 ColorPicker(Option<RgbColor>),
179 FilePicker(Option<Vec<SelectedFile>>),
180 ContextMenu(Option<ContextMenuAction>),
181}
182
183#[derive(Debug, Deserialize, Serialize)]
185pub struct SelectedFile {
186 pub id: Uuid,
187 pub filename: PathBuf,
188 pub modified: SystemTime,
189 pub size: u64,
190 pub type_string: String,
192 pub relative_path: Option<String>,
194}
195
196#[derive(Deserialize, Serialize)]
198pub enum SimpleDialogRequest {
199 Alert {
200 id: EmbedderControlId,
201 message: String,
202 response_sender: GenericSender<AlertResponse>,
203 },
204 Confirm {
205 id: EmbedderControlId,
206 message: String,
207 response_sender: GenericSender<ConfirmResponse>,
208 },
209 Prompt {
210 id: EmbedderControlId,
211 message: String,
212 default: String,
213 response_sender: GenericSender<PromptResponse>,
214 },
215}
216
217#[derive(Deserialize, PartialEq, Serialize)]
219pub enum AlertResponse {
220 Ok,
221}
222
223#[derive(Deserialize, PartialEq, Serialize)]
225pub enum ConfirmResponse {
226 Ok,
227 Cancel,
228}
229
230#[derive(Deserialize, PartialEq, Serialize)]
232pub enum PromptResponse {
233 Ok(String),
234 Cancel,
235}