Window.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // Authors:
  3. // Miguel de Icaza ([email protected])
  4. //
  5. // NOTE: Window is functionally identical to FrameView with the following exceptions.
  6. // - Window is a Toplevel
  7. // - FrameView Does not support padding (but should)
  8. // - FrameView Does not support mouse dragging
  9. // - FrameView Does not support IEnumerable
  10. // Any updates done here should probably be done in FrameView as well; TODO: Merge these classes
  11. using System;
  12. using System.Collections;
  13. using System.Text.Json.Serialization;
  14. using NStack;
  15. using Terminal.Gui.Configuration;
  16. using static Terminal.Gui.Configuration.ConfigurationManager;
  17. namespace Terminal.Gui {
  18. /// <summary>
  19. /// A <see cref="Toplevel"/> <see cref="View"/> that draws a border around its <see cref="View.Frame"/> with a Title at the top.
  20. /// </summary>
  21. /// <remarks>
  22. /// The 'client area' of a <see cref="Window"/> is a rectangle deflated by one or more rows/columns from <see cref="View.Bounds"/>. A this time there is no
  23. /// API to determine this rectangle.
  24. /// </remarks>
  25. public class Window : Toplevel {
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="Gui.Window"/> class with an optional title using <see cref="LayoutStyle.Absolute"/> positioning.
  28. /// </summary>
  29. /// <param name="frame">Superview-relative rectangle specifying the location and size</param>
  30. /// <param name="title">Title</param>
  31. /// <remarks>
  32. /// This constructor initializes a Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  33. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle.Computed"/>.
  34. /// </remarks>
  35. public Window (Rect frame, ustring title = null) : this (frame, title, padding: 0, border: null)
  36. {
  37. }
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="Window"/> class with an optional title using <see cref="LayoutStyle.Computed"/> positioning.
  40. /// </summary>
  41. /// <param name="title">Title.</param>
  42. /// <remarks>
  43. /// This constructor initializes a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  44. /// Use <see cref="View.X"/>, <see cref="View.Y"/>, <see cref="View.Width"/>, and <see cref="View.Height"/> properties to dynamically control the size and location of the view.
  45. /// </remarks>
  46. public Window (ustring title = null) : this (title, padding: 0, border: null)
  47. {
  48. }
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="Window"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  51. /// </summary>
  52. public Window () : this (title: null) { }
  53. /// <summary>
  54. /// Initializes a new instance of the <see cref="Window"/> using <see cref="LayoutStyle.Absolute"/> positioning with the specified frame for its location, with the specified frame padding,
  55. /// and an optional title.
  56. /// </summary>
  57. /// <param name="frame">Superview-relative rectangle specifying the location and size</param>
  58. /// <param name="title">Title</param>
  59. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  60. /// <param name="border">The <see cref="Border"/>.</param>
  61. /// <remarks>
  62. /// This constructor initializes a Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  63. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>
  64. /// </remarks>
  65. public Window (Rect frame, ustring title = null, int padding = 0, Border border = null) : base (frame)
  66. {
  67. SetInitialProperties (title, frame, padding, border);
  68. }
  69. /// <summary>
  70. /// Initializes a new instance of the <see cref="Window"/> using <see cref="LayoutStyle.Computed"/> positioning,
  71. /// and an optional title.
  72. /// </summary>
  73. /// <param name="title">Title.</param>
  74. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  75. /// <param name="border">The <see cref="Border"/>.</param>
  76. /// <remarks>
  77. /// This constructor initializes a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  78. /// Use <see cref="View.X"/>, <see cref="View.Y"/>, <see cref="View.Width"/>, and <see cref="View.Height"/> properties to dynamically control the size and location of the view.
  79. /// </remarks>
  80. public Window (ustring title = null, int padding = 0, Border border = null) : base ()
  81. {
  82. SetInitialProperties (title, Rect.Empty, padding, border);
  83. }
  84. /// <summary>
  85. /// The default <see cref="BorderStyle"/> for <see cref="FrameView"/>. The default is <see cref="BorderStyle.Single"/>.
  86. /// </summary>
  87. /// <remarks>
  88. /// This property can be set in a Theme to change the default <see cref="BorderStyle"/> for all <see cref="Window"/>s.
  89. /// </remarks>
  90. ///[SerializableConfigurationProperty (Scope = typeof (ThemeScope)), JsonConverter (typeof (JsonStringEnumConverter))]
  91. public static BorderStyle DefaultBorderStyle { get; set; } = BorderStyle.Single;
  92. void SetInitialProperties (ustring title, Rect frame, int padding = 0, Border border = null)
  93. {
  94. CanFocus = true;
  95. ColorScheme = Colors.Base;
  96. if (title == null) title = ustring.Empty;
  97. Title = title;
  98. if (border == null) {
  99. // TODO: v2 this is a hack until Border gets refactored
  100. Border = new Border () {
  101. BorderStyle = DefaultBorderStyle,
  102. PaddingThickness = new Thickness (padding),
  103. };
  104. } else {
  105. Border = border;
  106. }
  107. BorderFrame.Thickness = new Thickness (1);
  108. BorderFrame.BorderStyle = Border.BorderStyle;
  109. BorderFrame.ColorScheme = ColorScheme;
  110. BorderFrame.Data = "BorderFrame";
  111. // TODO: Hack until Border is refactored
  112. Padding.Thickness = Border.PaddingThickness ?? Padding.Thickness;
  113. }
  114. public override void BeginInit ()
  115. {
  116. base.BeginInit ();
  117. }
  118. /// <inheritdoc/>
  119. public override void Add (View view)
  120. {
  121. base.Add (view);
  122. if (view.CanFocus) {
  123. CanFocus = true;
  124. }
  125. AddMenuStatusBar (view);
  126. }
  127. /// <inheritdoc/>
  128. public override void Remove (View view)
  129. {
  130. if (view == null) {
  131. return;
  132. }
  133. SetNeedsDisplay ();
  134. base.Remove (view);
  135. RemoveMenuStatusBar (view);
  136. }
  137. /// <summary>
  138. /// Event arguments for <see cref="View.Title"/> change events.
  139. /// </summary>
  140. public class TitleEventArgs : EventArgs {
  141. /// <summary>
  142. /// The new Window Title.
  143. /// </summary>
  144. public ustring NewTitle { get; set; }
  145. /// <summary>
  146. /// The old Window Title.
  147. /// </summary>
  148. public ustring OldTitle { get; set; }
  149. /// <summary>
  150. /// Flag which allows canceling the Title change.
  151. /// </summary>
  152. public bool Cancel { get; set; }
  153. /// <summary>
  154. /// Initializes a new instance of <see cref="TitleEventArgs"/>
  155. /// </summary>
  156. /// <param name="oldTitle">The <see cref="View.Title"/> that is/has been replaced.</param>
  157. /// <param name="newTitle">The new <see cref="View.Title"/> to be replaced.</param>
  158. public TitleEventArgs (ustring oldTitle, ustring newTitle)
  159. {
  160. OldTitle = oldTitle;
  161. NewTitle = newTitle;
  162. }
  163. }
  164. /// <summary>
  165. /// Called before the <see cref="View.Title"/> changes. Invokes the <see cref="TitleChanging"/> event, which can be cancelled.
  166. /// </summary>
  167. /// <param name="oldTitle">The <see cref="View.Title"/> that is/has been replaced.</param>
  168. /// <param name="newTitle">The new <see cref="View.Title"/> to be replaced.</param>
  169. /// <returns>`true` if an event handler canceled the Title change.</returns>
  170. public virtual bool OnTitleChanging (ustring oldTitle, ustring newTitle)
  171. {
  172. var args = new TitleEventArgs (oldTitle, newTitle);
  173. TitleChanging?.Invoke (args);
  174. return args.Cancel;
  175. }
  176. /// <summary>
  177. /// Event fired when the <see cref="View.Title"/> is changing. Set <see cref="TitleEventArgs.Cancel"/> to
  178. /// `true` to cancel the Title change.
  179. /// </summary>
  180. public event Action<TitleEventArgs> TitleChanging;
  181. /// <summary>
  182. /// Called when the <see cref="View.Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.
  183. /// </summary>
  184. /// <param name="oldTitle">The <see cref="View.Title"/> that is/has been replaced.</param>
  185. /// <param name="newTitle">The new <see cref="View.Title"/> to be replaced.</param>
  186. public virtual void OnTitleChanged (ustring oldTitle, ustring newTitle)
  187. {
  188. var args = new TitleEventArgs (oldTitle, newTitle);
  189. TitleChanged?.Invoke (args);
  190. }
  191. /// <summary>
  192. /// Event fired after the <see cref="View.Title"/> has been changed.
  193. /// </summary>
  194. public event Action<TitleEventArgs> TitleChanged;
  195. }
  196. }