Window.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. Padding = new Thickness (padding),
  103. };
  104. } else {
  105. Border = border;
  106. }
  107. }
  108. public override void BeginInit ()
  109. {
  110. base.BeginInit ();
  111. BorderFrame.Thickness = new Thickness (2);
  112. BorderFrame.BorderStyle = Border.BorderStyle;
  113. BorderFrame.ColorScheme = ColorScheme;
  114. BorderFrame.Data = "BorderFrame";
  115. Padding.Thickness = Border.Padding;
  116. }
  117. /// <inheritdoc/>
  118. public override void Add (View view)
  119. {
  120. base.Add (view);
  121. if (view.CanFocus) {
  122. CanFocus = true;
  123. }
  124. AddMenuStatusBar (view);
  125. }
  126. /// <inheritdoc/>
  127. public override void Remove (View view)
  128. {
  129. if (view == null) {
  130. return;
  131. }
  132. SetNeedsDisplay ();
  133. base.Remove (view);
  134. RemoveMenuStatusBar (view);
  135. }
  136. /// <summary>
  137. /// Event arguments for <see cref="View.Title"/> change events.
  138. /// </summary>
  139. public class TitleEventArgs : EventArgs {
  140. /// <summary>
  141. /// The new Window Title.
  142. /// </summary>
  143. public ustring NewTitle { get; set; }
  144. /// <summary>
  145. /// The old Window Title.
  146. /// </summary>
  147. public ustring OldTitle { get; set; }
  148. /// <summary>
  149. /// Flag which allows canceling the Title change.
  150. /// </summary>
  151. public bool Cancel { get; set; }
  152. /// <summary>
  153. /// Initializes a new instance of <see cref="TitleEventArgs"/>
  154. /// </summary>
  155. /// <param name="oldTitle">The <see cref="View.Title"/> that is/has been replaced.</param>
  156. /// <param name="newTitle">The new <see cref="View.Title"/> to be replaced.</param>
  157. public TitleEventArgs (ustring oldTitle, ustring newTitle)
  158. {
  159. OldTitle = oldTitle;
  160. NewTitle = newTitle;
  161. }
  162. }
  163. /// <summary>
  164. /// Called before the <see cref="View.Title"/> changes. Invokes the <see cref="TitleChanging"/> event, which can be cancelled.
  165. /// </summary>
  166. /// <param name="oldTitle">The <see cref="View.Title"/> that is/has been replaced.</param>
  167. /// <param name="newTitle">The new <see cref="View.Title"/> to be replaced.</param>
  168. /// <returns>`true` if an event handler canceled the Title change.</returns>
  169. public virtual bool OnTitleChanging (ustring oldTitle, ustring newTitle)
  170. {
  171. var args = new TitleEventArgs (oldTitle, newTitle);
  172. TitleChanging?.Invoke (args);
  173. return args.Cancel;
  174. }
  175. /// <summary>
  176. /// Event fired when the <see cref="View.Title"/> is changing. Set <see cref="TitleEventArgs.Cancel"/> to
  177. /// `true` to cancel the Title change.
  178. /// </summary>
  179. public event Action<TitleEventArgs> TitleChanging;
  180. /// <summary>
  181. /// Called when the <see cref="View.Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.
  182. /// </summary>
  183. /// <param name="oldTitle">The <see cref="View.Title"/> that is/has been replaced.</param>
  184. /// <param name="newTitle">The new <see cref="View.Title"/> to be replaced.</param>
  185. public virtual void OnTitleChanged (ustring oldTitle, ustring newTitle)
  186. {
  187. var args = new TitleEventArgs (oldTitle, newTitle);
  188. TitleChanged?.Invoke (args);
  189. }
  190. /// <summary>
  191. /// Event fired after the <see cref="View.Title"/> has been changed.
  192. /// </summary>
  193. public event Action<TitleEventArgs> TitleChanged;
  194. }
  195. }