Skip to content

Commit

Permalink
Watch node custom size serialization (#13844)
Browse files Browse the repository at this point in the history
* Watch node custom size serialization

* Fix tests

* Fix edge cases.

* Add test

* Fix tests

* comments
  • Loading branch information
reddyashish authored and sm6srw committed Apr 5, 2023
1 parent 2f92775 commit d4ce715
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 24 deletions.
3 changes: 3 additions & 0 deletions src/DynamoCoreWpf/ViewModels/Preview/WatchViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Dynamo.Utilities;
using Microsoft.Practices.Prism.ViewModel;
using Dynamo.Configuration;
using CoreNodeModels;

namespace Dynamo.ViewModels
{
Expand Down Expand Up @@ -36,6 +37,8 @@ internal void Click()
public const string EMPTY_DICTIONARY = "Empty Dictionary";
public const string DICTIONARY = "Dictionary";

internal Watch WatchNode { get; set; }

private ObservableCollection<WatchViewModel> children = new ObservableCollection<WatchViewModel>();
private string label;
private string link;
Expand Down
33 changes: 24 additions & 9 deletions src/DynamoCoreWpf/Views/Preview/WatchTree.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Windows;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using Dynamo.ViewModels;
using System;
using CoreNodeModels;

namespace Dynamo.Controls
{
Expand All @@ -15,10 +16,10 @@ public partial class WatchTree : UserControl
{
private WatchViewModel _vm;
private WatchViewModel prevWatchViewModel;
private readonly double defaultWidthSize = 200;
private static readonly double defaultWidthSize = 200;
private readonly double extraWidthSize = 20;
private readonly double widthPerCharacter = 7.5;
private readonly int defaultHeightSize = 200;
private static readonly int defaultHeightSize = 200;
private readonly int minWidthSize = 100;
private readonly int minHeightSize = 38;
private readonly int minHeightForList = 83;
Expand All @@ -34,8 +35,8 @@ public WatchTree(WatchViewModel vm)
this.Unloaded += WatchTree_Unloaded;
}

internal double DefaultWidthSize { get { return defaultWidthSize; } }
internal double DefaultHeightSize { get { return defaultHeightSize; } }
internal static double DefaultWidthSize { get { return defaultWidthSize; } }
internal static double DefaultHeightSize { get { return defaultHeightSize; } }
internal double ExtratWidthSize { get { return extraWidthSize; } }
internal double WidthPerCharacter { get { return widthPerCharacter; } }
internal double MaxWidthSize { get { return defaultWidthSize * 2; } }
Expand All @@ -53,15 +54,27 @@ void WatchTree_Loaded(object sender, RoutedEventArgs e)

private void _vm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
//We need to restore the custom size for the watch nodes in the workspace.
var customSizesDict = Watch.NodeSizes;
var customWidth = double.NaN;
var customHeight= double.NaN;

if (_vm.WatchNode != null)
{
customSizesDict.TryGetValue(_vm.WatchNode.GUID, out var customSizes);
customWidth = customSizes == null ? double.NaN : customSizes.Item1;
customHeight = customSizes == null ? double.NaN : customSizes.Item2;
}

if (e.PropertyName == nameof(WatchViewModel.IsCollection))
{
// // The WatchTree controll will resize only if its role is a WatchNode (starts with an specific height), otherwise it won't resize (Bubble role).
if (!Double.IsNaN(this.Height))
{
if (_vm.IsCollection)
{
this.Height = defaultHeightSize;
this.inputGrid.MinHeight = minHeightForList;
Height = this.Height != customHeight ? defaultHeightSize : Height;
inputGrid.MinHeight = minHeightForList;
}
else
{
Expand All @@ -70,7 +83,7 @@ private void _vm_PropertyChanged(object sender, System.ComponentModel.PropertyCh
{
if (NodeLabel.Contains(Environment.NewLine) || NodeLabel.ToUpper() == nameof(WatchViewModel.DICTIONARY))
{
this.Height = defaultHeightSize;
Height = this.Height != customHeight ? defaultHeightSize : Height;
}
}
}
Expand Down Expand Up @@ -99,7 +112,7 @@ private void _vm_PropertyChanged(object sender, System.ComponentModel.PropertyCh
}
else
{
this.Width = defaultWidthSize;
this.Width = this.Width != customWidth ? defaultWidthSize : this.Width;
}
}
}
Expand Down Expand Up @@ -213,6 +226,8 @@ private void ThumbResizeThumbOnDragDeltaHandler(object sender, DragDeltaEventArg
{
Height = yAdjust;
}

Watch.NodeSizes[_vm.WatchNode.GUID] = new Tuple<double, double>(Width, Height);
}
}
}
19 changes: 17 additions & 2 deletions src/Libraries/CoreNodeModels/Watch.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using CoreNodeModels.Properties;
using Dynamo.Engine;
Expand Down Expand Up @@ -40,6 +40,22 @@ private Watch(IEnumerable<PortModel> inPorts, IEnumerable<PortModel> outPorts):b
HasRunOnce = false;
}

/// <summary>
/// Watch node's Width
public double WatchWidth { get; set; }
/// <summary>
/// Watch node's Height
public double WatchHeight { get; set; }

//Stores the custom sizes for each watch node.
public static Dictionary<Guid, Tuple<double,double>> NodeSizes = new Dictionary<Guid, Tuple<double, double>>();

public void SetWatchSize(double w, double h)
{
WatchWidth = w;
WatchHeight = h;
}

public Watch()
{
InPorts.Add(new PortModel(PortType.Input, this, new PortData("", Resources.WatchPortDataInputToolTip)));
Expand All @@ -48,7 +64,6 @@ public Watch()
RegisterAllPorts();

ArgumentLacing = LacingStrategy.Disabled;

ShouldDisplayPreviewCore = false;
HasRunOnce = false;
}
Expand Down
22 changes: 20 additions & 2 deletions src/Libraries/CoreNodeModelsWpf/NodeViewCustomizations/Watch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,35 @@ public void CustomizeView(Watch nodeModel, NodeView nodeView)
rootWatchViewModel = new WatchViewModel(dynamoViewModel.BackgroundPreviewViewModel.AddLabelForPath);

var watchTree = new WatchTree(rootWatchViewModel);

rootWatchViewModel.WatchNode = watch;
watchTree.BorderThickness = new Thickness(1, 1, 1, 1);
watchTree.BorderBrush = new SolidColorBrush(Color.FromRgb(220,220,220));

watchTree.SetWatchNodeProperties();

nodeView.SizeChanged += (sender, args) =>
nodeModel.SetSize(args.NewSize.Width, args.NewSize.Height);

watchTree.SizeChanged += (sender, args) =>
nodeModel.SetWatchSize(args.NewSize.Width, args.NewSize.Height);

nodeView.PresentationGrid.Children.Add(watchTree);
nodeView.PresentationGrid.Visibility = Visibility.Visible;
// disable preview control
nodeView.TogglePreviewControlAllowance();

ResetWatch();

watchTree.Width = nodeModel.WatchWidth == 0 ? watchTree.Width : nodeModel.WatchWidth;
watchTree.Height = nodeModel.WatchHeight == 0 ? watchTree.Height : nodeModel.WatchHeight;

//Store width and height info of the node in the dictionary.
Watch.NodeSizes[watch.GUID] = new Tuple<double, double>(nodeModel.WatchWidth, nodeModel.WatchHeight);

Bind(watchTree, nodeView);

Subscribe();
ResetWatch();
}

private void Bind(WatchTree watchTree, NodeView nodeView)
Expand Down Expand Up @@ -132,6 +147,9 @@ private void OnPortConnected(PortModel port, ConnectorModel connectorModel)
{
Tuple<int, NodeModel> input;

//set default width and height when a new connection is made to the watch node.
watch.SetWatchSize(WatchTree.DefaultWidthSize, WatchTree.DefaultHeightSize);

if (!watch.TryGetInput(watch.InPorts.IndexOf(connectorModel.End), out input)
|| astBeingComputed == null) return;

Expand Down Expand Up @@ -265,4 +283,4 @@ private void OnCopyToClipboardClick(object sender, RoutedEventArgs e)
if (!string.IsNullOrEmpty(content)) Clipboard.SetText(content);
}
}
}
}
8 changes: 4 additions & 4 deletions test/DynamoCoreWpfTests/PreviewBubbleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public void Watch_DefaultSize()
DispatcherUtil.DoEvents();

Assert.NotNull(rawEmptyWatchNode);
Assert.AreEqual(rawEmptyWatchNode.Width, rawEmptyWatchNode.DefaultWidthSize);
Assert.AreEqual(rawEmptyWatchNode.Width, WatchTree.DefaultWidthSize);
}

[Test]
Expand Down Expand Up @@ -358,7 +358,7 @@ public void Watch_ListSize()

Assert.NotNull(listWatchNode);
Assert.IsTrue(isTheCodeAList);
Assert.AreEqual(rawListWatchNode.Height, rawListWatchNode.DefaultHeightSize);
Assert.AreEqual(rawListWatchNode.Height, WatchTree.DefaultHeightSize);
}

[Test]
Expand Down Expand Up @@ -444,7 +444,7 @@ public void Watch_MultilineString()

Assert.NotNull(multiLineStringWatchNode);
Assert.IsTrue(containsNewLine);
Assert.AreEqual(rawMultiLineStringtWatchNode.DefaultHeightSize, rawMultiLineStringtWatchNode.Height);
Assert.AreEqual(WatchTree.DefaultHeightSize, rawMultiLineStringtWatchNode.Height);
}

[Test]
Expand All @@ -470,7 +470,7 @@ public void Watch_Dictionary()

Assert.NotNull(dictionaryWatchNode);
Assert.IsTrue(isDictionary);
Assert.AreEqual(rawDictionaryWatchNode.DefaultHeightSize, rawDictionaryWatchNode.Height);
Assert.AreEqual(WatchTree.DefaultHeightSize, rawDictionaryWatchNode.Height);
}

#endregion
Expand Down
53 changes: 46 additions & 7 deletions test/DynamoCoreWpfTests/WatchNodeTests.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using Dynamo.Interfaces;
using NUnit.Framework;
using System.IO;
using Dynamo.ViewModels;
using ProtoCore.Mirror;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using CoreNodeModels;
using Dynamo.Controls;
using Dynamo.Graph.Nodes;
using Dynamo.Graph.Nodes.ZeroTouch;
using Dynamo.Interfaces;
using Dynamo.Tests;
using System.Globalization;
using System.Threading;
using Dynamo.ViewModels;
using NUnit.Framework;
using ProtoCore.Mirror;

namespace DynamoCoreWpfTests
{
Expand Down Expand Up @@ -256,6 +257,44 @@ public void WatchNestedDictionary()
Assert.AreEqual(2, watchVM.NumberOfItems);
}

[Test]
public void WatchNodeSizeSerializationTest()
{
string openPath = Path.Combine(TestDirectory, @"core\watch\WatchSerializationTest.dyn");
ViewModel.OpenCommand.Execute(openPath);
ViewModel.HomeSpace.Run();

var watchNode = ViewModel.Model.CurrentWorkspace.NodeFromWorkspace("76ea40b1-5e21-48b8-9051-0b6b03ee5075") as Watch;

//assert default width and height for the watch node.
Assert.AreEqual(watchNode.WatchWidth, WatchTree.DefaultWidthSize);
Assert.AreEqual(watchNode.WatchHeight, WatchTree.DefaultHeightSize);

//Set the width and height of watch node to new values.
watchNode.WatchWidth = 150;
watchNode.WatchHeight = 300;

//save the workspace and reopen it to test (de)seralization
ViewModel.HomeSpace.Save(openPath);
ViewModel.HomeSpace.Clear();

ViewModel.OpenCommand.Execute(openPath);
ViewModel.HomeSpace.Run();

watchNode = ViewModel.Model.CurrentWorkspace.NodeFromWorkspace("76ea40b1-5e21-48b8-9051-0b6b03ee5075") as Watch;

//Assert new width and height
Assert.AreEqual(watchNode.WatchWidth, 150);
Assert.AreEqual(watchNode.WatchHeight, 300);

//reset back to default.
watchNode.WatchWidth = WatchTree.DefaultWidthSize;
watchNode.WatchHeight = WatchTree.DefaultHeightSize;

ViewModel.HomeSpace.Save(openPath);
ViewModel.HomeSpace.Clear();
}

[Test]
public void WatchNumber()
{
Expand Down
Loading

0 comments on commit d4ce715

Please sign in to comment.