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

Pin Wire from Right-click Context Menu #13102

Merged
merged 6 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
32 changes: 32 additions & 0 deletions src/DynamoCoreWpf/Commands/WorkspaceCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public partial class WorkspaceViewModel
private DelegateCommand showInCanvasSearchCommand;
private DelegateCommand pasteCommand;
private DelegateCommand hideAllPopupCommand;
private DelegateCommand showAllWiresCommand;
private DelegateCommand hideAllWiresCommand;

#endregion

Expand Down Expand Up @@ -243,6 +245,36 @@ public DelegateCommand HideAllPopupCommand
return hideAllPopupCommand;
}
}

/// <summary>
/// View Command to show all connection wires (on current selection), if any are hidden
/// </summary>
[JsonIgnore]
public DelegateCommand ShowAllWiresCommand
{
get
{
if(showAllWiresCommand == null)
showAllWiresCommand = new DelegateCommand(ShowAllWires, CanShowAllWires);

return showAllWiresCommand;
}
}

/// <summary>
/// View Command to hide all connection wires (on current selection), if any are shown
/// </summary>
[JsonIgnore]
public DelegateCommand HideAllWiresCommand
{
get
{
if(hideAllWiresCommand == null)
hideAllWiresCommand = new DelegateCommand(HideAllWires, CanHideAllWires);

return hideAllWiresCommand;
}
}
#endregion

#region Properties for Command Data Binding
Expand Down
36 changes: 36 additions & 0 deletions src/DynamoCoreWpf/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/DynamoCoreWpf/Properties/Resources.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3252,6 +3252,18 @@ You can manage this in Preferences -&gt; Security.</value>
<data name="UnableToAccessTrustedDirectory" xml:space="preserve">
<value>Unable To Access Trusted Directory</value>
</data>
<data name="ConnectorContextMenuHeaderPinConnector" xml:space="preserve">
<value>Pin Wire</value>
</data>
<data name="ContextMenuConnectionsHideAll" xml:space="preserve">
<value>Hide All Wires</value>
</data>
<data name="ContextMenuConnectionsShowAll" xml:space="preserve">
<value>Show All Wires</value>
</data>
<data name="ContextMenuNodeConnections" xml:space="preserve">
<value>Node Connections</value>
</data>
<data name="InvalidDraggingOperationMessgae" xml:space="preserve">
<value>Nothing is being dragged. If you see this message, most likely your recent Dynamo interaction is not recommended.</value>
</data>
Expand Down
12 changes: 12 additions & 0 deletions src/DynamoCoreWpf/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3239,6 +3239,18 @@ You can manage this in Preferences -&gt; Security.</value>
<data name="UnableToAccessTrustedDirectory" xml:space="preserve">
<value>Unable To Access Directory</value>
</data>
<data name="ConnectorContextMenuHeaderPinConnector" xml:space="preserve">
<value>Pin Wire</value>
</data>
<data name="ContextMenuConnectionsHideAll" xml:space="preserve">
<value>Hide All Wires</value>
</data>
<data name="ContextMenuConnectionsShowAll" xml:space="preserve">
<value>Show All Wires</value>
</data>
<data name="ContextMenuNodeConnections" xml:space="preserve">
<value>Node Connections</value>
</data>
<data name="InvalidDraggingOperationMessgae" xml:space="preserve">
<value>Nothing is being dragged. If you see this message, most likely your recent Dynamo interaction is not recommended.</value>
</data>
Expand Down
4 changes: 2 additions & 2 deletions src/DynamoCoreWpf/ViewModels/Core/ConnectorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ private void BreakConnectionCommandExecute(object parameter)
/// Toggles wire viz on/off. This can be overwritten when a node is selected in hidden mode.
/// </summary>
/// <param name="parameter"></param>
private void HideConnectorCommandExecute(object parameter)
internal void HideConnectorCommandExecute(object parameter)
{
// Use the inverse of the current visibility state,
// unless the command is coming from the port, in
Expand Down Expand Up @@ -843,7 +843,7 @@ private void SelectConnectedCommandExecute(object parameter)
private void PinConnectorCommandExecute(object parameters)
{
MousePosition = new Point(PanelX - ConnectorPinModel.StaticWidth, PanelY - ConnectorPinModel.StaticWidth);
ConnectorAnchorViewModel.CurrentPosition = MousePosition;
if (ConnectorAnchorViewModel != null) ConnectorAnchorViewModel.CurrentPosition = MousePosition;
if (MousePosition == new Point(0, 0)) return;
var connectorPinModel = new ConnectorPinModel(MousePosition.X, MousePosition.Y, Guid.NewGuid(), model.GUID);
ConnectorModel.AddPin(connectorPinModel);
Expand Down
51 changes: 50 additions & 1 deletion src/DynamoCoreWpf/ViewModels/Core/WorkspaceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,53 @@ private static bool CanDoGraphAutoLayout(object o)
return true;
}

private void ShowAllWires(object o)
{
var nodeModels = DynamoSelection.Instance.Selection.OfType<NodeModel>().Where(n => n.AllConnectors.Any(x => x.IsHidden)).ToList();
ShowHideAllWires(nodeModels, false);
}

private bool CanShowAllWires(object o)
{
return DynamoSelection.Instance.Selection.OfType<NodeModel>()
.Any(n => n.AllConnectors.Any(x => x.IsHidden));
}

private void HideAllWires(object o)
{
var nodeModels = DynamoSelection.Instance.Selection.OfType<NodeModel>().Where(n => n.AllConnectors.Any(x => !x.IsHidden)).ToList();
ShowHideAllWires(nodeModels, true);

}

private bool CanHideAllWires(object o)
{
return DynamoSelection.Instance.Selection.OfType<NodeModel>()
.Any(n => n.AllConnectors.Any(x => !x.IsHidden));
}

/// <summary>
/// Shows or Hides all wires of a list of nodeModels
/// </summary>
/// <param name="nodeModels"></param>
/// <param name="isHidden"></param>
private void ShowHideAllWires(List<NodeModel> nodeModels, bool isHidden)
{
if (!nodeModels.Any()) return;

foreach (var nodeModel in nodeModels)
{
var connectors = nodeModel.AllConnectors;
var enumerator = connectors.GetEnumerator();
while (enumerator.MoveNext())
reddyashish marked this conversation as resolved.
Show resolved Hide resolved
{
var connector = enumerator.Current;
if(connector != null)
connector.IsHidden = isHidden;
}
}
}

/// <summary>
/// Collapse a set of nodes and notes currently selected in workspace
/// </summary>
Expand Down Expand Up @@ -1553,7 +1600,9 @@ private void RefreshViewOnSelectionChange(object sender, NotifyCollectionChanged
{
AlignSelectedCommand.RaiseCanExecuteChanged();
ShowHideAllGeometryPreviewCommand.RaiseCanExecuteChanged();
SetArgumentLacingCommand.RaiseCanExecuteChanged();
SetArgumentLacingCommand.RaiseCanExecuteChanged();
ShowAllWiresCommand.RaiseCanExecuteChanged();
HideAllWiresCommand.RaiseCanExecuteChanged();
RaisePropertyChanged("HasSelection");
RaisePropertyChanged("IsGeometryOperationEnabled");
RaisePropertyChanged("AnyNodeVisible");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,24 @@ public override void Dispose()
public DelegateCommand HideConnectorSurrogateCommand { get; set; }

/// <summary>
/// Alerts ConnectorViewModel select connnected nodes.
/// Alerts ConnectorViewModel select connected nodes.
/// </summary>
public DelegateCommand SelectConnectedSurrogateCommand { get; set; }
/// <summary>
/// Alerts ConnectorViewModel to break the current connection.
/// </summary>
public DelegateCommand BreakConnectionsSurrogateCommand { get; set; }
/// <summary>
/// Alerts ConnectorViewModel to pin the connector
/// </summary>
public DelegateCommand PinConnectedSurrogateCommand { get; set; }

private void InitCommands()
{
HideConnectorSurrogateCommand = new DelegateCommand(HideConnectorSurrogateCommandExecute, x => true);
SelectConnectedSurrogateCommand = new DelegateCommand(SelectConnectedSurrogateCommandExecute, x => true);
BreakConnectionsSurrogateCommand = new DelegateCommand(BreakConnectionsSurrogateCommandExecute, x => true);
PinConnectedSurrogateCommand = new DelegateCommand(PinConnectedSurrogateCommandExecute, x => true);
}

/// <summary>
Expand Down Expand Up @@ -140,6 +145,17 @@ private void HideConnectorSurrogateCommandExecute(object obj)
ViewModel.ShowhideConnectorCommand.Execute(null);
}

/// <summary>
/// Request disposal of this viewmodel after command has run.
/// </summary>
/// <param name="obj"></param>
private void PinConnectedSurrogateCommandExecute(object obj)
{
ViewModel.PinConnectorCommand.Execute(null);
// Track pin connected nodes event
Analytics.TrackEvent(Actions.Pin, Categories.ConnectorOperations, "PinWire");
}

#endregion


Expand Down
4 changes: 3 additions & 1 deletion src/DynamoCoreWpf/Views/Core/ConnectorContextMenuView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
Width="170"
MouseLeave="OnMouseLeaveContextMenu"
ContextMenuClosing="OnContextMenuClosing">
<MenuItem x:Name="BreakItem" Header="{x:Static props:Resources.ConnectorContextMenuHeaderBreakConnection}"
<MenuItem x:Name="BreakItem" Header="{x:Static props:Resources.ConnectorContextMenuHeaderBreakConnection}"
Command="{Binding BreakConnectionsSurrogateCommand}"/>
<MenuItem x:Name="SelectItem" Header="{x:Static props:Resources.ConnectorContextMenuHeaderSelectConnected}"
Command="{Binding SelectConnectedSurrogateCommand}"/>
<MenuItem x:Name="PinItem" Header="{x:Static props:Resources.ConnectorContextMenuHeaderPinConnector}"
Command="{Binding PinConnectedSurrogateCommand}"/>
<MenuItem x:Name="HideItem" Command="{Binding HideConnectorSurrogateCommand}">
<MenuItem.Style>
<Style TargetType="MenuItem" BasedOn="{StaticResource ContextMenuItemStyle}">
Expand Down
29 changes: 29 additions & 0 deletions src/DynamoCoreWpf/Views/Core/WorkspaceView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,35 @@
Header="{x:Static p:Resources.ContextMenuNodesFromGeometry}"
Visibility="{Binding Path=CanFindNodesFromElements, Converter={StaticResource BooleanToVisibilityCollapsedConverter}}" />

<!-- Node Connections Menu -->
<controls:ParentMenuItem x:Name="NodeConnectionsMenu" Header="{x:Static p:Resources.ContextMenuNodeConnections}" >

<!-- Connections: Show All Wires -->
<MenuItem Command="{Binding Path=ShowAllWiresCommand}"
Header="{x:Static p:Resources.ContextMenuConnectionsShowAll}"
IsCheckable="True">
<MenuItem.IsChecked>
<Binding Converter="{StaticResource EnumToBoolConverter}"
ConverterParameter="Auto"
Mode="OneWay"
Path="SelectionArgumentLacing" />
</MenuItem.IsChecked>
</MenuItem>

<!-- Connections: Hide All Wires -->
<MenuItem Command="{Binding Path=HideAllWiresCommand}"
Header="{x:Static p:Resources.ContextMenuConnectionsHideAll}"
IsCheckable="True">
<MenuItem.IsChecked>
<Binding Converter="{StaticResource EnumToBoolConverter}"
ConverterParameter="Shortest"
Mode="OneWay"
Path="SelectionArgumentLacing" />
</MenuItem.IsChecked>
</MenuItem>

</controls:ParentMenuItem>

<Separator Visibility="{Binding Path=CanCopyOrPaste, Converter={StaticResource BooleanToVisibilityCollapsedConverter}}" />

<!-- Copy -->
Expand Down
60 changes: 58 additions & 2 deletions test/DynamoCoreWpfTests/ConnectorContextMenuTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using Dynamo.Selection;
using NUnit.Framework;
using static Dynamo.Models.DynamoModel;
using Dynamo.Utilities;
using Dynamo.Views;

namespace DynamoCoreWpfTests
{
Expand Down Expand Up @@ -89,6 +93,58 @@ public void HideConnectorFromContextMenuTest()
Assert.AreEqual(connectorViewModel.IsHidden, !initialVisibility);
}



[Test]
public void AreNodeConnectionsInMenu()
{
// Mock a WorkspaceView
var workspaceView = new WorkspaceView();

// Search the associated context menu for the lacing sub-menu
var contextMenu = workspaceView.FindName("NodeConnectionsMenu") as MenuItem;

// Show All Wires, Hide All Wires
Assert.AreEqual(contextMenu.Items.Count, 2);
}

[Test]
public void ShowAllConnectorFromWorkspaceContextMenuTest()
{
Open(@"UI/ConnectorShowHideAllWires.dyn");

var visibleConnectors = this.ViewModel.CurrentSpaceViewModel.Connectors.Where(x => !x.IsHidden);
var hiddenConnectors = this.ViewModel.CurrentSpaceViewModel.Connectors.Where(x => x.IsHidden);

// Default definition values
Assert.AreEqual(3, visibleConnectors.Count());
Assert.AreEqual(1, hiddenConnectors.Count());

SelectAllNodes();

this.ViewModel.CurrentSpaceViewModel.ShowAllWiresCommand.Execute(null);

// Values after Show All Wires is run
Assert.AreEqual(4, visibleConnectors.Count());
Assert.AreEqual(0, hiddenConnectors.Count());

this.ViewModel.CurrentSpaceViewModel.HideAllWiresCommand.Execute(null);

// Values after Hide All Wires is run
Assert.AreEqual(0, visibleConnectors.Count());
Assert.AreEqual(4, hiddenConnectors.Count());
}

/// <summary>
/// Helper method to select all (nodes) in the current Workspace
/// </summary>
/// <param name="nodes"></param>
private void SelectAllNodes()
{
DynamoSelection.Instance.ClearSelection();
foreach (var node in this.Model.CurrentWorkspace.Nodes)
{
DynamoSelection.Instance.Selection.Add(node);
}
}
}
}
Loading