Skip to content
Open
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
23 changes: 21 additions & 2 deletions src/DynamoCoreWpf/Commands/NoteCommands.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Dynamo.UI.Commands;
using Dynamo.Controls;
using Dynamo.UI.Commands;
using Dynamo.Utilities;
using Newtonsoft.Json;

namespace Dynamo.ViewModels
{
public partial class NoteViewModel
public partial class NoteViewModel: IWorkspaceElement
{
private DelegateCommand _selectCommand;
[JsonIgnore]
Expand Down Expand Up @@ -93,5 +95,22 @@ public DelegateCommand UnpinFromNodeCommand
return unpinFromNodeCommand;
}
}

/// <summary>
/// This signifies if the note should be rendered
/// </summary>
[JsonIgnore]
public bool IsVisibleInCanvas
{
get => isVisibleInCanvas;
set
{
isVisibleInCanvas = value;
RaisePropertyChanged(nameof(isVisibleInCanvas));
}
}
private bool isVisibleInCanvas = false;

public Rect2D Rect => Model.Rect;
}
}
132 changes: 132 additions & 0 deletions src/DynamoCoreWpf/Controls/DeferredContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using Dynamo.ViewModels;

namespace Dynamo.Controls
{
/// <summary>
/// https://stackoverflow.com/a/26543731
/// </summary>
public class DeferredContent : ContentPresenter
{
public static void Focus(double x, double y) => Priority.Focus(x, y);
private static DeferredContentRenderPriority Priority = new();

public DataTemplate DeferredContentTemplate
{
get => (DataTemplate)GetValue(DeferredContentTemplateProperty);
set => SetValue(DeferredContentTemplateProperty, value);
}

public static readonly DependencyProperty DeferredContentTemplateProperty =
DependencyProperty.Register(nameof(DeferredContentTemplate),
typeof(DataTemplate), typeof(DeferredContent), null);

public DeferredContent()
{
IsVisibleChanged += DeferredContent_IsVisibleChanged;
}

private void DeferredContent_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (true.Equals(e.NewValue))
{
IsVisibleChanged -= DeferredContent_IsVisibleChanged;
if (DataContext == null)
{
DataContextChanged += DeferredContent_DataContextChanged;
return;
}
//Dispatcher.BeginInvoke(ShowDeferredContent, DispatcherPriority.Background);
Priority.Enqueue(this);
}
}

private void DeferredContent_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
DataContextChanged -= DeferredContent_DataContextChanged;
// new node added,jump the queue
Dispatcher.BeginInvoke(ShowDeferredContent, DispatcherPriority.Render);
}

public void ShowDeferredContent()
{
if (DeferredContentTemplate != null)
{
base.Content = DeferredContentTemplate.LoadContent();
RaiseDeferredContentLoaded();
}
}

private void RaiseDeferredContentLoaded()
{
DeferredContentLoaded?.Invoke(this, new RoutedEventArgs());
}

public event EventHandler<RoutedEventArgs> DeferredContentLoaded;

private class DeferredContentRenderPriority
{
private bool isRunning = false;
private Point canvasCenter = default;
private readonly List<(DeferredContent, DispatcherOperation)> scheduled = [];
private readonly PriorityQueue<DeferredContent, double> pending =
new PriorityQueue<DeferredContent, double>();

public void Enqueue(DeferredContent control)
{
if (control.DataContext is not NodeViewModel nvm) return;

var dist = (canvasCenter - new Point(nvm.X, nvm.Y)).Length;
pending.Enqueue(control, dist);

if (!isRunning)
{
isRunning = true;
control.Dispatcher.BeginInvoke(SchedulePending, DispatcherPriority.ContextIdle);
}
}

public void Focus(double x, double y)
{
isRunning = false;
canvasCenter = new Point(x, y);

var toReschedule = new List<DeferredContent>();
if (pending.Count > 0)
{
toReschedule.AddRange(pending.UnorderedItems.Select(i => i.Element));
pending.Clear();
}

foreach (var (control, task) in scheduled)
{
if (task.Status != DispatcherOperationStatus.Completed && task.Abort())
{
toReschedule.Add(control);
}
}
scheduled.Clear();

foreach (var control in toReschedule)
{
Enqueue(control);
}
}

public void SchedulePending()
{
while (isRunning && pending.TryDequeue(out var control, out _))
{
scheduled.Add((control, control.Dispatcher.BeginInvoke(control.ShowDeferredContent, DispatcherPriority.Background)));
}

isRunning = false;
}
}
}
}
10 changes: 10 additions & 0 deletions src/DynamoCoreWpf/Controls/IWorkspaceElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Dynamo.Utilities;

namespace Dynamo.Controls
{
internal interface IWorkspaceElement
{
bool IsVisibleInCanvas { get; set; }
Rect2D Rect { get; }
}
}
2 changes: 2 additions & 0 deletions src/DynamoCoreWpf/DynamoCoreWpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
<DependentUpon>InstalledPackagesControl.xaml</DependentUpon>
</Compile>
<Compile Include="Commands\ConnectorPinCommands.cs" />
<Compile Include="Controls\IWorkspaceElement.cs" />
<Compile Include="Controls\NodeAutoCompleteSearchControl.xaml.cs">
<DependentUpon>NodeAutoCompleteSearchControl.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -456,6 +457,7 @@
<DependentUpon>AboutWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Views\CodeBlocks\CodeBlockEditor.cs" />
<Compile Include="Controls\DeferredContent.cs" />
<Compile Include="Views\Core\ConnectorAnchorView.xaml.cs">
<DependentUpon>ConnectorAnchorView.xaml</DependentUpon>
</Compile>
Expand Down
27 changes: 23 additions & 4 deletions src/DynamoCoreWpf/UI/Themes/Modern/DataTemplates.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModels="clr-namespace:Dynamo.ViewModels;assembly=DynamoCoreWpf"
xmlns:controls="clr-namespace:Dynamo.Controls;assembly=DynamoCoreWpf"
Expand All @@ -10,9 +10,28 @@
<ResourceDictionary.MergedDictionaries>
<ui:SharedResourceDictionary Source="{x:Static ui:SharedDictionaryManager.DynamoConvertersDictionaryUri}" />
</ResourceDictionary.MergedDictionaries>

<DataTemplate DataType="{x:Type viewModels:NodeViewModel}">
<controls:NodeView></controls:NodeView>
<controls:DeferredContent>
<controls:DeferredContent.Content>
<Border
Opacity="0.7"
CornerRadius="10 10 0 0"
Background="#3C3C3C"
Width="150"
Height="100">
<TextBlock
Text="loading..."
Foreground="White"
VerticalAlignment="Center"
HorizontalAlignment="Center" />
</Border>
</controls:DeferredContent.Content>
<controls:DeferredContent.DeferredContentTemplate>
<DataTemplate>
<controls:NodeView />
</DataTemplate>
</controls:DeferredContent.DeferredContentTemplate>
</controls:DeferredContent>
</DataTemplate>

<DataTemplate DataType="{x:Type viewModels:NoteViewModel}">
Expand All @@ -27,4 +46,4 @@
<nodes:AnnotationView></nodes:AnnotationView>
</DataTemplate>

</ResourceDictionary>
</ResourceDictionary>
21 changes: 20 additions & 1 deletion src/DynamoCoreWpf/ViewModels/Core/AnnotationViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows.Input;
using System.Windows.Media;
using Dynamo.Configuration;
using Dynamo.Controls;
using Dynamo.Graph;
using Dynamo.Graph.Annotations;
using Dynamo.Graph.Nodes;
Expand All @@ -20,7 +21,7 @@

namespace Dynamo.ViewModels
{
public class AnnotationViewModel : ViewModelBase
public class AnnotationViewModel : ViewModelBase, IWorkspaceElement
{
private AnnotationModel annotationModel;
private IEnumerable<PortModel> originalInPorts;
Expand Down Expand Up @@ -371,6 +372,24 @@ public ObservableCollection<Configuration.StyleItem> GroupStyleList
RaisePropertyChanged(nameof(GroupStyleList));
}
}

/// <summary>
/// This signifies if the node should be rendered
/// </summary>
[JsonIgnore]
public bool IsVisibleInCanvas
{
get => isVisibleInCanvas;
set
{
isVisibleInCanvas = value;
RaisePropertyChanged(nameof(isVisibleInCanvas));
}
}
private bool isVisibleInCanvas = false;

public Rect2D Rect => AnnotationModel.Rect;

#endregion

#region Commands
Expand Down
21 changes: 20 additions & 1 deletion src/DynamoCoreWpf/ViewModels/Core/NodeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using Dynamo.Configuration;
using Dynamo.Controls;
using Dynamo.Engine.CodeGeneration;
using Dynamo.Graph;
using Dynamo.Graph.Nodes;
Expand All @@ -20,6 +21,7 @@
using Dynamo.Logging;
using Dynamo.Models;
using Dynamo.Selection;
using Dynamo.Utilities;
using Dynamo.Wpf.ViewModels.Core;
using Newtonsoft.Json;
using Point = System.Windows.Point;
Expand All @@ -31,7 +33,7 @@ namespace Dynamo.ViewModels
/// Interaction logic for dynControl.xaml
/// </summary>

public partial class NodeViewModel : ViewModelBase
public partial class NodeViewModel : ViewModelBase, IWorkspaceElement
{
#region delegates
public delegate void SetToolTipDelegate(string message);
Expand Down Expand Up @@ -573,6 +575,23 @@ public bool NodeHoveringState
}
}

/// <summary>
/// This signifies if the node should be rendered
/// </summary>
[JsonIgnore]
public bool IsVisibleInCanvas
{
get => isVisibleInCanvas;
set
{
isVisibleInCanvas = value;
RaisePropertyChanged(nameof(isVisibleInCanvas));
}
}
private bool isVisibleInCanvas = false;

public Rect2D Rect => NodeModel.Rect;

private bool isNodeNewlyAdded;
private ImageSource imageSource;
private string imgGlyphOneSource;
Expand Down
Loading
Loading