View.Hierarchy.cs 13 KB

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