Line.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. namespace Terminal.Gui;
  2. /// <summary>Draws a single line using the <see cref="LineStyle"/> specified by <see cref="View.BorderStyle"/>.</summary>
  3. public class Line : View
  4. {
  5. /// <summary>Constructs a Line object.</summary>
  6. public Line ()
  7. {
  8. BorderStyle = LineStyle.Single;
  9. Border.Thickness = new Thickness (0);
  10. SuperViewRendersLineCanvas = true;
  11. }
  12. public Dim Length { get; set; } = Dim.Fill ();
  13. private Orientation _orientation;
  14. /// <summary>
  15. /// The direction of the line. If you change this you will need to manually update the Width/Height of the
  16. /// control to cover a relevant area based on the new direction.
  17. /// </summary>
  18. public Orientation Orientation
  19. {
  20. get => _orientation;
  21. set
  22. {
  23. _orientation = value;
  24. switch (Orientation)
  25. {
  26. case Orientation.Horizontal:
  27. Height = 1;
  28. // Width = Length;
  29. //Border.Thickness = new Thickness (1, 0, 1, 0);
  30. break;
  31. case Orientation.Vertical:
  32. Height = Length;
  33. Width = 1;
  34. break;
  35. }
  36. }
  37. }
  38. /// <inheritdoc/>
  39. public override void SetBorderStyle (LineStyle value)
  40. {
  41. // The default changes the thickness. We don't want that. We just set the style.
  42. Border.LineStyle = value;
  43. }
  44. /// <inheritdoc/>
  45. public override void OnDrawContent (Rectangle viewport)
  46. {
  47. LineCanvas lc = LineCanvas;
  48. if (SuperViewRendersLineCanvas)
  49. {
  50. lc = SuperView.LineCanvas;
  51. }
  52. if (SuperView is Adornment adornment)
  53. {
  54. lc = adornment.Parent.LineCanvas;
  55. }
  56. Point pos = ViewportToScreen (viewport).Location;
  57. int length = Orientation == Orientation.Horizontal ? Frame.Width : Frame.Height;
  58. if (SuperViewRendersLineCanvas && Orientation == Orientation.Horizontal)
  59. {
  60. pos.Offset (-SuperView.Border.Thickness.Left, 0);
  61. length += SuperView.Border.Thickness.Horizontal;
  62. }
  63. if (SuperViewRendersLineCanvas && Orientation == Orientation.Vertical)
  64. {
  65. pos.Offset (0, -SuperView.Border.Thickness.Top);
  66. length += SuperView.Border.Thickness.Vertical;
  67. }
  68. lc.AddLine (
  69. pos,
  70. length,
  71. Orientation,
  72. BorderStyle
  73. );
  74. }
  75. }