App: factor out the selection routine

This commit is contained in:
Aaron Fenyes 2024-11-24 11:33:21 -08:00
parent 17d4ed86e4
commit f4c9d3d7f4
3 changed files with 20 additions and 26 deletions

View File

@ -506,20 +506,7 @@ pub fn Display() -> View {
// if we clicked something, select it
match clicked {
Some((key, _)) => {
if event.shift_key() {
state.selection.update(|sel| {
if !sel.remove(&key) {
sel.insert(key);
}
})
} else {
state.selection.update(|sel| {
sel.clear();
sel.insert(key);
})
}
},
Some((key, _)) => state.select(key, event.shift_key()),
None => state.selection.update(|sel| sel.clear())
};
}

View File

@ -25,6 +25,24 @@ impl AppState {
selection: create_signal(FxHashSet::default())
}
}
// in single-selection mode, select the element with the given key. in
// multiple-selection mode, toggle whether the element with the given key
// is selected
fn select(&self, key: ElementKey, multi: bool) {
if multi {
self.selection.update(|sel| {
if !sel.remove(&key) {
sel.insert(key);
}
});
} else {
self.selection.update(|sel| {
sel.clear();
sel.insert(key);
});
}
}
}
fn main() {

View File

@ -83,18 +83,7 @@ fn ElementOutlineItem(key: ElementKey, element: assembly::Element) -> View {
move |event: KeyboardEvent| {
match event.key().as_str() {
"Enter" => {
if event.shift_key() {
state.selection.update(|sel| {
if !sel.remove(&key) {
sel.insert(key);
}
});
} else {
state.selection.update(|sel| {
sel.clear();
sel.insert(key);
});
}
state.select(key, event.shift_key());
event.prevent_default();
},
"ArrowRight" if constrained.get() => {