Window.cs 8.0 KB

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