Skip to content

Commit

Permalink
refactor into delegate command on nodesearchelementviewmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
aparajit-pratap committed Oct 15, 2020
1 parent 340a8a3 commit e6b9ed3
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ private void ExecuteSearchElement(ListBoxItem listBoxItem)
{
searchElement.Position = ViewModel.InCanvasSearchPosition;
PortViewModel port = ViewModel.PortViewModel;
searchElement.CreateAndConnectCommand.Execute(port.PortModel);
if (searchElement.CreateAndConnectCommand.CanExecute(port.PortModel))
{
searchElement.CreateAndConnectCommand.Execute(port.PortModel);
}
}
}

Expand Down
95 changes: 88 additions & 7 deletions src/DynamoCoreWpf/ViewModels/Search/NodeSearchElementViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
using System.Windows.Input;
using System.Windows.Media;
using Dynamo.Configuration;
using Dynamo.Controls;
using Dynamo.Graph.Nodes;
using Dynamo.Graph.Workspaces;
using Dynamo.Logging;
using Dynamo.Models;
using Dynamo.Search.SearchElements;
using Dynamo.Selection;
using Dynamo.ViewModels;
using FontAwesome.WPF;
using Microsoft.Practices.Prism.Commands;
Expand All @@ -20,6 +24,7 @@ public class NodeSearchElementViewModel : ViewModelBase, ISearchEntryViewModel

private bool isSelected;
private SearchViewModel searchViewModel;
private IDisposable undoRecorderGroup;

public event RequestBitmapSourceHandler RequestBitmapSource;
public void OnRequestBitmapSource(IconRequestEventArgs e)
Expand All @@ -44,10 +49,9 @@ public NodeSearchElementViewModel(NodeSearchElement element, SearchViewModel svm
if (searchViewModel != null)
{
Clicked += searchViewModel.OnSearchElementClicked;
CreateAndConnectToPort += searchViewModel.OnRequestConnectToPort;
}
ClickedCommand = new DelegateCommand(OnClicked);
CreateAndConnectCommand = new DelegateCommand<PortModel>(OnRequestCreateAndConnectToPort);
CreateAndConnectCommand = new DelegateCommand<PortModel>(CreateAndConnectToPort, CanCreateAndConnectToPort);

This comment has been minimized.

Copy link
@QilongTang

QilongTang Oct 15, 2020

Contributor

👍


LoadFonts();
}
Expand All @@ -62,9 +66,15 @@ public override void Dispose()
Model.VisibilityChanged -= ModelOnVisibilityChanged;
if (searchViewModel != null)
{
if (RequestBitmapSource != null)
{
RequestBitmapSource -= searchViewModel.SearchViewModelRequestBitmapSource;
}
Clicked -= searchViewModel.OnSearchElementClicked;
searchViewModel = null;
}


base.Dispose();
}

Expand Down Expand Up @@ -233,14 +243,85 @@ public ImageSource LargeIcon
/// <summary>
/// Create the search element as node and connect to target port
/// </summary>
public ICommand CreateAndConnectCommand { get; private set; }
public ICommand CreateAndConnectCommand { get; }

/// <summary>
/// Create new node for search element, connect to port and place using graph auto layout.
/// </summary>
/// <param name="parameter">Port model to connect to</param>
protected virtual void CreateAndConnectToPort(object parameter)
{
var portModel = (PortModel) parameter;
var dynamoViewModel = searchViewModel.dynamoViewModel;

// Initialize a new undo action group before calling
// node CreateAndConnect and AutoLayout commands.
if (undoRecorderGroup == null)
{
undoRecorderGroup = dynamoViewModel.CurrentSpace.UndoRecorder.BeginActionGroup();

// Node auto layout can be performed correctly only when the positions and sizes
// of nodes are known, which is possible only after the node views are ready.
dynamoViewModel.NodeViewReady += AutoLayoutNodes;
}

var initialNode = portModel.Owner;
var initialNodeVm = dynamoViewModel.CurrentSpaceViewModel.Nodes.FirstOrDefault(x => x.Id == initialNode.GUID);
var id = Guid.NewGuid();

var adjustedX = initialNodeVm.X;

var createAsDownStreamNode = portModel.PortType == PortType.Output;
// Placing the new node based on which input port it is connecting to.
if (createAsDownStreamNode)
{
// Placing the new node to the right of initial node
adjustedX += initialNode.Width + 50;
}
else
{
// Placing the new node to the left of initial node
adjustedX -= initialNode.Width + 50;
}

// Create a new node based on node creation name and connection ports
dynamoViewModel.ExecuteCommand(new DynamoModel.CreateAndConnectNodeCommand(id, initialNode.GUID,
Model.CreationName, 0, portModel.Index, adjustedX, 0, createAsDownStreamNode, false, true));

// Clear current selections and select all input nodes as we need to perform Auto layout on only the input nodes.
DynamoSelection.Instance.ClearSelection();
var inputNodes = initialNode.InputNodes.Values.Where(x => x != null).Select(y => y.Item2);

foreach (var inputNode in inputNodes)
{
DynamoSelection.Instance.Selection.AddUnique(inputNode);
}
}

public event Action<string, PortModel> CreateAndConnectToPort;
protected virtual void OnRequestCreateAndConnectToPort(PortModel portModel)
protected virtual bool CanCreateAndConnectToPort(object parameter)
{
if (CreateAndConnectToPort != null)
// Do not auto connect code block node since default code block node do not have output port
if (Model.CreationName.Contains("Code Block")) return false;

return true;
}

private void AutoLayoutNodes(object sender, EventArgs e)
{
var nodeView = (NodeView) sender;
var dynamoViewModel = nodeView.ViewModel.DynamoViewModel;

dynamoViewModel.CurrentSpace.DoGraphAutoLayout(true);

DynamoSelection.Instance.ClearSelection();

// Close the undo action group once the node is created, connected and placed.
if (undoRecorderGroup != null)
{
CreateAndConnectToPort(Model.CreationName, portModel);
undoRecorderGroup.Dispose();
undoRecorderGroup = null;

dynamoViewModel.NodeViewReady -= AutoLayoutNodes;
}
}

Expand Down
70 changes: 1 addition & 69 deletions src/DynamoCoreWpf/ViewModels/Search/SearchViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public void OnSearchTextChanged(object sender, EventArgs e)
#region Properties/Fields

private readonly IconServices iconServices;
private IDisposable undoRecorderGroup;

/// <summary>
/// Position, where canvas was clicked.
Expand Down Expand Up @@ -350,13 +349,6 @@ public override void Dispose()
Model.EntryUpdated -= UpdateEntry;
Model.EntryRemoved -= RemoveEntry;

if (undoRecorderGroup != null)
{
undoRecorderGroup.Dispose();
undoRecorderGroup = null;

dynamoViewModel.NodeViewReady -= AutoLayoutNodes;
}
base.Dispose();
}

Expand Down Expand Up @@ -783,7 +775,7 @@ private void AddEntryToExistingCategory(NodeCategoryViewModel category,
}
}

protected void SearchViewModelRequestBitmapSource(IconRequestEventArgs e)
internal void SearchViewModelRequestBitmapSource(IconRequestEventArgs e)
{
var warehouse = iconServices.GetForAssembly(e.IconAssembly, e.UseAdditionalResolutionPaths);
ImageSource icon = null;
Expand Down Expand Up @@ -1071,66 +1063,6 @@ public void OnSearchElementClicked(NodeModel nodeModel, Point position)
OnRequestFocusSearch();
}

internal void OnRequestConnectToPort(string nodeCreationName, PortModel portModel)
{
// Do not auto connect code block node since default code block node do not have output port
if (nodeCreationName.Contains("Code Block")) return;

var initialNode = dynamoViewModel.CurrentSpaceViewModel.Nodes.FirstOrDefault(x => x.Id == portModel.Owner.GUID);
var id = Guid.NewGuid();

var adjustedX = initialNode.X;

// Placing the new node based on which input port it is connecting to.
if (portModel.PortType == PortType.Input)
{
// Placing the new node to the left of initial node
adjustedX -= portModel.Owner.Width + 50;
}
else
{
// Placing the new node to the right of initial node
adjustedX += portModel.Owner.Width + 50;
}

// Initialize a new undo action group before calling
// node CreateAndConnect and AutoLayout commands.
if (undoRecorderGroup == null)
{
// Node auto layout can be performed correctly only when the positions and sizes
// of nodes are known, which is possible only after the node views are ready.
dynamoViewModel.NodeViewReady += AutoLayoutNodes;
undoRecorderGroup = dynamoViewModel.CurrentSpace.UndoRecorder.BeginActionGroup();
}

// Create a new node based on node creation name and connection ports
dynamoViewModel.ExecuteCommand(new DynamoModel.CreateAndConnectNodeCommand(id, portModel.Owner.GUID,
nodeCreationName, 0, portModel.Index, adjustedX, 0, false, false, true));

// Clear current selections and select all input nodes as we need to perform Auto layout on only the input nodes.
DynamoSelection.Instance.ClearSelection();
var inputNodes = portModel.Owner.InputNodes.Values.Where(x => x != null).Select(y => y.Item2);

foreach (var inputNode in inputNodes)
{
DynamoSelection.Instance.Selection.AddUnique(inputNode);
}
}

private void AutoLayoutNodes(object sender, EventArgs e)
{
dynamoViewModel.CurrentSpace.DoGraphAutoLayout(true);

// Close the undo action group once the node is created, connected and placed.
if (undoRecorderGroup != null)
{
undoRecorderGroup.Dispose();
undoRecorderGroup = null;

dynamoViewModel.NodeViewReady -= AutoLayoutNodes;
}
}

#endregion

#region Commands
Expand Down

0 comments on commit e6b9ed3

Please sign in to comment.