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

Use Bitmap cache for smoother panning #15811

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions src/DynamoCoreWpf/ViewModels/Core/WorkspaceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,8 @@ public double Zoom
/// <summary>
/// When enabled, some child wpf framework elements will not animate opacity changes.
/// Useful for improving performance during zoom.
/// TODO DYN-8193 a future optimiztion if found to be neccesary is to modify the styles this flag controls
/// to set visibility instead of opacity, this will likely lead to many fewers elements in the visual tree to
/// TODO DYN-8193 a future optimization if found to be necessary is to modify the styles this flag controls
/// to set visibility instead of opacity, this will likely lead to many fewer elements in the visual tree to
/// layout and render.
/// </summary>
[JsonIgnore]
Expand Down
80 changes: 78 additions & 2 deletions src/DynamoCoreWpf/Views/Core/WorkspaceView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Dynamo.Controls;
using Dynamo.Graph;
using Dynamo.Graph.Annotations;
Expand Down Expand Up @@ -149,6 +150,8 @@ private void RemoveViewModelsubscriptions(WorkspaceViewModel ViewModel)
ViewModel.Model.CurrentOffsetChanged -= vm_CurrentOffsetChanged;
DynamoSelection.Instance.Selection.CollectionChanged -= OnSelectionCollectionChanged;
infiniteGridView.DetachFromZoomBorder(zoomBorder);

currentRenderScale = -1;
}

/// <summary>
Expand Down Expand Up @@ -640,9 +643,82 @@ void vm_CurrentOffsetChanged(object sender, EventArgs e)

void vm_ZoomChanged(object sender, EventArgs e)
{
zoomBorder.SetZoom((e as ZoomEventArgs).Zoom);
if (PortContextMenu.IsOpen) DestroyPortContextMenu();
var newZoomScale = (e as ZoomEventArgs).Zoom;
zoomBorder.SetZoom(newZoomScale);
if (PortContextMenu.IsOpen)
{
DestroyPortContextMenu();
}

CheckZoomScaleAndApplyNodeViewCache(newZoomScale);
}

#region NodeView_BitmapCache
private double currentRenderScale = -1;
const double maxZoomScaleForCache = .8;

private void CheckZoomScaleAndApplyNodeViewCache(double newZoomScale)
{
if (!ViewModel.StopNodeViewOpacityAnimations)
{
if (currentRenderScale > 0) // number of nodes reduced below max threshold
{
Dispatcher.BeginInvoke(ClearNodeViewCache, DispatcherPriority.Normal);
}

return;
}

if (newZoomScale > maxZoomScaleForCache)
{
if (currentRenderScale > 0)
{
Dispatcher.BeginInvoke(ClearNodeViewCache, DispatcherPriority.Normal);
}

return;
}

var newRenderScale = newZoomScale <= .2 ? .2 : newZoomScale <= .5 ? .5 : 1;

if (Math.Abs(newRenderScale - currentRenderScale) <= 0.01)
{
return;
}

currentRenderScale = newRenderScale;
Dispatcher.BeginInvoke(UpdateNodeViewCacheScale, DispatcherPriority.Normal);
}

private void ClearNodeViewCache()
{
var nodes = this.ChildrenOfType<NodeView>();
foreach (var node in nodes)
{
node.CacheMode = null;
}

currentRenderScale = -1;
}

private void UpdateNodeViewCacheScale()
{
var nodes = this.ChildrenOfType<NodeView>();
foreach (var node in nodes)
{
var cache = node.CacheMode as BitmapCache;
if (cache == null)
{
cache = new BitmapCache(currentRenderScale);
node.CacheMode = cache;
}
else
{
cache.RenderAtScale = currentRenderScale;
}
}
}
#endregion

void vm_ZoomAtViewportCenter(object sender, EventArgs e)
{
Expand Down
Loading