servoshell: Port input dialog code to use egui intead of tinyfiledialogs (#35464)

Signed-off-by: L Ashwin B <lashwinib@gmail.com>
This commit is contained in:
chickenleaf 2025-02-14 20:32:39 +05:30 committed by GitHub
parent 3e1d15da9b
commit 32c13bcb1e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 56 additions and 47 deletions

View file

@ -357,7 +357,7 @@ impl WebViewDelegate for RunningAppState {
&self,
webview: servo::WebView,
definition: PromptDefinition,
origin: PromptOrigin,
_origin: PromptOrigin,
) {
if self.inner().headless {
let _ = match definition {
@ -378,24 +378,9 @@ impl WebViewDelegate for RunningAppState {
let okcancel_dialog = Dialog::new_okcancel_dialog(message, sender);
self.add_dialog(webview, okcancel_dialog);
},
_ => {
let _ = thread::Builder::new()
.name("AlertDialog".to_owned())
.spawn(move || match definition {
PromptDefinition::Input(mut message, mut default, sender) => {
if origin == PromptOrigin::Untrusted {
message = tiny_dialog_escape(&message);
default = tiny_dialog_escape(&default);
}
let result = tinyfiledialogs::input_box("", &message, &default);
sender.send(result)
},
_ => Ok(()),
})
.unwrap()
.join()
.expect("Thread spawning failed");
PromptDefinition::Input(message, default, sender) => {
let input_dialog = Dialog::new_input_dialog(message, default, sender);
self.add_dialog(webview, input_dialog);
},
}
}
@ -598,28 +583,3 @@ fn platform_get_selected_devices(devices: Vec<String>) -> Option<String> {
}
None
}
// This is a mitigation for #25498, not a verified solution.
// There may be codepaths in tinyfiledialog.c that this is
// inadquate against, as it passes the string via shell to
// different programs depending on what the user has installed.
#[cfg(target_os = "linux")]
fn tiny_dialog_escape(raw: &str) -> String {
let s: String = raw
.chars()
.filter_map(|c| match c {
'\n' => Some('\n'),
'\0'..='\x1f' => None,
'<' => Some('\u{FF1C}'),
'>' => Some('\u{FF1E}'),
'&' => Some('\u{FF06}'),
_ => Some(c),
})
.collect();
shellwords::escape(&s)
}
#[cfg(not(target_os = "linux"))]
fn tiny_dialog_escape(raw: &str) -> String {
raw.to_string()
}

View file

@ -25,6 +25,11 @@ pub enum Dialog {
message: String,
sender: IpcSender<PromptResult>,
},
Input {
message: String,
input_text: String,
sender: IpcSender<Option<String>>,
},
}
impl Dialog {
@ -65,6 +70,18 @@ impl Dialog {
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 update(&mut self, ctx: &egui::Context) -> bool {
match self {
Dialog::File {
@ -108,7 +125,7 @@ impl Dialog {
let mut is_open = true;
let modal = Modal::new("alert".into());
modal.show(ctx, |ui| {
make_dialog_label(message, ui);
make_dialog_label(message, ui, None);
egui::Sides::new().show(
ui,
|_ui| {},
@ -128,7 +145,7 @@ impl Dialog {
let mut is_open = true;
let modal = Modal::new("OkCancel".into());
modal.show(ctx, |ui| {
make_dialog_label(message, ui);
make_dialog_label(message, ui, None);
egui::Sides::new().show(
ui,
|_ui| {},
@ -150,13 +167,45 @@ impl Dialog {
});
is_open
},
Dialog::Input {
message,
input_text,
sender,
} => {
let mut is_open = true;
Modal::new("input".into()).show(ctx, |ui| {
make_dialog_label(message, ui, Some(input_text));
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())) {
warn!("Failed to send input dialog response: {}", e);
}
}
if ui.button("Cancel").clicked() {
is_open = false;
if let Err(e) = sender.send(None) {
warn!("Failed to send input dialog response: {}", e);
}
}
},
);
});
is_open
},
}
}
}
fn make_dialog_label(message: &str, ui: &mut egui::Ui) {
fn make_dialog_label(message: &str, ui: &mut egui::Ui, input_text: Option<&mut String>) {
let mut frame = egui::Frame::default().inner_margin(10.0).begin(ui);
frame.content_ui.set_min_width(150.0);
frame.content_ui.label(message);
if let Some(input_text) = input_text {
frame.content_ui.text_edit_singleline(input_text);
}
frame.end(ui);
}