View.Hierarchy.cs 11 KB

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