Toplevel.cs 29 KB

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