servoshell: Port SelectDevice dialog code to use egui instead of tinyfiledialogs (#35657)

Signed-off-by: L Ashwin B <lashwinib@gmail.com>
This commit is contained in:
chickenleaf 2025-02-26 15:29:47 +05:30 committed by GitHub
parent 0568820f01
commit e956f0c5af
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 69 additions and 38 deletions

View file

@ -503,12 +503,10 @@ impl WebViewDelegate for RunningAppState {
devices: Vec<String>,
response_sender: IpcSender<Option<String>>,
) {
let selected = platform_get_selected_devices(devices);
if let Err(e) = response_sender.send(selected) {
webview.send_error(format!(
"Failed to send GetSelectedBluetoothDevice response: {e}"
));
}
self.add_dialog(
webview,
Dialog::new_device_selection_dialog(devices, response_sender),
);
}
fn show_file_selection_dialog(
@ -583,34 +581,3 @@ impl WebViewDelegate for RunningAppState {
self.inner().window.hide_ime();
}
}
#[cfg(target_os = "linux")]
fn platform_get_selected_devices(devices: Vec<String>) -> Option<String> {
std::thread::Builder::new()
.name("DevicePicker".to_owned())
.spawn(move || {
let dialog_rows: Vec<&str> = devices.iter().map(|s| s.as_ref()).collect();
let dialog_rows: Option<&[&str]> = Some(dialog_rows.as_slice());
match tinyfiledialogs::list_dialog("Choose a device", &["Id", "Name"], dialog_rows) {
Some(device) => {
// The device string format will be "Address|Name". We need the first part of it.
device.split('|').next().map(|s| s.to_string())
},
None => None,
}
})
.unwrap()
.join()
.expect("Thread spawning failed")
}
#[cfg(not(target_os = "linux"))]
fn platform_get_selected_devices(devices: Vec<String>) -> Option<String> {
for device in devices {
if let Some(address) = device.split('|').next().map(|s| s.to_string()) {
return Some(address);
}
}
None
}

View file

@ -39,6 +39,11 @@ pub enum Dialog {
message: String,
request: Option<PermissionRequest>,
},
SelectDevice {
devices: Vec<String>,
selected_device_index: usize,
response_sender: IpcSender<Option<String>>,
},
}
impl Dialog {
@ -110,6 +115,17 @@ impl Dialog {
}
}
pub fn new_device_selection_dialog(
devices: Vec<String>,
response_sender: IpcSender<Option<String>>,
) -> Self {
Dialog::SelectDevice {
devices,
selected_device_index: 0,
response_sender,
}
}
pub fn update(&mut self, ctx: &egui::Context) -> bool {
match self {
Dialog::File {
@ -230,7 +246,7 @@ impl Dialog {
ref mut request,
} => {
let mut is_open = true;
Modal::new("input".into()).show(ctx, |ui| {
Modal::new("authentication".into()).show(ctx, |ui| {
let mut frame = egui::Frame::default().inner_margin(10.0).begin(ui);
frame.content_ui.set_min_width(150.0);
@ -302,6 +318,54 @@ impl Dialog {
});
is_open
},
Dialog::SelectDevice {
devices,
selected_device_index,
response_sender,
} => {
let mut is_open = true;
let modal = Modal::new("device_picker".into());
modal.show(ctx, |ui| {
let mut frame = egui::Frame::default().inner_margin(10.0).begin(ui);
frame.content_ui.set_min_width(150.0);
frame.content_ui.heading("Choose a Device");
frame.content_ui.add_space(10.0);
egui::ComboBox::from_label("")
.selected_text(&devices[*selected_device_index + 1])
.show_ui(&mut frame.content_ui, |ui| {
for i in (0..devices.len() - 1).step_by(2) {
let device_name = &devices[i + 1];
ui.selectable_value(selected_device_index, i, device_name);
}
});
frame.end(ui);
egui::Sides::new().show(
ui,
|_ui| {},
|ui| {
if ui.button("Ok").clicked() {
if let Err(e) = response_sender
.send(Some(devices[*selected_device_index].clone()))
{
warn!("Failed to send device selection: {}", e);
}
is_open = false;
}
if ui.button("Cancel").clicked() {
if let Err(e) = response_sender.send(None) {
warn!("Failed to send cancellation: {}", e);
}
is_open = false;
}
},
);
});
is_open
},
}
}
}