TreeContainerIndex.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Avalonia.Controls.Generators
  6. {
  7. /// <summary>
  8. /// Maintains an index of all item containers currently materialized by a <see cref="TreeView"/>.
  9. /// </summary>
  10. /// <remarks>
  11. /// Each <see cref="TreeViewItem"/> has its own <see cref="TreeItemContainerGenerator{T}"/>
  12. /// that maintains the list of its direct children, but they also share an instance of this
  13. /// class in their <see cref="TreeItemContainerGenerator{T}.Index"/> property which tracks
  14. /// the containers materialized for the entire tree.
  15. /// </remarks>
  16. public class TreeContainerIndex
  17. {
  18. private readonly Dictionary<object, IControl> _itemToContainer = new Dictionary<object, IControl>();
  19. private readonly Dictionary<IControl, object> _containerToItem = new Dictionary<IControl, object>();
  20. /// <summary>
  21. /// Signalled whenever new containers are materialized.
  22. /// </summary>
  23. public event EventHandler<ItemContainerEventArgs> Materialized;
  24. /// <summary>
  25. /// Event raised whenever containers are dematerialized.
  26. /// </summary>
  27. public event EventHandler<ItemContainerEventArgs> Dematerialized;
  28. /// <summary>
  29. /// Gets the currently materialized containers.
  30. /// </summary>
  31. public IEnumerable<IControl> Items => _containerToItem.Keys;
  32. /// <summary>
  33. /// Adds an entry to the index.
  34. /// </summary>
  35. /// <param name="item">The item.</param>
  36. /// <param name="container">The item container.</param>
  37. public void Add(object item, IControl container)
  38. {
  39. _itemToContainer.Add(item, container);
  40. _containerToItem.Add(container, item);
  41. Materialized?.Invoke(
  42. this,
  43. new ItemContainerEventArgs(0, new ItemContainerInfo(container, item, 0)));
  44. }
  45. /// <summary>
  46. /// Removes a container from the index.
  47. /// </summary>
  48. /// <param name="container">The item container.</param>
  49. public void Remove(IControl container)
  50. {
  51. var item = _containerToItem[container];
  52. _containerToItem.Remove(container);
  53. _itemToContainer.Remove(item);
  54. Dematerialized?.Invoke(
  55. this,
  56. new ItemContainerEventArgs(0, new ItemContainerInfo(container, item, 0)));
  57. }
  58. /// <summary>
  59. /// Removes a set of containers from the index.
  60. /// </summary>
  61. /// <param name="containers">The item containers.</param>
  62. public void Remove(IEnumerable<ItemContainerInfo> containers)
  63. {
  64. foreach (var container in containers)
  65. {
  66. var item = _containerToItem[container.ContainerControl];
  67. _containerToItem.Remove(container.ContainerControl);
  68. _itemToContainer.Remove(item);
  69. }
  70. }
  71. /// <summary>
  72. /// Gets the container for an item.
  73. /// </summary>
  74. /// <param name="item">The item.</param>
  75. /// <returns>The container, or null of not found.</returns>
  76. public IControl ContainerFromItem(object item)
  77. {
  78. IControl result;
  79. _itemToContainer.TryGetValue(item, out result);
  80. return result;
  81. }
  82. /// <summary>
  83. /// Gets the item for a container.
  84. /// </summary>
  85. /// <param name="container">The container.</param>
  86. /// <returns>The item, or null of not found.</returns>
  87. public object ItemFromContainer(IControl container)
  88. {
  89. object result;
  90. _containerToItem.TryGetValue(container, out result);
  91. return result;
  92. }
  93. }
  94. }