View.Hierarchy.cs 11 KB

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