Skip to content

Commit 337b8ba

Browse files
authored
Improve issues with selection history (#2138)
* Selection history when creating a new layer * fixed selection history * cleaned up the code, removed duplicate network_metadata calls * re: fixed selection history Added a default value for selection_undo_history to make sure its never actually empty.
1 parent ed119ad commit 337b8ba

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

editor/src/messages/portfolio/document/utility_types/network_interface.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,20 +1584,36 @@ impl NodeNetworkInterface {
15841584

15851585
/// Mutably get the selected nodes for the network at the network_path. Every time they are mutated, the transient metadata for the top of the stack gets unloaded.
15861586
pub fn selected_nodes_mut(&mut self, network_path: &[NodeId]) -> Option<&mut SelectedNodes> {
1587+
let (last_selection_state, prev_state, is_selection_empty) = {
1588+
let network_metadata = self.network_metadata(network_path)?;
1589+
let history = &network_metadata.persistent_metadata.selection_undo_history;
1590+
let current = history.back().cloned().unwrap_or_default();
1591+
let previous = history.iter().rev().nth(1).cloned();
1592+
let empty = current.selected_layers_except_artboards(self).next().is_none();
1593+
(current, previous, empty)
1594+
};
15871595
self.unload_stack_dependents(network_path);
1596+
15881597
let Some(network_metadata) = self.network_metadata_mut(network_path) else {
15891598
log::error!("Could not get nested network_metadata in selected_nodes");
15901599
return None;
15911600
};
15921601

1593-
let last_selection_state = network_metadata.persistent_metadata.selection_undo_history.back().cloned().unwrap_or_default();
1602+
// Initialize default value if selection_undo_history is empty
1603+
if network_metadata.persistent_metadata.selection_undo_history.is_empty() {
1604+
network_metadata.persistent_metadata.selection_undo_history.push_back(SelectedNodes::default());
1605+
}
15941606

1595-
network_metadata.persistent_metadata.selection_undo_history.push_back(last_selection_state);
1596-
network_metadata.persistent_metadata.selection_redo_history.clear();
1607+
// Update history only if selection is non-empty/does not contain only artboards
1608+
if !is_selection_empty && prev_state.as_ref() != Some(&last_selection_state) {
1609+
network_metadata.persistent_metadata.selection_undo_history.push_back(last_selection_state);
1610+
network_metadata.persistent_metadata.selection_redo_history.clear();
15971611

1598-
if network_metadata.persistent_metadata.selection_undo_history.len() > crate::consts::MAX_UNDO_HISTORY_LEN {
1599-
network_metadata.persistent_metadata.selection_undo_history.pop_front();
1612+
if network_metadata.persistent_metadata.selection_undo_history.len() > crate::consts::MAX_UNDO_HISTORY_LEN {
1613+
network_metadata.persistent_metadata.selection_undo_history.pop_front();
1614+
}
16001615
}
1616+
16011617
network_metadata.persistent_metadata.selection_undo_history.back_mut()
16021618
}
16031619

0 commit comments

Comments
 (0)