FrameView.cs 11 KB

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