View.Hierarchy.cs 11 KB

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