View.Hierarchy.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using System.Diagnostics;
  2. namespace Terminal.Gui;
  3. public partial class View // SuperView/SubView hierarchy management (SuperView, SubViews, Add, Remove, etc.)
  4. {
  5. private static readonly IList<View> _empty = new List<View> (0).AsReadOnly ();
  6. private List<View> _subviews; // This is null, and allocated on demand.
  7. private View _superView;
  8. /// <summary>Indicates whether the view was added to <see cref="SuperView"/>.</summary>
  9. public bool IsAdded { get; private set; }
  10. /// <summary>This returns a list of the subviews contained by this view.</summary>
  11. /// <value>The subviews.</value>
  12. public IList<View> Subviews => _subviews?.AsReadOnly () ?? _empty;
  13. /// <summary>Returns the container for this view, or null if this view has not been added to a container.</summary>
  14. /// <value>The super view.</value>
  15. public virtual View SuperView
  16. {
  17. get => _superView;
  18. set => throw new NotImplementedException ();
  19. }
  20. // Internally, we use InternalSubviews rather than subviews, as we do not expect us
  21. // to make the same mistakes our users make when they poke at the Subviews.
  22. internal IList<View> InternalSubviews => _subviews ?? _empty;
  23. /// <summary>Adds a subview (child) to this view.</summary>
  24. /// <remarks>
  25. /// <para>
  26. /// The Views that have been added to this view can be retrieved via the <see cref="Subviews"/> property. See also
  27. /// <seealso cref="Remove(View)"/> <seealso cref="RemoveAll"/>
  28. /// </para>
  29. /// <para>
  30. /// Subviews will be disposed when this View is disposed. In other-words, calling this method causes
  31. /// the lifecycle of the subviews to be transferred to this View.
  32. /// </para>
  33. /// </remarks>
  34. /// <param name="view">The view to add.</param>
  35. /// <returns>The view that was added.</returns>
  36. public virtual View Add (View view)
  37. {
  38. if (view is null)
  39. {
  40. return view;
  41. }
  42. if (_subviews is null)
  43. {
  44. _subviews = new ();
  45. }
  46. Debug.WriteLineIf (_subviews.Contains (view), $"BUGBUG: {view} has already been added to {this}.");
  47. _subviews.Add (view);
  48. view._superView = this;
  49. if (view.Enabled && view.Visible && view.CanFocus)
  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>Get the top superview of a given <see cref="View"/>.</summary>
  99. /// <returns>The superview view.</returns>
  100. public View GetTopSuperView (View view = null, View superview = null)
  101. {
  102. View top = superview ?? Application.Top;
  103. for (View v = view?.SuperView ?? this?.SuperView; v != null; v = v.SuperView)
  104. {
  105. top = v;
  106. if (top == superview)
  107. {
  108. break;
  109. }
  110. }
  111. return top;
  112. }
  113. /// <summary>Method invoked when a subview is being added to this view.</summary>
  114. /// <param name="e">Event where <see cref="ViewEventArgs.View"/> is the subview being added.</param>
  115. public virtual void OnAdded (SuperViewChangedEventArgs e)
  116. {
  117. View view = e.Child;
  118. view.IsAdded = true;
  119. view.OnResizeNeeded ();
  120. view.Added?.Invoke (this, e);
  121. }
  122. /// <summary>Method invoked when a subview is being removed from this view.</summary>
  123. /// <param name="e">Event args describing the subview being removed.</param>
  124. public virtual void OnRemoved (SuperViewChangedEventArgs e)
  125. {
  126. View view = e.Child;
  127. view.IsAdded = false;
  128. view.Removed?.Invoke (this, e);
  129. }
  130. /// <summary>Removes a subview added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.</summary>
  131. /// <remarks>
  132. /// <para>
  133. /// Normally Subviews will be disposed when this View is disposed. Removing a Subview causes ownership of the
  134. /// Subview's
  135. /// lifecycle to be transferred to the caller; the caller muse call <see cref="Dispose"/>.
  136. /// </para>
  137. /// </remarks>
  138. public virtual View Remove (View view)
  139. {
  140. if (view is null || _subviews is null)
  141. {
  142. return view;
  143. }
  144. Rectangle touched = view.Frame;
  145. // If a view being removed is focused, it should lose focus.
  146. if (view.HasFocus)
  147. {
  148. view.HasFocus = false;
  149. }
  150. _subviews.Remove (view);
  151. view._superView = null; // Null this AFTER removing focus
  152. SetNeedsLayout ();
  153. SetNeedsDisplay ();
  154. foreach (View v in _subviews)
  155. {
  156. if (v.Frame.IntersectsWith (touched))
  157. {
  158. view.SetNeedsDisplay ();
  159. }
  160. }
  161. if (HasFocus)
  162. {
  163. FocusDeepest (NavigationDirection.Forward, TabStop);
  164. }
  165. OnRemoved (new (this, view));
  166. return view;
  167. }
  168. /// <summary>
  169. /// Removes all subviews (children) added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
  170. /// </summary>
  171. /// <remarks>
  172. /// <para>
  173. /// Normally Subviews will be disposed when this View is disposed. Removing a Subview causes ownership of the
  174. /// Subview's
  175. /// lifecycle to be transferred to the caller; the caller must call <see cref="Dispose"/> on any Views that were
  176. /// added.
  177. /// </para>
  178. /// </remarks>
  179. public virtual void RemoveAll ()
  180. {
  181. if (_subviews is null)
  182. {
  183. return;
  184. }
  185. while (_subviews.Count > 0)
  186. {
  187. Remove (_subviews [0]);
  188. }
  189. }
  190. /// <summary>Event fired when this view is removed from another.</summary>
  191. public event EventHandler<SuperViewChangedEventArgs> Removed;
  192. /// <summary>Moves <paramref name="subview"/> one position towards the start of the <see cref="Subviews"/> list</summary>
  193. /// <param name="subview">The subview to move forward.</param>
  194. public void BringSubviewForward (View subview)
  195. {
  196. PerformActionForSubview (
  197. subview,
  198. x =>
  199. {
  200. int idx = _subviews.IndexOf (x);
  201. if (idx + 1 < _subviews.Count)
  202. {
  203. _subviews.Remove (x);
  204. _subviews.Insert (idx + 1, x);
  205. }
  206. }
  207. );
  208. }
  209. /// <summary>Moves <paramref name="subview"/> to the start of the <see cref="Subviews"/> list.</summary>
  210. /// <param name="subview">The subview to send to the start.</param>
  211. public void BringSubviewToFront (View subview)
  212. {
  213. PerformActionForSubview (
  214. subview,
  215. x =>
  216. {
  217. _subviews.Remove (x);
  218. _subviews.Add (x);
  219. }
  220. );
  221. }
  222. /// <summary>Moves <paramref name="subview"/> one position towards the end of the <see cref="Subviews"/> list</summary>
  223. /// <param name="subview">The subview to move backwards.</param>
  224. public void SendSubviewBackwards (View subview)
  225. {
  226. PerformActionForSubview (
  227. subview,
  228. x =>
  229. {
  230. int idx = _subviews.IndexOf (x);
  231. if (idx > 0)
  232. {
  233. _subviews.Remove (x);
  234. _subviews.Insert (idx - 1, x);
  235. }
  236. }
  237. );
  238. }
  239. /// <summary>Moves <paramref name="subview"/> to the end of the <see cref="Subviews"/> list.</summary>
  240. /// <param name="subview">The subview to send to the end.</param>
  241. public void SendSubviewToBack (View subview)
  242. {
  243. PerformActionForSubview (
  244. subview,
  245. x =>
  246. {
  247. _subviews.Remove (x);
  248. _subviews.Insert (0, subview);
  249. }
  250. );
  251. }
  252. /// <summary>
  253. /// Internal API that runs <paramref name="action"/> on a subview if it is part of the <see cref="Subviews"/> list.
  254. /// </summary>
  255. /// <param name="subview"></param>
  256. /// <param name="action"></param>
  257. private void PerformActionForSubview (View subview, Action<View> action)
  258. {
  259. if (_subviews.Contains (subview))
  260. {
  261. action (subview);
  262. }
  263. // BUGBUG: this is odd. Why is this needed?
  264. SetNeedsDisplay ();
  265. subview.SetNeedsDisplay ();
  266. }
  267. }