View.Hierarchy.cs 11 KB

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