diff --git a/experiments/2024-12-09/src/ui.rs b/experiments/2024-12-09/src/ui.rs index 9c42a7a07..ea34e0696 100644 --- a/experiments/2024-12-09/src/ui.rs +++ b/experiments/2024-12-09/src/ui.rs @@ -2,14 +2,14 @@ use crate::geometry::Operation; pub struct OperationView { operation: Box, - selected: usize, + selected: Option, } impl OperationView { pub fn new(operation: impl Operation + 'static) -> Self { Self { operation: Box::new(operation), - selected: 0, + selected: None, } } @@ -18,29 +18,35 @@ impl OperationView { .children() .into_iter() .enumerate() - .map(|(i, op)| (op, i == self.selected)) + .map(|(i, op)| (op, Some(i) == self.selected)) .collect() } pub fn select_last(&mut self) { let last_index = self.operations().len().saturating_sub(1); - self.selected = last_index; + self.selected = Some(last_index); } pub fn select_next(&mut self) { - if self.selected + 1 < self.operations().len() { - self.selected += 1; + if let Some(selected) = self.selected { + if selected + 1 < self.operations().len() { + self.selected = Some(selected + 1); + } } } pub fn select_previous(&mut self) { - self.selected = self.selected.saturating_sub(1); + if let Some(selected) = self.selected { + self.selected = Some(selected.saturating_sub(1)); + } } pub fn selected(&self) -> Option> { + let selected = self.selected?; + self.operations() .into_iter() - .nth(self.selected) + .nth(selected) .map(|(op, _)| op) } }