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

Add control cache support #293

Merged
merged 4 commits into from
Aug 31, 2023
Merged
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
6 changes: 5 additions & 1 deletion src/Dock.Avalonia/Controls/DocumentControl.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@
<ContentControl x:Name="PART_ContentPresenter"
Content="{Binding}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
VerticalAlignment="Stretch">
<ContentControl.ContentTemplate>
<ControlRecyclingDataTemplate Parent="{Binding #PART_ContentPresenter}" />
</ContentControl.ContentTemplate>
</ContentControl>
</DockableControl>
</Border>
</DockPanel>
Expand Down
2 changes: 1 addition & 1 deletion src/Dock.Avalonia/Controls/HostWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public void Present(bool isDialog)
Window.Factory?.OnWindowOpened(Window);
}

ShowDialog(null); // FIXME: Set correct parent window.
ShowDialog(null!); // FIXME: Set correct parent window.
}
}
else
Expand Down
67 changes: 67 additions & 0 deletions src/Dock.Avalonia/Controls/Recycling/ControlRecycling.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using Avalonia.Controls;
using Avalonia.Controls.Templates;

namespace Dock.Avalonia.Controls.Recycling;

/// <summary>
///
/// </summary>
public class ControlRecycling
{
private readonly Dictionary<object, Control> _cache = new();

private bool TryGetValue(object? data, out Control? control)
{
if (data is null)
{
control = null;
return false;
}

return _cache.TryGetValue(data, out control);
}

private void Add(object data, Control control)
{
_cache[data] = control;
}

/// <summary>
///
/// </summary>
/// <param name="data"></param>
/// <param name="existing"></param>
/// <param name="parent"></param>
/// <returns></returns>
public Control? Build(object? data, Control? existing, Control? parent)
{
if (data is null)
{
return null;
}

if (TryGetValue(data, out var control))
{
#if DEBUG
Console.WriteLine($"[Cached] {data}, {control}");
#endif
return control;
}

var dataTemplate = parent?.FindDataTemplate(data, null);

control = dataTemplate?.Build(data);
if (control is null)
{
return null;
}

Add(data, control);
#if DEBUG
Console.WriteLine($"[Added] {data}, {control}");
#endif
return control;
}
}
59 changes: 59 additions & 0 deletions src/Dock.Avalonia/Controls/Recycling/RecylingDataTemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Templates;

namespace Dock.Avalonia.Controls.Recycling;

/// <summary>
///
/// </summary>
public class ControlRecyclingDataTemplate : AvaloniaObject, IRecyclingDataTemplate
{
private static readonly ControlRecycling s_controlRecycling = new();

/// <summary>
///
/// </summary>
public static readonly StyledProperty<Control?> ParentProperty =
AvaloniaProperty.Register<ControlRecyclingDataTemplate, Control?>("Parent");

/// <summary>
///
/// </summary>
public Control? Parent
{
get => GetValue(ParentProperty);
set => SetValue(ParentProperty, value);
}

/// <summary>
///
/// </summary>
/// <param name="param"></param>
/// <returns></returns>
public Control? Build(object? param)
{
return null;
}

/// <summary>
///
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public bool Match(object? data)
{
return true;
}

/// <summary>
///
/// </summary>
/// <param name="data"></param>
/// <param name="existing"></param>
/// <returns></returns>
public Control? Build(object? data, Control? existing)
{
return s_controlRecycling.Build(data, existing, Parent);
}
}
6 changes: 5 additions & 1 deletion src/Dock.Avalonia/Controls/ToolControl.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
<ContentControl x:Name="PART_ContentPresenter"
Content="{Binding}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
VerticalAlignment="Stretch">
<ContentControl.ContentTemplate>
<ControlRecyclingDataTemplate Parent="{Binding #PART_ContentPresenter}" />
</ContentControl.ContentTemplate>
</ContentControl>
</DockableControl>
</Border>
</DockPanel>
Expand Down
1 change: 1 addition & 0 deletions src/Dock.Avalonia/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Dock.Avalonia")]
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Dock.Avalonia.Controls")]
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Dock.Avalonia.Controls.Recycling")]
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Dock.Avalonia.Converters")]
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Dock.Avalonia.MarkupExtension")]
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Dock.Avalonia.Themes")]
13 changes: 12 additions & 1 deletion src/Dock.Model.Avalonia/Controls/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class Document : DockableBase, IDocument, IDocumentContent, ITemplate<Con
public static readonly StyledProperty<object?> ContentProperty =
AvaloniaProperty.Register<Document, object?>(nameof(Content));

private Control? _cached;

/// <summary>
/// Initializes new instance of the <see cref="Document"/> class.
/// </summary>
Expand Down Expand Up @@ -97,7 +99,16 @@ public bool Match(object? data)
/// <returns></returns>
public Control? Build(object? data, Control? existing)
{
return existing ?? TemplateContent.Load(Content)?.Result;
if (_cached is not null)
{
return _cached;
}
var control = TemplateContent.Load(Content)?.Result;
if (control is not null)
{
_cached = control;
}
return control;
}

private static TemplateResult<Control>? Load(object? templateContent)
Expand Down
13 changes: 12 additions & 1 deletion src/Dock.Model.Avalonia/Controls/Tool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class Tool : DockableBase, ITool, IDocument, IToolContent, ITemplate<Cont
public static readonly StyledProperty<object?> ContentProperty =
AvaloniaProperty.Register<Tool, object?>(nameof(Content));

private Control? _cached;

/// <summary>
/// Initializes new instance of the <see cref="Tool"/> class.
/// </summary>
Expand Down Expand Up @@ -99,7 +101,16 @@ public bool Match(object? data)
/// <returns></returns>
public Control? Build(object? data, Control? existing)
{
return existing ?? TemplateContent.Load(Content)?.Result;
if (_cached is not null)
{
return _cached;
}
var control = TemplateContent.Load(Content)?.Result;
if (control is not null)
{
_cached = control;
}
return control;
}

private static TemplateResult<Control>? Load(object? templateContent)
Expand Down
Loading