View.Hierarchy.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #nullable enable
  2. using System.Diagnostics;
  3. namespace Terminal.Gui;
  4. public partial class View // SuperView/SubView hierarchy management (SuperView, SubViews, Add, Remove, etc.)
  5. {
  6. private static readonly IList<View> _empty = new List<View> (0).AsReadOnly ();
  7. private List<View>? _subviews; // This is null, and allocated on demand.
  8. // Internally, we use InternalSubviews rather than subviews, as we do not expect us
  9. // to make the same mistakes our users make when they poke at the Subviews.
  10. internal IList<View> InternalSubviews => _subviews ?? _empty;
  11. /// <summary>This returns a list of the subviews contained by this view.</summary>
  12. /// <value>The subviews.</value>
  13. public IList<View> Subviews => _subviews?.AsReadOnly () ?? _empty;
  14. private View? _superView;
  15. /// <summary>Returns the container for this view, or null if this view has not been added to a container.</summary>
  16. /// <value>The super view.</value>
  17. public virtual View? SuperView
  18. {
  19. get => _superView!;
  20. set => throw new NotImplementedException ();
  21. }
  22. #region AddRemove
  23. /// <summary>Indicates whether the view was added to <see cref="SuperView"/>.</summary>
  24. public bool IsAdded { get; private set; }
  25. /// <summary>Adds a subview (child) to this view.</summary>
  26. /// <remarks>
  27. /// <para>
  28. /// The Views that have been added to this view can be retrieved via the <see cref="Subviews"/> property. See also
  29. /// <seealso cref="Remove(View)"/> <seealso cref="RemoveAll"/>
  30. /// </para>
  31. /// <para>
  32. /// Subviews will be disposed when this View is disposed. In other-words, calling this method causes
  33. /// the lifecycle of the subviews to be transferred to this View.
  34. /// </para>
  35. /// </remarks>
  36. /// <param name="view">The view to add.</param>
  37. /// <returns>The view that was added.</returns>
  38. public virtual View Add (View view)
  39. {
  40. if (_subviews is null)
  41. {
  42. _subviews = [];
  43. }
  44. Debug.WriteLineIf (_subviews.Contains (view), $"WARNING: {view} has already been added to {this}.");
  45. // TileView likes to add views that were previously added and have HasFocus = true. No bueno.
  46. view.HasFocus = false;
  47. _subviews.Add (view);
  48. view._superView = this;
  49. if (view is { Enabled: true, Visible: true, CanFocus: true })
  50. {
  51. // Add will cause the newly added subview to gain focus if it's focusable
  52. if (HasFocus)
  53. {
  54. view.SetFocus ();
  55. }
  56. }
  57. if (view.Enabled && !Enabled)
  58. {
  59. view._oldEnabled = true;
  60. view.Enabled = false;
  61. }
  62. OnAdded (new (this, view));
  63. if (IsInitialized && !view.IsInitialized)
  64. {
  65. view.BeginInit ();
  66. view.EndInit ();
  67. }
  68. CheckDimAuto ();
  69. SetNeedsLayout ();
  70. SetNeedsDisplay ();
  71. return view;
  72. }
  73. /// <summary>Adds the specified views (children) to the view.</summary>
  74. /// <param name="views">Array of one or more views (can be optional parameter).</param>
  75. /// <remarks>
  76. /// <para>
  77. /// The Views that have been added to this view can be retrieved via the <see cref="Subviews"/> property. See also
  78. /// <seealso cref="Remove(View)"/> and <seealso cref="RemoveAll"/>.
  79. /// </para>
  80. /// <para>
  81. /// Subviews will be disposed when this View is disposed. In other-words, calling this method causes
  82. /// the lifecycle of the subviews to be transferred to this View.
  83. /// </para>
  84. /// </remarks>
  85. public void Add (params View []? views)
  86. {
  87. if (views is null)
  88. {
  89. return;
  90. }
  91. foreach (View view in views)
  92. {
  93. Add (view);
  94. }
  95. }
  96. /// <summary>Event fired when this view is added to another.</summary>
  97. public event EventHandler<SuperViewChangedEventArgs>? Added;
  98. /// <summary>Method invoked when a subview is being added to this view.</summary>
  99. /// <param name="e">Event where <see cref="ViewEventArgs.View"/> is the subview being added.</param>
  100. public virtual void OnAdded (SuperViewChangedEventArgs e)
  101. {
  102. View view = e.SubView;
  103. view.IsAdded = true;
  104. view.OnResizeNeeded ();
  105. view.Added?.Invoke (this, e);
  106. }
  107. /// <summary>Method invoked when a subview is being removed from this view.</summary>
  108. /// <param name="e">Event args describing the subview being removed.</param>
  109. public virtual void OnRemoved (SuperViewChangedEventArgs e)
  110. {
  111. View view = e.SubView;
  112. view.IsAdded = false;
  113. view.Removed?.Invoke (this, e);
  114. }
  115. /// <summary>Removes a subview added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.</summary>
  116. /// <remarks>
  117. /// <para>
  118. /// Normally Subviews will be disposed when this View is disposed. Removing a Subview causes ownership of the
  119. /// Subview's
  120. /// lifecycle to be transferred to the caller; the caller muse call <see cref="Dispose"/>.
  121. /// </para>
  122. /// </remarks>
  123. /// <returns>
  124. /// The removed View. <see langword="null"/> if the View could not be removed.
  125. /// </returns>
  126. public virtual View? Remove (View view)
  127. {
  128. if (_subviews is null)
  129. {
  130. return view;
  131. }
  132. Rectangle touched = view.Frame;
  133. // If a view being removed is focused, it should lose focus.
  134. if (view.HasFocus)
  135. {
  136. view.HasFocus = false;
  137. }
  138. _subviews.Remove (view);
  139. view._superView = null; // Null this AFTER removing focus
  140. SetNeedsLayout ();
  141. SetNeedsDisplay ();
  142. foreach (View v in _subviews)
  143. {
  144. if (v.Frame.IntersectsWith (touched))
  145. {
  146. view.SetNeedsDisplay ();
  147. }
  148. }
  149. if (HasFocus)
  150. {
  151. FocusDeepest (NavigationDirection.Forward, TabStop);
  152. }
  153. OnRemoved (new (this, view));
  154. return view;
  155. }
  156. /// <summary>
  157. /// Removes all subviews (children) added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
  158. /// </summary>
  159. /// <remarks>
  160. /// <para>
  161. /// Normally Subviews will be disposed when this View is disposed. Removing a Subview causes ownership of the
  162. /// Subview's
  163. /// lifecycle to be transferred to the caller; the caller must call <see cref="Dispose"/> on any Views that were
  164. /// added.
  165. /// </para>
  166. /// </remarks>
  167. public virtual void RemoveAll ()
  168. {
  169. if (_subviews is null)
  170. {
  171. return;
  172. }
  173. while (_subviews.Count > 0)
  174. {
  175. Remove (_subviews [0]);
  176. }
  177. }
  178. /// <summary>Event fired when this view is removed from another.</summary>
  179. public event EventHandler<SuperViewChangedEventArgs>? Removed;
  180. #endregion AddRemove
  181. // TODO: Mark as internal. Or nuke.
  182. /// <summary>Get the top superview of a given <see cref="View"/>.</summary>
  183. /// <returns>The superview view.</returns>
  184. public View? GetTopSuperView (View? view = null, View? superview = null)
  185. {
  186. View? top = superview ?? Application.Top;
  187. for (View? v = view?.SuperView ?? this?.SuperView; v != null; v = v.SuperView)
  188. {
  189. top = v;
  190. if (top == superview)
  191. {
  192. break;
  193. }
  194. }
  195. return top;
  196. }
  197. #region SubViewOrdering
  198. /// <summary>
  199. /// Moves <paramref name="subview"/> one position towards the end of the <see cref="Subviews"/> list.
  200. /// </summary>
  201. /// <param name="subview">The subview to move.</param>
  202. public void MoveSubviewTowardsEnd (View subview)
  203. {
  204. PerformActionForSubview (
  205. subview,
  206. x =>
  207. {
  208. int idx = _subviews!.IndexOf (x);
  209. if (idx + 1 < _subviews.Count)
  210. {
  211. _subviews.Remove (x);
  212. _subviews.Insert (idx + 1, x);
  213. }
  214. }
  215. );
  216. }
  217. /// <summary>
  218. /// Moves <paramref name="subview"/> to the end of the <see cref="Subviews"/> list.
  219. /// </summary>
  220. /// <param name="subview">The subview to move.</param>
  221. public void MoveSubviewToEnd (View subview)
  222. {
  223. PerformActionForSubview (
  224. subview,
  225. x =>
  226. {
  227. _subviews!.Remove (x);
  228. _subviews.Add (x);
  229. }
  230. );
  231. }
  232. /// <summary>
  233. /// Moves <paramref name="subview"/> one position towards the start of the <see cref="Subviews"/> list.
  234. /// </summary>
  235. /// <param name="subview">The subview to move.</param>
  236. public void MoveSubviewTowardsStart (View subview)
  237. {
  238. PerformActionForSubview (
  239. subview,
  240. x =>
  241. {
  242. int idx = _subviews!.IndexOf (x);
  243. if (idx > 0)
  244. {
  245. _subviews.Remove (x);
  246. _subviews.Insert (idx - 1, x);
  247. }
  248. }
  249. );
  250. }
  251. /// <summary>
  252. /// Moves <paramref name="subview"/> to the start of the <see cref="Subviews"/> list.
  253. /// </summary>
  254. /// <param name="subview">The subview to move.</param>
  255. public void MoveSubviewToStart (View subview)
  256. {
  257. PerformActionForSubview (
  258. subview,
  259. x =>
  260. {
  261. _subviews!.Remove (x);
  262. _subviews.Insert (0, subview);
  263. }
  264. );
  265. }
  266. /// <summary>
  267. /// Internal API that runs <paramref name="action"/> on a subview if it is part of the <see cref="Subviews"/> list.
  268. /// </summary>
  269. /// <param name="subview"></param>
  270. /// <param name="action"></param>
  271. private void PerformActionForSubview (View subview, Action<View> action)
  272. {
  273. if (_subviews!.Contains (subview))
  274. {
  275. action (subview);
  276. }
  277. // BUGBUG: this is odd. Why is this needed?
  278. SetNeedsDisplay ();
  279. subview.SetNeedsDisplay ();
  280. }
  281. #endregion SubViewOrdering
  282. }