From aedff994b0cb0f7a903ad4bdbea800cf9351524f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20=C5=A0olt=C3=A9s?= Date: Sat, 2 Sep 2023 14:43:44 +0200 Subject: [PATCH] Add TryToUseIdAsKey property --- samples/DockXamlSample/App.axaml | 2 +- .../Controls/Recycling/ControlRecycling.cs | 40 ++++++++++++++++--- src/Dock.Model/Core/IControlRecycling.cs | 5 +++ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/samples/DockXamlSample/App.axaml b/samples/DockXamlSample/App.axaml index de5d35096..37ead93d5 100644 --- a/samples/DockXamlSample/App.axaml +++ b/samples/DockXamlSample/App.axaml @@ -8,7 +8,7 @@ RequestedThemeVariant="Light"> - + Transparent diff --git a/src/Dock.Avalonia/Controls/Recycling/ControlRecycling.cs b/src/Dock.Avalonia/Controls/Recycling/ControlRecycling.cs index 1dbc39237..0e568c656 100644 --- a/src/Dock.Avalonia/Controls/Recycling/ControlRecycling.cs +++ b/src/Dock.Avalonia/Controls/Recycling/ControlRecycling.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Templates; using Dock.Model.Core; @@ -9,9 +10,25 @@ namespace Dock.Avalonia.Controls.Recycling; /// /// /// -public class ControlRecycling : IControlRecycling +public class ControlRecycling : AvaloniaObject, IControlRecycling { private readonly Dictionary _cache = new(); + private bool _tryToUseIdAsKey; + + /// + /// + /// + public static readonly DirectProperty TryToUseIdAsKeyProperty = + AvaloniaProperty.RegisterDirect(nameof(TryToUseIdAsKey), o => o.TryToUseIdAsKey, (o, v) => o.TryToUseIdAsKey = v); + + /// + /// + /// + public bool TryToUseIdAsKey + { + get => _tryToUseIdAsKey; + set => SetAndRaise(TryToUseIdAsKeyProperty, ref _tryToUseIdAsKey, value); + } /// /// @@ -54,10 +71,23 @@ public void Add(object data, object control) return null; } - if (TryGetValue(data, out var control)) + var key = data; + + if (TryToUseIdAsKey && data is IDockable dockable) + { +#if DEBUG + Console.WriteLine($"Build: {data}, Id='{dockable.Id}'"); +#endif + if (!string.IsNullOrWhiteSpace(dockable.Id)) + { + key = dockable.Id; + } + } + + if (TryGetValue(key, out var control)) { #if DEBUG - Console.WriteLine($"[Cached] {data}, {control}"); + Console.WriteLine($"[Cached] {key}, {control}"); #endif return control; } @@ -70,9 +100,9 @@ public void Add(object data, object control) return null; } - Add(data, control); + Add(key, control); #if DEBUG - Console.WriteLine($"[Added] {data}, {control}"); + Console.WriteLine($"[Added] {key}, {control}"); #endif return control; } diff --git a/src/Dock.Model/Core/IControlRecycling.cs b/src/Dock.Model/Core/IControlRecycling.cs index d86ee82af..a007a933e 100644 --- a/src/Dock.Model/Core/IControlRecycling.cs +++ b/src/Dock.Model/Core/IControlRecycling.cs @@ -5,6 +5,11 @@ namespace Dock.Model.Core; /// public interface IControlRecycling { + /// + /// + /// + public bool TryToUseIdAsKey { get; set; } + /// /// ///