Browse Source
In doing so fixed a bug in DevTools where tree items would become unselectable.pull/386/head
7 changed files with 115 additions and 113 deletions
@ -0,0 +1,87 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System.Collections.Generic; |
|||
|
|||
namespace Perspex.Controls.Generators |
|||
{ |
|||
/// <summary>
|
|||
/// Maintains an index of all item containers currently materialized by a <see cref="TreeView"/>.
|
|||
/// </summary>
|
|||
/// <remarks>
|
|||
/// Each <see cref="TreeViewItem"/> has its own <see cref="TreeItemContainerGenerator{T}"/>
|
|||
/// that maintains the list of its direct children, but they also share an instance of this
|
|||
/// class in their <see cref="TreeItemContainerGenerator{T}.Index"/> property which tracks
|
|||
/// the containers materialized for the entire tree.
|
|||
/// </remarks>
|
|||
public class TreeContainerIndex |
|||
{ |
|||
private readonly Dictionary<object, IControl> _itemToContainer = new Dictionary<object, IControl>(); |
|||
private readonly Dictionary<IControl, object> _containerToItem = new Dictionary<IControl, object>(); |
|||
|
|||
/// <summary>
|
|||
/// Gets the currently materialized containers.
|
|||
/// </summary>
|
|||
public IEnumerable<IControl> Items => _containerToItem.Keys; |
|||
|
|||
/// <summary>
|
|||
/// Adds an entry to the index.
|
|||
/// </summary>
|
|||
/// <param name="item">The item.</param>
|
|||
/// <param name="container">The item container.</param>
|
|||
public void Add(object item, IControl container) |
|||
{ |
|||
_itemToContainer.Add(item, container); |
|||
_containerToItem.Add(container, item); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Removes a container from the index.
|
|||
/// </summary>
|
|||
/// <param name="container">The item container.</param>
|
|||
public void Remove(IControl container) |
|||
{ |
|||
var item = _containerToItem[container]; |
|||
_containerToItem.Remove(container); |
|||
_itemToContainer.Remove(item); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Removes a set of containers from the index.
|
|||
/// </summary>
|
|||
/// <param name="containers">The item containers.</param>
|
|||
public void Remove(IEnumerable<ItemContainer> containers) |
|||
{ |
|||
foreach (var container in containers) |
|||
{ |
|||
var item = _containerToItem[container.ContainerControl]; |
|||
_containerToItem.Remove(container.ContainerControl); |
|||
_itemToContainer.Remove(item); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the container for an item.
|
|||
/// </summary>
|
|||
/// <param name="item">The item.</param>
|
|||
/// <returns>The container, or null of not found.</returns>
|
|||
public IControl ContainerFromItem(object item) |
|||
{ |
|||
IControl result; |
|||
_itemToContainer.TryGetValue(item, out result); |
|||
return result; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the item for a container.
|
|||
/// </summary>
|
|||
/// <param name="container">The container.</param>
|
|||
/// <returns>The item, or null of not found.</returns>
|
|||
public object ItemFromContainer(IControl container) |
|||
{ |
|||
object result; |
|||
_containerToItem.TryGetValue(container, out result); |
|||
return result; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue