View.Hierarchy.cs 11 KB

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