Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Note PinUnpin Bugfix #12925

Merged
merged 3 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions src/DynamoCore/Graph/Notes/NoteModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ namespace Dynamo.Graph.Notes
/// </summary>
public class NoteModel : ModelBase
{
public enum UndoAction
{
Pin, Unpin
}

/// <summary>
/// This action is triggered when undo command is pressed and a node is pinned
/// </summary>
Expand Down Expand Up @@ -45,11 +50,33 @@ public NodeModel PinnedNode
get { return pinnedNode; }
set
{
pinnedNode = value;
pinnedNode = value;
RaisePropertyChanged(nameof(PinnedNode));
}
}

private Guid pinnedNodeGuid;

public Guid PinnedNodeGuid
{
get { return pinnedNodeGuid; }
set
{
pinnedNodeGuid = value;
RaisePropertyChanged(nameof(PinnedNodeGuid));
}
}

private UndoAction undoAction;
public UndoAction UndoRedoAction
{
get { return undoAction; }
set
{
undoAction = value;
RaisePropertyChanged(nameof(UndoRedoAction));
}
}

/// <summary>
/// Creates NoteModel.
Expand Down Expand Up @@ -91,13 +118,13 @@ protected override bool UpdateValueCore(UpdateValueParams updateValueParams)
string name = updateValueParams.PropertyName;
string value = updateValueParams.PropertyValue;

if (name != "Text")
if (name != "Text")
return base.UpdateValueCore(updateValueParams);

Text = value;

return true;
}
}
#endregion

#region Serialization/Deserialization Methods
Expand All @@ -120,7 +147,13 @@ protected override void DeserializeCore(XmlElement nodeElement, SaveContext cont
X = helper.ReadDouble("x", 0.0);
Y = helper.ReadDouble("y", 0.0);

if(pinnedNode != null)
try
{
PinnedNodeGuid = helper.ReadGuid("pinnedNode");
}
catch (Exception) { }

if (pinnedNode != null && helper.ReadGuid("pinnedNode") != Guid.Empty)
pinnedNode.GUID = helper.ReadGuid("pinnedNode");

// Notify listeners that the position of the note has changed,
Expand All @@ -134,8 +167,15 @@ protected override void DeserializeCore(XmlElement nodeElement, SaveContext cont
/// </summary>
internal void TryToSubscribeUndoNote()
{
if (pinnedNode!=null && pinnedNode.GUID == Guid.Empty && UndoRequest != null)
if (pinnedNode != null && PinnedNodeGuid == Guid.Empty && UndoRequest != null)
{
UndoRedoAction = UndoAction.Unpin;
UndoRequest(this);
return;
}
else if (pinnedNode == null && PinnedNodeGuid != Guid.Empty && UndoRequest != null)
{
UndoRedoAction = UndoAction.Pin;
UndoRequest(this);
}
}
Expand Down
38 changes: 32 additions & 6 deletions src/DynamoCoreWpf/ViewModels/Core/NoteViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Dynamo.ViewModels
{
public partial class NoteViewModel: ViewModelBase
public partial class NoteViewModel : ViewModelBase
{
private int DISTANCE_TO_PINNED_NODE = 16;
private int DISTANCE_TO_PINNED_NODE_WITH_WARNING = 64;
Expand Down Expand Up @@ -131,7 +131,7 @@ public NodeViewModel PinnedNode
{
get
{
if (Model.PinnedNode==null)
if (Model.PinnedNode == null)
{
return null;
}
Expand All @@ -149,6 +149,7 @@ public NoteViewModel(WorkspaceViewModel workspaceViewModel, NoteModel model)
this.WorkspaceViewModel = workspaceViewModel;
_model = model;
model.PropertyChanged += note_PropertyChanged;
model.UndoRequest += note_PinUnpinToNode;
DynamoSelection.Instance.Selection.CollectionChanged += SelectionOnCollectionChanged;
ZIndex = ++StaticZIndex; // places the note on top of all nodes/notes

Expand All @@ -166,9 +167,36 @@ public override void Dispose()
UnsuscribeFromPinnedNode();
}
_model.PropertyChanged -= note_PropertyChanged;
_model.UndoRequest -= note_PinUnpinToNode;
DynamoSelection.Instance.Selection.CollectionChanged -= SelectionOnCollectionChanged;
}

private void note_PinUnpinToNode(ModelBase obj)
{
if (Model.UndoRedoAction.Equals(NoteModel.UndoAction.Unpin))
{
UnpinFromNode(obj);
return;
}
if (Model.UndoRedoAction.Equals(NoteModel.UndoAction.Pin))
{
NodeModel node = WorkspaceViewModel.Model.Nodes
.Where(x => x.GUID.Equals(Model.PinnedNodeGuid))
.FirstOrDefault();

if (node == null) return;

// In case the user has selected a different Node before Undo
// We run the risk of pinning to the wrong Node
// Therefore clear selection before running
DynamoSelection.Instance.ClearSelection();
DynamoSelection.Instance.Selection.Add(node);
PinToNode(obj);
return;
}
}


private void SelectionOnCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
CreateGroupCommand.RaiseCanExecuteChanged();
Expand All @@ -184,7 +212,7 @@ private void Select(object parameter)

public void UpdateSizeFromView(double w, double h)
{
this._model.SetSize(w,h);
this._model.SetSize(w, h);
MoveNoteAbovePinnedNode();
}

Expand Down Expand Up @@ -307,7 +335,6 @@ private void PinToNode(object parameters)
}

Model.PinnedNode = nodeToPin;
Model.UndoRequest += UnpinFromNode;

MoveNoteAbovePinnedNode();
SubscribeToPinnedNode();
Expand All @@ -327,7 +354,7 @@ private bool CanPinToNode(object parameters)
var noteSelection = DynamoSelection.Instance.Selection
.OfType<NoteModel>();

if (nodeSelection == null || noteSelection == null ||
if (nodeSelection == null || noteSelection == null ||
nodeSelection.Count() != 1 || noteSelection.Count() != 1)
return false;

Expand Down Expand Up @@ -361,7 +388,6 @@ private bool CanUnpinFromNode(object parameters)
private void UnpinFromNode(object parameters)
{
UnsuscribeFromPinnedNode();
Model.UndoRequest -= UnpinFromNode;

Model.PinnedNode = null;
WorkspaceViewModel.HasUnsavedChanges = true;
Expand Down