Container.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Xml.Linq;
  6. using Terminal.Gui.Graphs;
  7. namespace Terminal.Gui {
  8. public class Container : View
  9. {
  10. public Container ()
  11. {
  12. IgnoreBorderPropertyOnRedraw = true;
  13. }
  14. public virtual void OnDrawSubViews (Rect clipRect)
  15. {
  16. if (Subviews == null) {
  17. return;
  18. }
  19. foreach (var view in Subviews) {
  20. if (!view.NeedDisplay.IsEmpty || view.ChildNeedsDisplay || view.LayoutNeeded) {
  21. if (true) {//) && (view.Frame.IntersectsWith (boundsAdjustedForBorder) || boundsAdjustedForBorder.X < 0 || bounds.Y < 0)) {
  22. if (view.LayoutNeeded) {
  23. view.LayoutSubviews ();
  24. }
  25. // Draw the subview
  26. // Use the view's bounds (view-relative; Location will always be (0,0)
  27. if (view.Visible && view.Frame.Width > 0 && view.Frame.Height > 0) {
  28. var rect = view.Bounds;
  29. //view.OnDrawContent (rect);
  30. view.Redraw (rect);
  31. //view.OnDrawContentComplete (rect);
  32. }
  33. }
  34. view.NeedDisplay = Rect.Empty;
  35. view.ChildNeedsDisplay = false;
  36. }
  37. }
  38. }
  39. public override void OnDrawContent (Rect viewport)
  40. {
  41. if (!ustring.IsNullOrEmpty (TextFormatter.Text)) {
  42. Clear (viewport);
  43. SetChildNeedsDisplay ();
  44. // Draw any Text
  45. if (TextFormatter != null) {
  46. TextFormatter.NeedsFormat = true;
  47. }
  48. Rect containerBounds = GetContainerBounds ();
  49. TextFormatter?.Draw (ViewToScreen (viewport), HasFocus ? ColorScheme.Focus : GetNormalColor (),
  50. HasFocus ? ColorScheme.HotFocus : Enabled ? ColorScheme.HotNormal : ColorScheme.Disabled,
  51. containerBounds);
  52. }
  53. //base.OnDrawContent (viewport);
  54. }
  55. public override void OnDrawContentComplete (Rect viewport)
  56. {
  57. //base.OnDrawContentComplete (viewport);
  58. }
  59. public override void Redraw (Rect bounds)
  60. {
  61. if (!CanBeVisible (this)) {
  62. return;
  63. }
  64. if (ColorScheme != null) {
  65. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  66. }
  67. OnDrawContent (bounds);
  68. OnDrawSubViews (bounds);
  69. OnDrawContentComplete (bounds);
  70. }
  71. }
  72. public class Frame : Container {
  73. public Label DiagnosticsLabel { get; set; }
  74. public BorderStyle BorderStyle { get; set; } = BorderStyle.None;
  75. public Frame ()
  76. {
  77. IgnoreBorderPropertyOnRedraw = true;
  78. DiagnosticsLabel = new Label () {
  79. AutoSize = false,
  80. X = 0,
  81. Y = Pos.AnchorEnd (1),
  82. Width = Dim.Fill (),
  83. TextAlignment = TextAlignment.Centered
  84. };
  85. Add (DiagnosticsLabel);
  86. SetNeedsLayout ();
  87. }
  88. public Thickness Thickness { get; set; }
  89. public new Rect Bounds {
  90. get {
  91. if (Thickness != null) {
  92. new Rect (Point.Empty, Frame.Size);
  93. }
  94. var frameRelativeBounds = Thickness.GetInnerRect (new Rect (Point.Empty, Frame.Size));
  95. return frameRelativeBounds;
  96. }
  97. set {
  98. throw new InvalidOperationException ("It makes no sense to explicitly set Bounds.");
  99. //Frame = new Rect (Frame.Location, value.Size
  100. // + new Size (Margin.Thickness.Right, Margin.Thickness.Bottom)
  101. // + new Size (BorderFrame.Thickness.Right, BorderFrame.Thickness.Bottom)
  102. // + new Size (BorderFrame.Thickness.Right, BorderFrame.Thickness.Bottom));
  103. }
  104. }
  105. public override void OnDrawContent (Rect viewport)
  106. {
  107. // do nothing
  108. }
  109. public override void Redraw (Rect bounds)
  110. {
  111. if (ColorScheme != null) {
  112. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  113. }
  114. if (Text != null) {
  115. Thickness?.Draw (Frame, $"{Text} {DiagnosticsLabel?.Text}");
  116. }
  117. if (BorderStyle != BorderStyle.None) {
  118. var lc = new LineCanvas ();
  119. lc.AddLine (Frame.Location, Frame.Width - 1, Orientation.Horizontal, BorderStyle);
  120. lc.AddLine (Frame.Location, Frame.Height - 1, Orientation.Vertical, BorderStyle);
  121. lc.AddLine (new Point (Frame.X, Frame.Y + Frame.Height - 1), Frame.Width - 1, Orientation.Horizontal, BorderStyle);
  122. lc.AddLine (new Point (Frame.X + Frame.Width - 1, Frame.Y), Frame.Height - 1, Orientation.Vertical, BorderStyle);
  123. foreach (var p in lc.GenerateImage(Frame)) {
  124. Driver.Move (p.Key.X, p.Key.Y);
  125. Driver.AddRune (p.Value);
  126. }
  127. Driver.DrawWindowTitle (Frame, $"{Text} {Thickness}", 0, 0, 0, 0);
  128. }
  129. base.Redraw (bounds);
  130. }
  131. }
  132. }