FrameView.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. //
  2. // Authors:
  3. // Miguel de Icaza ([email protected])
  4. //
  5. // NOTE: FrameView is functionally identical to Window with the following exceptions.
  6. // - Is not a Toplevel
  7. // - Does not support mouse dragging
  8. // - Does not support padding (but should)
  9. // - Does not support IEnumerable
  10. // Any udpates done here should probably be done in Window as well; TODO: Merge these classes
  11. using System;
  12. using System.Linq;
  13. using System.Text.Json.Serialization;
  14. using NStack;
  15. using Terminal.Gui.Graphs;
  16. using static Terminal.Gui.Configuration.ConfigurationManager;
  17. namespace Terminal.Gui {
  18. /// <summary>
  19. /// The FrameView is a container frame that draws a frame around the contents. It is similar to
  20. /// a GroupBox in Windows.
  21. /// </summary>
  22. public class FrameView : View {
  23. //internal class FrameViewConfig : Configuration.Config<FrameViewConfig> {
  24. // /// <summary>
  25. // ///
  26. // /// </summary>
  27. // ///
  28. // [JsonConverter (typeof (JsonStringEnumConverter))]
  29. // public BorderStyle? DefaultBorderStyle { get; set; }
  30. // public override void Apply ()
  31. // {
  32. // if (DefaultBorderStyle.HasValue) {
  33. // FrameView.DefaultBorderStyle = DefaultBorderStyle.Value;
  34. // }
  35. // }
  36. // public override void CopyUpdatedProperitesFrom (FrameViewConfig changedConfig)
  37. // {
  38. // if (changedConfig.DefaultBorderStyle.HasValue) {
  39. // DefaultBorderStyle = changedConfig.DefaultBorderStyle;
  40. // }
  41. // }
  42. // public override void GetHardCodedDefaults ()
  43. // {
  44. // DefaultBorderStyle = FrameView.DefaultBorderStyle;
  45. // }
  46. //}
  47. //[Configuration.ConfigProperty]
  48. //internal static FrameViewConfig Config { get; set; } = new FrameViewConfig ();
  49. View contentView;
  50. ustring title;
  51. /// <summary>
  52. /// The title to be displayed for this <see cref="FrameView"/>.
  53. /// </summary>
  54. /// <value>The title.</value>
  55. public ustring Title {
  56. get => title;
  57. set {
  58. title = value;
  59. if (Border != null) {
  60. Border.Title = title;
  61. }
  62. SetNeedsDisplay ();
  63. }
  64. }
  65. /// <inheritdoc/>
  66. public override Border Border {
  67. get => base.Border;
  68. set {
  69. if (base.Border != null && base.Border.Child != null && value.Child == null) {
  70. value.Child = base.Border.Child;
  71. }
  72. base.Border = value;
  73. if (value == null) {
  74. return;
  75. }
  76. Rect frame;
  77. if (contentView != null && (contentView.Width is Dim || contentView.Height is Dim)) {
  78. frame = Rect.Empty;
  79. } else {
  80. frame = Frame;
  81. }
  82. AdjustContentView (frame);
  83. Border.BorderChanged += Border_BorderChanged;
  84. }
  85. }
  86. void Border_BorderChanged (Border border)
  87. {
  88. Rect frame;
  89. if (contentView != null && (contentView.Width is Dim || contentView.Height is Dim)) {
  90. frame = Rect.Empty;
  91. } else {
  92. frame = Frame;
  93. }
  94. AdjustContentView (frame);
  95. }
  96. /// <summary>
  97. /// ContentView is an internal implementation detail of Window. It is used to host Views added with <see cref="Add(View)"/>.
  98. /// Its ONLY reason for being is to provide a simple way for Window to expose to those SubViews that the Window's Bounds
  99. /// are actually deflated due to the border.
  100. /// </summary>
  101. class ContentView : View {
  102. public ContentView (Rect frame) : base (frame) { }
  103. public ContentView () : base () { }
  104. }
  105. /// <summary>
  106. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  107. /// </summary>
  108. /// <param name="frame">Frame.</param>
  109. /// <param name="title">Title.</param>
  110. /// <param name="views">Views.</param>
  111. /// <param name="border">The <see cref="Border"/>.</param>
  112. public FrameView (Rect frame, ustring title = null, View [] views = null, Border border = null) : base (frame)
  113. {
  114. //var cFrame = new Rect (1, 1, Math.Max (frame.Width - 2, 0), Math.Max (frame.Height - 2, 0));
  115. Initialize (frame, title, views, border);
  116. }
  117. /// <summary>
  118. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  119. /// </summary>
  120. /// <param name="title">Title.</param>
  121. /// <param name="border">The <see cref="Border"/>.</param>
  122. public FrameView (ustring title, Border border = null)
  123. {
  124. Initialize (Rect.Empty, title, null, border);
  125. }
  126. /// <summary>
  127. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  128. /// </summary>
  129. public FrameView () : this (title: string.Empty) { }
  130. /// <summary>
  131. /// The default <see cref="BorderStyle"/> for <see cref="FrameView"/>. The default is <see cref="BorderStyle.Single"/>.
  132. /// </summary>
  133. /// <remarks>
  134. /// This property can be set in a Theme to change the default <see cref="BorderStyle"/> for all <see cref="FrameView"/>s.
  135. /// </remarks>
  136. [SerializableConfigurationProperty (Scope = typeof (ThemeScope)), JsonConverter (typeof (JsonStringEnumConverter))]
  137. public static BorderStyle DefaultBorderStyle { get; set; } = BorderStyle.Single;
  138. void Initialize (Rect frame, ustring title, View [] views = null, Border border = null)
  139. {
  140. if (title == null) title = ustring.Empty;
  141. this.Title = title;
  142. if (border == null) {
  143. Border = new Border () {
  144. BorderStyle = DefaultBorderStyle
  145. };
  146. } else {
  147. Border = border;
  148. if (ustring.IsNullOrEmpty (border.Title)) {
  149. border.Title = title;
  150. }
  151. }
  152. AdjustContentView (frame, views);
  153. }
  154. void AdjustContentView (Rect frame, View [] views = null)
  155. {
  156. var borderLength = Border.DrawMarginFrame ? 1 : 0;
  157. var sumPadding = Border.GetSumThickness ();
  158. var wp = new Point ();
  159. var wb = new Size ();
  160. if (frame == Rect.Empty) {
  161. wp.X = borderLength + sumPadding.Left;
  162. wp.Y = borderLength + sumPadding.Top;
  163. wb.Width = borderLength + sumPadding.Right;
  164. wb.Height = borderLength + sumPadding.Bottom;
  165. if (contentView == null) {
  166. contentView = new ContentView () {
  167. X = wp.X,
  168. Y = wp.Y,
  169. Width = Dim.Fill (wb.Width),
  170. Height = Dim.Fill (wb.Height)
  171. };
  172. } else {
  173. contentView.X = wp.X;
  174. contentView.Y = wp.Y;
  175. contentView.Width = Dim.Fill (wb.Width);
  176. contentView.Height = Dim.Fill (wb.Height);
  177. }
  178. } else {
  179. wb.Width = (2 * borderLength) + sumPadding.Right + sumPadding.Left;
  180. wb.Height = (2 * borderLength) + sumPadding.Bottom + sumPadding.Top;
  181. var cFrame = new Rect (borderLength + sumPadding.Left, borderLength + sumPadding.Top, frame.Width - wb.Width, frame.Height - wb.Height);
  182. if (contentView == null) {
  183. contentView = new ContentView (cFrame);
  184. } else {
  185. contentView.Frame = cFrame;
  186. }
  187. }
  188. if (views != null) {
  189. foreach (var view in views) {
  190. contentView.Add (view);
  191. }
  192. }
  193. if (Subviews?.Count == 0) {
  194. base.Add (contentView);
  195. contentView.Text = base.Text;
  196. }
  197. Border.Child = contentView;
  198. }
  199. void DrawFrame ()
  200. {
  201. DrawFrame (new Rect (0, 0, Frame.Width, Frame.Height), 0, fill: true);
  202. }
  203. /// <summary>
  204. /// Add the specified <see cref="View"/> to this container.
  205. /// </summary>
  206. /// <param name="view"><see cref="View"/> to add to this container</param>
  207. public override void Add (View view)
  208. {
  209. contentView.Add (view);
  210. if (view.CanFocus)
  211. CanFocus = true;
  212. }
  213. /// <summary>
  214. /// Removes a <see cref="View"/> from this container.
  215. /// </summary>
  216. /// <remarks>
  217. /// </remarks>
  218. public override void Remove (View view)
  219. {
  220. if (view == null)
  221. return;
  222. SetNeedsDisplay ();
  223. var touched = view.Frame;
  224. contentView.Remove (view);
  225. if (contentView.InternalSubviews.Count < 1)
  226. this.CanFocus = false;
  227. }
  228. /// <summary>
  229. /// Removes all <see cref="View"/>s from this container.
  230. /// </summary>
  231. /// <remarks>
  232. /// </remarks>
  233. public override void RemoveAll ()
  234. {
  235. contentView.RemoveAll ();
  236. }
  237. ///<inheritdoc/>
  238. public override void Redraw (Rect bounds)
  239. {
  240. var padding = Border.GetSumThickness ();
  241. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  242. if (!NeedDisplay.IsEmpty) {
  243. Driver.SetAttribute (GetNormalColor ());
  244. //Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: true);
  245. Clear ();
  246. }
  247. var savedClip = contentView.ClipToBounds ();
  248. contentView.Redraw (!NeedDisplay.IsEmpty ? contentView.Bounds : bounds);
  249. Driver.Clip = savedClip;
  250. ClearNeedsDisplay ();
  251. if (!IgnoreBorderPropertyOnRedraw) {
  252. Driver.SetAttribute (GetNormalColor ());
  253. //Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: false);
  254. Border.DrawContent (this, false);
  255. if (HasFocus)
  256. Driver.SetAttribute (ColorScheme.HotNormal);
  257. if (Border.DrawMarginFrame)
  258. Driver.DrawWindowTitle (scrRect, Title, padding.Left, padding.Top, padding.Right, padding.Bottom);
  259. Driver.SetAttribute (GetNormalColor ());
  260. } else {
  261. var lc = new LineCanvas ();
  262. if (Border?.BorderStyle != BorderStyle.None) {
  263. lc.AddLine (new Point (0, 0), bounds.Width - 1, Orientation.Horizontal, Border.BorderStyle);
  264. lc.AddLine (new Point (0, 0), bounds.Height - 1, Orientation.Vertical, Border.BorderStyle);
  265. lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Width + 1, Orientation.Horizontal, Border.BorderStyle);
  266. lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Height + 1, Orientation.Vertical, Border.BorderStyle);
  267. }
  268. //foreach (var subview in contentView.Subviews) {
  269. // lc.AddLine (new Point (subview.Frame.X + 1, subview.Frame.Y + 1), subview.Frame.Width - 1, Orientation.Horizontal, subview.Border.BorderStyle);
  270. // lc.AddLine (new Point (subview.Frame.X + 1, subview.Frame.Y + 1), subview.Frame.Height - 1, Orientation.Vertical, subview.Border.BorderStyle);
  271. // lc.AddLine (new Point (subview.Frame.X + subview.Frame.Width, subview.Frame.Y + subview.Frame.Height), -subview.Frame.Width + 1, Orientation.Horizontal, subview.Border.BorderStyle);
  272. // lc.AddLine (new Point (subview.Frame.X + subview.Frame.Width, subview.Frame.Y + subview.Frame.Height), -subview.Frame.Height + 1, Orientation.Vertical, subview.Border.BorderStyle);
  273. //}
  274. Driver.SetAttribute (ColorScheme.Normal);
  275. foreach (var p in lc.GenerateImage (bounds)) {
  276. this.AddRune (p.Key.X, p.Key.Y, p.Value);
  277. }
  278. // Redraw the lines so that focus/drag symbol renders
  279. foreach (var subview in contentView.Subviews) {
  280. // line.DrawSplitterSymbol ();
  281. }
  282. // Draw Titles over Border
  283. foreach (var subview in contentView.Subviews) {
  284. // TODO: Use reflection to see if subview has a Title property
  285. if (subview is FrameView viewWithTite) {
  286. var rect = viewWithTite.Frame;
  287. rect.X = rect.X + 1;
  288. rect.Y = rect.Y + 2;
  289. // TODO: Do focus color correctly
  290. Driver.DrawWindowTitle (rect, viewWithTite.Title, padding.Left, padding.Top, padding.Right, padding.Bottom);
  291. }
  292. }
  293. }
  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. ///<inheritdoc/>
  318. public override bool OnEnter (View view)
  319. {
  320. if (Subviews.Count == 0 || !Subviews.Any (subview => subview.CanFocus)) {
  321. Application.Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  322. }
  323. return base.OnEnter (view);
  324. }
  325. /// <inheritdoc/>
  326. public override void OnCanFocusChanged ()
  327. {
  328. if (contentView != null) {
  329. contentView.CanFocus = CanFocus;
  330. }
  331. base.OnCanFocusChanged ();
  332. }
  333. }
  334. }