Skip to content
Closed
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
95 changes: 78 additions & 17 deletions src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using Dynamo.Graph.Connectors;
using Dynamo.Graph.Nodes;
using Dynamo.Graph.Nodes.CustomNodes;
using Dynamo.Graph.Notes;
using Dynamo.Graph.Workspaces;
using Dynamo.Interfaces;
using Dynamo.Logging;
Expand Down Expand Up @@ -4521,6 +4522,21 @@
}

#region "Home And End key press events in Canvas"

private readonly struct HomeEndCandidate
{
public HomeEndCandidate(double left, double right, IReadOnlyList<ISelectable> models)
{
Left = left;
Right = right;
Models = models;
}

public double Left { get; }
public double Right { get; }
public IReadOnlyList<ISelectable> Models { get; }
}

/// <summary>
/// Enable the shortcut key events when there is no selection.
/// </summary>
Expand All @@ -4530,36 +4546,81 @@
return BackgroundPreviewViewModel != null &&
!CurrentSpaceViewModel.HasSelection;
}
private void FitCanvasToSelectedNodes(List<NodeViewModel> nodes)

private IEnumerable<HomeEndCandidate> BuildHomeEndCandidates()
{
var nodeSet = new HashSet<NodeModel>(nodes.Select(nvm => nvm.NodeModel));
var groups = CurrentSpaceViewModel.Annotations.Where(a => a.Nodes.Any(n => nodeSet.Contains(n)));
nodes.ForEach((ele) => DynamoSelection.Instance.Selection.Add(ele.NodeModel));
groups.ToList().ForEach((grp) => DynamoSelection.Instance.Selection.Add(grp.AnnotationModel));
var annotations = CurrentSpaceViewModel.Annotations;

foreach (var nvm in CurrentSpaceViewModel.Nodes)
{
var node = nvm.NodeModel;
var models = new List<ISelectable> { node };
foreach (var avm in annotations)

Check warning on line 4558 in src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Loops should be simplified using the "Where" LINQ method

See more on https://sonarcloud.io/project/issues?id=DynamoDS_Dynamo&issues=AZ6MOg1yIP87Uc_zamYV&open=AZ6MOg1yIP87Uc_zamYV&pullRequest=17136
{
if (avm.AnnotationModel.ContainsModel(node))
{
models.Add(avm.AnnotationModel);
}
}
Comment on lines +4558 to +4564
yield return new HomeEndCandidate(nvm.X, nvm.X + nvm.ActualWidth, models);
}

foreach (var noteVM in CurrentSpaceViewModel.Notes)
{
var noteModel = noteVM.Model;
if (annotations.Any(a => a.AnnotationModel.ContainsModel(noteModel)))
{
continue;
}
yield return new HomeEndCandidate(noteVM.Left, noteVM.Left + noteModel.Width, new ISelectable[] { noteModel });
}

foreach (var avm in annotations)
{
var hasNodes = avm.Nodes.OfType<NodeModel>().Any();
var hasNotes = avm.Nodes.OfType<NoteModel>().Any();
if (hasNodes || !hasNotes)
{
continue;
}
yield return new HomeEndCandidate(avm.Left, avm.Left + avm.Width, new ISelectable[] { avm.AnnotationModel });
}
}

private void FitCanvasToSelectedCandidates(IEnumerable<HomeEndCandidate> candidates)
{
foreach (var candidate in candidates)
{
foreach (var model in candidate.Models)

Check warning on line 4594 in src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename 'model' which hides the field with the same name.

See more on https://sonarcloud.io/project/issues?id=DynamoDS_Dynamo&issues=AZ6MOg1yIP87Uc_zamYU&open=AZ6MOg1yIP87Uc_zamYU&pullRequest=17136
{
DynamoSelection.Instance.Selection.Add(model);
}
}
FitViewCommand.Execute(true);
DynamoSelection.Instance.ClearSelection();
}

internal void GoToLeftMostNode(object parameter)
{
if (CurrentSpaceViewModel.Nodes.Count > 0)
{
double minX = CurrentSpaceViewModel.Nodes.Min(x => x.X);
var nodes = CurrentSpaceViewModel.Nodes.Where(x => x.X <= minX + tolerance).ToList();
FitCanvasToSelectedNodes(nodes);
}
var candidates = BuildHomeEndCandidates().ToList();
if (candidates.Count == 0) return;

double minX = candidates.Min(c => c.Left);
var winners = candidates.Where(c => c.Left <= minX + tolerance);
FitCanvasToSelectedCandidates(winners);
}
internal bool CanGoToLeftMostNode(object obj)
{
return IsHomeAndEndKeyEnabled();
}
internal void GoToRightMostNode(object parameter)
{
if (CurrentSpaceViewModel.Nodes.Count > 0)
{
double maxX = CurrentSpaceViewModel.Nodes.Max(x => x.X + x.ActualWidth);
var nodes = CurrentSpaceViewModel.Nodes.Where(x => x.X + x.ActualWidth >= maxX - tolerance).ToList();
FitCanvasToSelectedNodes(nodes);
}
var candidates = BuildHomeEndCandidates().ToList();
if (candidates.Count == 0) return;

double maxRight = candidates.Max(c => c.Right);
var winners = candidates.Where(c => c.Right >= maxRight - tolerance);
FitCanvasToSelectedCandidates(winners);
}
internal bool CanGoToRightMostNode(object obj)
{
Expand Down
Loading
Loading