Skip to content

Commit

Permalink
DYN-6864 undo renaming and collapsing group (#15151)
Browse files Browse the repository at this point in the history
* undo expand/collapse

first draft - works but needs cleaning

* draft - clean code working

* check

* Undo group renaming and collapse with test

* final draft

* regression fix

should resolve the issue with regressions in GraphLayoutTests

---------

Co-authored-by: pinzart90 <[email protected]>
  • Loading branch information
ivaylo-matov and pinzart90 authored May 6, 2024
1 parent a6c07fa commit a8b023d
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/DynamoCore/Core/UndoRedoRecorder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
Expand Down
11 changes: 8 additions & 3 deletions src/DynamoCore/Graph/Annotations/AnnotationModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ public double TextBlockHeight
}
}


private double textMaxWidth;
/// <summary>
/// Returns the maxWidth of text area of the group
Expand Down Expand Up @@ -657,6 +656,9 @@ protected override bool UpdateValueCore(UpdateValueParams updateValueParams)
case nameof(AnnotationDescriptionText):
AnnotationDescriptionText = value;
break;
case nameof(IsExpanded):
IsExpanded = Convert.ToBoolean(value);
break;
}

return base.UpdateValueCore(updateValueParams);
Expand All @@ -680,6 +682,7 @@ void SerializeCore(XmlElement element, SaveContext context)
helper.SetAttribute("TextblockHeight", this.TextBlockHeight);
helper.SetAttribute("backgrouund", (this.Background == null ? "" : this.Background.ToString()));
helper.SetAttribute(nameof(IsSelected), IsSelected);
helper.SetAttribute(nameof(IsExpanded), this.IsExpanded);

//Serialize Selected models
XmlDocument xmlDoc = element.OwnerDocument;
Expand Down Expand Up @@ -711,12 +714,13 @@ protected override void DeserializeCore(XmlElement element, SaveContext context)
this.textBlockHeight = helper.ReadDouble("TextblockHeight", DoubleValue);
this.InitialTop = helper.ReadDouble("InitialTop", DoubleValue);
this.InitialHeight = helper.ReadDouble("InitialHeight", DoubleValue);
this.IsSelected = helper.ReadBoolean(nameof(IsSelected), false);
this.IsSelected = helper.ReadBoolean(nameof(IsSelected), false);
this.IsExpanded = helper.ReadBoolean(nameof(IsExpanded), true);

if (IsSelected)
DynamoSelection.Instance.Selection.Add(this);
else
DynamoSelection.Instance.Selection.Remove(this);
DynamoSelection.Instance.Selection.Remove(this);

//Deserialize Selected models
if (element.HasChildNodes)
Expand Down Expand Up @@ -756,6 +760,7 @@ protected override void DeserializeCore(XmlElement element, SaveContext context)
RaisePropertyChanged(nameof(GroupStyleId));
RaisePropertyChanged(nameof(AnnotationText));
RaisePropertyChanged(nameof(Nodes));
RaisePropertyChanged(nameof(IsExpanded));
this.ReportPosition();
}

Expand Down
63 changes: 46 additions & 17 deletions src/DynamoCoreWpf/ViewModels/Core/AnnotationViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,25 +260,20 @@ public bool IsExpanded
get => annotationModel.IsExpanded;
set
{
annotationModel.IsExpanded = value;
InPorts.Clear();
OutPorts.Clear();
if (value)
// This change is triggered by the user interaction in the View.
// Before we updating the value in the Model and ViewModel
// we record the current state in the UndoRedoStack.
// This ensures that any modifications can be reverted by the user.
var undoRecorder = WorkspaceViewModel.Model.UndoRecorder;
using (undoRecorder.BeginActionGroup())
{
this.ShowGroupContents();
undoRecorder.RecordModificationForUndo(annotationModel);
}
else
{
this.SetGroupInputPorts();
this.SetGroupOutPorts();
this.CollapseGroupContents(true);
RaisePropertyChanged(nameof(NodeContentCount));
}
WorkspaceViewModel.HasUnsavedChanges = true;
AddGroupToGroupCommand.RaiseCanExecuteChanged();
RaisePropertyChanged(nameof(IsExpanded));
RedrawConnectors();
ReportNodesPosition();

annotationModel.IsExpanded = value;

// Methods to collapse or expand the group based on the new value of IsExpanded.
ManageAnnotationMVExpansionAndCollapse();
}
}

Expand Down Expand Up @@ -1051,6 +1046,36 @@ private void UpdateConnectorsAndPortsOnShowContents(IEnumerable<ModelBase> nodes
}
}


/// <summary>
/// Manages the expansion or collapse of the annotation group in the view model.
/// </summary>
private void ManageAnnotationMVExpansionAndCollapse()
{
if (InPorts.Any() || OutPorts.Any())
{
InPorts.Clear();
OutPorts.Clear();
}

if (annotationModel.IsExpanded)
{
this.ShowGroupContents();
}
else
{
this.SetGroupInputPorts();
this.SetGroupOutPorts();
this.CollapseGroupContents(true);
RaisePropertyChanged(nameof(NodeContentCount));
}
WorkspaceViewModel.HasUnsavedChanges = true;
AddGroupToGroupCommand.RaiseCanExecuteChanged();
RaisePropertyChanged(nameof(IsExpanded));
RedrawConnectors();
ReportNodesPosition();
}

private void UpdateFontSize(object parameter)
{
if (parameter == null) return;
Expand Down Expand Up @@ -1203,6 +1228,10 @@ private void model_PropertyChanged(object sender, System.ComponentModel.Property
RaisePropertyChanged(nameof(AnnotationModel.Position));
UpdateProxyPortsPosition();
break;
case nameof(IsExpanded):
ManageAnnotationMVExpansionAndCollapse();
break;

}
}

Expand Down
2 changes: 1 addition & 1 deletion src/DynamoCoreWpf/Views/Core/AnnotationView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
Expand Down
37 changes: 37 additions & 0 deletions test/DynamoCoreWpfTests/AnnotationViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,44 @@ public void UndoDeletingTheGroupShouldBringTheGroupAndModelsBack()
annotation = ViewModel.Model.CurrentWorkspace.Annotations.FirstOrDefault();
Assert.IsNotNull(annotation);
Assert.AreEqual(1, annotation.Nodes.Count());
}

/// <summary>
/// Tests the Undo functionality for group collapse/expand and rename actions
/// </summary>
[Test]
[Category("DynamoUI")]
public void UndoGroupCollapseAndRenameShouldRestorePreviousStates()
{
OpenModel(@"core\annotationViewModelTests\groupsTestFile.dyn");

string newName = "A1B2C3";
var workspaceVm = ViewModel.CurrentSpaceViewModel;
var groupVm = workspaceVm.Annotations.First();
groupVm.IsExpanded = true;

// Assert that initial conditions are met
Assert.IsNotNull(groupVm, "Expected an initial group to be present");
Assert.IsTrue(groupVm.IsExpanded, "Group should be expanded");
Assert.IsFalse(groupVm.AnnotationText.Equals(newName));

// Rename and collapse the group
groupVm.AnnotationText = newName;
groupVm.IsExpanded = false;

// Assert initial action
Assert.IsFalse(groupVm.IsExpanded, "Group should be collapsed");
Assert.AreEqual(newName, groupVm.AnnotationText, "Group should be renamed");

// Undo collapse
ViewModel.UndoCommand.Execute(null);
Assert.IsTrue(groupVm.IsExpanded, "Group should be expanded after undo");
Assert.AreEqual(newName, groupVm.AnnotationText, "Name should remain renamed after undo");

// Undo rename
ViewModel.UndoCommand.Execute(null);
Assert.IsTrue(groupVm.IsExpanded, "Group should remain expanded after second undo");
Assert.AreNotEqual(newName, groupVm.AnnotationText, "Group name should revert to original");
}

[Test]
Expand Down

0 comments on commit a8b023d

Please sign in to comment.