Repository: GraphiteEditor/Graphite (language: rust)

Issue:
Drawing new shapes creates multiple selection history states
- Create a layer (e.g. with the rectangle or polygon tool)
- Go back in the selection history <kbd>Alt</kbd> + <kbd>[</kbd>
- Observe how the rectangle or polygon node is selected.

This is not the desired behaviour. We would expect the selection that was made before drawing the layer to be restored.


Discussion / hints:

Fix patch:
diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface.rs b/editor/src/messages/portfolio/document/utility_types/network_interface.rs
index b75eaa2b02..5427a27ff4 100644
--- a/editor/src/messages/portfolio/document/utility_types/network_interface.rs
+++ b/editor/src/messages/portfolio/document/utility_types/network_interface.rs
@@ -1584,20 +1584,36 @@ impl NodeNetworkInterface {
 
 	/// 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.
 	pub fn selected_nodes_mut(&mut self, network_path: &[NodeId]) -> Option<&mut SelectedNodes> {
+		let (last_selection_state, prev_state, is_selection_empty) = {
+			let network_metadata = self.network_metadata(network_path)?;
+			let history = &network_metadata.persistent_metadata.selection_undo_history;
+			let current = history.back().cloned().unwrap_or_default();
+			let previous = history.iter().rev().nth(1).cloned();
+			let empty = current.selected_layers_except_artboards(self).next().is_none();
+			(current, previous, empty)
+		};
 		self.unload_stack_dependents(network_path);
+
 		let Some(network_metadata) = self.network_metadata_mut(network_path) else {
 			log::error!("Could not get nested network_metadata in selected_nodes");
 			return None;
 		};
 
-		let last_selection_state = network_metadata.persistent_metadata.selection_undo_history.back().cloned().unwrap_or_default();
+		// Initialize default value if selection_undo_history is empty
+		if network_metadata.persistent_metadata.selection_undo_history.is_empty() {
+			network_metadata.persistent_metadata.selection_undo_history.push_back(SelectedNodes::default());
+		}
 
-		network_metadata.persistent_metadata.selection_undo_history.push_back(last_selection_state);
-		network_metadata.persistent_metadata.selection_redo_history.clear();
+		// Update history only if selection is non-empty/does not contain only artboards
+		if !is_selection_empty && prev_state.as_ref() != Some(&last_selection_state) {
+			network_metadata.persistent_metadata.selection_undo_history.push_back(last_selection_state);
+			network_metadata.persistent_metadata.selection_redo_history.clear();
 
-		if network_metadata.persistent_metadata.selection_undo_history.len() > crate::consts::MAX_UNDO_HISTORY_LEN {
-			network_metadata.persistent_metadata.selection_undo_history.pop_front();
+			if network_metadata.persistent_metadata.selection_undo_history.len() > crate::consts::MAX_UNDO_HISTORY_LEN {
+				network_metadata.persistent_metadata.selection_undo_history.pop_front();
+			}
 		}
+
 		network_metadata.persistent_metadata.selection_undo_history.back_mut()
 	}
 


Test patch: