Prepare to support nested operations

This commit is contained in:
Hanno Braun 2024-12-10 18:54:23 +01:00
parent ccf6d9a1d1
commit 529068cec5

View File

@ -2,14 +2,14 @@ use crate::geometry::Operation;
pub struct OperationView { pub struct OperationView {
operation: Box<dyn Operation>, operation: Box<dyn Operation>,
selected: usize, selected: Option<usize>,
} }
impl OperationView { impl OperationView {
pub fn new(operation: impl Operation + 'static) -> Self { pub fn new(operation: impl Operation + 'static) -> Self {
Self { Self {
operation: Box::new(operation), operation: Box::new(operation),
selected: 0, selected: None,
} }
} }
@ -18,29 +18,35 @@ impl OperationView {
.children() .children()
.into_iter() .into_iter()
.enumerate() .enumerate()
.map(|(i, op)| (op, i == self.selected)) .map(|(i, op)| (op, Some(i) == self.selected))
.collect() .collect()
} }
pub fn select_last(&mut self) { pub fn select_last(&mut self) {
let last_index = self.operations().len().saturating_sub(1); let last_index = self.operations().len().saturating_sub(1);
self.selected = last_index; self.selected = Some(last_index);
} }
pub fn select_next(&mut self) { pub fn select_next(&mut self) {
if self.selected + 1 < self.operations().len() { if let Some(selected) = self.selected {
self.selected += 1; if selected + 1 < self.operations().len() {
self.selected = Some(selected + 1);
}
} }
} }
pub fn select_previous(&mut self) { 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<Box<dyn Operation>> { pub fn selected(&self) -> Option<Box<dyn Operation>> {
let selected = self.selected?;
self.operations() self.operations()
.into_iter() .into_iter()
.nth(self.selected) .nth(selected)
.map(|(op, _)| op) .map(|(op, _)| op)
} }
} }