Toplevel.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. #define UNIX_KEY_BINDINGS
  2. using System.Net.Mime;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// Toplevel views are used for both an application's main view (filling the entire screen and for modal (pop-up)
  6. /// views such as <see cref="Dialog"/>, <see cref="MessageBox"/>, and <see cref="Wizard"/>).
  7. /// </summary>
  8. /// <remarks>
  9. /// <para>
  10. /// Toplevels can run as modal (popup) views, started by calling
  11. /// <see cref="Application.Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/>. They return control to the caller when
  12. /// <see cref="Application.RequestStop(Toplevel)"/> has been called (which sets the <see cref="Toplevel.Running"/>
  13. /// property to <c>false</c>).
  14. /// </para>
  15. /// <para>
  16. /// A Toplevel is created when an application initializes Terminal.Gui by calling <see cref="Application.Init"/>.
  17. /// The application Toplevel can be accessed via <see cref="Application.Top"/>. Additional Toplevels can be created
  18. /// and run (e.g. <see cref="Dialog"/>s. To run a Toplevel, create the <see cref="Toplevel"/> and call
  19. /// <see cref="Application.Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/>.
  20. /// </para>
  21. /// </remarks>
  22. public partial class Toplevel : View
  23. {
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="Toplevel"/> class,
  26. /// defaulting to full screen. The <see cref="View.Width"/> and <see cref="View.Height"/> properties will be set to the
  27. /// dimensions of the terminal using <see cref="Dim.Fill"/>.
  28. /// </summary>
  29. public Toplevel ()
  30. {
  31. Arrangement = ViewArrangement.Fixed;
  32. Width = Dim.Fill ();
  33. Height = Dim.Fill ();
  34. ColorScheme = Colors.ColorSchemes ["TopLevel"];
  35. // Things this view knows how to do
  36. AddCommand (
  37. Command.QuitToplevel,
  38. () =>
  39. {
  40. QuitToplevel ();
  41. return true;
  42. }
  43. );
  44. AddCommand (
  45. Command.Suspend,
  46. () =>
  47. {
  48. Driver.Suspend ();
  49. ;
  50. return true;
  51. }
  52. );
  53. AddCommand (
  54. Command.NextView,
  55. () =>
  56. {
  57. MoveNextView ();
  58. return true;
  59. }
  60. );
  61. AddCommand (
  62. Command.PreviousView,
  63. () =>
  64. {
  65. MovePreviousView ();
  66. return true;
  67. }
  68. );
  69. AddCommand (
  70. Command.NextViewOrTop,
  71. () =>
  72. {
  73. MoveNextViewOrTop ();
  74. return true;
  75. }
  76. );
  77. AddCommand (
  78. Command.PreviousViewOrTop,
  79. () =>
  80. {
  81. MovePreviousViewOrTop ();
  82. return true;
  83. }
  84. );
  85. AddCommand (
  86. Command.Refresh,
  87. () =>
  88. {
  89. Application.Refresh ();
  90. return true;
  91. }
  92. );
  93. // Default keybindings for this view
  94. KeyBindings.Add (Application.QuitKey, Command.QuitToplevel);
  95. KeyBindings.Add (Key.CursorRight, Command.NextView);
  96. KeyBindings.Add (Key.CursorDown, Command.NextView);
  97. KeyBindings.Add (Key.CursorLeft, Command.PreviousView);
  98. KeyBindings.Add (Key.CursorUp, Command.PreviousView);
  99. KeyBindings.Add (Key.Tab, Command.NextView);
  100. KeyBindings.Add (Key.Tab.WithShift, Command.PreviousView);
  101. KeyBindings.Add (Key.Tab.WithCtrl, Command.NextViewOrTop);
  102. KeyBindings.Add (Key.Tab.WithShift.WithCtrl, Command.PreviousViewOrTop);
  103. KeyBindings.Add (Key.F5, Command.Refresh);
  104. KeyBindings.Add (Application.AlternateForwardKey, Command.NextViewOrTop); // Needed on Unix
  105. KeyBindings.Add (Application.AlternateBackwardKey, Command.PreviousViewOrTop); // Needed on Unix
  106. #if UNIX_KEY_BINDINGS
  107. KeyBindings.Add (Key.Z.WithCtrl, Command.Suspend);
  108. KeyBindings.Add (Key.L.WithCtrl, Command.Refresh); // Unix
  109. KeyBindings.Add (Key.F.WithCtrl, Command.NextView); // Unix
  110. KeyBindings.Add (Key.I.WithCtrl, Command.NextView); // Unix
  111. KeyBindings.Add (Key.B.WithCtrl, Command.PreviousView); // Unix
  112. #endif
  113. MouseClick += Toplevel_MouseClick;
  114. CanFocus = true;
  115. }
  116. private void Toplevel_MouseClick (object sender, MouseEventEventArgs e)
  117. {
  118. e.Handled = InvokeCommand (Command.HotKey) == true;
  119. }
  120. /// <summary>
  121. /// <see langword="true"/> if was already loaded by the <see cref="Application.Begin(Toplevel)"/>
  122. /// <see langword="false"/>, otherwise.
  123. /// </summary>
  124. public bool IsLoaded { get; private set; }
  125. /// <summary>Gets or sets the menu for this Toplevel.</summary>
  126. public virtual MenuBar MenuBar { get; set; }
  127. /// <summary>
  128. /// Determines whether the <see cref="Toplevel"/> is modal or not. If set to <c>false</c> (the default):
  129. /// <list type="bullet">
  130. /// <item>
  131. /// <description><see cref="View.OnKeyDown"/> events will propagate keys upwards.</description>
  132. /// </item>
  133. /// <item>
  134. /// <description>The Toplevel will act as an embedded view (not a modal/pop-up).</description>
  135. /// </item>
  136. /// </list>
  137. /// If set to <c>true</c>:
  138. /// <list type="bullet">
  139. /// <item>
  140. /// <description><see cref="View.OnKeyDown"/> events will NOT propagate keys upwards.</description>
  141. /// </item>
  142. /// <item>
  143. /// <description>The Toplevel will and look like a modal (pop-up) (e.g. see <see cref="Dialog"/>.</description>
  144. /// </item>
  145. /// </list>
  146. /// </summary>
  147. public bool Modal { get; set; }
  148. /// <summary>Gets or sets whether the main loop for this <see cref="Toplevel"/> is running or not.</summary>
  149. /// <remarks>Setting this property directly is discouraged. Use <see cref="Application.RequestStop"/> instead.</remarks>
  150. public bool Running { get; set; }
  151. /// <summary>Gets or sets the status bar for this Toplevel.</summary>
  152. public virtual StatusBar StatusBar { get; set; }
  153. /// <summary>Invoked when the Toplevel <see cref="RunState"/> becomes the <see cref="Application.Current"/> Toplevel.</summary>
  154. public event EventHandler<ToplevelEventArgs> Activate;
  155. /// <inheritdoc/>
  156. public override void Add (View view)
  157. {
  158. CanFocus = true;
  159. AddMenuStatusBar (view);
  160. base.Add (view);
  161. }
  162. /// <summary>
  163. /// Invoked when the last child of the Toplevel <see cref="RunState"/> is closed from by
  164. /// <see cref="Application.End(RunState)"/>.
  165. /// </summary>
  166. public event EventHandler AllChildClosed;
  167. /// <summary>Invoked when the <see cref="Application.AlternateBackwardKey"/> is changed.</summary>
  168. public event EventHandler<KeyChangedEventArgs> AlternateBackwardKeyChanged;
  169. /// <summary>Invoked when the <see cref="Application.AlternateForwardKey"/> is changed.</summary>
  170. public event EventHandler<KeyChangedEventArgs> AlternateForwardKeyChanged;
  171. /// <summary>
  172. /// Invoked when a child of the Toplevel <see cref="RunState"/> is closed by
  173. /// <see cref="Application.End(RunState)"/>.
  174. /// </summary>
  175. public event EventHandler<ToplevelEventArgs> ChildClosed;
  176. /// <summary>Invoked when a child Toplevel's <see cref="RunState"/> has been loaded.</summary>
  177. public event EventHandler<ToplevelEventArgs> ChildLoaded;
  178. /// <summary>Invoked when a cjhild Toplevel's <see cref="RunState"/> has been unloaded.</summary>
  179. public event EventHandler<ToplevelEventArgs> ChildUnloaded;
  180. /// <summary>Invoked when the Toplevel's <see cref="RunState"/> is closed by <see cref="Application.End(RunState)"/>.</summary>
  181. public event EventHandler<ToplevelEventArgs> Closed;
  182. /// <summary>
  183. /// Invoked when the Toplevel's <see cref="RunState"/> is being closed by
  184. /// <see cref="Application.RequestStop(Toplevel)"/>.
  185. /// </summary>
  186. public event EventHandler<ToplevelClosingEventArgs> Closing;
  187. /// <summary>Invoked when the Toplevel<see cref="RunState"/> ceases to be the <see cref="Application.Current"/> Toplevel.</summary>
  188. public event EventHandler<ToplevelEventArgs> Deactivate;
  189. /// <summary>
  190. /// Invoked when the <see cref="Toplevel"/> <see cref="RunState"/> has begun to be loaded. A Loaded event handler
  191. /// is a good place to finalize initialization before calling <see cref="Application.RunLoop(RunState)"/>.
  192. /// </summary>
  193. public event EventHandler Loaded;
  194. /// <summary>Virtual method to invoke the <see cref="AlternateBackwardKeyChanged"/> event.</summary>
  195. /// <param name="e"></param>
  196. public virtual void OnAlternateBackwardKeyChanged (KeyChangedEventArgs e)
  197. {
  198. KeyBindings.Replace (e.OldKey, e.NewKey);
  199. AlternateBackwardKeyChanged?.Invoke (this, e);
  200. }
  201. /// <summary>Virtual method to invoke the <see cref="AlternateForwardKeyChanged"/> event.</summary>
  202. /// <param name="e"></param>
  203. public virtual void OnAlternateForwardKeyChanged (KeyChangedEventArgs e)
  204. {
  205. KeyBindings.Replace (e.OldKey, e.NewKey);
  206. AlternateForwardKeyChanged?.Invoke (this, e);
  207. }
  208. /// <inheritdoc/>
  209. public override void OnDrawContent (Rectangle viewport)
  210. {
  211. if (!Visible)
  212. {
  213. return;
  214. }
  215. if (NeedsDisplay || SubViewNeedsDisplay || LayoutNeeded)
  216. {
  217. //Driver.SetAttribute (GetNormalColor ());
  218. // TODO: It's bad practice for views to always clear. Defeats the purpose of clipping etc...
  219. Clear ();
  220. LayoutSubviews ();
  221. PositionToplevels ();
  222. if (this == Application.OverlappedTop)
  223. {
  224. foreach (Toplevel top in Application.OverlappedChildren.AsEnumerable ().Reverse ())
  225. {
  226. if (top.Frame.IntersectsWith (Viewport))
  227. {
  228. if (top != this && !top.IsCurrentTop && !OutsideTopFrame (top) && top.Visible)
  229. {
  230. top.SetNeedsLayout ();
  231. top.SetNeedsDisplay (top.Viewport);
  232. top.Draw ();
  233. top.OnRenderLineCanvas ();
  234. }
  235. }
  236. }
  237. }
  238. // This should not be here, but in base
  239. foreach (View view in Subviews)
  240. {
  241. if (view.Frame.IntersectsWith (Viewport) && !OutsideTopFrame (this))
  242. {
  243. //view.SetNeedsLayout ();
  244. view.SetNeedsDisplay ();
  245. view.SetSubViewNeedsDisplay ();
  246. }
  247. }
  248. base.OnDrawContent (viewport);
  249. // This is causing the menus drawn incorrectly if UseSubMenusSingleFrame is true
  250. //if (this.MenuBar is { } && this.MenuBar.IsMenuOpen && this.MenuBar.openMenu is { }) {
  251. // // TODO: Hack until we can get compositing working right.
  252. // this.MenuBar.openMenu.Redraw (this.MenuBar.openMenu.Viewport);
  253. //}
  254. }
  255. }
  256. /// <inheritdoc/>
  257. public override bool OnEnter (View view) { return MostFocused?.OnEnter (view) ?? base.OnEnter (view); }
  258. /// <inheritdoc/>
  259. public override bool OnLeave (View view) { return MostFocused?.OnLeave (view) ?? base.OnLeave (view); }
  260. /// <summary>
  261. /// Called from <see cref="Application.Begin(Toplevel)"/> before the <see cref="Toplevel"/> redraws for the first
  262. /// time.
  263. /// </summary>
  264. public virtual void OnLoaded ()
  265. {
  266. IsLoaded = true;
  267. foreach (Toplevel tl in Subviews.Where (v => v is Toplevel))
  268. {
  269. tl.OnLoaded ();
  270. }
  271. Loaded?.Invoke (this, EventArgs.Empty);
  272. }
  273. /// <summary>Virtual method to invoke the <see cref="QuitKeyChanged"/> event.</summary>
  274. /// <param name="e"></param>
  275. public virtual void OnQuitKeyChanged (KeyChangedEventArgs e)
  276. {
  277. KeyBindings.Replace (e.OldKey, e.NewKey);
  278. QuitKeyChanged?.Invoke (this, e);
  279. }
  280. /// <inheritdoc/>
  281. public override Point? PositionCursor ()
  282. {
  283. if (!IsOverlappedContainer)
  284. {
  285. if (Focused is null)
  286. {
  287. EnsureFocus ();
  288. }
  289. return null;
  290. }
  291. // This code path only happens when the Toplevel is an Overlapped container
  292. if (Focused is null)
  293. {
  294. // TODO: this is an Overlapped hack
  295. foreach (Toplevel top in Application.OverlappedChildren)
  296. {
  297. if (top != this && top.Visible)
  298. {
  299. top.SetFocus ();
  300. return null;
  301. }
  302. }
  303. }
  304. var cursor2 = base.PositionCursor ();
  305. return null;
  306. }
  307. /// <summary>
  308. /// Adjusts the location and size of <paramref name="top"/> within this Toplevel. Virtual method enabling
  309. /// implementation of specific positions for inherited <see cref="Toplevel"/> views.
  310. /// </summary>
  311. /// <param name="top">The Toplevel to adjust.</param>
  312. public virtual void PositionToplevel (Toplevel top)
  313. {
  314. View superView = GetLocationEnsuringFullVisibility (
  315. top,
  316. top.Frame.X,
  317. top.Frame.Y,
  318. out int nx,
  319. out int ny,
  320. out StatusBar sb
  321. );
  322. if (superView is null)
  323. {
  324. return;
  325. }
  326. var layoutSubviews = false;
  327. var maxWidth = 0;
  328. if (superView.Margin is { } && superView == top.SuperView)
  329. {
  330. maxWidth -= superView.GetAdornmentsThickness ().Left + superView.GetAdornmentsThickness ().Right;
  331. }
  332. if ((superView != top || top?.SuperView is { } || (top != Application.Top && top.Modal) || (top?.SuperView is null && top.IsOverlapped))
  333. && (top.Frame.X + top.Frame.Width > maxWidth || ny > top.Frame.Y))
  334. {
  335. if ((top.X is null || top.X is PosAbsolute) && top.Frame.X != nx)
  336. {
  337. top.X = nx;
  338. layoutSubviews = true;
  339. }
  340. if ((top.Y is null || top.Y is PosAbsolute) && top.Frame.Y != ny)
  341. {
  342. top.Y = ny;
  343. layoutSubviews = true;
  344. }
  345. }
  346. // TODO: v2 - This is a hack to get the StatusBar to be positioned correctly.
  347. if (sb != null
  348. && !top.Subviews.Contains (sb)
  349. && ny + top.Frame.Height != superView.Frame.Height - (sb.Visible ? 1 : 0)
  350. && top.Height is DimFill
  351. && -top.Height.GetAnchor (0) < 1)
  352. {
  353. top.Height = Dim.Fill (sb.Visible ? 1 : 0);
  354. layoutSubviews = true;
  355. }
  356. if (superView.LayoutNeeded || layoutSubviews)
  357. {
  358. superView.LayoutSubviews ();
  359. }
  360. if (LayoutNeeded)
  361. {
  362. LayoutSubviews ();
  363. }
  364. }
  365. /// <summary>Invoked when the <see cref="Application.QuitKey"/> is changed.</summary>
  366. public event EventHandler<KeyChangedEventArgs> QuitKeyChanged;
  367. /// <summary>
  368. /// Invoked when the <see cref="Toplevel"/> main loop has started it's first iteration. Subscribe to this event to
  369. /// perform tasks when the <see cref="Toplevel"/> has been laid out and focus has been set. changes.
  370. /// <para>
  371. /// A Ready event handler is a good place to finalize initialization after calling
  372. /// <see cref="Application.Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/> on this <see cref="Toplevel"/>.
  373. /// </para>
  374. /// </summary>
  375. public event EventHandler Ready;
  376. /// <inheritdoc/>
  377. public override void Remove (View view)
  378. {
  379. if (this is Toplevel { MenuBar: { } })
  380. {
  381. RemoveMenuStatusBar (view);
  382. }
  383. base.Remove (view);
  384. }
  385. /// <inheritdoc/>
  386. public override void RemoveAll ()
  387. {
  388. if (this == Application.Top)
  389. {
  390. MenuBar?.Dispose ();
  391. MenuBar = null;
  392. StatusBar?.Dispose ();
  393. StatusBar = null;
  394. }
  395. base.RemoveAll ();
  396. }
  397. /// <summary>
  398. /// Stops and closes this <see cref="Toplevel"/>. If this Toplevel is the top-most Toplevel,
  399. /// <see cref="Application.RequestStop(Toplevel)"/> will be called, causing the application to exit.
  400. /// </summary>
  401. public virtual void RequestStop ()
  402. {
  403. if (IsOverlappedContainer
  404. && Running
  405. && (Application.Current == this
  406. || Application.Current?.Modal == false
  407. || (Application.Current?.Modal == true && Application.Current?.Running == false)))
  408. {
  409. foreach (Toplevel child in Application.OverlappedChildren)
  410. {
  411. var ev = new ToplevelClosingEventArgs (this);
  412. if (child.OnClosing (ev))
  413. {
  414. return;
  415. }
  416. child.Running = false;
  417. Application.RequestStop (child);
  418. }
  419. Running = false;
  420. Application.RequestStop (this);
  421. }
  422. else if (IsOverlappedContainer && Running && Application.Current?.Modal == true && Application.Current?.Running == true)
  423. {
  424. var ev = new ToplevelClosingEventArgs (Application.Current);
  425. if (OnClosing (ev))
  426. {
  427. return;
  428. }
  429. Application.RequestStop (Application.Current);
  430. }
  431. else if (!IsOverlappedContainer && Running && (!Modal || (Modal && Application.Current != this)))
  432. {
  433. var ev = new ToplevelClosingEventArgs (this);
  434. if (OnClosing (ev))
  435. {
  436. return;
  437. }
  438. Running = false;
  439. Application.RequestStop (this);
  440. }
  441. else
  442. {
  443. Application.RequestStop (Application.Current);
  444. }
  445. }
  446. /// <summary>
  447. /// Stops and closes the <see cref="Toplevel"/> specified by <paramref name="top"/>. If <paramref name="top"/> is
  448. /// the top-most Toplevel, <see cref="Application.RequestStop(Toplevel)"/> will be called, causing the application to
  449. /// exit.
  450. /// </summary>
  451. /// <param name="top">The Toplevel to request stop.</param>
  452. public virtual void RequestStop (Toplevel top) { top.RequestStop (); }
  453. /// <summary>Invoked when the terminal has been resized. The new <see cref="Size"/> of the terminal is provided.</summary>
  454. public event EventHandler<SizeChangedEventArgs> SizeChanging;
  455. /// <summary>
  456. /// Invoked when the Toplevel <see cref="RunState"/> has been unloaded. A Unloaded event handler is a good place
  457. /// to dispose objects after calling <see cref="Application.End(RunState)"/>.
  458. /// </summary>
  459. public event EventHandler Unloaded;
  460. internal void AddMenuStatusBar (View view)
  461. {
  462. if (view is MenuBar)
  463. {
  464. MenuBar = view as MenuBar;
  465. }
  466. if (view is StatusBar)
  467. {
  468. StatusBar = view as StatusBar;
  469. }
  470. }
  471. internal virtual void OnActivate (Toplevel deactivated) { Activate?.Invoke (this, new ToplevelEventArgs (deactivated)); }
  472. internal virtual void OnAllChildClosed () { AllChildClosed?.Invoke (this, EventArgs.Empty); }
  473. internal virtual void OnChildClosed (Toplevel top)
  474. {
  475. if (IsOverlappedContainer)
  476. {
  477. SetSubViewNeedsDisplay ();
  478. }
  479. ChildClosed?.Invoke (this, new ToplevelEventArgs (top));
  480. }
  481. internal virtual void OnChildLoaded (Toplevel top) { ChildLoaded?.Invoke (this, new ToplevelEventArgs (top)); }
  482. internal virtual void OnChildUnloaded (Toplevel top) { ChildUnloaded?.Invoke (this, new ToplevelEventArgs (top)); }
  483. internal virtual void OnClosed (Toplevel top) { Closed?.Invoke (this, new ToplevelEventArgs (top)); }
  484. internal virtual bool OnClosing (ToplevelClosingEventArgs ev)
  485. {
  486. Closing?.Invoke (this, ev);
  487. return ev.Cancel;
  488. }
  489. internal virtual void OnDeactivate (Toplevel activated) { Deactivate?.Invoke (this, new ToplevelEventArgs (activated)); }
  490. /// <summary>
  491. /// Called from <see cref="Application.RunLoop"/> after the <see cref="Toplevel"/> has entered the first iteration
  492. /// of the loop.
  493. /// </summary>
  494. internal virtual void OnReady ()
  495. {
  496. foreach (Toplevel tl in Subviews.Where (v => v is Toplevel))
  497. {
  498. tl.OnReady ();
  499. }
  500. Ready?.Invoke (this, EventArgs.Empty);
  501. }
  502. // TODO: Make cancelable?
  503. internal virtual void OnSizeChanging (SizeChangedEventArgs size) { SizeChanging?.Invoke (this, size); }
  504. /// <summary>Called from <see cref="Application.End(RunState)"/> before the <see cref="Toplevel"/> is disposed.</summary>
  505. internal virtual void OnUnloaded ()
  506. {
  507. foreach (Toplevel tl in Subviews.Where (v => v is Toplevel))
  508. {
  509. tl.OnUnloaded ();
  510. }
  511. Unloaded?.Invoke (this, EventArgs.Empty);
  512. }
  513. // TODO: v2 - Not sure this is needed anymore.
  514. internal void PositionToplevels ()
  515. {
  516. PositionToplevel (this);
  517. foreach (View top in Subviews)
  518. {
  519. if (top is Toplevel)
  520. {
  521. PositionToplevel ((Toplevel)top);
  522. }
  523. }
  524. }
  525. internal void RemoveMenuStatusBar (View view)
  526. {
  527. if (view is MenuBar)
  528. {
  529. MenuBar?.Dispose ();
  530. MenuBar = null;
  531. }
  532. if (view is StatusBar)
  533. {
  534. StatusBar?.Dispose ();
  535. StatusBar = null;
  536. }
  537. }
  538. private void FocusNearestView (IEnumerable<View> views, NavigationDirection direction)
  539. {
  540. if (views is null)
  541. {
  542. return;
  543. }
  544. var found = false;
  545. var focusProcessed = false;
  546. var idx = 0;
  547. foreach (View v in views)
  548. {
  549. if (v == this)
  550. {
  551. found = true;
  552. }
  553. if (found && v != this)
  554. {
  555. if (direction == NavigationDirection.Forward)
  556. {
  557. SuperView?.FocusNext ();
  558. }
  559. else
  560. {
  561. SuperView?.FocusPrev ();
  562. }
  563. focusProcessed = true;
  564. if (SuperView.Focused is { } && SuperView.Focused != this)
  565. {
  566. return;
  567. }
  568. }
  569. else if (found && !focusProcessed && idx == views.Count () - 1)
  570. {
  571. views.ToList () [0].SetFocus ();
  572. }
  573. idx++;
  574. }
  575. }
  576. private View GetDeepestFocusedSubview (View view)
  577. {
  578. if (view is null)
  579. {
  580. return null;
  581. }
  582. foreach (View v in view.Subviews)
  583. {
  584. if (v.HasFocus)
  585. {
  586. return GetDeepestFocusedSubview (v);
  587. }
  588. }
  589. return view;
  590. }
  591. private void MoveNextView ()
  592. {
  593. View old = GetDeepestFocusedSubview (Focused);
  594. if (!FocusNext ())
  595. {
  596. FocusNext ();
  597. }
  598. if (old != Focused && old != Focused?.Focused)
  599. {
  600. old?.SetNeedsDisplay ();
  601. Focused?.SetNeedsDisplay ();
  602. }
  603. else
  604. {
  605. FocusNearestView (SuperView?.TabIndexes, NavigationDirection.Forward);
  606. }
  607. }
  608. private void MoveNextViewOrTop ()
  609. {
  610. if (Application.OverlappedTop is null)
  611. {
  612. Toplevel top = Modal ? this : Application.Top;
  613. top.FocusNext ();
  614. if (top.Focused is null)
  615. {
  616. top.FocusNext ();
  617. }
  618. top.SetNeedsDisplay ();
  619. Application.BringOverlappedTopToFront ();
  620. }
  621. else
  622. {
  623. Application.OverlappedMoveNext ();
  624. }
  625. }
  626. private void MovePreviousView ()
  627. {
  628. View old = GetDeepestFocusedSubview (Focused);
  629. if (!FocusPrev ())
  630. {
  631. FocusPrev ();
  632. }
  633. if (old != Focused && old != Focused?.Focused)
  634. {
  635. old?.SetNeedsDisplay ();
  636. Focused?.SetNeedsDisplay ();
  637. }
  638. else
  639. {
  640. FocusNearestView (SuperView?.TabIndexes?.Reverse (), NavigationDirection.Backward);
  641. }
  642. }
  643. private void MovePreviousViewOrTop ()
  644. {
  645. if (Application.OverlappedTop is null)
  646. {
  647. Toplevel top = Modal ? this : Application.Top;
  648. top.FocusPrev ();
  649. if (top.Focused is null)
  650. {
  651. top.FocusPrev ();
  652. }
  653. top.SetNeedsDisplay ();
  654. Application.BringOverlappedTopToFront ();
  655. }
  656. else
  657. {
  658. Application.OverlappedMovePrevious ();
  659. }
  660. }
  661. private bool OutsideTopFrame (Toplevel top)
  662. {
  663. if (top.Frame.X > Driver.Cols || top.Frame.Y > Driver.Rows)
  664. {
  665. return true;
  666. }
  667. return false;
  668. }
  669. private void QuitToplevel ()
  670. {
  671. if (Application.OverlappedTop is { })
  672. {
  673. RequestStop (this);
  674. }
  675. else
  676. {
  677. Application.RequestStop ();
  678. }
  679. }
  680. }
  681. /// <summary>
  682. /// Implements the <see cref="IEqualityComparer{T}"/> for comparing two <see cref="Toplevel"/>s used by
  683. /// <see cref="StackExtensions"/>.
  684. /// </summary>
  685. public class ToplevelEqualityComparer : IEqualityComparer<Toplevel>
  686. {
  687. /// <summary>Determines whether the specified objects are equal.</summary>
  688. /// <param name="x">The first object of type <see cref="Toplevel"/> to compare.</param>
  689. /// <param name="y">The second object of type <see cref="Toplevel"/> to compare.</param>
  690. /// <returns><see langword="true"/> if the specified objects are equal; otherwise, <see langword="false"/>.</returns>
  691. public bool Equals (Toplevel x, Toplevel y)
  692. {
  693. if (y is null && x is null)
  694. {
  695. return true;
  696. }
  697. if (x is null || y is null)
  698. {
  699. return false;
  700. }
  701. if (x.Id == y.Id)
  702. {
  703. return true;
  704. }
  705. return false;
  706. }
  707. /// <summary>Returns a hash code for the specified object.</summary>
  708. /// <param name="obj">The <see cref="Toplevel"/> for which a hash code is to be returned.</param>
  709. /// <returns>A hash code for the specified object.</returns>
  710. /// <exception cref="ArgumentNullException">
  711. /// The type of <paramref name="obj"/> is a reference type and
  712. /// <paramref name="obj"/> is <see langword="null"/>.
  713. /// </exception>
  714. public int GetHashCode (Toplevel obj)
  715. {
  716. if (obj is null)
  717. {
  718. throw new ArgumentNullException ();
  719. }
  720. var hCode = 0;
  721. if (int.TryParse (obj.Id, out int result))
  722. {
  723. hCode = result;
  724. }
  725. return hCode.GetHashCode ();
  726. }
  727. }
  728. /// <summary>
  729. /// Implements the <see cref="IComparer{T}"/> to sort the <see cref="Toplevel"/> from the
  730. /// <see cref="Application.OverlappedChildren"/> if needed.
  731. /// </summary>
  732. public sealed class ToplevelComparer : IComparer<Toplevel>
  733. {
  734. /// <summary>
  735. /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the
  736. /// other.
  737. /// </summary>
  738. /// <param name="x">The first object to compare.</param>
  739. /// <param name="y">The second object to compare.</param>
  740. /// <returns>
  741. /// A signed integer that indicates the relative values of <paramref name="x"/> and <paramref name="y"/>, as shown
  742. /// in the following table.Value Meaning Less than zero <paramref name="x"/> is less than <paramref name="y"/>.Zero
  743. /// <paramref name="x"/> equals <paramref name="y"/> .Greater than zero <paramref name="x"/> is greater than
  744. /// <paramref name="y"/>.
  745. /// </returns>
  746. public int Compare (Toplevel x, Toplevel y)
  747. {
  748. if (ReferenceEquals (x, y))
  749. {
  750. return 0;
  751. }
  752. if (x is null)
  753. {
  754. return -1;
  755. }
  756. if (y is null)
  757. {
  758. return 1;
  759. }
  760. return string.Compare (x.Id, y.Id);
  761. }
  762. }