use std::collections::HashMap;
pub fn enum_dropdown<T: PartialEq + Clone>(
ui: &mut egui::Ui,
key: &str,
choices: &[(T, &str, &str)],
current: &mut T,
width: Option<f32>,
mut hits: Option<&mut HashMap<String, egui::Rect>>,
) -> (bool, egui::Response) {
let shown = choices
.iter()
.find(|(value, _, _)| value == current)
.map_or_else(String::new, |(_, _, label)| label.to_string());
let mut combo = egui::ComboBox::from_id_salt(("form-enum", key));
if let Some(width) = width {
combo = combo.width(width);
}
let mut changed = false;
let combo = combo.selected_text(shown).show_ui(ui, |ui| {
for (value, id, label) in choices {
let item = ui.selectable_value(current, value.clone(), *label);
changed |= item.changed();
if let Some(map) = hits.as_deref_mut() {
map.insert(format!("{key}#{id}"), item.rect);
}
}
});
if let Some(map) = hits.as_deref_mut() {
map.insert(key.to_string(), combo.response.rect);
}
(changed, combo.response)
}