ViewSubViews.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Terminal.Gui;
  4. public partial class View {
  5. static readonly IList<View> _empty = new List<View> (0).AsReadOnly ();
  6. internal bool _addingView;
  7. List<View> _subviews; // This is null, and allocated on demand.
  8. View _superView;
  9. /// <summary>
  10. /// Returns the container for this view, or null if this view has not been added to a container.
  11. /// </summary>
  12. /// <value>The super view.</value>
  13. public virtual View SuperView {
  14. get => _superView;
  15. set => throw new NotImplementedException ();
  16. }
  17. /// <summary>
  18. /// This returns a list of the subviews contained by this view.
  19. /// </summary>
  20. /// <value>The subviews.</value>
  21. public IList<View> Subviews => _subviews?.AsReadOnly () ?? _empty;
  22. // Internally, we use InternalSubviews rather than subviews, as we do not expect us
  23. // to make the same mistakes our users make when they poke at the Subviews.
  24. internal IList<View> InternalSubviews => _subviews ?? _empty;
  25. /// <summary>
  26. /// Returns a value indicating if this View is currently on Top (Active)
  27. /// </summary>
  28. public bool IsCurrentTop => Application.Current == this;
  29. /// <summary>
  30. /// Indicates whether the view was added to <see cref="SuperView"/>.
  31. /// </summary>
  32. public bool IsAdded { get; private set; }
  33. /// <summary>
  34. /// Event fired when this view is added to another.
  35. /// </summary>
  36. public event EventHandler<SuperViewChangedEventArgs> Added;
  37. /// <summary>
  38. /// Adds a subview (child) to this view.
  39. /// </summary>
  40. /// <remarks>
  41. /// The Views that have been added to this view can be retrieved via the <see cref="Subviews"/> property.
  42. /// See also <seealso cref="Remove(View)"/> <seealso cref="RemoveAll"/>
  43. /// </remarks>
  44. public virtual void Add (View view)
  45. {
  46. if (view == null) {
  47. return;
  48. }
  49. if (_subviews == null) {
  50. _subviews = new List<View> ();
  51. }
  52. if (_tabIndexes == null) {
  53. _tabIndexes = new List<View> ();
  54. }
  55. _subviews.Add (view);
  56. _tabIndexes.Add (view);
  57. view._superView = this;
  58. if (view.CanFocus) {
  59. _addingView = true;
  60. if (SuperView?.CanFocus == false) {
  61. SuperView._addingView = true;
  62. SuperView.CanFocus = true;
  63. SuperView._addingView = false;
  64. }
  65. CanFocus = true;
  66. view._tabIndex = _tabIndexes.IndexOf (view);
  67. _addingView = false;
  68. }
  69. if (view.Enabled && !Enabled) {
  70. view._oldEnabled = true;
  71. view.Enabled = false;
  72. }
  73. OnAdded (new SuperViewChangedEventArgs (this, view));
  74. if (IsInitialized && !view.IsInitialized) {
  75. view.BeginInit ();
  76. view.EndInit ();
  77. }
  78. CheckDimAuto ();
  79. SetNeedsLayout ();
  80. SetNeedsDisplay ();
  81. }
  82. /// <summary>
  83. /// Adds the specified views (children) to the view.
  84. /// </summary>
  85. /// <param name="views">Array of one or more views (can be optional parameter).</param>
  86. /// <remarks>
  87. /// The Views that have been added to this view can be retrieved via the <see cref="Subviews"/> property.
  88. /// See also <seealso cref="Remove(View)"/> <seealso cref="RemoveAll"/>
  89. /// </remarks>
  90. public void Add (params View [] views)
  91. {
  92. if (views == null) {
  93. return;
  94. }
  95. foreach (var view in views) {
  96. Add (view);
  97. }
  98. }
  99. /// <summary>
  100. /// Method invoked when a subview is being added to this view.
  101. /// </summary>
  102. /// <param name="e">Event where <see cref="ViewEventArgs.View"/> is the subview being added.</param>
  103. public virtual void OnAdded (SuperViewChangedEventArgs e)
  104. {
  105. var view = e.Child;
  106. view.IsAdded = true;
  107. view.OnResizeNeeded ();
  108. view.Added?.Invoke (this, e);
  109. }
  110. /// <summary>
  111. /// Event fired when this view is removed from another.
  112. /// </summary>
  113. public event EventHandler<SuperViewChangedEventArgs> Removed;
  114. /// <summary>
  115. /// Removes all subviews (children) added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
  116. /// </summary>
  117. public virtual void RemoveAll ()
  118. {
  119. if (_subviews == null) {
  120. return;
  121. }
  122. while (_subviews.Count > 0) {
  123. Remove (_subviews [0]);
  124. }
  125. }
  126. /// <summary>
  127. /// Removes a subview added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
  128. /// </summary>
  129. /// <remarks>
  130. /// </remarks>
  131. public virtual void Remove (View view)
  132. {
  133. if (view == null || _subviews == null) {
  134. return;
  135. }
  136. var touched = view.Frame;
  137. _subviews.Remove (view);
  138. _tabIndexes.Remove (view);
  139. view._superView = null;
  140. view._tabIndex = -1;
  141. SetNeedsLayout ();
  142. SetNeedsDisplay ();
  143. foreach (var v in _subviews) {
  144. if (v.Frame.IntersectsWith (touched)) {
  145. view.SetNeedsDisplay ();
  146. }
  147. }
  148. OnRemoved (new SuperViewChangedEventArgs (this, view));
  149. if (Focused == view) {
  150. Focused = null;
  151. }
  152. }
  153. /// <summary>
  154. /// Method invoked when a subview is being removed from this view.
  155. /// </summary>
  156. /// <param name="e">Event args describing the subview being removed.</param>
  157. public virtual void OnRemoved (SuperViewChangedEventArgs e)
  158. {
  159. var view = e.Child;
  160. view.IsAdded = false;
  161. view.Removed?.Invoke (this, e);
  162. }
  163. void PerformActionForSubview (View subview, Action<View> action)
  164. {
  165. if (_subviews.Contains (subview)) {
  166. action (subview);
  167. }
  168. SetNeedsDisplay ();
  169. subview.SetNeedsDisplay ();
  170. }
  171. /// <summary>
  172. /// Brings the specified subview to the front so it is drawn on top of any other views.
  173. /// </summary>
  174. /// <param name="subview">The subview to send to the front</param>
  175. /// <remarks>
  176. /// <seealso cref="SendSubviewToBack"/>.
  177. /// </remarks>
  178. public void BringSubviewToFront (View subview) => PerformActionForSubview (subview, x => {
  179. _subviews.Remove (x);
  180. _subviews.Add (x);
  181. });
  182. /// <summary>
  183. /// Sends the specified subview to the front so it is the first view drawn
  184. /// </summary>
  185. /// <param name="subview">The subview to send to the front</param>
  186. /// <remarks>
  187. /// <seealso cref="BringSubviewToFront(View)"/>.
  188. /// </remarks>
  189. public void SendSubviewToBack (View subview) => PerformActionForSubview (subview, x => {
  190. _subviews.Remove (x);
  191. _subviews.Insert (0, subview);
  192. });
  193. /// <summary>
  194. /// Moves the subview backwards in the hierarchy, only one step
  195. /// </summary>
  196. /// <param name="subview">The subview to send backwards</param>
  197. /// <remarks>
  198. /// If you want to send the view all the way to the back use SendSubviewToBack.
  199. /// </remarks>
  200. public void SendSubviewBackwards (View subview) => PerformActionForSubview (subview, x => {
  201. var idx = _subviews.IndexOf (x);
  202. if (idx > 0) {
  203. _subviews.Remove (x);
  204. _subviews.Insert (idx - 1, x);
  205. }
  206. });
  207. /// <summary>
  208. /// Moves the subview backwards in the hierarchy, only one step
  209. /// </summary>
  210. /// <param name="subview">The subview to send backwards</param>
  211. /// <remarks>
  212. /// If you want to send the view all the way to the back use SendSubviewToBack.
  213. /// </remarks>
  214. public void BringSubviewForward (View subview) => PerformActionForSubview (subview, x => {
  215. var idx = _subviews.IndexOf (x);
  216. if (idx + 1 < _subviews.Count) {
  217. _subviews.Remove (x);
  218. _subviews.Insert (idx + 1, x);
  219. }
  220. });
  221. /// <summary>
  222. /// Get the top superview of a given <see cref="View"/>.
  223. /// </summary>
  224. /// <returns>The superview view.</returns>
  225. public View GetTopSuperView (View view = null, View superview = null)
  226. {
  227. var top = superview ?? Application.Top;
  228. for (var v = view?.SuperView ?? this?.SuperView; v != null; v = v.SuperView) {
  229. top = v;
  230. if (top == superview) {
  231. break;
  232. }
  233. }
  234. return top;
  235. }
  236. #region Focus
  237. internal enum Direction {
  238. Forward,
  239. Backward
  240. }
  241. /// <summary>
  242. /// Event fired when the view gets focus.
  243. /// </summary>
  244. public event EventHandler<FocusEventArgs> Enter;
  245. /// <summary>
  246. /// Event fired when the view looses focus.
  247. /// </summary>
  248. public event EventHandler<FocusEventArgs> Leave;
  249. Direction _focusDirection;
  250. internal Direction FocusDirection {
  251. get => SuperView?.FocusDirection ?? _focusDirection;
  252. set {
  253. if (SuperView != null) {
  254. SuperView.FocusDirection = value;
  255. } else {
  256. _focusDirection = value;
  257. }
  258. }
  259. }
  260. // BUGBUG: v2 - Seems weird that this is in View and not Responder.
  261. bool _hasFocus;
  262. /// <inheritdoc/>
  263. public override bool HasFocus => _hasFocus;
  264. void SetHasFocus (bool value, View view, bool force = false)
  265. {
  266. if (_hasFocus != value || force) {
  267. _hasFocus = value;
  268. if (value) {
  269. OnEnter (view);
  270. } else {
  271. OnLeave (view);
  272. }
  273. SetNeedsDisplay ();
  274. }
  275. // Remove focus down the chain of subviews if focus is removed
  276. if (!value && Focused != null) {
  277. var f = Focused;
  278. f.OnLeave (view);
  279. f.SetHasFocus (false, view);
  280. Focused = null;
  281. }
  282. }
  283. /// <summary>
  284. /// Event fired when the <see cref="CanFocus"/> value is being changed.
  285. /// </summary>
  286. public event EventHandler CanFocusChanged;
  287. /// <inheritdoc/>
  288. public override void OnCanFocusChanged () => CanFocusChanged?.Invoke (this, EventArgs.Empty);
  289. bool _oldCanFocus;
  290. /// <inheritdoc/>
  291. public override bool CanFocus {
  292. get => base.CanFocus;
  293. set {
  294. if (!_addingView && IsInitialized && SuperView?.CanFocus == false && value) {
  295. throw new InvalidOperationException ("Cannot set CanFocus to true if the SuperView CanFocus is false!");
  296. }
  297. if (base.CanFocus != value) {
  298. base.CanFocus = value;
  299. switch (value) {
  300. case false when _tabIndex > -1:
  301. TabIndex = -1;
  302. break;
  303. case true when SuperView?.CanFocus == false && _addingView:
  304. SuperView.CanFocus = true;
  305. break;
  306. }
  307. if (value && _tabIndex == -1) {
  308. TabIndex = SuperView != null ? SuperView._tabIndexes.IndexOf (this) : -1;
  309. }
  310. TabStop = value;
  311. if (!value && SuperView?.Focused == this) {
  312. SuperView.Focused = null;
  313. }
  314. if (!value && HasFocus) {
  315. SetHasFocus (false, this);
  316. SuperView?.EnsureFocus ();
  317. if (SuperView != null && SuperView.Focused == null) {
  318. SuperView.FocusNext ();
  319. if (SuperView.Focused == null && Application.Current != null) {
  320. Application.Current.FocusNext ();
  321. }
  322. Application.BringOverlappedTopToFront ();
  323. }
  324. }
  325. if (_subviews != null && IsInitialized) {
  326. foreach (var view in _subviews) {
  327. if (view.CanFocus != value) {
  328. if (!value) {
  329. view._oldCanFocus = view.CanFocus;
  330. view._oldTabIndex = view._tabIndex;
  331. view.CanFocus = false;
  332. view._tabIndex = -1;
  333. } else {
  334. if (_addingView) {
  335. view._addingView = true;
  336. }
  337. view.CanFocus = view._oldCanFocus;
  338. view._tabIndex = view._oldTabIndex;
  339. view._addingView = false;
  340. }
  341. }
  342. }
  343. }
  344. OnCanFocusChanged ();
  345. SetNeedsDisplay ();
  346. }
  347. }
  348. }
  349. /// <inheritdoc/>
  350. public override bool OnEnter (View view)
  351. {
  352. var args = new FocusEventArgs (view);
  353. Enter?.Invoke (this, args);
  354. if (args.Handled) {
  355. return true;
  356. }
  357. if (base.OnEnter (view)) {
  358. return true;
  359. }
  360. return false;
  361. }
  362. /// <inheritdoc/>
  363. public override bool OnLeave (View view)
  364. {
  365. var args = new FocusEventArgs (view);
  366. Leave?.Invoke (this, args);
  367. if (args.Handled) {
  368. return true;
  369. }
  370. if (base.OnLeave (view)) {
  371. return true;
  372. }
  373. Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  374. return false;
  375. }
  376. /// <summary>
  377. /// Returns the currently focused view inside this view, or null if nothing is focused.
  378. /// </summary>
  379. /// <value>The focused.</value>
  380. public View Focused { get; private set; }
  381. /// <summary>
  382. /// Returns the most focused view in the chain of subviews (the leaf view that has the focus).
  383. /// </summary>
  384. /// <value>The most focused View.</value>
  385. public View MostFocused {
  386. get {
  387. if (Focused == null) {
  388. return null;
  389. }
  390. var most = Focused.MostFocused;
  391. if (most != null) {
  392. return most;
  393. }
  394. return Focused;
  395. }
  396. }
  397. /// <summary>
  398. /// Causes the specified subview to have focus.
  399. /// </summary>
  400. /// <param name="view">View.</param>
  401. void SetFocus (View view)
  402. {
  403. if (view == null) {
  404. return;
  405. }
  406. //Console.WriteLine ($"Request to focus {view}");
  407. if (!view.CanFocus || !view.Visible || !view.Enabled) {
  408. return;
  409. }
  410. if (Focused?._hasFocus == true && Focused == view) {
  411. return;
  412. }
  413. if (Focused?._hasFocus == true && Focused?.SuperView == view || view == this) {
  414. if (!view._hasFocus) {
  415. view._hasFocus = true;
  416. }
  417. return;
  418. }
  419. // Make sure that this view is a subview
  420. View c;
  421. for (c = view._superView; c != null; c = c._superView) {
  422. if (c == this) {
  423. break;
  424. }
  425. }
  426. if (c == null) {
  427. throw new ArgumentException ("the specified view is not part of the hierarchy of this view");
  428. }
  429. if (Focused != null) {
  430. Focused.SetHasFocus (false, view);
  431. }
  432. var f = Focused;
  433. Focused = view;
  434. Focused.SetHasFocus (true, f);
  435. Focused.EnsureFocus ();
  436. // Send focus upwards
  437. if (SuperView != null) {
  438. SuperView.SetFocus (this);
  439. } else {
  440. SetFocus (this);
  441. }
  442. }
  443. /// <summary>
  444. /// Causes the specified view and the entire parent hierarchy to have the focused order updated.
  445. /// </summary>
  446. public void SetFocus ()
  447. {
  448. if (!CanBeVisible (this) || !Enabled) {
  449. if (HasFocus) {
  450. SetHasFocus (false, this);
  451. }
  452. return;
  453. }
  454. if (SuperView != null) {
  455. SuperView.SetFocus (this);
  456. } else {
  457. SetFocus (this);
  458. }
  459. }
  460. /// <summary>
  461. /// Finds the first view in the hierarchy that wants to get the focus if nothing is currently focused, otherwise, does
  462. /// nothing.
  463. /// </summary>
  464. public void EnsureFocus ()
  465. {
  466. if (Focused == null && _subviews?.Count > 0) {
  467. if (FocusDirection == Direction.Forward) {
  468. FocusFirst ();
  469. } else {
  470. FocusLast ();
  471. }
  472. }
  473. }
  474. /// <summary>
  475. /// Focuses the first focusable subview if one exists.
  476. /// </summary>
  477. public void FocusFirst ()
  478. {
  479. if (!CanBeVisible (this)) {
  480. return;
  481. }
  482. if (_tabIndexes == null) {
  483. SuperView?.SetFocus (this);
  484. return;
  485. }
  486. foreach (var view in _tabIndexes) {
  487. if (view.CanFocus && view._tabStop && view.Visible && view.Enabled) {
  488. SetFocus (view);
  489. return;
  490. }
  491. }
  492. }
  493. /// <summary>
  494. /// Focuses the last focusable subview if one exists.
  495. /// </summary>
  496. public void FocusLast ()
  497. {
  498. if (!CanBeVisible (this)) {
  499. return;
  500. }
  501. if (_tabIndexes == null) {
  502. SuperView?.SetFocus (this);
  503. return;
  504. }
  505. for (var i = _tabIndexes.Count; i > 0;) {
  506. i--;
  507. var v = _tabIndexes [i];
  508. if (v.CanFocus && v._tabStop && v.Visible && v.Enabled) {
  509. SetFocus (v);
  510. return;
  511. }
  512. }
  513. }
  514. /// <summary>
  515. /// Focuses the previous view.
  516. /// </summary>
  517. /// <returns><see langword="true"/> if previous was focused, <see langword="false"/> otherwise.</returns>
  518. public bool FocusPrev ()
  519. {
  520. if (!CanBeVisible (this)) {
  521. return false;
  522. }
  523. FocusDirection = Direction.Backward;
  524. if (_tabIndexes == null || _tabIndexes.Count == 0) {
  525. return false;
  526. }
  527. if (Focused == null) {
  528. FocusLast ();
  529. return Focused != null;
  530. }
  531. var focusedIdx = -1;
  532. for (var i = _tabIndexes.Count; i > 0;) {
  533. i--;
  534. var w = _tabIndexes [i];
  535. if (w.HasFocus) {
  536. if (w.FocusPrev ()) {
  537. return true;
  538. }
  539. focusedIdx = i;
  540. continue;
  541. }
  542. if (w.CanFocus && focusedIdx != -1 && w._tabStop && w.Visible && w.Enabled) {
  543. Focused.SetHasFocus (false, w);
  544. if (w.CanFocus && w._tabStop && w.Visible && w.Enabled) {
  545. w.FocusLast ();
  546. }
  547. SetFocus (w);
  548. return true;
  549. }
  550. }
  551. if (Focused != null) {
  552. Focused.SetHasFocus (false, this);
  553. Focused = null;
  554. }
  555. return false;
  556. }
  557. /// <summary>
  558. /// Focuses the next view.
  559. /// </summary>
  560. /// <returns><see langword="true"/> if next was focused, <see langword="false"/> otherwise.</returns>
  561. public bool FocusNext ()
  562. {
  563. if (!CanBeVisible (this)) {
  564. return false;
  565. }
  566. FocusDirection = Direction.Forward;
  567. if (_tabIndexes == null || _tabIndexes.Count == 0) {
  568. return false;
  569. }
  570. if (Focused == null) {
  571. FocusFirst ();
  572. return Focused != null;
  573. }
  574. var focusedIdx = -1;
  575. for (var i = 0; i < _tabIndexes.Count; i++) {
  576. var w = _tabIndexes [i];
  577. if (w.HasFocus) {
  578. if (w.FocusNext ()) {
  579. return true;
  580. }
  581. focusedIdx = i;
  582. continue;
  583. }
  584. if (w.CanFocus && focusedIdx != -1 && w._tabStop && w.Visible && w.Enabled) {
  585. Focused.SetHasFocus (false, w);
  586. if (w.CanFocus && w._tabStop && w.Visible && w.Enabled) {
  587. w.FocusFirst ();
  588. }
  589. SetFocus (w);
  590. return true;
  591. }
  592. }
  593. if (Focused != null) {
  594. Focused.SetHasFocus (false, this);
  595. Focused = null;
  596. }
  597. return false;
  598. }
  599. View GetMostFocused (View view)
  600. {
  601. if (view == null) {
  602. return null;
  603. }
  604. return view.Focused != null ? GetMostFocused (view.Focused) : view;
  605. }
  606. /// <summary>
  607. /// Positions the cursor in the right position based on the currently focused view in the chain.
  608. /// </summary>
  609. /// Views that are focusable should override
  610. /// <see cref="PositionCursor"/>
  611. /// to ensure
  612. /// the cursor is placed in a location that makes sense. Unix terminals do not have
  613. /// a way of hiding the cursor, so it can be distracting to have the cursor left at
  614. /// the last focused view. Views should make sure that they place the cursor
  615. /// in a visually sensible place.
  616. public virtual void PositionCursor ()
  617. {
  618. if (!CanBeVisible (this) || !Enabled) {
  619. return;
  620. }
  621. // BUGBUG: v2 - This needs to support children of Frames too
  622. if (Focused == null && SuperView != null) {
  623. SuperView.EnsureFocus ();
  624. } else if (Focused?.Visible == true && Focused?.Enabled == true && Focused?.Frame.Width > 0 && Focused.Frame.Height > 0) {
  625. Focused.PositionCursor ();
  626. } else if (Focused?.Visible == true && Focused?.Enabled == false) {
  627. Focused = null;
  628. } else if (CanFocus && HasFocus && Visible && Frame.Width > 0 && Frame.Height > 0) {
  629. Move (TextFormatter.HotKeyPos == -1 ? 0 : TextFormatter.CursorPosition, 0);
  630. } else {
  631. Move (_frame.X, _frame.Y);
  632. }
  633. }
  634. #endregion Focus
  635. }