View2.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using Terminal.Gui.Graphs;
  6. namespace Terminal.Gui {
  7. public class Container : View
  8. {
  9. public Container ()
  10. {
  11. IgnoreBorderPropertyOnRedraw = true;
  12. }
  13. public virtual void OnDrawSubViews (Rect clipRect)
  14. {
  15. if (Subviews == null) {
  16. return;
  17. }
  18. foreach (var view in Subviews) {
  19. if (!view.NeedDisplay.IsEmpty || view.ChildNeedsDisplay || view.LayoutNeeded) {
  20. if (view.Frame.IntersectsWith (clipRect)) {// && (view.Frame.IntersectsWith (boundsAdjustedForBorder) || boundsAdjustedForBorder.X < 0 || bounds.Y < 0)) {
  21. if (view.LayoutNeeded) {
  22. view.LayoutSubviews ();
  23. }
  24. // Draw the subview
  25. // Use the view's bounds (view-relative; Location will always be (0,0)
  26. if (view.Visible && view.Frame.Width > 0 && view.Frame.Height > 0) {
  27. var rect = view.Bounds;
  28. //view.OnDrawContent (rect);
  29. view.Redraw (rect);
  30. //view.OnDrawContentComplete (rect);
  31. }
  32. }
  33. view.NeedDisplay = Rect.Empty;
  34. view.ChildNeedsDisplay = false;
  35. }
  36. }
  37. }
  38. public override void OnDrawContent (Rect viewport)
  39. {
  40. if (!ustring.IsNullOrEmpty (TextFormatter.Text)) {
  41. Clear (viewport);
  42. SetChildNeedsDisplay ();
  43. // Draw any Text
  44. if (TextFormatter != null) {
  45. TextFormatter.NeedsFormat = true;
  46. }
  47. Rect containerBounds = GetContainerBounds ();
  48. TextFormatter?.Draw (ViewToScreen (viewport), HasFocus ? ColorScheme.Focus : GetNormalColor (),
  49. HasFocus ? ColorScheme.HotFocus : Enabled ? ColorScheme.HotNormal : ColorScheme.Disabled,
  50. containerBounds);
  51. }
  52. //base.OnDrawContent (viewport);
  53. }
  54. public override void OnDrawContentComplete (Rect viewport)
  55. {
  56. //base.OnDrawContentComplete (viewport);
  57. }
  58. public override void Redraw (Rect bounds)
  59. {
  60. if (!CanBeVisible (this)) {
  61. return;
  62. }
  63. if (ColorScheme != null) {
  64. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  65. }
  66. OnDrawContent (bounds);
  67. OnDrawSubViews (bounds);
  68. OnDrawContentComplete (bounds);
  69. }
  70. }
  71. public class Frame : Container {
  72. public Label DiagnosticsLabel { get; set; }
  73. public BorderStyle BorderStyle { get; set; } = BorderStyle.None;
  74. public Frame ()
  75. {
  76. IgnoreBorderPropertyOnRedraw = true;
  77. DiagnosticsLabel = new Label () {
  78. AutoSize = false,
  79. X = 0,
  80. Y = Pos.AnchorEnd (1),
  81. Width = Dim.Fill (),
  82. TextAlignment = TextAlignment.Centered
  83. };
  84. Add (DiagnosticsLabel);
  85. SetNeedsLayout ();
  86. }
  87. public Thickness Thickness { get; set; }
  88. public override void OnDrawContent (Rect viewport)
  89. {
  90. // do nothing
  91. }
  92. public override void Redraw (Rect bounds)
  93. {
  94. if (ColorScheme != null) {
  95. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  96. }
  97. Thickness.Draw (Frame, $"{Text} {DiagnosticsLabel.Text}");
  98. if (BorderStyle != BorderStyle.None) {
  99. var lc = new LineCanvas ();
  100. lc.AddLine (Frame.Location, Frame.Width - 1, Orientation.Horizontal, BorderStyle);
  101. lc.AddLine (Frame.Location, Frame.Height - 1, Orientation.Vertical, BorderStyle);
  102. lc.AddLine (new Point (Frame.X, Frame.Y + Frame.Height - 1), Frame.Width - 1, Orientation.Horizontal, BorderStyle);
  103. lc.AddLine (new Point (Frame.X + Frame.Width - 1, Frame.Y), Frame.Height - 1, Orientation.Vertical, BorderStyle);
  104. lc.Draw (this, Frame);
  105. Driver.DrawWindowTitle (Frame, $"{Text} {Thickness}", 0, 0, 0, 0);
  106. }
  107. base.Redraw (bounds);
  108. }
  109. }
  110. public class View2 : Container {
  111. public Frame Margin { get; set; }
  112. public new Frame Border { get; set; }
  113. public Frame Padding{ get; set; }
  114. public View2 ()
  115. {
  116. IgnoreBorderPropertyOnRedraw = true;
  117. Margin = new Frame () {
  118. Text = "Margin",
  119. Thickness = new Thickness (15, 2, 15, 4),
  120. ColorScheme = Colors.ColorSchemes ["Error"]
  121. };
  122. //Margin.DiagnosticsLabel.Text = "Margin";
  123. Border = new Frame () {
  124. Text = "Border",
  125. BorderStyle = BorderStyle.Single,
  126. Thickness = new Thickness (2),
  127. ColorScheme = Colors.ColorSchemes ["Dialog"]
  128. };
  129. Padding = new Frame () {
  130. Text = "Padding",
  131. Thickness = new Thickness (3),
  132. ColorScheme = Colors.ColorSchemes ["Toplevel"]
  133. };
  134. SetNeedsLayout ();
  135. }
  136. public override void LayoutSubviews ()
  137. {
  138. Margin.X = Frame.Location.X;
  139. Margin.Y = Frame.Location.Y;
  140. Margin.Width = Frame.Size.Width;
  141. Margin.Height = Frame.Size.Height;
  142. Margin.SetNeedsLayout ();
  143. Margin.LayoutSubviews ();
  144. Margin.SetNeedsDisplay ();
  145. var border = Margin.Thickness.GetInnerRect (Frame);
  146. Border.X = border.Location.X;
  147. Border.Y = border.Location.Y;
  148. Border.Width = border.Size.Width;
  149. Border.Height = border.Size.Height;
  150. Border.SetNeedsLayout ();
  151. Border.LayoutSubviews ();
  152. Border.SetNeedsDisplay ();
  153. var padding = Border.Thickness.GetInnerRect (border);
  154. Padding.X = padding.Location.X;
  155. Padding.Y = padding.Location.Y;
  156. Padding.Width = padding.Size.Width;
  157. Padding.Height = padding.Size.Height;
  158. Padding.SetNeedsLayout ();
  159. Padding.LayoutSubviews ();
  160. Padding.SetNeedsDisplay ();
  161. Bounds = Padding.Thickness.GetInnerRect (padding);
  162. base.LayoutSubviews ();
  163. }
  164. public virtual void OnDrawFrames (Rect frame)
  165. {
  166. Margin.Redraw (Margin.Bounds);
  167. Border.Redraw (Border.Bounds);
  168. Padding.Redraw (Border.Bounds);
  169. var border = Margin.Thickness.GetInnerRect (frame);
  170. var padding = Border.Thickness.GetInnerRect (border);
  171. var content = Padding.Thickness.GetInnerRect (padding);
  172. // Draw the diagnostics label on the bottom of the content
  173. var tf = new TextFormatter () {
  174. Text = "Content",
  175. Alignment = TextAlignment.Centered,
  176. VerticalAlignment = VerticalTextAlignment.Bottom
  177. };
  178. tf.Draw (content, ColorScheme.Normal, ColorScheme.Normal);
  179. }
  180. public override void Redraw (Rect bounds)
  181. {
  182. if (!CanBeVisible (this)) {
  183. return;
  184. }
  185. if (ColorScheme != null) {
  186. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  187. }
  188. OnDrawFrames (Frame);
  189. base.Redraw (bounds);
  190. }
  191. protected override void Dispose (bool disposing)
  192. {
  193. base.Dispose (disposing);
  194. Margin?.Dispose ();
  195. Margin = null;
  196. Border?.Dispose ();
  197. Border = null;
  198. Padding?.Dispose ();
  199. Padding = null;
  200. }
  201. }
  202. }