Skip to content

Commit

Permalink
Add TryToUseIdAsKey property
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Sep 2, 2023
1 parent 2e784f5 commit aedff99
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion samples/DockXamlSample/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
RequestedThemeVariant="Light">

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

Expand Down
40 changes: 35 additions & 5 deletions src/Dock.Avalonia/Controls/Recycling/ControlRecycling.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using Dock.Model.Core;
Expand All @@ -9,9 +10,25 @@ namespace Dock.Avalonia.Controls.Recycling;
/// <summary>
///
/// </summary>
public class ControlRecycling : IControlRecycling
public class ControlRecycling : AvaloniaObject, IControlRecycling
{
private readonly Dictionary<object, object> _cache = new();
private bool _tryToUseIdAsKey;

/// <summary>
///
/// </summary>
public static readonly DirectProperty<ControlRecycling, bool> TryToUseIdAsKeyProperty =
AvaloniaProperty.RegisterDirect<ControlRecycling, bool>(nameof(TryToUseIdAsKey), o => o.TryToUseIdAsKey, (o, v) => o.TryToUseIdAsKey = v);

/// <summary>
///
/// </summary>
public bool TryToUseIdAsKey
{
get => _tryToUseIdAsKey;
set => SetAndRaise(TryToUseIdAsKeyProperty, ref _tryToUseIdAsKey, value);
}

/// <summary>
///
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Dock.Model/Core/IControlRecycling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ namespace Dock.Model.Core;
/// </summary>
public interface IControlRecycling
{
/// <summary>
///
/// </summary>
public bool TryToUseIdAsKey { get; set; }

/// <summary>
///
/// </summary>
Expand Down

0 comments on commit aedff99

Please sign in to comment.