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

Refactor recycling cache #294

Merged
merged 3 commits into from
Sep 1, 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
7 changes: 7 additions & 0 deletions Dock.sln
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "github", "github", "{1F5D6B
.github\workflows\build.yml = .github\workflows\build.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dock.Settings", "src\Dock.Settings\Dock.Settings.csproj", "{6ACDD1B2-CC63-4F4D-8E99-3F8948CA2A6E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -144,6 +146,10 @@ Global
{3606F724-6EF1-4929-9183-A1F8271D522A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3606F724-6EF1-4929-9183-A1F8271D522A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3606F724-6EF1-4929-9183-A1F8271D522A}.Release|Any CPU.Build.0 = Release|Any CPU
{6ACDD1B2-CC63-4F4D-8E99-3F8948CA2A6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6ACDD1B2-CC63-4F4D-8E99-3F8948CA2A6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6ACDD1B2-CC63-4F4D-8E99-3F8948CA2A6E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6ACDD1B2-CC63-4F4D-8E99-3F8948CA2A6E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -166,6 +172,7 @@ Global
{55F9EF68-6A62-4D70-8A20-D82D36D3598B} = {FC61A082-2335-4C45-A38C-1EBEEA7BBE0A}
{3606F724-6EF1-4929-9183-A1F8271D522A} = {71FD12F4-0BCB-422A-9FB0-4137E898E991}
{1F5D6BD4-CED0-409F-89D8-096F5F457A6D} = {C4E2763D-8CA1-4332-808F-E32E9F4DDA94}
{6ACDD1B2-CC63-4F4D-8E99-3F8948CA2A6E} = {FC61A082-2335-4C45-A38C-1EBEEA7BBE0A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5617659E-FC7E-40D7-9D62-258513329CA1}
Expand Down
10 changes: 10 additions & 0 deletions samples/DockMvvmSample/App.axaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DockMvvmSample"
xmlns:ids="using:Dock.Settings"
xmlns:idc="using:Dock.Avalonia.Controls"
xmlns:idcr="using:Dock.Avalonia.Controls.Recycling"
xmlns:core="using:Dock.Model.Core"
Name="Dock Avalonia Demo"
x:Class="DockMvvmSample.App">
Expand All @@ -10,8 +12,16 @@
<local:ViewLocator />
</Application.DataTemplates>

<Application.Resources>
<idcr:ControlRecycling x:Key="ControlRecyclingKey" />
</Application.Resources>

<Application.Styles>

<Style Selector="idc|DockControl">
<Setter Property="(ids:DockProperties.ControlRecycling)" Value="{StaticResource ControlRecyclingKey}" />
</Style>

<Style Selector="idc|DocumentControl">
<Setter Property="HeaderTemplate">
<DataTemplate DataType="core:IDockable">
Expand Down
6 changes: 3 additions & 3 deletions samples/DockMvvmSample/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:id="clr-namespace:Dock.Avalonia;assembly=Dock.Avalonia"
xmlns:ids="using:Dock.Settings"
xmlns:dm="using:Dock.Model.Core"
xmlns:dmc="using:Dock.Model.Controls"
xmlns:vm="using:DockMvvmSample.ViewModels"
Expand All @@ -30,15 +30,15 @@
<MenuItem Header="_Options">
<MenuItem x:Name="OptionsIsDragEnabled" Header="Enable Drag">
<MenuItem.Icon>
<CheckBox IsChecked="{Binding $parent[Window].(id:DockProperties.IsDragEnabled)}"
<CheckBox IsChecked="{Binding $parent[Window].(ids:DockProperties.IsDragEnabled)}"
BorderThickness="0"
IsHitTestVisible="False" />
</MenuItem.Icon>
</MenuItem>
<Separator />
<MenuItem x:Name="OptionsIsDropEnabled" Header="Enable Drop">
<MenuItem.Icon>
<CheckBox IsChecked="{Binding $parent[Window].(id:DockProperties.IsDropEnabled)}"
<CheckBox IsChecked="{Binding $parent[Window].(ids:DockProperties.IsDropEnabled)}"
BorderThickness="0"
IsHitTestVisible="False" />
</MenuItem.Icon>
Expand Down
1 change: 1 addition & 0 deletions samples/DockMvvmSample/Views/MainView.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Dock.Avalonia;
using Dock.Settings;

namespace DockMvvmSample.Views;

Expand Down
4 changes: 2 additions & 2 deletions samples/DockMvvmSample/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:id="using:Dock.Avalonia"
xmlns:ids="using:Dock.Settings"
xmlns:vm="using:DockMvvmSample.ViewModels"
xmlns:views="using:DockMvvmSample.Views"
mc:Ignorable="d"
Expand All @@ -19,7 +19,7 @@
Title="Dock Avalonia Demo" Height="680" Width="1200"
ExtendClientAreaToDecorationsHint="True"
ExtendClientAreaChromeHints="PreferSystemChrome"
id:DockProperties.IsDragEnabled="True" id:DockProperties.IsDropEnabled="True">
ids:DockProperties.IsDragEnabled="True" ids:DockProperties.IsDropEnabled="True">
<Design.DataContext>
<vm:MainWindowViewModel />
</Design.DataContext>
Expand Down
20 changes: 16 additions & 4 deletions samples/DockXamlSample/App.axaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="DockXamlSample.App"
xmlns:ids="using:Dock.Settings"
xmlns:idc="using:Dock.Avalonia.Controls"
xmlns:idcr="using:Dock.Avalonia.Controls.Recycling"
Name="Dock Avalonia Demo"
RequestedThemeVariant="Light">

<Application.Resources>
<idcr:ControlRecycling x:Key="ControlRecyclingKey" />
<Color x:Key="RegionColor">Transparent</Color>
</Application.Resources>

<Application.Styles>

<FluentTheme />

<StyleInclude Source="avares://Dock.Avalonia/Themes/DockFluentTheme.axaml" />

<Style Selector="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>

<Style Selector="idc|DockControl">
<Setter Property="(ids:DockProperties.ControlRecycling)" Value="{StaticResource ControlRecyclingKey}" />
</Style>

</Application.Styles>

<Application.Resources>
<Color x:Key="RegionColor">Transparent</Color>
</Application.Resources>

</Application>
17 changes: 17 additions & 0 deletions samples/Notepad/App.axaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Notepad"
xmlns:ids="using:Dock.Settings"
xmlns:idc="using:Dock.Avalonia.Controls"
xmlns:idcr="using:Dock.Avalonia.Controls.Recycling"
x:Class="Notepad.App"
Name="Notepad"
RequestedThemeVariant="Light">

<Application.DataTemplates>
<local:ViewLocator />
</Application.DataTemplates>

<Application.Resources>
<idcr:ControlRecycling x:Key="ControlRecyclingKey" />
</Application.Resources>

<Application.Styles>

<FluentTheme />

<StyleInclude Source="avares://Dock.Avalonia/Themes/DockFluentTheme.axaml" />

<Style Selector="idc|DockControl">
<Setter Property="(ids:DockProperties.ControlRecycling)" Value="{StaticResource ControlRecyclingKey}" />
</Style>

</Application.Styles>

</Application>
4 changes: 2 additions & 2 deletions samples/Notepad/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:id="using:Dock.Avalonia"
xmlns:ids="using:Dock.Settings"
xmlns:vm="using:Notepad.ViewModels"
xmlns:views="using:Notepad.Views"
mc:Ignorable="d"
Expand All @@ -17,7 +17,7 @@
Title="Notepad" Height="600" Width="900"
ExtendClientAreaToDecorationsHint="True"
ExtendClientAreaChromeHints="PreferSystemChrome"
id:DockProperties.IsDragEnabled="True" id:DockProperties.IsDropEnabled="True">
ids:DockProperties.IsDragEnabled="True" ids:DockProperties.IsDropEnabled="True">
<Design.DataContext>
<vm:MainWindowViewModel />
</Design.DataContext>
Expand Down
2 changes: 2 additions & 0 deletions src/Dock.Avalonia/Controls/DockControl.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Metadata;
Expand Down
32 changes: 26 additions & 6 deletions src/Dock.Avalonia/Controls/Recycling/ControlRecycling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@
using System.Collections.Generic;
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using Dock.Model.Core;

namespace Dock.Avalonia.Controls.Recycling;

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

private bool TryGetValue(object? data, out Control? control)
/// <summary>
///
/// </summary>
/// <param name="data"></param>
/// <param name="control"></param>
/// <returns></returns>
public bool TryGetValue(object? data, out object? control)
{
if (data is null)
{
Expand All @@ -23,7 +30,12 @@ private bool TryGetValue(object? data, out Control? control)
return _cache.TryGetValue(data, out control);
}

private void Add(object data, Control control)
/// <summary>
///
/// </summary>
/// <param name="data"></param>
/// <param name="control"></param>
public void Add(object data, object control)
{
_cache[data] = control;
}
Expand All @@ -35,7 +47,7 @@ private void Add(object data, Control control)
/// <param name="existing"></param>
/// <param name="parent"></param>
/// <returns></returns>
public Control? Build(object? data, Control? existing, Control? parent)
public object? Build(object? data, object? existing, object? parent)
{
if (data is null)
{
Expand All @@ -50,7 +62,7 @@ private void Add(object data, Control control)
return control;
}

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

control = dataTemplate?.Build(data);
if (control is null)
Expand All @@ -64,4 +76,12 @@ private void Add(object data, Control control)
#endif
return control;
}

/// <summary>
///
/// </summary>
public void Clear()
{
_cache.Clear();
}
}
19 changes: 15 additions & 4 deletions src/Dock.Avalonia/Controls/Recycling/RecylingDataTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using Dock.Model.Core;
using Dock.Settings;

namespace Dock.Avalonia.Controls.Recycling;

Expand All @@ -9,13 +11,11 @@ namespace Dock.Avalonia.Controls.Recycling;
/// </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");
AvaloniaProperty.Register<ControlRecyclingDataTemplate, Control?>(nameof(Parent));

/// <summary>
///
Expand Down Expand Up @@ -54,6 +54,17 @@ public bool Match(object? data)
/// <returns></returns>
public Control? Build(object? data, Control? existing)
{
return s_controlRecycling.Build(data, existing, Parent);
if (Parent is not { } parent)
{
return null;
}

var controlRecycling = DockProperties.GetControlRecycling(parent);
if (controlRecycling is not null)
{
return controlRecycling.Build(data, existing, parent) as Control;
}

return parent.FindDataTemplate(data)?.Build(data);
}
}
1 change: 1 addition & 0 deletions src/Dock.Avalonia/Dock.Avalonia.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

<ItemGroup>
<ProjectReference Include="..\Dock.Model\Dock.Model.csproj" />
<ProjectReference Include="..\Dock.Settings\Dock.Settings.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions src/Dock.Avalonia/Internal/DockControlState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Avalonia.VisualTree;
using Dock.Avalonia.Controls;
using Dock.Model.Core;
using Dock.Settings;

namespace Dock.Avalonia.Internal;

Expand Down
1 change: 1 addition & 0 deletions src/Dock.Avalonia/Internal/HostWindowState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Avalonia.VisualTree;
using Dock.Avalonia.Controls;
using Dock.Model.Core;
using Dock.Settings;

namespace Dock.Avalonia.Internal;

Expand Down
25 changes: 2 additions & 23 deletions src/Dock.Model.Avalonia/Controls/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using Avalonia.Markup.Xaml.Templates;
using Avalonia.Metadata;
using Avalonia.Styling;
using Dock.Model.Avalonia.Core;
Expand All @@ -24,8 +23,6 @@ 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 @@ -60,7 +57,7 @@ public object? Content
/// <returns></returns>
public Control? Build()
{
return Load(Content)?.Result;
return TemplateHelper.Load(Content)?.Result;
}

/// <summary>
Expand Down Expand Up @@ -99,24 +96,6 @@ public bool Match(object? data)
/// <returns></returns>
public Control? Build(object? data, Control? existing)
{
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)
{
if (templateContent is Func<IServiceProvider, object> direct)
{
return (TemplateResult<Control>)direct(null!);
}
throw new ArgumentException(nameof(templateContent));
return TemplateHelper.Build(Content, this);
}
}
Loading
Loading