Toplevel.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. namespace Terminal.Gui.Views;
  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. /// Toplevel views can run as modal (popup) views, started by calling
  9. /// <see cref="IApplication.Run(Toplevel, Func{Exception, bool})"/>. They return control to the caller when
  10. /// <see cref="IApplication.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="IApplication.Init"/>.
  15. /// The application Toplevel can be accessed via <see cref="IApplication.Current"/>. 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="IApplication.Run(Toplevel, Func{Exception, bool})"/>.
  18. /// </para>
  19. /// <para>
  20. /// Toplevel implements <see cref="IRunnable"/> to support the runnable session lifecycle with proper event handling
  21. /// following the Cancellable Work Pattern (CWP).
  22. /// </para>
  23. /// </remarks>
  24. public partial class Toplevel : View, IRunnable
  25. {
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="Toplevel"/> class,
  28. /// defaulting to full screen. The <see cref="View.Width"/> and <see cref="View.Height"/> properties will be set to the
  29. /// dimensions of the terminal using <see cref="Dim.Fill(Dim)"/>.
  30. /// </summary>
  31. public Toplevel ()
  32. {
  33. CanFocus = true;
  34. TabStop = TabBehavior.TabGroup;
  35. Arrangement = ViewArrangement.Overlapped;
  36. Width = Dim.Fill ();
  37. Height = Dim.Fill ();
  38. SchemeName = SchemeManager.SchemesToSchemeName (Schemes.Toplevel);
  39. MouseClick += Toplevel_MouseClick;
  40. }
  41. #region Keyboard & Mouse
  42. // TODO: IRunnable: Re-implement - Modal means IRunnable, ViewArrangement.Overlapped where modalView.Z > allOtherViews.Max (v = v.Z), and exclusive key/mouse input.
  43. /// <summary>
  44. /// Determines whether the <see cref="Toplevel"/> is modal or not. If set to <c>false</c> (the default):
  45. /// <list type="bullet">
  46. /// <item>
  47. /// <description><see cref="View.OnKeyDown"/> events will propagate keys upwards.</description>
  48. /// </item>
  49. /// <item>
  50. /// <description>The Toplevel will act as an embedded view (not a modal/pop-up).</description>
  51. /// </item>
  52. /// </list>
  53. /// If set to <c>true</c>:
  54. /// <list type="bullet">
  55. /// <item>
  56. /// <description><see cref="View.OnKeyDown"/> events will NOT propagate keys upwards.</description>
  57. /// </item>
  58. /// <item>
  59. /// <description>The Toplevel will and look like a modal (pop-up) (e.g. see <see cref="Dialog"/>.</description>
  60. /// </item>
  61. /// </list>
  62. /// </summary>
  63. public bool Modal { get; set; }
  64. private void Toplevel_MouseClick (object? sender, MouseEventArgs e) { e.Handled = InvokeCommand (Command.HotKey) == true; }
  65. #endregion
  66. #region SubViews
  67. // TODO: Deprecate - Any view can host a menubar in v2
  68. /// <summary>Gets the latest <see cref="MenuBar"/> added into this Toplevel.</summary>
  69. public MenuBar? MenuBar => (MenuBar?)SubViews?.LastOrDefault (s => s is MenuBar);
  70. //// TODO: Deprecate - Any view can host a statusbar in v2
  71. ///// <summary>Gets the latest <see cref="StatusBar"/> added into this Toplevel.</summary>
  72. //public StatusBar? StatusBar => (StatusBar?)SubViews?.LastOrDefault (s => s is StatusBar);
  73. #endregion
  74. #region Life Cycle
  75. // TODO: IRunnable: Re-implement as a property on IRunnable
  76. /// <summary>Gets or sets whether the main loop for this <see cref="Toplevel"/> is running or not.</summary>
  77. /// <remarks>Setting this property directly is discouraged. Use <see cref="IApplication.RequestStop()"/> instead.</remarks>
  78. public bool Running { get; set; }
  79. // TODO: IRunnable: Re-implement in IRunnable
  80. /// <summary>
  81. /// <see langword="true"/> if was already loaded by the <see cref="IApplication.Begin(Toplevel)"/>
  82. /// <see langword="false"/>, otherwise.
  83. /// </summary>
  84. public bool IsLoaded { get; private set; }
  85. /// <summary>Invoked when the Toplevel <see cref="SessionToken"/> active.</summary>
  86. /// <remarks>
  87. /// <para>
  88. /// <b>Obsolete:</b> Use <see cref="IRunnable.Activated"/> instead. This event is maintained for backward
  89. /// compatibility and will be raised alongside the new <see cref="IRunnable.Activated"/> event.
  90. /// </para>
  91. /// </remarks>
  92. [Obsolete ("Use IRunnable.Activated instead. This event is maintained for backward compatibility.")]
  93. public event EventHandler<ToplevelEventArgs>? Activate;
  94. /// <summary>Invoked when the Toplevel<see cref="SessionToken"/> ceases to be active.</summary>
  95. /// <remarks>
  96. /// <para>
  97. /// <b>Obsolete:</b> Use <see cref="IRunnable.Deactivated"/> instead. This event is maintained for backward
  98. /// compatibility and will be raised alongside the new <see cref="IRunnable.Deactivated"/> event.
  99. /// </para>
  100. /// </remarks>
  101. [Obsolete ("Use IRunnable.Deactivated instead. This event is maintained for backward compatibility.")]
  102. public event EventHandler<ToplevelEventArgs>? Deactivate;
  103. /// <summary>Invoked when the Toplevel's <see cref="SessionToken"/> is closed by <see cref="IApplication.End(SessionToken)"/>.</summary>
  104. /// <remarks>
  105. /// <para>
  106. /// <b>Obsolete:</b> This event is maintained for backward compatibility. The IRunnable architecture
  107. /// combines this functionality into the <see cref="IRunnable.Stopped"/> event.
  108. /// </para>
  109. /// </remarks>
  110. [Obsolete ("Use IRunnable.Stopped instead. This event is maintained for backward compatibility.")]
  111. public event EventHandler<ToplevelEventArgs>? Closed;
  112. /// <summary>
  113. /// Invoked when the Toplevel's <see cref="SessionToken"/> is being closed by
  114. /// <see cref="IApplication.RequestStop(Toplevel)"/>.
  115. /// </summary>
  116. /// <remarks>
  117. /// <para>
  118. /// <b>Obsolete:</b> Use <see cref="IRunnable.Stopping"/> instead. This event is maintained for backward
  119. /// compatibility and will be raised alongside the new <see cref="IRunnable.Stopping"/> event.
  120. /// </para>
  121. /// </remarks>
  122. [Obsolete ("Use IRunnable.Stopping instead. This event is maintained for backward compatibility.")]
  123. public event EventHandler<ToplevelClosingEventArgs>? Closing;
  124. /// <summary>
  125. /// Invoked when the <see cref="Toplevel"/> <see cref="SessionToken"/> has begun to be loaded. A Loaded event handler
  126. /// is a good place to finalize initialization before calling Run.
  127. /// </summary>
  128. /// <remarks>
  129. /// <para>
  130. /// <b>Obsolete:</b> Use <see cref="View.Initialized"/> instead. The Loaded event conceptually maps to
  131. /// the Initialized event from the ISupportInitialize pattern, which is now part of IRunnable.
  132. /// </para>
  133. /// </remarks>
  134. [Obsolete ("Use View.Initialized instead. The Loaded event maps to the Initialized event from ISupportInitialize.")]
  135. public event EventHandler? Loaded;
  136. /// <summary>
  137. /// Called from <see cref="IApplication.Begin(Toplevel)"/> before the <see cref="Toplevel"/> redraws for the first
  138. /// time.
  139. /// </summary>
  140. /// <remarks>
  141. /// Overrides must call base.OnLoaded() to ensure any Toplevel subviews are initialized properly and the
  142. /// <see cref="Loaded"/> event is raised.
  143. /// </remarks>
  144. public virtual void OnLoaded ()
  145. {
  146. IsLoaded = true;
  147. foreach (var view in SubViews.Where (v => v is Toplevel))
  148. {
  149. var tl = (Toplevel)view;
  150. tl.OnLoaded ();
  151. }
  152. Loaded?.Invoke (this, EventArgs.Empty);
  153. }
  154. /// <summary>
  155. /// Invoked when the <see cref="Toplevel"/> main loop has started it's first iteration. Subscribe to this event to
  156. /// perform tasks when the <see cref="Toplevel"/> has been laid out and focus has been set. changes.
  157. /// <para>
  158. /// A Ready event handler is a good place to finalize initialization after calling
  159. /// <see cref="IApplication.Run(Toplevel, Func{Exception, bool})"/> on this <see cref="Toplevel"/>.
  160. /// </para>
  161. /// </summary>
  162. /// <remarks>
  163. /// <para>
  164. /// <b>Obsolete:</b> Use <see cref="View.Initialized"/> instead. The Ready event is similar to Initialized
  165. /// but was fired later in the lifecycle. The IRunnable architecture consolidates these into Initialized.
  166. /// </para>
  167. /// </remarks>
  168. [Obsolete ("Use View.Initialized instead. The Ready event is consolidated into the Initialized event.")]
  169. public event EventHandler? Ready;
  170. /// <summary>
  171. /// Stops and closes this <see cref="Toplevel"/>. If this Toplevel is the top-most Toplevel,
  172. /// <see cref="IApplication.RequestStop(Toplevel)"/> will be called, causing the application to exit.
  173. /// </summary>
  174. public virtual void RequestStop ()
  175. {
  176. App?.RequestStop (App?.Current);
  177. }
  178. /// <summary>
  179. /// Invoked when the Toplevel <see cref="SessionToken"/> has been unloaded. A Unloaded event handler is a good place
  180. /// to dispose objects after calling <see cref="IApplication.End(SessionToken)"/>.
  181. /// </summary>
  182. /// <remarks>
  183. /// <para>
  184. /// <b>Obsolete:</b> Use <see cref="IRunnable.Stopped"/> instead. The Unloaded event is consolidated into
  185. /// the Stopped event in the IRunnable architecture.
  186. /// </para>
  187. /// </remarks>
  188. [Obsolete ("Use IRunnable.Stopped instead. The Unloaded event is consolidated into the Stopped event.")]
  189. public event EventHandler? Unloaded;
  190. internal virtual void OnActivate (Toplevel deactivated) { Activate?.Invoke (this, new (deactivated)); }
  191. internal virtual void OnClosed (Toplevel top) { Closed?.Invoke (this, new (top)); }
  192. internal virtual bool OnClosing (ToplevelClosingEventArgs ev)
  193. {
  194. Closing?.Invoke (this, ev);
  195. return ev.Cancel;
  196. }
  197. internal virtual void OnDeactivate (Toplevel activated) { Deactivate?.Invoke (this, new (activated)); }
  198. /// <summary>
  199. /// Called from run loop after the <see cref="Toplevel"/> has entered the first iteration
  200. /// of the loop.
  201. /// </summary>
  202. internal virtual void OnReady ()
  203. {
  204. foreach (var view in SubViews.Where (v => v is Toplevel))
  205. {
  206. var tl = (Toplevel)view;
  207. tl.OnReady ();
  208. }
  209. Ready?.Invoke (this, EventArgs.Empty);
  210. }
  211. /// <summary>Called from <see cref="IApplication.End(SessionToken)"/> before the <see cref="Toplevel"/> is disposed.</summary>
  212. internal virtual void OnUnloaded ()
  213. {
  214. foreach (var view in SubViews.Where (v => v is Toplevel))
  215. {
  216. var tl = (Toplevel)view;
  217. tl.OnUnloaded ();
  218. }
  219. Unloaded?.Invoke (this, EventArgs.Empty);
  220. }
  221. #endregion
  222. #region IRunnable Implementation
  223. // Note: Running property is already defined above in the Life Cycle region (line 86)
  224. // Note: Initializing and Initialized events are inherited from View (ISupportInitialize pattern)
  225. /// <inheritdoc/>
  226. public event EventHandler<System.ComponentModel.CancelEventArgs>? Stopping;
  227. /// <inheritdoc/>
  228. public event EventHandler? Stopped;
  229. /// <inheritdoc/>
  230. public event EventHandler<RunnableActivatingEventArgs>? Activating;
  231. /// <inheritdoc/>
  232. public event EventHandler<RunnableEventArgs>? Activated;
  233. /// <inheritdoc/>
  234. public event EventHandler<RunnableDeactivatingEventArgs>? Deactivating;
  235. /// <inheritdoc/>
  236. public event EventHandler<RunnableEventArgs>? Deactivated;
  237. /// <inheritdoc/>
  238. public virtual void RaiseStoppingEvent ()
  239. {
  240. // CWP Phase 1: Pre-notification via virtual method (can cancel)
  241. if (OnStopping ())
  242. {
  243. return; // Stopping canceled
  244. }
  245. // CWP Phase 2: Event notification (can cancel)
  246. var args = new System.ComponentModel.CancelEventArgs ();
  247. Stopping?.Invoke (this, args);
  248. if (args.Cancel)
  249. {
  250. return; // Stopping canceled
  251. }
  252. // CWP Phase 3: Perform the work (stop the session)
  253. Running = false;
  254. // CWP Phase 4: Post-notification via virtual method
  255. OnStopped ();
  256. // CWP Phase 5: Post-notification event
  257. Stopped?.Invoke (this, EventArgs.Empty);
  258. }
  259. /// <inheritdoc/>
  260. public virtual bool RaiseActivatingEvent (IRunnable? deactivated)
  261. {
  262. // CWP Phase 1: Pre-notification via virtual method (can cancel)
  263. if (OnActivating (deactivated))
  264. {
  265. return true; // Activation canceled
  266. }
  267. // CWP Phase 2: Event notification (can cancel)
  268. var args = new RunnableActivatingEventArgs (this, deactivated);
  269. Activating?.Invoke (this, args);
  270. if (args.Cancel)
  271. {
  272. return true; // Activation canceled
  273. }
  274. // CWP Phase 3: Work is done by Application (setting Current)
  275. // CWP Phase 4 & 5: Call post-notification methods
  276. OnActivated (deactivated);
  277. return false; // Activation succeeded
  278. }
  279. /// <inheritdoc/>
  280. public virtual void RaiseActivatedEvent (IRunnable? deactivated)
  281. {
  282. Activated?.Invoke (this, new RunnableEventArgs (this));
  283. }
  284. /// <inheritdoc/>
  285. public virtual bool RaiseDeactivatingEvent (IRunnable? activated)
  286. {
  287. // CWP Phase 1: Pre-notification via virtual method (can cancel)
  288. if (OnDeactivating (activated))
  289. {
  290. return true; // Deactivation canceled
  291. }
  292. // CWP Phase 2: Event notification (can cancel)
  293. var args = new RunnableDeactivatingEventArgs (this, activated);
  294. Deactivating?.Invoke (this, args);
  295. if (args.Cancel)
  296. {
  297. return true; // Deactivation canceled
  298. }
  299. // CWP Phase 3: Work is done by Application (changing Current)
  300. // CWP Phase 4 & 5: Call post-notification methods
  301. OnDeactivated (activated);
  302. return false; // Deactivation succeeded
  303. }
  304. /// <inheritdoc/>
  305. public virtual void RaiseDeactivatedEvent (IRunnable? activated)
  306. {
  307. Deactivated?.Invoke (this, new RunnableEventArgs (this));
  308. }
  309. /// <summary>
  310. /// Called before <see cref="Stopping"/> event. Override to cancel stopping.
  311. /// </summary>
  312. /// <returns><see langword="true"/> to cancel; <see langword="false"/> to proceed.</returns>
  313. /// <remarks>
  314. /// <para>
  315. /// This is the first phase of the Cancellable Work Pattern for stopping.
  316. /// Default implementation calls the legacy <see cref="OnClosing"/> method for backward compatibility.
  317. /// </para>
  318. /// </remarks>
  319. protected virtual bool OnStopping ()
  320. {
  321. // For backward compatibility, delegate to legacy OnClosing method
  322. var ev = new ToplevelClosingEventArgs (this);
  323. return OnClosing (ev);
  324. }
  325. /// <summary>
  326. /// Called after session has stopped. Override for post-stop cleanup.
  327. /// </summary>
  328. /// <remarks>
  329. /// Default implementation does nothing. For backward compatibility, the legacy <see cref="Closed"/>
  330. /// event is raised by Application.End().
  331. /// </remarks>
  332. protected virtual void OnStopped ()
  333. {
  334. // Default: do nothing
  335. // Note: Legacy Closed event is raised by Application.End()
  336. }
  337. /// <summary>
  338. /// Called before <see cref="Activating"/> event. Override to cancel activation.
  339. /// </summary>
  340. /// <param name="deactivated">The previously active runnable being deactivated, or null if none.</param>
  341. /// <returns><see langword="true"/> to cancel; <see langword="false"/> to proceed.</returns>
  342. /// <remarks>
  343. /// Default implementation returns false (allow activation). For backward compatibility,
  344. /// the legacy <see cref="OnActivate"/> method is called after activation succeeds.
  345. /// </remarks>
  346. protected virtual bool OnActivating (IRunnable? deactivated)
  347. {
  348. return false; // Default: allow activation
  349. }
  350. /// <summary>
  351. /// Called after activation succeeds. Override for post-activation logic.
  352. /// </summary>
  353. /// <param name="deactivated">The previously active runnable that was deactivated, or null if none.</param>
  354. /// <remarks>
  355. /// Default implementation raises the <see cref="Activated"/> event and calls the legacy
  356. /// <see cref="OnActivate"/> method for backward compatibility.
  357. /// </remarks>
  358. protected virtual void OnActivated (IRunnable? deactivated)
  359. {
  360. RaiseActivatedEvent (deactivated);
  361. // For backward compatibility, call legacy OnActivate if deactivated is a Toplevel
  362. if (deactivated is Toplevel tl)
  363. {
  364. OnActivate (tl);
  365. }
  366. else
  367. {
  368. // If not a Toplevel, still raise the legacy Activate event with null
  369. Activate?.Invoke (this, new ToplevelEventArgs (null));
  370. }
  371. }
  372. /// <summary>
  373. /// Called before <see cref="Deactivating"/> event. Override to cancel deactivation.
  374. /// </summary>
  375. /// <param name="activated">The newly activated runnable, or null if none.</param>
  376. /// <returns><see langword="true"/> to cancel; <see langword="false"/> to proceed.</returns>
  377. /// <remarks>
  378. /// Default implementation returns false (allow deactivation).
  379. /// </remarks>
  380. protected virtual bool OnDeactivating (IRunnable? activated)
  381. {
  382. return false; // Default: allow deactivation
  383. }
  384. /// <summary>
  385. /// Called after deactivation succeeds. Override for post-deactivation logic.
  386. /// </summary>
  387. /// <param name="activated">The newly activated runnable, or null if none.</param>
  388. /// <remarks>
  389. /// Default implementation raises the <see cref="Deactivated"/> event and calls the legacy
  390. /// <see cref="OnDeactivate"/> method for backward compatibility.
  391. /// </remarks>
  392. protected virtual void OnDeactivated (IRunnable? activated)
  393. {
  394. RaiseDeactivatedEvent (activated);
  395. // For backward compatibility, call legacy OnDeactivate if activated is a Toplevel
  396. if (activated is Toplevel tl)
  397. {
  398. OnDeactivate (tl);
  399. }
  400. else
  401. {
  402. // If not a Toplevel, still raise the legacy Deactivate event with null
  403. Deactivate?.Invoke (this, new ToplevelEventArgs (null));
  404. }
  405. }
  406. #endregion
  407. #region Size / Position Management
  408. // TODO: Make cancelable?
  409. internal void OnSizeChanging (SizeChangedEventArgs size) { SizeChanging?.Invoke (this, size); }
  410. /// <summary>
  411. /// Adjusts the location and size of <paramref name="top"/> within this Toplevel. Virtual method enabling
  412. /// implementation of specific positions for inherited <see cref="Toplevel"/> views.
  413. /// </summary>
  414. /// <param name="top">The Toplevel to adjust.</param>
  415. public virtual void PositionToplevel (Toplevel? top)
  416. {
  417. if (top is null)
  418. {
  419. return;
  420. }
  421. View? superView = GetLocationEnsuringFullVisibility (
  422. top,
  423. top.Frame.X,
  424. top.Frame.Y,
  425. out int nx,
  426. out int ny
  427. //,
  428. // out StatusBar? sb
  429. );
  430. if (superView is null)
  431. {
  432. return;
  433. }
  434. //var layoutSubViews = false;
  435. var maxWidth = 0;
  436. if (superView.Margin is { } && superView == top.SuperView)
  437. {
  438. maxWidth -= superView.GetAdornmentsThickness ().Left + superView.GetAdornmentsThickness ().Right;
  439. }
  440. // BUGBUG: The && true is a temp hack
  441. if ((superView != top || top?.SuperView is { } || (top != App?.Current && top!.Modal) || (top == App?.Current && top?.SuperView is null))
  442. && (top!.Frame.X + top.Frame.Width > maxWidth || ny > top.Frame.Y))
  443. {
  444. if (top?.X is null or PosAbsolute && top?.Frame.X != nx)
  445. {
  446. top!.X = nx;
  447. //layoutSubViews = true;
  448. }
  449. if (top?.Y is null or PosAbsolute && top?.Frame.Y != ny)
  450. {
  451. top!.Y = ny;
  452. //layoutSubViews = true;
  453. }
  454. }
  455. //if (superView.IsLayoutNeeded () || layoutSubViews)
  456. //{
  457. // superView.LayoutSubViews ();
  458. //}
  459. //if (IsLayoutNeeded ())
  460. //{
  461. // LayoutSubViews ();
  462. //}
  463. }
  464. /// <summary>Invoked when the terminal has been resized. The new <see cref="Size"/> of the terminal is provided.</summary>
  465. public event EventHandler<SizeChangedEventArgs>? SizeChanging;
  466. #endregion
  467. }
  468. /// <summary>
  469. /// Implements the <see cref="IEqualityComparer{T}"/> for comparing two <see cref="Toplevel"/>s used by
  470. /// <see cref="StackExtensions"/>.
  471. /// </summary>
  472. public class ToplevelEqualityComparer : IEqualityComparer<Toplevel>
  473. {
  474. /// <summary>Determines whether the specified objects are equal.</summary>
  475. /// <param name="x">The first object of type <see cref="Toplevel"/> to compare.</param>
  476. /// <param name="y">The second object of type <see cref="Toplevel"/> to compare.</param>
  477. /// <returns><see langword="true"/> if the specified objects are equal; otherwise, <see langword="false"/>.</returns>
  478. public bool Equals (Toplevel? x, Toplevel? y)
  479. {
  480. if (y is null && x is null)
  481. {
  482. return true;
  483. }
  484. if (x is null || y is null)
  485. {
  486. return false;
  487. }
  488. if (x.Id == y.Id)
  489. {
  490. return true;
  491. }
  492. return false;
  493. }
  494. /// <summary>Returns a hash code for the specified object.</summary>
  495. /// <param name="obj">The <see cref="Toplevel"/> for which a hash code is to be returned.</param>
  496. /// <returns>A hash code for the specified object.</returns>
  497. /// <exception cref="ArgumentNullException">
  498. /// The type of <paramref name="obj"/> is a reference type and
  499. /// <paramref name="obj"/> is <see langword="null"/>.
  500. /// </exception>
  501. public int GetHashCode (Toplevel obj)
  502. {
  503. if (obj is null)
  504. {
  505. throw new ArgumentNullException ();
  506. }
  507. var hCode = 0;
  508. if (int.TryParse (obj.Id, out int result))
  509. {
  510. hCode = result;
  511. }
  512. return hCode.GetHashCode ();
  513. }
  514. }