use super::{trigger::Trigger, ComboBoxOption, ComboBoxState, Ui};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Choice {
Pointer(usize),
Keyboard(usize),
Access(usize),
}
impl Choice {
pub(super) fn of_pass(list: Option<Self>, open: bool, access: Option<usize>) -> Option<Self> {
list.filter(|_| open).or(access.map(Self::Access))
}
fn index(self) -> usize {
match self {
Self::Pointer(index) | Self::Keyboard(index) | Self::Access(index) => index,
}
}
}
pub(super) fn apply<T: Clone + PartialEq>(
ui: &mut Ui<'_>,
trigger: &Trigger,
choice: Option<Choice>,
options: &[ComboBoxOption<T>],
selected: &mut Option<T>,
state: &mut ComboBoxState,
) -> bool {
let mut changed = false;
if let Some(choice) = choice {
let value = &options[choice.index()].value;
if selected.as_ref() != Some(value) {
*selected = Some(value.clone());
changed = true;
}
state.open = false;
}
if !state.open && ui.context.popup_open(trigger.popup) {
ui.context.close_popup_of_builder(trigger.popup);
trigger.animate_closed(ui);
}
changed
}