Window.cs 13 KB

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