Skip to main content

csi_webclient/ui/
selector.rs

1//! Device multi-select widgets shared by the top bar and Devices tab.
2
3use crate::state::{AppState, DeviceState, UserIntent};
4
5/// Summary label for the top-bar combo box.
6pub fn selection_label(state: &AppState) -> String {
7    match state.selected_device_ids.len() {
8        0 => "(none)".to_owned(),
9        1 => state.selected_device_ids[0].clone(),
10        n => format!("{n} selected"),
11    }
12}
13
14/// Combo-box popup: checkboxes + Select All / Clear.
15pub fn render_popup(ui: &mut egui::Ui, state: &AppState, intents: &mut Vec<UserIntent>) {
16    ui.horizontal(|ui| {
17        if ui.button("Select All").clicked() {
18            intents.push(UserIntent::SelectAllDevices);
19        }
20        if ui.button("Clear").clicked() {
21            intents.push(UserIntent::ClearDeviceSelection);
22        }
23    });
24    ui.separator();
25
26    egui::ScrollArea::vertical()
27        .id_salt("device_selector_popup_scroll")
28        .auto_shrink([false, false])
29        .max_height(240.0)
30        .show(ui, |ui| {
31            for device in &state.devices {
32                let mut selected = state.is_selected(&device.id);
33                let label = device_label(device);
34                if ui.checkbox(&mut selected, label).changed() {
35                    intents.push(UserIntent::ToggleDeviceSelection(device.id.clone()));
36                }
37            }
38        });
39}
40
41/// Checkbox column in the Devices table.
42pub fn render_row_checkbox(
43    ui: &mut egui::Ui,
44    state: &AppState,
45    device_id: &str,
46    intents: &mut Vec<UserIntent>,
47) {
48    let mut selected = state.is_selected(device_id);
49    if ui.checkbox(&mut selected, "").changed() {
50        intents.push(UserIntent::ToggleDeviceSelection(device_id.to_owned()));
51    }
52}
53
54fn device_label(device: &DeviceState) -> String {
55    match device.latest_info.as_ref().and_then(|i| i.chip.clone()) {
56        Some(chip) => format!("{} ({chip})", device.id),
57        None => device.id.clone(),
58    }
59}