FrameView.cs 11 KB

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