Browse Source

Use CondtionalWeakTable to avoid FocusManager becoming a GC root for all focus scopes.

pull/3331/head
Dariusz Komosinski 7 years ago
parent
commit
24c5519e71
  1. 18
      src/Avalonia.Input/FocusManager.cs

18
src/Avalonia.Input/FocusManager.cs

@ -4,6 +4,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.VisualTree; using Avalonia.VisualTree;
@ -17,8 +18,8 @@ namespace Avalonia.Input
/// <summary> /// <summary>
/// The focus scopes in which the focus is currently defined. /// The focus scopes in which the focus is currently defined.
/// </summary> /// </summary>
private readonly Dictionary<IFocusScope, IInputElement> _focusScopes = private readonly ConditionalWeakTable<IFocusScope, IInputElement> _focusScopes =
new Dictionary<IFocusScope, IInputElement>(); new ConditionalWeakTable<IFocusScope, IInputElement>();
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="FocusManager"/> class. /// Initializes a new instance of the <see cref="FocusManager"/> class.
@ -110,7 +111,18 @@ namespace Avalonia.Input
{ {
Contract.Requires<ArgumentNullException>(scope != null); Contract.Requires<ArgumentNullException>(scope != null);
_focusScopes[scope] = element; if (_focusScopes.TryGetValue(scope, out IInputElement existingElement))
{
if (element != existingElement)
{
_focusScopes.Remove(scope);
_focusScopes.Add(scope, element);
}
}
else
{
_focusScopes.Add(scope, element);
}
if (Scope == scope) if (Scope == scope)
{ {

Loading…
Cancel
Save