forked from DynamoDS/Dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPortViewModel.cs
More file actions
696 lines (614 loc) · 24.7 KB
/
Copy pathPortViewModel.cs
File metadata and controls
696 lines (614 loc) · 24.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using Dynamo.Graph.Nodes;
using Dynamo.Graph.Workspaces;
using Dynamo.Models;
using Dynamo.Search.SearchElements;
using Dynamo.UI.Commands;
using Dynamo.Utilities;
using static Dynamo.ViewModels.SearchViewModel;
namespace Dynamo.ViewModels
{
/// <summary>
/// Port View Model
/// </summary>
public partial class PortViewModel : ViewModelBase
{
#region Properties/Fields
protected readonly PortModel port;
protected readonly NodeViewModel node;
private DelegateCommand useLevelsCommand;
private DelegateCommand keepListStructureCommand;
private bool showUseLevelMenu;
private const double autocompletePopupSpacing = 2.5;
private const double proxyPortContextMenuOffset = 20;
protected static readonly SolidColorBrush PortBackgroundColorPreviewOff = new SolidColorBrush(Color.FromRgb(102, 102, 102));
protected static readonly SolidColorBrush PortBackgroundColorDefault = new SolidColorBrush(Color.FromRgb(60, 60, 60));
protected static readonly SolidColorBrush PortBorderBrushColorDefault = new SolidColorBrush(Color.FromRgb(161, 161, 161));
private SolidColorBrush portBorderBrushColor = PortBorderBrushColorDefault;
private SolidColorBrush portBackgroundColor = PortBackgroundColorDefault;
private Visibility highlight = Visibility.Collapsed;
private bool nodeAutoCompleteMarkerEnabled = false;
/// <summary>
/// Port model.
/// </summary>
public PortModel PortModel
{
get { return port; }
}
/// <summary>
/// The content of tooltip.
/// </summary>
public string ToolTipContent
{
get { return port.ToolTip; }
}
/// <summary>
/// Port name.
/// </summary>
public string PortName
{
get { return GetPortDisplayName(port.Name); }
}
/// <summary>
/// Port type.
/// </summary>
public PortType PortType
{
get { return port.PortType; }
}
/// <summary>
/// If port is selected.
/// </summary>
public bool IsSelected
{
get { return node.IsSelected; }
}
/// <summary>
/// If port is connected.
/// </summary>
public bool IsConnected
{
get => port.IsConnected;
}
/// <summary>
/// Sets the condensed styling on Code Block output ports.
/// This is used to style the output ports on Code Blocks to be smaller.
/// </summary>
public bool IsPortCondensed
{
get
{
return this.PortModel.Owner is CodeBlockNodeModel && PortType == PortType.Output;
}
}
/// <summary>
/// If port is enabled.
/// </summary>
public bool IsEnabled
{
get { return port.IsEnabled; }
}
/// <summary>
/// The height of port.
/// </summary>
public double Height
{
get { return port.Height; }
}
/// <summary>
/// The center point of port.
/// </summary>
public Point Center
{
get { return port.Center.AsWindowsType(); }
}
/// <summary>
/// The state of host node.
/// </summary>
public ElementState State
{
get { return node.State; }
}
/// <summary>
/// Returns whether the port is using its default value, or whether this been disabled
/// </summary>
[Obsolete("This method will be removed in a future version of Dynamo - please use the InPortViewModel")]
internal bool UsingDefaultValue
{
get { return port.UsingDefaultValue; }
set
{
port.UsingDefaultValue = value;
}
}
/// <summary>
/// IsHitTestVisible property gets a value that declares whether
/// a Snapping rectangle can possibly be returned as a hit test result.
/// When FirstActiveConnector is not null, Snapping rectangle handles click events.
/// When FirstActiveConnector is null, Snapping rectangle does not handle click events
/// and user can "click though invisible snapping area".
/// </summary>
public bool IsHitTestVisible
{
get { return node.WorkspaceViewModel.FirstActiveConnector != null; }
}
/// <summary>
/// The margin thickness of port view.
/// </summary>
public System.Windows.Thickness MarginThickness
{
get { return port.MarginThickness.AsWindowsType(); }
}
public PortEventType EventType { get; set; }
/// <summary>
/// If should display Use Levels popup menu.
/// </summary>
[Obsolete("This method will be removed in a future version of Dynamo - please use the InPortViewModel")]
internal bool ShowUseLevelMenu
{
get
{
return showUseLevelMenu;
}
set
{
showUseLevelMenu = value;
RaisePropertyChanged(nameof(ShowUseLevelMenu));
}
}
internal NodeViewModel NodeViewModel
{
get => node;
}
/// <summary>
/// Sets the color of the port's border brush
/// </summary>
public SolidColorBrush PortBorderBrushColor
{
get => portBorderBrushColor;
set
{
portBorderBrushColor = value;
RaisePropertyChanged(nameof(PortBorderBrushColor));
}
}
/// <summary>
/// Highlight or clear highlight of the port.
/// </summary>
public Visibility Highlight
{
get => highlight;
set
{
highlight = value;
RaisePropertyChanged(nameof(Highlight));
}
}
/// <summary>
/// Controls whether the node autocomplete marker is visible
/// </summary>
public bool NodeAutoCompleteMarkerEnabled
{
get
{
if (node.WorkspaceViewModel.FirstActiveConnector != null || !NodeViewModel.DynamoViewModel.EnableNodeAutoComplete)
{
return false;
}
return CanHaveAutoCompleteMarker() && nodeAutoCompleteMarkerEnabled;
}
set
{
nodeAutoCompleteMarkerEnabled = value;
RaisePropertyChanged(nameof(NodeAutoCompleteMarkerEnabled));
}
}
internal bool CanHaveAutoCompleteMarker()
{
return (this is InPortViewModel && PortModel.Connectors.Count == 0 || this is OutPortViewModel)
&& !this.PortModel.IsProxyPort
&& NodeViewModel.NodeModel is not CodeBlockNodeModel
&& NodeViewModel.NodeModel is not CoreNodeModels.Watch
&& NodeViewModel.NodeModel is not Watch3DNodeModels.Watch3D
&& NodeViewModel.NodeModel is not PythonNodeModels.PythonNode
&& NodeViewModel.NodeModel is not PythonNodeModels.PythonStringNode
&& !NodeViewModel.IsTransient
&& !NodeViewModel.IsFrozen;
}
/// <summary>
/// Sets the color of the port's background - affected by multiple factors such as
/// MouseOver, IsConnected, Node States (active, inactive, frozen
/// </summary>
public SolidColorBrush PortBackgroundColor
{
get => portBackgroundColor;
set
{
portBackgroundColor = value;
RaisePropertyChanged(nameof(PortBackgroundColor));
}
}
#endregion
#region events
public event EventHandler MouseEnter;
public event EventHandler MouseLeave;
public event EventHandler MouseLeftButtonDown;
public event EventHandler MouseLeftButtonDownOnLevel;
#endregion
public PortViewModel(NodeViewModel node, PortModel port)
{
this.node = node;
this.port = port;
this.port.PropertyChanged += PortPropertyChanged;
this.node.PropertyChanged += NodePropertyChanged;
this.node.WorkspaceViewModel.PropertyChanged += WorkspacePropertyChanged;
this.node.WorkspaceViewModel.DynamoViewModel.PreferencesViewModel.PropertyChanged += PreferencesViewModelPropertyChanged;
//turn on the autocomplete marker if enabled
NodeAutoCompleteMarkerEnabled = NodeViewModel.DynamoViewModel.EnableNodeAutoComplete;
RefreshPortColors();
}
public override void Dispose()
{
port.PropertyChanged -= PortPropertyChanged;
node.PropertyChanged -= NodePropertyChanged;
node.WorkspaceViewModel.PropertyChanged -= WorkspacePropertyChanged;
node.WorkspaceViewModel.DynamoViewModel.PreferencesViewModel.PropertyChanged -= PreferencesViewModelPropertyChanged;
}
internal virtual PortViewModel CreateProxyPortViewModel(PortModel portModel)
{
portModel.IsProxyPort = true;
return new PortViewModel(node, portModel);
}
private UIElement FindProxyPortUIElement(PortViewModel proxyPortViewModel)
{
var mainWindow = Application.Current.MainWindow;
return FindChild<UIElement>(mainWindow, e =>
e is FrameworkElement fe && fe.DataContext == proxyPortViewModel);
}
private T FindChild<T>(DependencyObject parent, Func<T, bool> predicate) where T : DependencyObject
{
if (parent == null) return null;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is T t && predicate(t))
{
return t;
}
var foundChild = FindChild(child, predicate);
if (foundChild != null)
return foundChild;
}
return null;
}
/// <summary>
/// Sets up the node autocomplete window to be placed relative to the node.
/// </summary>
/// <param name="window">Node autocomplete popup.</param>
internal void SetupNodeAutoCompleteWindowPlacement(Window window)
{
node.OnRequestAutoCompletePopupPlacementTarget(window, PortModel, autocompletePopupSpacing);
}
/// <summary>
/// Sets up the node cluster autocomplete flyout window to be placed relative to the node.
/// </summary>
/// <param name="window">Node cluster autocomplete window.</param>
internal void SetupNodeAutoCompleteClusterWindowPlacement(Window window)
{
node.OnClusterRequestAutoCompletePopupPlacementTarget(window, autocompletePopupSpacing);
}
/// <summary>
/// Sets up the PortContextMenu window to be placed relative to the port.
/// </summary>
/// <param name="popup">Node context menu popup.</param>
internal void SetupPortContextMenuPlacement(Popup popup)
{
var zoom = node.WorkspaceViewModel.Zoom;
if (PortModel.IsProxyPort)
{
// Find the UI element associated with the proxy port
var proxyPortElement = FindProxyPortUIElement(this);
if (proxyPortElement != null)
{
popup.PlacementTarget = proxyPortElement;
ConfigurePopupPlacement(popup, zoom);
return;
}
}
node.OnRequestPortContextMenuPlacementTarget(popup);
popup.CustomPopupPlacementCallback = PlacePortContextMenu;
}
/// <summary>
/// Configures the custom placement of the proxyport context menu popup.
/// </summary>
private void ConfigurePopupPlacement(Popup popup, double zoom)
{
popup.CustomPopupPlacementCallback = (popupSize, targetSize, offset) =>
{
double x;
double y = (targetSize.Height - popupSize.Height) / 2;
if (this is InPortViewModel)
{
x = -popupSize.Width + proxyPortContextMenuOffset * zoom;
}
else
{
x = targetSize.Width - proxyPortContextMenuOffset * zoom;
}
return new[] { new CustomPopupPlacement(new Point(x, y), PopupPrimaryAxis.None) };
};
}
private CustomPopupPlacement[] PlacePortContextMenu(Size popupSize, Size targetSize, Point offset)
{
// The actual zoom here is confusing
// What matters is the zoom factor measured from the scaled : unscaled node size
var zoom = node.WorkspaceViewModel.Zoom;
var source = PresentationSource.FromVisual(Application.Current.MainWindow);
var dpiScale = source?.CompositionTarget?.TransformToDevice.M22 ?? 1.0;
double x;
var scaledWidth = autocompletePopupSpacing * targetSize.Width / node.ActualWidth;
if (PortModel.PortType == PortType.Input)
{
// Offset popup to the left by its width from left edge of node and spacing.
x = -scaledWidth - popupSize.Width;
}
else
{
// Offset popup to the right by node width and spacing from left edge of node.
x = scaledWidth + targetSize.Width;
}
// Important - while zooming in and out, Node elements are scaled, while popup is not
// Calculate absolute popup halfheight to deduct from the overall y pos
// Then add the header, port height and port index position
var popupHeightOffset = - popupSize.Height * 0.5;
var headerHeightOffset = NodeModel.HeaderHeight * zoom;
var portHalfHeight = PortModel.Height * 0.5 * zoom;
var rowOffset = PortModel.Index * PortModel.Height * zoom;
var customNodeOffset = NodeModel.CustomNodeTopBorderHeight * zoom;
// popupSize.Height is already DPI-scaled (in screen pixels), so we do NOT apply dpiScale to it
// All other layout values are in logical units and must be multiplied by dpiScale for correct placement
var y = popupHeightOffset + (headerHeightOffset + portHalfHeight + rowOffset + customNodeOffset) * dpiScale;
var placement = new CustomPopupPlacement(new Point(x, y), PopupPrimaryAxis.None);
return new[] { placement };
}
private void WorkspacePropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "ActiveConnector":
RaisePropertyChanged(nameof(IsHitTestVisible));
RaisePropertyChanged(nameof(NodeAutoCompleteMarkerEnabled));
break;
}
}
private void PreferencesViewModelPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "NodeAutocompleteIsChecked":
RaisePropertyChanged(nameof(NodeAutoCompleteMarkerEnabled));
break;
}
}
private void NodePropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(IsSelected):
RaisePropertyChanged(nameof(IsSelected));
break;
case nameof(State):
RaisePropertyChanged(nameof(State));
RefreshPortColors();
break;
case nameof(ToolTipContent):
RaisePropertyChanged(nameof(ToolTipContent));
break;
case nameof(node.IsVisible):
RefreshPortColors();
break;
case nameof(node.NodeModel.CachedValue):
RefreshPortColors();
break;
}
}
private void PortPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "ToolTip":
RaisePropertyChanged(nameof(ToolTipContent));
break;
case nameof(PortType):
RaisePropertyChanged(nameof(PortType));
break;
case nameof(PortName):
RaisePropertyChanged(nameof(PortName));
break;
case nameof(IsConnected):
RaisePropertyChanged(nameof(IsConnected));
RaisePropertyChanged(nameof(NodeAutoCompleteMarkerEnabled));
RefreshPortColors();
break;
case nameof(IsEnabled):
RaisePropertyChanged(nameof(IsEnabled));
break;
case nameof(Center):
RaisePropertyChanged(nameof(Center));
break;
case nameof(MarginThickness):
RaisePropertyChanged(nameof(MarginThickness));
break;
case nameof(UsingDefaultValue):
RefreshPortColors();
break;
}
}
private void Connect(object parameter)
{
DynamoViewModel dynamoViewModel = this.node.DynamoViewModel;
WorkspaceViewModel workspaceViewModel = dynamoViewModel.CurrentSpaceViewModel;
workspaceViewModel.HandlePortClicked(this);
}
protected bool CanConnect(object parameter)
{
if (node?.IsTransient is true ||
PortModel?.HasTransientConnections() is true)
{
return false;
}
return true;
}
// Handler to invoke node Auto Complete
private void AutoComplete(object parameter)
{
//handle the mouse event to prevent connection from starting
MouseButtonEventArgs evArgs = parameter as MouseButtonEventArgs;
if (evArgs != null)
{
evArgs.Handled = true;
}
var wsViewModel = node?.WorkspaceViewModel;
if (wsViewModel is null || wsViewModel.NodeAutoCompleteSearchViewModel is null)
{
return;
}
var existingPort = wsViewModel.NodeAutoCompleteSearchViewModel.PortViewModel;
if (existingPort != null)
{
existingPort.Highlight = Visibility.Collapsed;
}
wsViewModel.NodeAutoCompleteSearchViewModel.PortViewModel = this;
wsViewModel.OnRequestNodeAutoCompleteSearch();
}
// Handler to invoke Node autocomplete cluster
private void AutoCompleteCluster(object parameter)
{
//handle the mouse event to prevent connection from starting
MouseButtonEventArgs evArgs = parameter as MouseButtonEventArgs;
if (evArgs != null)
{
evArgs.Handled = true;
}
var wsViewModel = node.WorkspaceViewModel;
wsViewModel?.OnRequestNodeAutocompleteBar(this);
}
private void NodePortContextMenu(object obj)
{
// If this port does not display a Chevron button to open the context menu and it doesn't
// have a default value then using right-click to open the context menu should also do nothing.
// Added check for Python node model (allow input context menu for rename)
if (obj is InPortViewModel inPortViewModel &&
inPortViewModel.UseLevelVisibility == Visibility.Collapsed &&
!inPortViewModel.DefaultValueEnabled &&
!(inPortViewModel.NodeViewModel.NodeModel is PythonNodeModels.PythonNode)) return;
var wsViewModel = node.WorkspaceViewModel;
wsViewModel.CancelActiveState();
wsViewModel.OnRequestPortContextMenu(ShowHideFlags.Show, this);
}
private bool CanShowContextMenu(object obj)
{
if (node?.IsTransient is true ||
PortModel?.HasTransientConnections() is true)
{
return false;
}
return true;
}
private bool CanAutoComplete(object parameter)
{
DynamoViewModel dynamoViewModel = node.DynamoViewModel;
// If user trying to trigger Node AutoComplete from proxy ports, display notification
// telling user it is not available that way
if (port.IsProxyPort)
{
dynamoViewModel.MainGuideManager.CreateRealTimeInfoWindow(Wpf.Properties.Resources.NodeAutoCompleteNotAvailableForCollapsedGroups);
}
// We can AutoComplete if the feature is enabled from Dynamo experiment setting,
// if user interaction is not on proxy ports and if the port is not an input already connected.
return dynamoViewModel.EnableNodeAutoComplete && !port.IsProxyPort &&
!(PortType == PortType.Input && PortModel?.Connectors?.FirstOrDefault()?.Start?.Owner != null);
}
/// <summary>
/// Handles the Mouse enter event on the port
/// </summary>
/// <param name="parameter">The parameter.</param>
private void OnRectangleMouseEnter(object parameter)
{
MouseEnter?.Invoke(parameter, null);
}
/// <summary>
/// Handles the Mouse leave on the port
/// </summary>
/// <param name="parameter">The parameter.</param>
private void OnRectangleMouseLeave(object parameter)
{
MouseLeave?.Invoke(parameter, null);
}
/// <summary>
/// Handles the Mouse left button down on the port
/// </summary>
/// <param name="parameter">The parameter.</param>
private void OnRectangleMouseLeftButtonDown(object parameter)
{
MouseLeftButtonDown?.Invoke(parameter, null);
}
/// <summary>
/// Handles the Mouse left button down on the level.
/// </summary>
/// <param name="parameter"></param>
private void OnMouseLeftButtonDownOnLevel(object parameter)
{
ShowUseLevelMenu = true;
}
/// <summary>
/// Handle the Mouse left from Use Level popup.
/// </summary>
/// <param name="parameter"></param>
private void OnMouseLeftUseLevel(object parameter)
{
ShowUseLevelMenu = false;
}
/// <summary>
/// Handles the logic for updating the PortBackgroundColor and PortBackgroundBrushColor
/// </summary>
protected virtual void RefreshPortColors()
{
PortBackgroundColor = PortBackgroundColorDefault;
PortBorderBrushColor = PortBorderBrushColorDefault;
}
/// <summary>
/// Replaces the old PortNameConverter.
/// Ports without names are generally converter chevrons i.e. '>'. However, if an output
/// port is displaying its context menu chevron AND has no name (e.g. the Function node)
/// the output port is renamed in order to avoid confusing the user with double chevrons.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private string GetPortDisplayName(string value)
{
if (value is string && !string.IsNullOrEmpty(value as string))
{
return value as string;
}
if (node.ArgumentLacing != LacingStrategy.Disabled)
{
switch (port.PortType)
{
case PortType.Input:
return Properties.Resources.InputPortAlternativeName;
case PortType.Output:
return Properties.Resources.OutputPortAlternativeName;
}
}
return ">";
}
}
}