mirror of
https://github.com/servo/servo.git
synced 2025-08-19 12:25:33 +01:00
libservo: Clean up interfaces for alert()/confirm()/prompt() (#35579)
Signed-off-by: Delan Azabani <dazabani@igalia.com>
This commit is contained in:
parent
03e953e22c
commit
276f6a3ba7
16 changed files with 278 additions and 222 deletions
|
@ -18,8 +18,8 @@ use servo::webrender_api::units::{DeviceIntPoint, DeviceIntRect, DeviceIntSize};
|
|||
use servo::webrender_api::ScrollLocation;
|
||||
use servo::{
|
||||
AllowOrDenyRequest, AuthenticationRequest, FilterPattern, GamepadHapticEffectType, LoadStatus,
|
||||
PermissionRequest, PromptDefinition, PromptOrigin, PromptResult, Servo, ServoDelegate,
|
||||
ServoError, TouchEventType, WebView, WebViewDelegate,
|
||||
PermissionRequest, Servo, ServoDelegate, ServoError, SimpleDialog, TouchEventType, WebView,
|
||||
WebViewDelegate,
|
||||
};
|
||||
use url::Url;
|
||||
|
||||
|
@ -415,36 +415,25 @@ impl WebViewDelegate for RunningAppState {
|
|||
self.inner().window.request_resize(&webview, new_size);
|
||||
}
|
||||
|
||||
fn show_prompt(
|
||||
&self,
|
||||
webview: servo::WebView,
|
||||
definition: PromptDefinition,
|
||||
_origin: PromptOrigin,
|
||||
) {
|
||||
fn show_simple_dialog(&self, webview: servo::WebView, dialog: SimpleDialog) {
|
||||
if self.servoshell_preferences.headless {
|
||||
let _ = match definition {
|
||||
PromptDefinition::Alert(_message, sender) => sender.send(()),
|
||||
PromptDefinition::OkCancel(_message, sender) => sender.send(PromptResult::Primary),
|
||||
PromptDefinition::Input(_message, default, sender) => {
|
||||
sender.send(Some(default.to_owned()))
|
||||
},
|
||||
// TODO: Avoid copying this from the default trait impl?
|
||||
// Return the DOM-specified default value for when we **cannot show simple dialogs**.
|
||||
let _ = match dialog {
|
||||
SimpleDialog::Alert {
|
||||
response_sender, ..
|
||||
} => response_sender.send(Default::default()),
|
||||
SimpleDialog::Confirm {
|
||||
response_sender, ..
|
||||
} => response_sender.send(Default::default()),
|
||||
SimpleDialog::Prompt {
|
||||
response_sender, ..
|
||||
} => response_sender.send(Default::default()),
|
||||
};
|
||||
return;
|
||||
}
|
||||
match definition {
|
||||
PromptDefinition::Alert(message, sender) => {
|
||||
let alert_dialog = Dialog::new_alert_dialog(message, sender);
|
||||
self.add_dialog(webview, alert_dialog);
|
||||
},
|
||||
PromptDefinition::OkCancel(message, sender) => {
|
||||
let okcancel_dialog = Dialog::new_okcancel_dialog(message, sender);
|
||||
self.add_dialog(webview, okcancel_dialog);
|
||||
},
|
||||
PromptDefinition::Input(message, default, sender) => {
|
||||
let input_dialog = Dialog::new_input_dialog(message, default, sender);
|
||||
self.add_dialog(webview, input_dialog);
|
||||
},
|
||||
}
|
||||
let dialog = Dialog::new_simple_dialog(dialog);
|
||||
self.add_dialog(webview, dialog);
|
||||
}
|
||||
|
||||
fn request_authentication(
|
||||
|
|
|
@ -9,7 +9,10 @@ use egui::Modal;
|
|||
use egui_file_dialog::{DialogState, FileDialog as EguiFileDialog};
|
||||
use log::warn;
|
||||
use servo::ipc_channel::ipc::IpcSender;
|
||||
use servo::{AuthenticationRequest, FilterPattern, PermissionRequest, PromptResult};
|
||||
use servo::{
|
||||
AlertResponse, AuthenticationRequest, ConfirmResponse, FilterPattern, PermissionRequest,
|
||||
PromptResponse, SimpleDialog,
|
||||
};
|
||||
|
||||
pub enum Dialog {
|
||||
File {
|
||||
|
@ -17,19 +20,8 @@ pub enum Dialog {
|
|||
multiple: bool,
|
||||
response_sender: IpcSender<Option<Vec<PathBuf>>>,
|
||||
},
|
||||
Alert {
|
||||
message: String,
|
||||
sender: IpcSender<()>,
|
||||
},
|
||||
OkCancel {
|
||||
message: String,
|
||||
sender: IpcSender<PromptResult>,
|
||||
},
|
||||
Input {
|
||||
message: String,
|
||||
input_text: String,
|
||||
sender: IpcSender<Option<String>>,
|
||||
},
|
||||
#[allow(clippy::enum_variant_names, reason = "spec terminology")]
|
||||
SimpleDialog(SimpleDialog),
|
||||
Authentication {
|
||||
username: String,
|
||||
password: String,
|
||||
|
@ -76,24 +68,8 @@ impl Dialog {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn new_alert_dialog(message: String, sender: IpcSender<()>) -> Self {
|
||||
Dialog::Alert { message, sender }
|
||||
}
|
||||
|
||||
pub fn new_okcancel_dialog(message: String, sender: IpcSender<PromptResult>) -> Self {
|
||||
Dialog::OkCancel { message, sender }
|
||||
}
|
||||
|
||||
pub fn new_input_dialog(
|
||||
message: String,
|
||||
default: String,
|
||||
sender: IpcSender<Option<String>>,
|
||||
) -> Self {
|
||||
Dialog::Input {
|
||||
message,
|
||||
input_text: default,
|
||||
sender,
|
||||
}
|
||||
pub fn new_simple_dialog(dialog: SimpleDialog) -> Self {
|
||||
Self::SimpleDialog(dialog)
|
||||
}
|
||||
|
||||
pub fn new_authentication_dialog(authentication_request: AuthenticationRequest) -> Self {
|
||||
|
@ -165,9 +141,12 @@ impl Dialog {
|
|||
DialogState::Closed => false,
|
||||
}
|
||||
},
|
||||
Dialog::Alert { message, sender } => {
|
||||
Dialog::SimpleDialog(SimpleDialog::Alert {
|
||||
message,
|
||||
response_sender,
|
||||
}) => {
|
||||
let mut is_open = true;
|
||||
let modal = Modal::new("alert".into());
|
||||
let modal = Modal::new("Alert".into());
|
||||
modal.show(ctx, |ui| {
|
||||
make_dialog_label(message, ui, None);
|
||||
egui::Sides::new().show(
|
||||
|
@ -176,7 +155,7 @@ impl Dialog {
|
|||
|ui| {
|
||||
if ui.button("Close").clicked() {
|
||||
is_open = false;
|
||||
if let Err(e) = sender.send(()) {
|
||||
if let Err(e) = response_sender.send(AlertResponse::Ok) {
|
||||
warn!("Failed to send alert dialog response: {}", e);
|
||||
}
|
||||
}
|
||||
|
@ -185,9 +164,12 @@ impl Dialog {
|
|||
});
|
||||
is_open
|
||||
},
|
||||
Dialog::OkCancel { message, sender } => {
|
||||
Dialog::SimpleDialog(SimpleDialog::Confirm {
|
||||
message,
|
||||
response_sender,
|
||||
}) => {
|
||||
let mut is_open = true;
|
||||
let modal = Modal::new("OkCancel".into());
|
||||
let modal = Modal::new("Confirm".into());
|
||||
modal.show(ctx, |ui| {
|
||||
make_dialog_label(message, ui, None);
|
||||
egui::Sides::new().show(
|
||||
|
@ -196,13 +178,13 @@ impl Dialog {
|
|||
|ui| {
|
||||
if ui.button("Ok").clicked() {
|
||||
is_open = false;
|
||||
if let Err(e) = sender.send(PromptResult::Primary) {
|
||||
if let Err(e) = response_sender.send(ConfirmResponse::Ok) {
|
||||
warn!("Failed to send alert dialog response: {}", e);
|
||||
}
|
||||
}
|
||||
if ui.button("Cancel").clicked() {
|
||||
is_open = false;
|
||||
if let Err(e) = sender.send(PromptResult::Secondary) {
|
||||
if let Err(e) = response_sender.send(ConfirmResponse::Cancel) {
|
||||
warn!("Failed to send alert dialog response: {}", e);
|
||||
}
|
||||
}
|
||||
|
@ -211,27 +193,30 @@ impl Dialog {
|
|||
});
|
||||
is_open
|
||||
},
|
||||
Dialog::Input {
|
||||
Dialog::SimpleDialog(SimpleDialog::Prompt {
|
||||
message,
|
||||
input_text,
|
||||
sender,
|
||||
} => {
|
||||
// The `default` field gets reused as the input buffer.
|
||||
default: input,
|
||||
response_sender,
|
||||
}) => {
|
||||
let mut is_open = true;
|
||||
Modal::new("input".into()).show(ctx, |ui| {
|
||||
make_dialog_label(message, ui, Some(input_text));
|
||||
Modal::new("Prompt".into()).show(ctx, |ui| {
|
||||
make_dialog_label(message, ui, Some(input));
|
||||
egui::Sides::new().show(
|
||||
ui,
|
||||
|_ui| {},
|
||||
|ui| {
|
||||
if ui.button("Ok").clicked() {
|
||||
is_open = false;
|
||||
if let Err(e) = sender.send(Some(input_text.clone())) {
|
||||
if let Err(e) =
|
||||
response_sender.send(PromptResponse::Ok(input.clone()))
|
||||
{
|
||||
warn!("Failed to send input dialog response: {}", e);
|
||||
}
|
||||
}
|
||||
if ui.button("Cancel").clicked() {
|
||||
is_open = false;
|
||||
if let Err(e) = sender.send(None) {
|
||||
if let Err(e) = response_sender.send(PromptResponse::Cancel) {
|
||||
warn!("Failed to send input dialog response: {}", e);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue