Toplevel.cs 29 KB

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