FrameView.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. Title = title
  146. };
  147. } else {
  148. Border = border;
  149. if (ustring.IsNullOrEmpty (border.Title)) {
  150. border.Title = title;
  151. }
  152. }
  153. AdjustContentView (frame, views);
  154. }
  155. void AdjustContentView (Rect frame, View [] views = null)
  156. {
  157. var borderLength = Border.DrawMarginFrame ? 1 : 0;
  158. var sumPadding = Border.GetSumThickness ();
  159. var wp = new Point ();
  160. var wb = new Size ();
  161. if (frame == Rect.Empty) {
  162. wp.X = borderLength + sumPadding.Left;
  163. wp.Y = borderLength + sumPadding.Top;
  164. wb.Width = borderLength + sumPadding.Right;
  165. wb.Height = borderLength + sumPadding.Bottom;
  166. if (contentView == null) {
  167. contentView = new ContentView () {
  168. X = wp.X,
  169. Y = wp.Y,
  170. Width = Dim.Fill (wb.Width),
  171. Height = Dim.Fill (wb.Height)
  172. };
  173. } else {
  174. contentView.X = wp.X;
  175. contentView.Y = wp.Y;
  176. contentView.Width = Dim.Fill (wb.Width);
  177. contentView.Height = Dim.Fill (wb.Height);
  178. }
  179. } else {
  180. wb.Width = (2 * borderLength) + sumPadding.Right + sumPadding.Left;
  181. wb.Height = (2 * borderLength) + sumPadding.Bottom + sumPadding.Top;
  182. var cFrame = new Rect (borderLength + sumPadding.Left, borderLength + sumPadding.Top, frame.Width - wb.Width, frame.Height - wb.Height);
  183. if (contentView == null) {
  184. contentView = new ContentView (cFrame);
  185. } else {
  186. contentView.Frame = cFrame;
  187. }
  188. }
  189. if (views != null) {
  190. foreach (var view in views) {
  191. contentView.Add (view);
  192. }
  193. }
  194. if (Subviews?.Count == 0) {
  195. base.Add (contentView);
  196. contentView.Text = base.Text;
  197. }
  198. Border.Child = contentView;
  199. }
  200. void DrawFrame ()
  201. {
  202. DrawFrame (new Rect (0, 0, Frame.Width, Frame.Height), 0, fill: true);
  203. }
  204. /// <summary>
  205. /// Add the specified <see cref="View"/> to this container.
  206. /// </summary>
  207. /// <param name="view"><see cref="View"/> to add to this container</param>
  208. public override void Add (View view)
  209. {
  210. contentView.Add (view);
  211. if (view.CanFocus)
  212. CanFocus = true;
  213. }
  214. /// <summary>
  215. /// Removes a <see cref="View"/> from this container.
  216. /// </summary>
  217. /// <remarks>
  218. /// </remarks>
  219. public override void Remove (View view)
  220. {
  221. if (view == null)
  222. return;
  223. SetNeedsDisplay ();
  224. var touched = view.Frame;
  225. contentView.Remove (view);
  226. if (contentView.InternalSubviews.Count < 1)
  227. this.CanFocus = false;
  228. }
  229. /// <summary>
  230. /// Removes all <see cref="View"/>s from this container.
  231. /// </summary>
  232. /// <remarks>
  233. /// </remarks>
  234. public override void RemoveAll ()
  235. {
  236. contentView.RemoveAll ();
  237. }
  238. ///<inheritdoc/>
  239. public override void Redraw (Rect bounds)
  240. {
  241. if (!NeedDisplay.IsEmpty) {
  242. Driver.SetAttribute (GetNormalColor ());
  243. Clear ();
  244. }
  245. var savedClip = contentView.ClipToBounds ();
  246. contentView.Redraw (!NeedDisplay.IsEmpty ? contentView.Bounds : bounds);
  247. Driver.Clip = savedClip;
  248. ClearLayoutNeeded ();
  249. ClearNeedsDisplay ();
  250. if (!IgnoreBorderPropertyOnRedraw) {
  251. Driver.SetAttribute (GetNormalColor ());
  252. Border.DrawContent (this, false);
  253. } else {
  254. var lc = new LineCanvas ();
  255. if (Border?.BorderStyle != BorderStyle.None) {
  256. lc.AddLine (new Point (0, 0), bounds.Width - 1, Orientation.Horizontal, Border.BorderStyle);
  257. lc.AddLine (new Point (0, 0), bounds.Height - 1, Orientation.Vertical, Border.BorderStyle);
  258. lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Width + 1, Orientation.Horizontal, Border.BorderStyle);
  259. lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Height + 1, Orientation.Vertical, Border.BorderStyle);
  260. }
  261. //foreach (var subview in contentView.Subviews) {
  262. // lc.AddLine (new Point (subview.Frame.X + 1, subview.Frame.Y + 1), subview.Frame.Width - 1, Orientation.Horizontal, subview.Border.BorderStyle);
  263. // lc.AddLine (new Point (subview.Frame.X + 1, subview.Frame.Y + 1), subview.Frame.Height - 1, Orientation.Vertical, subview.Border.BorderStyle);
  264. // 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);
  265. // 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);
  266. //}
  267. //Driver.SetAttribute (ColorScheme.Normal);
  268. //foreach (var p in lc.GenerateImage (bounds)) {
  269. // this.AddRune (p.Key.X, p.Key.Y, p.Value);
  270. //}
  271. //// Redraw the lines so that focus/drag symbol renders
  272. //foreach (var subview in contentView.Subviews) {
  273. // // line.DrawSplitterSymbol ();
  274. //}
  275. //// Draw Titles over Border
  276. //foreach (var subview in contentView.Subviews) {
  277. // // TODO: Use reflection to see if subview has a Title property
  278. // if (subview is FrameView viewWithTite) {
  279. // var rect = viewWithTite.Frame;
  280. // rect.X = rect.X + 1;
  281. // rect.Y = rect.Y + 2;
  282. // // TODO: Do focus color correctly
  283. // Driver.DrawWindowTitle (rect, viewWithTite.Title, padding.Left, padding.Top, padding.Right, padding.Bottom);
  284. // }
  285. //}
  286. }
  287. }
  288. /// <summary>
  289. /// The text displayed by the <see cref="Label"/>.
  290. /// </summary>
  291. public override ustring Text {
  292. get => contentView?.Text;
  293. set {
  294. base.Text = value;
  295. if (contentView != null) {
  296. contentView.Text = value;
  297. }
  298. }
  299. }
  300. /// <summary>
  301. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  302. /// </summary>
  303. /// <value>The text alignment.</value>
  304. public override TextAlignment TextAlignment {
  305. get => contentView.TextAlignment;
  306. set {
  307. base.TextAlignment = contentView.TextAlignment = value;
  308. }
  309. }
  310. ///<inheritdoc/>
  311. public override bool OnEnter (View view)
  312. {
  313. if (Subviews.Count == 0 || !Subviews.Any (subview => subview.CanFocus)) {
  314. Application.Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  315. }
  316. return base.OnEnter (view);
  317. }
  318. /// <inheritdoc/>
  319. public override void OnCanFocusChanged ()
  320. {
  321. if (contentView != null) {
  322. contentView.CanFocus = CanFocus;
  323. }
  324. base.OnCanFocusChanged ();
  325. }
  326. }
  327. }