Toplevel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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.TopRunnable"/>. 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. /// </remarks>
  20. public partial class Toplevel : View
  21. {
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="Toplevel"/> class,
  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(Dim)"/>.
  26. /// </summary>
  27. public Toplevel ()
  28. {
  29. CanFocus = true;
  30. TabStop = TabBehavior.TabGroup;
  31. Arrangement = ViewArrangement.Overlapped;
  32. Width = Dim.Fill ();
  33. Height = Dim.Fill ();
  34. SchemeName = SchemeManager.SchemesToSchemeName (Schemes.Toplevel);
  35. MouseClick += Toplevel_MouseClick;
  36. }
  37. #region Keyboard & Mouse
  38. // TODO: IRunnable: Re-implement - Modal means IRunnable, ViewArrangement.Overlapped where modalView.Z > allOtherViews.Max (v = v.Z), and exclusive key/mouse input.
  39. /// <summary>
  40. /// Determines whether the <see cref="Toplevel"/> is modal or not. If set to <c>false</c> (the default):
  41. /// <list type="bullet">
  42. /// <item>
  43. /// <description><see cref="View.OnKeyDown"/> events will propagate keys upwards.</description>
  44. /// </item>
  45. /// <item>
  46. /// <description>The Toplevel will act as an embedded view (not a modal/pop-up).</description>
  47. /// </item>
  48. /// </list>
  49. /// If set to <c>true</c>:
  50. /// <list type="bullet">
  51. /// <item>
  52. /// <description><see cref="View.OnKeyDown"/> events will NOT propagate keys upwards.</description>
  53. /// </item>
  54. /// <item>
  55. /// <description>The Toplevel will and look like a modal (pop-up) (e.g. see <see cref="Dialog"/>.</description>
  56. /// </item>
  57. /// </list>
  58. /// </summary>
  59. public bool Modal { get; set; }
  60. private void Toplevel_MouseClick (object? sender, MouseEventArgs e) { e.Handled = InvokeCommand (Command.HotKey) == true; }
  61. #endregion
  62. #region SubViews
  63. //// TODO: Deprecate - Any view can host a menubar in v2
  64. ///// <summary>Gets the latest <see cref="MenuBar"/> added into this Toplevel.</summary>
  65. //public MenuBar? MenuBar => (MenuBar?)SubViews?.LastOrDefault (s => s is MenuBar);
  66. #endregion
  67. #region Life Cycle
  68. // TODO: IRunnable: Re-implement as a property on IRunnable
  69. /// <summary>Gets or sets whether the main loop for this <see cref="Toplevel"/> is running or not.</summary>
  70. /// <remarks>Setting this property directly is discouraged. Use <see cref="IApplication.RequestStop()"/> instead.</remarks>
  71. public bool Running { get; set; }
  72. // TODO: Deprecate. Other than a few tests, this is not used anywhere.
  73. /// <summary>
  74. /// <see langword="true"/> if was already loaded by the <see cref="IApplication.Begin(Toplevel)"/>
  75. /// <see langword="false"/>, otherwise.
  76. /// </summary>
  77. public bool IsLoaded { get; private set; }
  78. // TODO: IRunnable: Re-implement as an event on IRunnable; IRunnable.Activating/Activate
  79. /// <summary>Invoked when the Toplevel <see cref="SessionToken"/> active.</summary>
  80. public event EventHandler<ToplevelEventArgs>? Activate;
  81. // TODO: IRunnable: Re-implement as an event on IRunnable; IRunnable.Deactivating/Deactivate?
  82. /// <summary>Invoked when the Toplevel<see cref="SessionToken"/> ceases to be active.</summary>
  83. public event EventHandler<ToplevelEventArgs>? Deactivate;
  84. /// <summary>Invoked when the Toplevel's <see cref="SessionToken"/> is closed by <see cref="IApplication.End(SessionToken)"/>.</summary>
  85. public event EventHandler<ToplevelEventArgs>? Closed;
  86. /// <summary>
  87. /// Invoked when the Toplevel's <see cref="SessionToken"/> is being closed by
  88. /// <see cref="IApplication.RequestStop(Toplevel)"/>.
  89. /// </summary>
  90. public event EventHandler<ToplevelClosingEventArgs>? Closing;
  91. /// <summary>
  92. /// Invoked when the <see cref="Toplevel"/> <see cref="SessionToken"/> has begun to be loaded. A Loaded event handler
  93. /// is a good place to finalize initialization before calling Run.
  94. /// </summary>
  95. public event EventHandler? Loaded;
  96. /// <summary>
  97. /// Called from <see cref="IApplication.Begin(Toplevel)"/> before the <see cref="Toplevel"/> redraws for the first
  98. /// time.
  99. /// </summary>
  100. /// <remarks>
  101. /// Overrides must call base.OnLoaded() to ensure any Toplevel subviews are initialized properly and the
  102. /// <see cref="Loaded"/> event is raised.
  103. /// </remarks>
  104. public virtual void OnLoaded ()
  105. {
  106. IsLoaded = true;
  107. foreach (var view in SubViews.Where (v => v is Toplevel))
  108. {
  109. var tl = (Toplevel)view;
  110. tl.OnLoaded ();
  111. }
  112. Loaded?.Invoke (this, EventArgs.Empty);
  113. }
  114. /// <summary>
  115. /// Invoked when the <see cref="Toplevel"/> main loop has started it's first iteration. Subscribe to this event to
  116. /// perform tasks when the <see cref="Toplevel"/> has been laid out and focus has been set. changes.
  117. /// <para>
  118. /// A Ready event handler is a good place to finalize initialization after calling
  119. /// <see cref="IApplication.Run(Toplevel, Func{Exception, bool})"/> on this <see cref="Toplevel"/>.
  120. /// </para>
  121. /// </summary>
  122. public event EventHandler? Ready;
  123. /// <summary>
  124. /// Stops and closes this <see cref="Toplevel"/>. If this Toplevel is the top-most Toplevel,
  125. /// <see cref="IApplication.RequestStop(Toplevel)"/> will be called, causing the application to exit.
  126. /// </summary>
  127. public virtual void RequestStop ()
  128. {
  129. App?.RequestStop (App?.TopRunnable);
  130. }
  131. /// <summary>
  132. /// Invoked when the Toplevel <see cref="SessionToken"/> has been unloaded. A Unloaded event handler is a good place
  133. /// to dispose objects after calling <see cref="IApplication.End(SessionToken)"/>.
  134. /// </summary>
  135. public event EventHandler? Unloaded;
  136. internal virtual void OnActivate (Toplevel deactivated) { Activate?.Invoke (this, new (deactivated)); }
  137. internal virtual void OnClosed (Toplevel top) { Closed?.Invoke (this, new (top)); }
  138. internal virtual bool OnClosing (ToplevelClosingEventArgs ev)
  139. {
  140. Closing?.Invoke (this, ev);
  141. return ev.Cancel;
  142. }
  143. internal virtual void OnDeactivate (Toplevel activated) { Deactivate?.Invoke (this, new (activated)); }
  144. /// <summary>
  145. /// Called from run loop after the <see cref="Toplevel"/> has entered the first iteration
  146. /// of the loop.
  147. /// </summary>
  148. internal virtual void OnReady ()
  149. {
  150. foreach (var view in SubViews.Where (v => v is Toplevel))
  151. {
  152. var tl = (Toplevel)view;
  153. tl.OnReady ();
  154. }
  155. Ready?.Invoke (this, EventArgs.Empty);
  156. }
  157. /// <summary>Called from <see cref="IApplication.End(SessionToken)"/> before the <see cref="Toplevel"/> is disposed.</summary>
  158. internal virtual void OnUnloaded ()
  159. {
  160. foreach (var view in SubViews.Where (v => v is Toplevel))
  161. {
  162. var tl = (Toplevel)view;
  163. tl.OnUnloaded ();
  164. }
  165. Unloaded?.Invoke (this, EventArgs.Empty);
  166. }
  167. #endregion
  168. #region Size / Position Management
  169. // TODO: Make cancelable?
  170. internal void OnSizeChanging (SizeChangedEventArgs size) { SizeChanging?.Invoke (this, size); }
  171. /// <summary>
  172. /// Adjusts the location and size of <paramref name="top"/> within this Toplevel. Virtual method enabling
  173. /// implementation of specific positions for inherited <see cref="Toplevel"/> views.
  174. /// </summary>
  175. /// <param name="top">The Toplevel to adjust.</param>
  176. public virtual void PositionToplevel (Toplevel? top)
  177. {
  178. if (top is null)
  179. {
  180. return;
  181. }
  182. View? superView = GetLocationEnsuringFullVisibility (
  183. top,
  184. top.Frame.X,
  185. top.Frame.Y,
  186. out int nx,
  187. out int ny
  188. //,
  189. // out StatusBar? sb
  190. );
  191. if (superView is null)
  192. {
  193. return;
  194. }
  195. //var layoutSubViews = false;
  196. var maxWidth = 0;
  197. if (superView.Margin is { } && superView == top.SuperView)
  198. {
  199. maxWidth -= superView.GetAdornmentsThickness ().Left + superView.GetAdornmentsThickness ().Right;
  200. }
  201. // BUGBUG: The && true is a temp hack
  202. if ((superView != top || top?.SuperView is { } || (top != App?.TopRunnable && top!.Modal) || (top == App?.TopRunnable && top?.SuperView is null))
  203. && (top!.Frame.X + top.Frame.Width > maxWidth || ny > top.Frame.Y))
  204. {
  205. if (top?.X is null or PosAbsolute && top?.Frame.X != nx)
  206. {
  207. top!.X = nx;
  208. //layoutSubViews = true;
  209. }
  210. if (top?.Y is null or PosAbsolute && top?.Frame.Y != ny)
  211. {
  212. top!.Y = ny;
  213. //layoutSubViews = true;
  214. }
  215. }
  216. //if (superView.IsLayoutNeeded () || layoutSubViews)
  217. //{
  218. // superView.LayoutSubViews ();
  219. //}
  220. //if (IsLayoutNeeded ())
  221. //{
  222. // LayoutSubViews ();
  223. //}
  224. }
  225. /// <summary>Invoked when the terminal has been resized. The new <see cref="Size"/> of the terminal is provided.</summary>
  226. public event EventHandler<SizeChangedEventArgs>? SizeChanging;
  227. #endregion
  228. }
  229. /// <summary>
  230. /// Implements the <see cref="IEqualityComparer{T}"/> for comparing two <see cref="Toplevel"/>s used by
  231. /// <see cref="StackExtensions"/>.
  232. /// </summary>
  233. public class ToplevelEqualityComparer : IEqualityComparer<Toplevel>
  234. {
  235. /// <summary>Determines whether the specified objects are equal.</summary>
  236. /// <param name="x">The first object of type <see cref="Toplevel"/> to compare.</param>
  237. /// <param name="y">The second object of type <see cref="Toplevel"/> to compare.</param>
  238. /// <returns><see langword="true"/> if the specified objects are equal; otherwise, <see langword="false"/>.</returns>
  239. public bool Equals (Toplevel? x, Toplevel? y)
  240. {
  241. if (y is null && x is null)
  242. {
  243. return true;
  244. }
  245. if (x is null || y is null)
  246. {
  247. return false;
  248. }
  249. if (x.Id == y.Id)
  250. {
  251. return true;
  252. }
  253. return false;
  254. }
  255. /// <summary>Returns a hash code for the specified object.</summary>
  256. /// <param name="obj">The <see cref="Toplevel"/> for which a hash code is to be returned.</param>
  257. /// <returns>A hash code for the specified object.</returns>
  258. /// <exception cref="ArgumentNullException">
  259. /// The type of <paramref name="obj"/> is a reference type and
  260. /// <paramref name="obj"/> is <see langword="null"/>.
  261. /// </exception>
  262. public int GetHashCode (Toplevel obj)
  263. {
  264. if (obj is null)
  265. {
  266. throw new ArgumentNullException ();
  267. }
  268. var hCode = 0;
  269. if (int.TryParse (obj.Id, out int result))
  270. {
  271. hCode = result;
  272. }
  273. return hCode.GetHashCode ();
  274. }
  275. }