Window.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. View contentView;
  27. ustring title = ustring.Empty;
  28. /// <summary>
  29. /// The title to be displayed for this window.
  30. /// </summary>
  31. /// <value>The title</value>
  32. public ustring Title {
  33. get => title;
  34. set {
  35. if (!OnTitleChanging (title, value)) {
  36. var old = title;
  37. title = value;
  38. OnTitleChanged (old, title);
  39. }
  40. SetNeedsDisplay ();
  41. }
  42. }
  43. /// <inheritdoc/>
  44. public override Border Border {
  45. get => base.Border;
  46. set {
  47. if (base.Border != null && base.Border.Child != null && value.Child == null) {
  48. value.Child = base.Border.Child;
  49. }
  50. base.Border = value;
  51. if (value == null) {
  52. return;
  53. }
  54. Rect frame;
  55. if (contentView != null && (contentView.Width is Dim || contentView.Height is Dim)) {
  56. frame = Rect.Empty;
  57. } else {
  58. frame = Frame;
  59. }
  60. AdjustContentView (frame);
  61. Border.BorderChanged += Border_BorderChanged;
  62. }
  63. }
  64. void Border_BorderChanged (Border border)
  65. {
  66. Rect frame;
  67. if (contentView != null && (contentView.Width is Dim || contentView.Height is Dim)) {
  68. frame = Rect.Empty;
  69. } else {
  70. frame = Frame;
  71. }
  72. AdjustContentView (frame);
  73. }
  74. /// <summary>
  75. /// ContentView is an internal implementation detail of Window. It is used to host Views added with <see cref="Add(View)"/>.
  76. /// Its ONLY reason for being is to provide a simple way for Window to expose to those SubViews that the Window's Bounds
  77. /// are actually deflated due to the border.
  78. /// </summary>
  79. class ContentView : View {
  80. Window instance;
  81. public ContentView (Rect frame, Window instance) : base (frame)
  82. {
  83. this.instance = instance;
  84. }
  85. public ContentView (Window instance) : base ()
  86. {
  87. this.instance = instance;
  88. }
  89. public override void OnCanFocusChanged ()
  90. {
  91. if (MostFocused == null && CanFocus && Visible) {
  92. EnsureFocus ();
  93. }
  94. base.OnCanFocusChanged ();
  95. }
  96. public override bool OnMouseEvent (MouseEvent mouseEvent)
  97. {
  98. return instance.OnMouseEvent (mouseEvent);
  99. }
  100. }
  101. /// <summary>
  102. /// Initializes a new instance of the <see cref="Gui.Window"/> class with an optional title using <see cref="LayoutStyle.Absolute"/> positioning.
  103. /// </summary>
  104. /// <param name="frame">Superview-relative rectangle specifying the location and size</param>
  105. /// <param name="title">Title</param>
  106. /// <remarks>
  107. /// This constructor initializes a Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  108. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle.Computed"/>.
  109. /// </remarks>
  110. public Window (Rect frame, ustring title = null) : this (frame, title, padding: 0, border: null)
  111. {
  112. }
  113. /// <summary>
  114. /// Initializes a new instance of the <see cref="Window"/> class with an optional title using <see cref="LayoutStyle.Computed"/> positioning.
  115. /// </summary>
  116. /// <param name="title">Title.</param>
  117. /// <remarks>
  118. /// This constructor initializes a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  119. /// 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.
  120. /// </remarks>
  121. public Window (ustring title = null) : this (title, padding: 0, border: null)
  122. {
  123. }
  124. /// <summary>
  125. /// Initializes a new instance of the <see cref="Window"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  126. /// </summary>
  127. public Window () : this (title: null) { }
  128. /// <summary>
  129. /// 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,
  130. /// and an optional title.
  131. /// </summary>
  132. /// <param name="frame">Superview-relative rectangle specifying the location and size</param>
  133. /// <param name="title">Title</param>
  134. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  135. /// <param name="border">The <see cref="Border"/>.</param>
  136. /// <remarks>
  137. /// This constructor initializes a Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  138. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>
  139. /// </remarks>
  140. public Window (Rect frame, ustring title = null, int padding = 0, Border border = null) : base (frame)
  141. {
  142. Initialize (title, frame, padding, border);
  143. }
  144. /// <summary>
  145. /// Initializes a new instance of the <see cref="Window"/> using <see cref="LayoutStyle.Computed"/> positioning,
  146. /// and an optional title.
  147. /// </summary>
  148. /// <param name="title">Title.</param>
  149. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  150. /// <param name="border">The <see cref="Border"/>.</param>
  151. /// <remarks>
  152. /// This constructor initializes a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  153. /// 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.
  154. /// </remarks>
  155. public Window (ustring title = null, int padding = 0, Border border = null) : base ()
  156. {
  157. Initialize (title, Rect.Empty, padding, border);
  158. }
  159. /// <summary>
  160. /// The default <see cref="BorderStyle"/> for <see cref="FrameView"/>. The default is <see cref="BorderStyle.Single"/>.
  161. /// </summary>
  162. /// <remarks>
  163. /// This property can be set in a Theme to change the default <see cref="BorderStyle"/> for all <see cref="Window"/>s.
  164. /// </remarks>
  165. ///[SerializableConfigurationProperty (Scope = typeof (ThemeScope)), JsonConverter (typeof (JsonStringEnumConverter))]
  166. public static BorderStyle DefaultBorderStyle { get; set; } = BorderStyle.Single;
  167. void Initialize (ustring title, Rect frame, int padding = 0, Border border = null)
  168. {
  169. CanFocus = true;
  170. ColorScheme = Colors.Base;
  171. if (title == null) title = ustring.Empty;
  172. Title = title;
  173. if (border == null) {
  174. Border = new Border () {
  175. BorderStyle = DefaultBorderStyle,
  176. Padding = new Thickness (padding),
  177. BorderBrush = ColorScheme.Normal.Background
  178. };
  179. } else {
  180. Border = border;
  181. }
  182. AdjustContentView (frame);
  183. }
  184. void AdjustContentView (Rect frame)
  185. {
  186. var borderLength = Border.DrawMarginFrame ? 1 : 0;
  187. var sumPadding = Border.GetSumThickness ();
  188. var wp = new Point ();
  189. var wb = new Size ();
  190. if (frame == Rect.Empty) {
  191. wp.X = borderLength + sumPadding.Left;
  192. wp.Y = borderLength + sumPadding.Top;
  193. wb.Width = borderLength + sumPadding.Right;
  194. wb.Height = borderLength + sumPadding.Bottom;
  195. if (contentView == null) {
  196. contentView = new ContentView (this) {
  197. X = wp.X,
  198. Y = wp.Y,
  199. Width = Dim.Fill (wb.Width),
  200. Height = Dim.Fill (wb.Height)
  201. };
  202. } else {
  203. contentView.X = wp.X;
  204. contentView.Y = wp.Y;
  205. contentView.Width = Dim.Fill (wb.Width);
  206. contentView.Height = Dim.Fill (wb.Height);
  207. }
  208. } else {
  209. wb.Width = (2 * borderLength) + sumPadding.Right + sumPadding.Left;
  210. wb.Height = (2 * borderLength) + sumPadding.Bottom + sumPadding.Top;
  211. var cFrame = new Rect (borderLength + sumPadding.Left, borderLength + sumPadding.Top, frame.Width - wb.Width, frame.Height - wb.Height);
  212. if (contentView == null) {
  213. contentView = new ContentView (cFrame, this);
  214. } else {
  215. contentView.Frame = cFrame;
  216. }
  217. }
  218. if (Subviews?.Count == 0)
  219. base.Add (contentView);
  220. Border.Child = contentView;
  221. }
  222. ///// <summary>
  223. ///// Enumerates the various <see cref="View"/>s in the embedded <see cref="ContentView"/>.
  224. ///// </summary>
  225. ///// <returns>The enumerator.</returns>
  226. //public new IEnumerator GetEnumerator ()
  227. //{
  228. // return contentView.GetEnumerator ();
  229. //}
  230. /// <inheritdoc/>
  231. public override void Add (View view)
  232. {
  233. contentView.Add (view);
  234. if (view.CanFocus) {
  235. CanFocus = true;
  236. }
  237. AddMenuStatusBar (view);
  238. }
  239. /// <inheritdoc/>
  240. public override void Remove (View view)
  241. {
  242. if (view == null) {
  243. return;
  244. }
  245. SetNeedsDisplay ();
  246. contentView.Remove (view);
  247. if (contentView.InternalSubviews.Count < 1) {
  248. CanFocus = false;
  249. }
  250. RemoveMenuStatusBar (view);
  251. if (view != contentView && Focused == null) {
  252. FocusFirst ();
  253. }
  254. }
  255. /// <inheritdoc/>
  256. public override void RemoveAll ()
  257. {
  258. contentView.RemoveAll ();
  259. }
  260. ///<inheritdoc/>
  261. public override void Redraw (Rect bounds)
  262. {
  263. var padding = Border.GetSumThickness ();
  264. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  265. if (!NeedDisplay.IsEmpty || ChildNeedsDisplay || LayoutNeeded) {
  266. Driver.SetAttribute (GetNormalColor ());
  267. Clear ();
  268. contentView.SetNeedsDisplay ();
  269. }
  270. var savedClip = contentView.ClipToBounds ();
  271. // Redraw our contentView
  272. // DONE: smartly constrict contentView.Bounds to just be what intersects with the 'bounds' we were passed
  273. contentView.Redraw (!NeedDisplay.IsEmpty || ChildNeedsDisplay || LayoutNeeded ? contentView.Bounds : bounds);
  274. Driver.Clip = savedClip;
  275. ClearLayoutNeeded ();
  276. ClearNeedsDisplay ();
  277. Driver.SetAttribute (GetNormalColor ());
  278. //Driver.DrawWindowFrame (scrRect, padding.Left + borderLength, padding.Top + borderLength, padding.Right + borderLength, padding.Bottom + borderLength,
  279. // Border.BorderStyle != BorderStyle.None, fill: true, Border.BorderStyle);
  280. Border.DrawContent (this, false);
  281. if (HasFocus)
  282. Driver.SetAttribute (ColorScheme.HotNormal);
  283. if (Border.DrawMarginFrame)
  284. Driver.DrawWindowTitle (scrRect, Title, padding.Left, padding.Top, padding.Right, padding.Bottom);
  285. Driver.SetAttribute (GetNormalColor ());
  286. }
  287. /// <inheritdoc/>
  288. public override void OnCanFocusChanged ()
  289. {
  290. if (contentView != null) {
  291. contentView.CanFocus = CanFocus;
  292. }
  293. base.OnCanFocusChanged ();
  294. }
  295. /// <summary>
  296. /// The text displayed by the <see cref="Label"/>.
  297. /// </summary>
  298. public override ustring Text {
  299. get => contentView?.Text;
  300. set {
  301. base.Text = value;
  302. if (contentView != null) {
  303. contentView.Text = value;
  304. }
  305. }
  306. }
  307. /// <summary>
  308. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  309. /// </summary>
  310. /// <value>The text alignment.</value>
  311. public override TextAlignment TextAlignment {
  312. get => contentView.TextAlignment;
  313. set {
  314. base.TextAlignment = contentView.TextAlignment = value;
  315. }
  316. }
  317. /// <summary>
  318. /// Event arguments for <see cref="Title"/> chane events.
  319. /// </summary>
  320. public class TitleEventArgs : EventArgs {
  321. /// <summary>
  322. /// The new Window Title.
  323. /// </summary>
  324. public ustring NewTitle { get; set; }
  325. /// <summary>
  326. /// The old Window Title.
  327. /// </summary>
  328. public ustring OldTitle { get; set; }
  329. /// <summary>
  330. /// Flag which allows cancelling the Title change.
  331. /// </summary>
  332. public bool Cancel { get; set; }
  333. /// <summary>
  334. /// Initializes a new instance of <see cref="TitleEventArgs"/>
  335. /// </summary>
  336. /// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
  337. /// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
  338. public TitleEventArgs (ustring oldTitle, ustring newTitle)
  339. {
  340. OldTitle = oldTitle;
  341. NewTitle = newTitle;
  342. }
  343. }
  344. /// <summary>
  345. /// Called before the <see cref="Window.Title"/> changes. Invokes the <see cref="TitleChanging"/> event, which can be cancelled.
  346. /// </summary>
  347. /// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
  348. /// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
  349. /// <returns>`true` if an event handler cancelled the Title change.</returns>
  350. public virtual bool OnTitleChanging (ustring oldTitle, ustring newTitle)
  351. {
  352. var args = new TitleEventArgs (oldTitle, newTitle);
  353. TitleChanging?.Invoke (args);
  354. return args.Cancel;
  355. }
  356. /// <summary>
  357. /// Event fired when the <see cref="Window.Title"/> is changing. Set <see cref="TitleEventArgs.Cancel"/> to
  358. /// `true` to cancel the Title change.
  359. /// </summary>
  360. public event Action<TitleEventArgs> TitleChanging;
  361. /// <summary>
  362. /// Called when the <see cref="Window.Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.
  363. /// </summary>
  364. /// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
  365. /// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
  366. public virtual void OnTitleChanged (ustring oldTitle, ustring newTitle)
  367. {
  368. var args = new TitleEventArgs (oldTitle, newTitle);
  369. TitleChanged?.Invoke (args);
  370. }
  371. /// <summary>
  372. /// Event fired after the <see cref="Window.Title"/> has been changed.
  373. /// </summary>
  374. public event Action<TitleEventArgs> TitleChanged;
  375. }
  376. }