2
0

View.Hierarchy.cs 11 KB

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