Container.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. 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. // BUGBUG: v2 - shouldn't this be !view.LayoutNeeded? Why draw if layout is going to happen and we'll just draw again?
  20. if (view.LayoutNeeded) {
  21. view.LayoutSubviews ();
  22. }
  23. if ((view.Visible && !view.NeedDisplay.IsEmpty && view.Frame.Width > 0 && view.Frame.Height > 0) || view.ChildNeedsDisplay) {
  24. view.Redraw (view.Bounds);
  25. view.NeedDisplay = Rect.Empty;
  26. // BUGBUG - v2 why does this need to be set to false?
  27. // Shouldn't it be set when the subviews draw?
  28. view.ChildNeedsDisplay = false;
  29. }
  30. }
  31. }
  32. public override void OnDrawContent (Rect viewport)
  33. {
  34. if (!ustring.IsNullOrEmpty (TextFormatter.Text)) {
  35. Clear (viewport);
  36. SetChildNeedsDisplay ();
  37. // Draw any Text
  38. if (TextFormatter != null) {
  39. TextFormatter.NeedsFormat = true;
  40. Rect containerBounds = GetContainerBounds ();
  41. TextFormatter?.Draw (ViewToScreen (viewport), HasFocus ? ColorScheme.Focus : GetNormalColor (),
  42. HasFocus ? ColorScheme.HotFocus : Enabled ? ColorScheme.HotNormal : ColorScheme.Disabled,
  43. containerBounds);
  44. }
  45. }
  46. }
  47. public override void Redraw (Rect bounds)
  48. {
  49. if (!CanBeVisible (this)) {
  50. return;
  51. }
  52. if (ColorScheme != null) {
  53. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  54. }
  55. OnDrawContent (bounds);
  56. OnDrawSubViews (bounds);
  57. OnDrawContentComplete (bounds);
  58. }
  59. }
  60. /// <summary>
  61. /// A <see cref="Container"/> used for the rectangles that compose the outer frames of a <see cref="View"/>.
  62. /// </summary>
  63. public class Frame : Container {
  64. //public Label DiagnosticsLabel { get; set; }
  65. // TODO: v2 = This is teporary; need to also enable (or not) simple way of setting
  66. // other border properties
  67. // TOOD: v2 - Missing 3D effect
  68. public BorderStyle BorderStyle { get; set; } = BorderStyle.None;
  69. public Thickness Thickness { get; set; }
  70. // TODO: v2 - This is confusing. It is a read-only property and actually only returns a size, so
  71. // should not be a Rect. However, it may make sense to keep it a Rect and support negative Location
  72. // for scrolling. Still noodling this.
  73. /// <summary>
  74. /// Gets the rectangle that describes the inner area of the frame. The Location is always 0, 0.
  75. /// </summary>
  76. public new Rect Bounds {
  77. get {
  78. if (Thickness != null) {
  79. new Rect (Point.Empty, Frame.Size);
  80. }
  81. // Return the frame-relative bounds
  82. return Thickness.GetInnerRect (new Rect (Point.Empty, Frame.Size));
  83. }
  84. set {
  85. throw new InvalidOperationException ("It makes no sense to explicitly set Bounds.");
  86. }
  87. }
  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. //if (Text != null) {
  98. // Thickness?.Draw (Frame, $"{Text} {DiagnosticsLabel?.Text}");
  99. //}
  100. if (BorderStyle != BorderStyle.None) {
  101. var lc = new LineCanvas ();
  102. lc.AddLine (Frame.Location, Frame.Width - 1, Orientation.Horizontal, BorderStyle);
  103. lc.AddLine (Frame.Location, Frame.Height - 1, Orientation.Vertical, BorderStyle);
  104. lc.AddLine (new Point (Frame.X, Frame.Y + Frame.Height - 1), Frame.Width - 1, Orientation.Horizontal, BorderStyle);
  105. lc.AddLine (new Point (Frame.X + Frame.Width - 1, Frame.Y), Frame.Height - 1, Orientation.Vertical, BorderStyle);
  106. foreach (var p in lc.GenerateImage (Frame)) {
  107. Driver.Move (p.Key.X, p.Key.Y);
  108. Driver.AddRune (p.Value);
  109. }
  110. if (!ustring.IsNullOrEmpty (Title)) {
  111. Driver.DrawWindowTitle (Frame, Title, 0, 0, 0, 0);
  112. }
  113. }
  114. base.Redraw (bounds);
  115. }
  116. }
  117. }