Skip to content

Commit 0206c4e

Browse files
committed
Add comments to history-related messages and handlers
1 parent 4022836 commit 0206c4e

File tree

2 files changed

+48
-25
lines changed

2 files changed

+48
-25
lines changed

editor/src/messages/portfolio/document/document_message.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ pub enum DocumentMessage {
4949
},
5050
DeleteSelectedLayers,
5151
DeselectAllLayers,
52-
DocumentHistoryBackward,
53-
DocumentHistoryForward,
5452
DocumentStructureChanged,
5553
DrawArtboardOverlays {
5654
context: OverlayContext,
@@ -110,7 +108,6 @@ pub enum DocumentMessage {
110108
mouse: Option<(f64, f64)>,
111109
parent_and_insert_index: Option<(LayerNodeIdentifier, usize)>,
112110
},
113-
Redo,
114111
RenameDocument {
115112
new_name: String,
116113
},
@@ -179,10 +176,23 @@ pub enum DocumentMessage {
179176
SetRenderMode {
180177
render_mode: RenderMode,
181178
},
179+
Undo,
180+
Redo,
181+
DocumentHistoryBackward,
182+
DocumentHistoryForward,
183+
// TODO: Rename to HistoryStepPush
184+
/// Create a snapshot of the document at this point in time, by immediately starting and committing a transaction.
182185
AddTransaction,
186+
// TODO: Rename to HistoryTransactionStart
187+
/// Take a snapshot of the document to an intermediate state, and then depending on what we do next, we might either commit or abort it.
183188
StartTransaction,
189+
// TODO: Rename to HistoryTransactionEnd
190+
/// Either commit (creating a new history step) or cancel (removing the last history step, as if it never happened) the last transaction started with `StartTransaction`.
184191
EndTransaction,
192+
/// Cause the document to revert back to the state when the transaction was started. For example, the user may be dragging
193+
/// something around and hits Escape to abort the drag. This jumps the document back to the point before the drag began.
185194
AbortTransaction,
195+
/// The same as `AbortTransaction` with one step back, but it can also be called with multiple steps back in the history of undos.
186196
RepeatedAbortTransaction {
187197
undo_count: usize,
188198
},
@@ -206,7 +216,6 @@ pub enum DocumentMessage {
206216
UpdateClipTargets {
207217
clip_targets: HashSet<NodeId>,
208218
},
209-
Undo,
210219
UngroupSelectedLayers,
211220
UngroupLayer {
212221
layer: LayerNodeIdentifier,

editor/src/messages/portfolio/document/document_message_handler.rs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,6 @@ impl MessageHandler<DocumentMessage, DocumentMessageContext<'_>> for DocumentMes
372372
responses.add(NodeGraphMessage::SelectedNodesSet { nodes: vec![] });
373373
self.layer_range_selection_reference = None;
374374
}
375-
DocumentMessage::DocumentHistoryBackward => self.undo_with_history(viewport, responses),
376-
DocumentMessage::DocumentHistoryForward => self.redo_with_history(viewport, responses),
377375
DocumentMessage::DocumentStructureChanged => {
378376
if layers_panel_open {
379377
self.network_interface.load_structure();
@@ -953,15 +951,6 @@ impl MessageHandler<DocumentMessage, DocumentMessageContext<'_>> for DocumentMes
953951
responses.add(NodeGraphMessage::SelectedNodesSet { nodes: vec![layer.to_node()] });
954952
responses.add(ToolMessage::ActivateTool { tool_type: ToolType::Select });
955953
}
956-
DocumentMessage::Redo => {
957-
if self.network_interface.transaction_status() != TransactionStatus::Finished {
958-
return;
959-
}
960-
responses.add(SelectToolMessage::Abort);
961-
responses.add(DocumentMessage::DocumentHistoryForward);
962-
responses.add(ToolMessage::Redo);
963-
responses.add(OverlaysMessage::Draw);
964-
}
965954
DocumentMessage::RenameDocument { new_name } => {
966955
self.name = new_name.clone();
967956

@@ -1291,6 +1280,27 @@ impl MessageHandler<DocumentMessage, DocumentMessageContext<'_>> for DocumentMes
12911280
self.render_mode = render_mode;
12921281
responses.add_front(NodeGraphMessage::RunDocumentGraph);
12931282
}
1283+
DocumentMessage::Undo => {
1284+
if self.network_interface.transaction_status() != TransactionStatus::Finished {
1285+
return;
1286+
}
1287+
responses.add(ToolMessage::PreUndo);
1288+
responses.add(DocumentMessage::DocumentHistoryBackward);
1289+
responses.add(OverlaysMessage::Draw);
1290+
responses.add(ToolMessage::Undo);
1291+
}
1292+
DocumentMessage::Redo => {
1293+
if self.network_interface.transaction_status() != TransactionStatus::Finished {
1294+
return;
1295+
}
1296+
responses.add(SelectToolMessage::Abort);
1297+
responses.add(DocumentMessage::DocumentHistoryForward);
1298+
responses.add(ToolMessage::Redo);
1299+
responses.add(OverlaysMessage::Draw);
1300+
}
1301+
DocumentMessage::DocumentHistoryBackward => self.undo_with_history(viewport, responses),
1302+
DocumentMessage::DocumentHistoryForward => self.redo_with_history(viewport, responses),
1303+
// Create a snapshot of the document at this point in time, by immediately starting and committing a transaction.
12941304
DocumentMessage::AddTransaction => {
12951305
self.start_transaction(responses);
12961306
self.commit_transaction(responses);
@@ -1299,37 +1309,50 @@ impl MessageHandler<DocumentMessage, DocumentMessageContext<'_>> for DocumentMes
12991309
DocumentMessage::StartTransaction => {
13001310
self.start_transaction(responses);
13011311
}
1302-
// Commits the transaction if the network was mutated since the transaction started, otherwise it cancels the transaction
1312+
// Either commit (creating a new history step) or cancel (removing the last history step, as if it never happened) the last transaction started with `StartTransaction`.
13031313
DocumentMessage::EndTransaction => match self.network_interface.transaction_status() {
1314+
// This is used if, between the start and end of the transaction, the changes were undone by the user.
1315+
// For example, dragging something around and then dropping it back at its exact original position.
1316+
// So we cancel the transaction to return to the point before the transaction was started.
13041317
TransactionStatus::Started => {
13051318
self.network_interface.finish_transaction();
13061319
self.document_undo_history.pop_back();
13071320
}
1321+
// This is used if, between the start and end of the transaction, actual changes did occur and we want to keep them as part of a history step that the user can undo/redo.
13081322
TransactionStatus::Modified => {
13091323
self.commit_transaction(responses);
13101324
}
13111325
TransactionStatus::Finished => {}
13121326
},
13131327
DocumentMessage::AbortTransaction => match self.network_interface.transaction_status() {
1328+
// If we abort a transaction without any changes having been made, we simply remove the transaction as if it never occurred.
13141329
TransactionStatus::Started => {
13151330
self.network_interface.finish_transaction();
13161331
self.document_undo_history.pop_back();
13171332
}
1333+
// If we abort a transaction after changes have been made, we need to undo those changes.
13181334
TransactionStatus::Modified => {
13191335
responses.add(DocumentMessage::RepeatedAbortTransaction { undo_count: 1 });
13201336
}
1337+
// This is an erroneous state indicating that a transaction is being aborted without having ever been started.
13211338
TransactionStatus::Finished => {}
13221339
},
1340+
// The same as `AbortTransaction` with one step back, but it can also be called with multiple steps back in the history of undos.
13231341
DocumentMessage::RepeatedAbortTransaction { undo_count } => {
1342+
// This prevents us from aborting a transaction multiple times in a row, which would be erroneous.
13241343
if self.network_interface.transaction_status() == TransactionStatus::Finished {
13251344
return;
13261345
}
13271346

1347+
// Sometimes (like successive G/R/S transformations) we may need to undo multiple steps to fully abort the transaction, before we finish.
13281348
for _ in 0..undo_count {
13291349
self.undo(viewport, responses);
13301350
}
13311351

1352+
// Finally finish the transaction, ensuring that any future operations are not erroneously redone as part of this aborted transaction.
13321353
self.network_interface.finish_transaction();
1354+
1355+
// Refresh state
13331356
responses.add(OverlaysMessage::Draw);
13341357
responses.add(PortfolioMessage::UpdateOpenDocumentsList);
13351358
}
@@ -1401,15 +1424,6 @@ impl MessageHandler<DocumentMessage, DocumentMessageContext<'_>> for DocumentMes
14011424
DocumentMessage::UpdateClipTargets { clip_targets } => {
14021425
self.network_interface.update_clip_targets(clip_targets);
14031426
}
1404-
DocumentMessage::Undo => {
1405-
if self.network_interface.transaction_status() != TransactionStatus::Finished {
1406-
return;
1407-
}
1408-
responses.add(ToolMessage::PreUndo);
1409-
responses.add(DocumentMessage::DocumentHistoryBackward);
1410-
responses.add(OverlaysMessage::Draw);
1411-
responses.add(ToolMessage::Undo);
1412-
}
14131427
DocumentMessage::UngroupSelectedLayers => {
14141428
if !self.selection_network_path.is_empty() {
14151429
log::error!("Ungrouping selected layers is only supported for the Document Network");

0 commit comments

Comments
 (0)