Window.cs 8.5 KB

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