PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Avalonia.Controls/Generators/TreeContainerIndex.cs

https://gitlab.com/kush/Avalonia
C# | 112 lines | 55 code | 13 blank | 44 comment | 0 complexity | 2baf4b82b1621851e2cf5e651fd1e1ff MD5 | raw file
  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. using System.Linq;
  6. namespace Avalonia.Controls.Generators
  7. {
  8. /// <summary>
  9. /// Maintains an index of all item containers currently materialized by a <see cref="TreeView"/>.
  10. /// </summary>
  11. /// <remarks>
  12. /// Each <see cref="TreeViewItem"/> has its own <see cref="TreeItemContainerGenerator{T}"/>
  13. /// that maintains the list of its direct children, but they also share an instance of this
  14. /// class in their <see cref="TreeItemContainerGenerator{T}.Index"/> property which tracks
  15. /// the containers materialized for the entire tree.
  16. /// </remarks>
  17. public class TreeContainerIndex
  18. {
  19. private readonly Dictionary<object, IControl> _itemToContainer = new Dictionary<object, IControl>();
  20. private readonly Dictionary<IControl, object> _containerToItem = new Dictionary<IControl, object>();
  21. /// <summary>
  22. /// Signaled whenever new containers are materialized.
  23. /// </summary>
  24. public event EventHandler<ItemContainerEventArgs> Materialized;
  25. /// <summary>
  26. /// Event raised whenever containers are dematerialized.
  27. /// </summary>
  28. public event EventHandler<ItemContainerEventArgs> Dematerialized;
  29. /// <summary>
  30. /// Gets the currently materialized containers.
  31. /// </summary>
  32. public IEnumerable<IControl> Items => _containerToItem.Keys;
  33. /// <summary>
  34. /// Adds an entry to the index.
  35. /// </summary>
  36. /// <param name="item">The item.</param>
  37. /// <param name="container">The item container.</param>
  38. public void Add(object item, IControl container)
  39. {
  40. _itemToContainer.Add(item, container);
  41. _containerToItem.Add(container, item);
  42. Materialized?.Invoke(
  43. this,
  44. new ItemContainerEventArgs(new ItemContainerInfo(container, item, 0)));
  45. }
  46. /// <summary>
  47. /// Removes a container from the index.
  48. /// </summary>
  49. /// <param name="container">The item container.</param>
  50. public void Remove(IControl container)
  51. {
  52. var item = _containerToItem[container];
  53. _containerToItem.Remove(container);
  54. _itemToContainer.Remove(item);
  55. Dematerialized?.Invoke(
  56. this,
  57. new ItemContainerEventArgs(new ItemContainerInfo(container, item, 0)));
  58. }
  59. /// <summary>
  60. /// Removes a set of containers from the index.
  61. /// </summary>
  62. /// <param name="startingIndex">The index of the first item.</param>
  63. /// <param name="containers">The item containers.</param>
  64. public void Remove(int startingIndex, IEnumerable<ItemContainerInfo> containers)
  65. {
  66. foreach (var container in containers)
  67. {
  68. var item = _containerToItem[container.ContainerControl];
  69. _containerToItem.Remove(container.ContainerControl);
  70. _itemToContainer.Remove(item);
  71. }
  72. Dematerialized?.Invoke(
  73. this,
  74. new ItemContainerEventArgs(startingIndex, containers.ToList()));
  75. }
  76. /// <summary>
  77. /// Gets the container for an item.
  78. /// </summary>
  79. /// <param name="item">The item.</param>
  80. /// <returns>The container, or null of not found.</returns>
  81. public IControl ContainerFromItem(object item)
  82. {
  83. IControl result;
  84. _itemToContainer.TryGetValue(item, out result);
  85. return result;
  86. }
  87. /// <summary>
  88. /// Gets the item for a container.
  89. /// </summary>
  90. /// <param name="container">The container.</param>
  91. /// <returns>The item, or null of not found.</returns>
  92. public object ItemFromContainer(IControl container)
  93. {
  94. object result;
  95. _containerToItem.TryGetValue(container, out result);
  96. return result;
  97. }
  98. }
  99. }