Toplevel.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. #nullable enable
  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. /// Toplevel views can run as modal (popup) views, started by calling
  10. /// <see cref="Application.Run(Toplevel, Func{Exception, bool})"/>. 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})"/>.
  19. /// </para>
  20. /// </remarks>
  21. public partial class Toplevel : View
  22. {
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="Toplevel"/> class,
  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. CanFocus = true;
  31. TabStop = TabBehavior.TabGroup;
  32. Arrangement = ViewArrangement.Fixed;
  33. Width = Dim.Fill ();
  34. Height = Dim.Fill ();
  35. ColorScheme = Colors.ColorSchemes ["TopLevel"];
  36. MouseClick += Toplevel_MouseClick;
  37. }
  38. #region Keyboard & Mouse
  39. // TODO: IRunnable: Re-implement - Modal means IRunnable, ViewArrangement.Overlapped where modalView.Z > allOtherViews.Max (v = v.Z), and exclusive key/mouse input.
  40. /// <summary>
  41. /// Determines whether the <see cref="Toplevel"/> is modal or not. If set to <c>false</c> (the default):
  42. /// <list type="bullet">
  43. /// <item>
  44. /// <description><see cref="View.OnKeyDown"/> events will propagate keys upwards.</description>
  45. /// </item>
  46. /// <item>
  47. /// <description>The Toplevel will act as an embedded view (not a modal/pop-up).</description>
  48. /// </item>
  49. /// </list>
  50. /// If set to <c>true</c>:
  51. /// <list type="bullet">
  52. /// <item>
  53. /// <description><see cref="View.OnKeyDown"/> events will NOT propagate keys upwards.</description>
  54. /// </item>
  55. /// <item>
  56. /// <description>The Toplevel will and look like a modal (pop-up) (e.g. see <see cref="Dialog"/>.</description>
  57. /// </item>
  58. /// </list>
  59. /// </summary>
  60. public bool Modal { get; set; }
  61. private void Toplevel_MouseClick (object? sender, MouseEventEventArgs e) { e.Handled = InvokeCommand (Command.HotKey) == true; }
  62. #endregion
  63. #region Subviews
  64. // TODO: Deprecate - Any view can host a menubar in v2
  65. /// <summary>Gets or sets the menu for this Toplevel.</summary>
  66. public MenuBar? MenuBar { get; set; }
  67. // TODO: Deprecate - Any view can host a statusbar in v2
  68. /// <summary>Gets or sets the status bar for this Toplevel.</summary>
  69. public StatusBar? StatusBar { get; set; }
  70. /// <inheritdoc/>
  71. public override View Add (View view)
  72. {
  73. AddMenuStatusBar (view);
  74. return base.Add (view);
  75. }
  76. /// <inheritdoc/>
  77. public override View Remove (View view)
  78. {
  79. if (this is Toplevel { MenuBar: { } })
  80. {
  81. RemoveMenuStatusBar (view);
  82. }
  83. return base.Remove (view);
  84. }
  85. /// <inheritdoc/>
  86. public override void RemoveAll ()
  87. {
  88. if (this == Application.Top)
  89. {
  90. MenuBar?.Dispose ();
  91. MenuBar = null;
  92. StatusBar?.Dispose ();
  93. StatusBar = null;
  94. }
  95. base.RemoveAll ();
  96. }
  97. internal void AddMenuStatusBar (View view)
  98. {
  99. if (view is MenuBar)
  100. {
  101. MenuBar = view as MenuBar;
  102. }
  103. if (view is StatusBar)
  104. {
  105. StatusBar = view as StatusBar;
  106. }
  107. }
  108. internal void RemoveMenuStatusBar (View view)
  109. {
  110. if (view is MenuBar)
  111. {
  112. MenuBar?.Dispose ();
  113. MenuBar = null;
  114. }
  115. if (view is StatusBar)
  116. {
  117. StatusBar?.Dispose ();
  118. StatusBar = null;
  119. }
  120. }
  121. // TODO: Overlapped - Rename to AllSubviewsClosed - Move to View?
  122. /// <summary>
  123. /// Invoked when the last child of the Toplevel <see cref="RunState"/> is closed from by
  124. /// <see cref="Application.End(RunState)"/>.
  125. /// </summary>
  126. public event EventHandler? AllChildClosed;
  127. // TODO: Overlapped - Rename to *Subviews* - Move to View?
  128. /// <summary>
  129. /// Invoked when a child of the Toplevel <see cref="RunState"/> is closed by
  130. /// <see cref="Application.End(RunState)"/>.
  131. /// </summary>
  132. public event EventHandler<ToplevelEventArgs>? ChildClosed;
  133. // TODO: Overlapped - Rename to *Subviews* - Move to View?
  134. /// <summary>Invoked when a child Toplevel's <see cref="RunState"/> has been loaded.</summary>
  135. public event EventHandler<ToplevelEventArgs>? ChildLoaded;
  136. // TODO: Overlapped - Rename to *Subviews* - Move to View?
  137. /// <summary>Invoked when a cjhild Toplevel's <see cref="RunState"/> has been unloaded.</summary>
  138. public event EventHandler<ToplevelEventArgs>? ChildUnloaded;
  139. #endregion
  140. #region Life Cycle
  141. // TODO: IRunnable: Re-implement as a property on IRunnable
  142. /// <summary>Gets or sets whether the main loop for this <see cref="Toplevel"/> is running or not.</summary>
  143. /// <remarks>Setting this property directly is discouraged. Use <see cref="Application.RequestStop"/> instead.</remarks>
  144. public bool Running { get; set; }
  145. // TODO: IRunnable: Re-implement in IRunnable
  146. /// <summary>
  147. /// <see langword="true"/> if was already loaded by the <see cref="Application.Begin(Toplevel)"/>
  148. /// <see langword="false"/>, otherwise.
  149. /// </summary>
  150. public bool IsLoaded { get; private set; }
  151. // TODO: IRunnable: Re-implement as an event on IRunnable; IRunnable.Activating/Activate
  152. /// <summary>Invoked when the Toplevel <see cref="RunState"/> becomes the <see cref="Application.Current"/> Toplevel.</summary>
  153. public event EventHandler<ToplevelEventArgs>? Activate;
  154. // TODO: IRunnable: Re-implement as an event on IRunnable; IRunnable.Deactivating/Deactivate?
  155. /// <summary>Invoked when the Toplevel<see cref="RunState"/> ceases to be the <see cref="Application.Current"/> Toplevel.</summary>
  156. public event EventHandler<ToplevelEventArgs>? Deactivate;
  157. /// <summary>Invoked when the Toplevel's <see cref="RunState"/> is closed by <see cref="Application.End(RunState)"/>.</summary>
  158. public event EventHandler<ToplevelEventArgs>? Closed;
  159. /// <summary>
  160. /// Invoked when the Toplevel's <see cref="RunState"/> is being closed by
  161. /// <see cref="Application.RequestStop(Toplevel)"/>.
  162. /// </summary>
  163. public event EventHandler<ToplevelClosingEventArgs>? Closing;
  164. /// <summary>
  165. /// Invoked when the <see cref="Toplevel"/> <see cref="RunState"/> has begun to be loaded. A Loaded event handler
  166. /// is a good place to finalize initialization before calling <see cref="Application.RunLoop(RunState)"/>.
  167. /// </summary>
  168. public event EventHandler? Loaded;
  169. /// <summary>
  170. /// Called from <see cref="Application.Begin(Toplevel)"/> before the <see cref="Toplevel"/> redraws for the first
  171. /// time.
  172. /// </summary>
  173. /// <remarks>
  174. /// Overrides must call base.OnLoaded() to ensure any Toplevel subviews are initialized properly and the
  175. /// <see cref="Loaded"/> event is raised.
  176. /// </remarks>
  177. public virtual void OnLoaded ()
  178. {
  179. IsLoaded = true;
  180. foreach (var view in Subviews.Where (v => v is Toplevel))
  181. {
  182. var tl = (Toplevel)view;
  183. tl.OnLoaded ();
  184. }
  185. Loaded?.Invoke (this, EventArgs.Empty);
  186. }
  187. /// <summary>
  188. /// Invoked when the <see cref="Toplevel"/> main loop has started it's first iteration. Subscribe to this event to
  189. /// perform tasks when the <see cref="Toplevel"/> has been laid out and focus has been set. changes.
  190. /// <para>
  191. /// A Ready event handler is a good place to finalize initialization after calling
  192. /// <see cref="Application.Run(Toplevel, Func{Exception, bool})"/> on this <see cref="Toplevel"/>.
  193. /// </para>
  194. /// </summary>
  195. public event EventHandler? Ready;
  196. /// <summary>
  197. /// Stops and closes this <see cref="Toplevel"/>. If this Toplevel is the top-most Toplevel,
  198. /// <see cref="Application.RequestStop(Toplevel)"/> will be called, causing the application to exit.
  199. /// </summary>
  200. public virtual void RequestStop ()
  201. {
  202. if (IsOverlappedContainer
  203. && Running
  204. && (Application.Current == this
  205. || Application.Current?.Modal == false
  206. || (Application.Current?.Modal == true && Application.Current?.Running == false)))
  207. {
  208. foreach (Toplevel child in ApplicationOverlapped.OverlappedChildren!)
  209. {
  210. var ev = new ToplevelClosingEventArgs (this);
  211. if (child.OnClosing (ev))
  212. {
  213. return;
  214. }
  215. child.Running = false;
  216. Application.RequestStop (child);
  217. }
  218. Running = false;
  219. Application.RequestStop (this);
  220. }
  221. else if (IsOverlappedContainer && Running && Application.Current?.Modal == true && Application.Current?.Running == true)
  222. {
  223. var ev = new ToplevelClosingEventArgs (Application.Current);
  224. if (OnClosing (ev))
  225. {
  226. return;
  227. }
  228. Application.RequestStop (Application.Current);
  229. }
  230. else if (!IsOverlappedContainer && Running && (!Modal || (Modal && Application.Current != this)))
  231. {
  232. var ev = new ToplevelClosingEventArgs (this);
  233. if (OnClosing (ev))
  234. {
  235. return;
  236. }
  237. Running = false;
  238. Application.RequestStop (this);
  239. }
  240. else
  241. {
  242. Application.RequestStop (Application.Current);
  243. }
  244. }
  245. /// <summary>
  246. /// Invoked when the Toplevel <see cref="RunState"/> has been unloaded. A Unloaded event handler is a good place
  247. /// to dispose objects after calling <see cref="Application.End(RunState)"/>.
  248. /// </summary>
  249. public event EventHandler? Unloaded;
  250. internal virtual void OnActivate (Toplevel deactivated) { Activate?.Invoke (this, new (deactivated)); }
  251. /// <summary>
  252. /// Stops and closes the <see cref="Toplevel"/> specified by <paramref name="top"/>. If <paramref name="top"/> is
  253. /// the top-most Toplevel, <see cref="Application.RequestStop(Toplevel)"/> will be called, causing the application to
  254. /// exit.
  255. /// </summary>
  256. /// <param name="top">The Toplevel to request stop.</param>
  257. public virtual void RequestStop (Toplevel top) { top.RequestStop (); }
  258. internal virtual void OnAllChildClosed () { AllChildClosed?.Invoke (this, EventArgs.Empty); }
  259. internal virtual void OnChildClosed (Toplevel top)
  260. {
  261. if (IsOverlappedContainer)
  262. {
  263. SetSubViewNeedsDisplay ();
  264. }
  265. ChildClosed?.Invoke (this, new (top));
  266. }
  267. internal virtual void OnChildLoaded (Toplevel top) { ChildLoaded?.Invoke (this, new (top)); }
  268. internal virtual void OnChildUnloaded (Toplevel top) { ChildUnloaded?.Invoke (this, new (top)); }
  269. internal virtual void OnClosed (Toplevel top) { Closed?.Invoke (this, new (top)); }
  270. internal virtual bool OnClosing (ToplevelClosingEventArgs ev)
  271. {
  272. Closing?.Invoke (this, ev);
  273. return ev.Cancel;
  274. }
  275. internal virtual void OnDeactivate (Toplevel activated) { Deactivate?.Invoke (this, new (activated)); }
  276. /// <summary>
  277. /// Called from <see cref="Application.RunLoop"/> after the <see cref="Toplevel"/> has entered the first iteration
  278. /// of the loop.
  279. /// </summary>
  280. internal virtual void OnReady ()
  281. {
  282. foreach (var view in Subviews.Where (v => v is Toplevel))
  283. {
  284. var tl = (Toplevel)view;
  285. tl.OnReady ();
  286. }
  287. Ready?.Invoke (this, EventArgs.Empty);
  288. }
  289. /// <summary>Called from <see cref="Application.End(RunState)"/> before the <see cref="Toplevel"/> is disposed.</summary>
  290. internal virtual void OnUnloaded ()
  291. {
  292. foreach (var view in Subviews.Where (v => v is Toplevel))
  293. {
  294. var tl = (Toplevel)view;
  295. tl.OnUnloaded ();
  296. }
  297. Unloaded?.Invoke (this, EventArgs.Empty);
  298. }
  299. #endregion
  300. #region Draw
  301. /// <inheritdoc/>
  302. public override void OnDrawContent (Rectangle viewport)
  303. {
  304. if (!Visible)
  305. {
  306. return;
  307. }
  308. if (NeedsDisplay || SubViewNeedsDisplay /*|| LayoutNeeded*/)
  309. {
  310. Clear ();
  311. //LayoutSubviews ();
  312. PositionToplevels ();
  313. if (this == ApplicationOverlapped.OverlappedTop)
  314. {
  315. // This enables correct draw behavior when switching between overlapped subviews
  316. foreach (Toplevel top in ApplicationOverlapped.OverlappedChildren!.AsEnumerable ().Reverse ())
  317. {
  318. if (top.Frame.IntersectsWith (Viewport))
  319. {
  320. if (top != this && !top.IsCurrentTop && !OutsideTopFrame (top) && top.Visible)
  321. {
  322. top.SetNeedsLayout ();
  323. top.SetNeedsDisplay (top.Viewport);
  324. top.Draw ();
  325. top.OnRenderLineCanvas ();
  326. }
  327. }
  328. }
  329. }
  330. // BUGBUG: This appears to be a hack to get ScrollBarViews to render correctly.
  331. foreach (View view in Subviews)
  332. {
  333. if (view.Frame.IntersectsWith (Viewport) && !OutsideTopFrame (this))
  334. {
  335. //view.SetNeedsLayout ();
  336. view.SetNeedsDisplay ();
  337. view.SetSubViewNeedsDisplay ();
  338. }
  339. }
  340. base.OnDrawContent (viewport);
  341. }
  342. }
  343. #endregion
  344. #region Navigation
  345. ///// <inheritdoc/>
  346. //protected override bool OnEnter (View view)
  347. //{
  348. // return MostFocused?.OnEnter (view) ?? false;
  349. //}
  350. ///// <inheritdoc/>
  351. //protected override void OnLeave (View view) { return MostFocused?.OnLeave (view) ?? false; }
  352. #endregion
  353. #region Size / Position Management
  354. // TODO: Make cancelable?
  355. internal virtual void OnSizeChanging (SizeChangedEventArgs size) { SizeChanging?.Invoke (this, size); }
  356. /// <inheritdoc/>
  357. public override Point? PositionCursor ()
  358. {
  359. if (!IsOverlappedContainer)
  360. {
  361. if (GetFocused () is null)
  362. {
  363. RestoreFocus (null);
  364. }
  365. return null;
  366. }
  367. // This code path only happens when the Toplevel is an Overlapped container
  368. if (GetFocused () is null)
  369. {
  370. // TODO: this is an Overlapped hack
  371. foreach (Toplevel top in ApplicationOverlapped.OverlappedChildren!)
  372. {
  373. if (top != this && top.Visible)
  374. {
  375. top.SetFocus ();
  376. return null;
  377. }
  378. }
  379. }
  380. Point? cursor2 = base.PositionCursor ();
  381. return null;
  382. }
  383. /// <summary>
  384. /// Adjusts the location and size of <paramref name="top"/> within this Toplevel. Virtual method enabling
  385. /// implementation of specific positions for inherited <see cref="Toplevel"/> views.
  386. /// </summary>
  387. /// <param name="top">The Toplevel to adjust.</param>
  388. public virtual void PositionToplevel (Toplevel? top)
  389. {
  390. if (top is null)
  391. {
  392. return;
  393. }
  394. View? superView = GetLocationEnsuringFullVisibility (
  395. top,
  396. top.Frame.X,
  397. top.Frame.Y,
  398. out int nx,
  399. out int ny,
  400. out StatusBar? sb
  401. );
  402. if (superView is null)
  403. {
  404. return;
  405. }
  406. var layoutSubviews = false;
  407. var maxWidth = 0;
  408. if (superView.Margin is { } && superView == top.SuperView)
  409. {
  410. maxWidth -= superView.GetAdornmentsThickness ().Left + superView.GetAdornmentsThickness ().Right;
  411. }
  412. if ((superView != top || top?.SuperView is { } || (top != Application.Top && top!.Modal) || (top?.SuperView is null && ApplicationOverlapped.IsOverlapped (top)))
  413. && (top!.Frame.X + top.Frame.Width > maxWidth || ny > top.Frame.Y))
  414. {
  415. if (top?.X is null or PosAbsolute && top?.Frame.X != nx)
  416. {
  417. top!.X = nx;
  418. layoutSubviews = true;
  419. }
  420. if (top?.Y is null or PosAbsolute && top?.Frame.Y != ny)
  421. {
  422. top!.Y = ny;
  423. layoutSubviews = true;
  424. }
  425. }
  426. // TODO: v2 - This is a hack to get the StatusBar to be positioned correctly.
  427. if (sb != null
  428. && !top!.Subviews.Contains (sb)
  429. && ny + top.Frame.Height != superView.Frame.Height - (sb.Visible ? 1 : 0)
  430. && top.Height is DimFill
  431. && -top.Height.GetAnchor (0) < 1)
  432. {
  433. top.Height = Dim.Fill (sb.Visible ? 1 : 0);
  434. layoutSubviews = true;
  435. }
  436. if (superView.LayoutNeeded || layoutSubviews)
  437. {
  438. superView.LayoutSubviews ();
  439. }
  440. if (LayoutNeeded)
  441. {
  442. LayoutSubviews ();
  443. }
  444. }
  445. /// <summary>Invoked when the terminal has been resized. The new <see cref="Size"/> of the terminal is provided.</summary>
  446. public event EventHandler<SizeChangedEventArgs>? SizeChanging;
  447. private bool OutsideTopFrame (Toplevel top)
  448. {
  449. if (top.Frame.X > Driver.Cols || top.Frame.Y > Driver.Rows)
  450. {
  451. return true;
  452. }
  453. return false;
  454. }
  455. // TODO: v2 - Not sure this is needed anymore.
  456. internal void PositionToplevels ()
  457. {
  458. PositionToplevel (this);
  459. foreach (View top in Subviews)
  460. {
  461. if (top is Toplevel)
  462. {
  463. PositionToplevel ((Toplevel)top);
  464. }
  465. }
  466. }
  467. #endregion
  468. }
  469. /// <summary>
  470. /// Implements the <see cref="IEqualityComparer{T}"/> for comparing two <see cref="Toplevel"/>s used by
  471. /// <see cref="StackExtensions"/>.
  472. /// </summary>
  473. public class ToplevelEqualityComparer : IEqualityComparer<Toplevel>
  474. {
  475. /// <summary>Determines whether the specified objects are equal.</summary>
  476. /// <param name="x">The first object of type <see cref="Toplevel"/> to compare.</param>
  477. /// <param name="y">The second object of type <see cref="Toplevel"/> to compare.</param>
  478. /// <returns><see langword="true"/> if the specified objects are equal; otherwise, <see langword="false"/>.</returns>
  479. public bool Equals (Toplevel? x, Toplevel? y)
  480. {
  481. if (y is null && x is null)
  482. {
  483. return true;
  484. }
  485. if (x is null || y is null)
  486. {
  487. return false;
  488. }
  489. if (x.Id == y.Id)
  490. {
  491. return true;
  492. }
  493. return false;
  494. }
  495. /// <summary>Returns a hash code for the specified object.</summary>
  496. /// <param name="obj">The <see cref="Toplevel"/> for which a hash code is to be returned.</param>
  497. /// <returns>A hash code for the specified object.</returns>
  498. /// <exception cref="ArgumentNullException">
  499. /// The type of <paramref name="obj"/> is a reference type and
  500. /// <paramref name="obj"/> is <see langword="null"/>.
  501. /// </exception>
  502. public int GetHashCode (Toplevel obj)
  503. {
  504. if (obj is null)
  505. {
  506. throw new ArgumentNullException ();
  507. }
  508. var hCode = 0;
  509. if (int.TryParse (obj.Id, out int result))
  510. {
  511. hCode = result;
  512. }
  513. return hCode.GetHashCode ();
  514. }
  515. }
  516. /// <summary>
  517. /// Implements the <see cref="IComparer{T}"/> to sort the <see cref="Toplevel"/> from the
  518. /// <see cref="ApplicationOverlapped.OverlappedChildren"/> if needed.
  519. /// </summary>
  520. public sealed class ToplevelComparer : IComparer<Toplevel>
  521. {
  522. /// <summary>
  523. /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the
  524. /// other.
  525. /// </summary>
  526. /// <param name="x">The first object to compare.</param>
  527. /// <param name="y">The second object to compare.</param>
  528. /// <returns>
  529. /// A signed integer that indicates the relative values of <paramref name="x"/> and <paramref name="y"/>, as shown
  530. /// in the following table.Value Meaning Less than zero <paramref name="x"/> is less than <paramref name="y"/>.Zero
  531. /// <paramref name="x"/> equals <paramref name="y"/> .Greater than zero <paramref name="x"/> is greater than
  532. /// <paramref name="y"/>.
  533. /// </returns>
  534. public int Compare (Toplevel? x, Toplevel? y)
  535. {
  536. if (ReferenceEquals (x, y))
  537. {
  538. return 0;
  539. }
  540. if (x is null)
  541. {
  542. return -1;
  543. }
  544. if (y is null)
  545. {
  546. return 1;
  547. }
  548. return string.CompareOrdinal (x.Id, y.Id);
  549. }
  550. }