Line.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. private Orientation _orientation;
  13. /// <summary>
  14. /// The direction of the line. If you change this you will need to manually update the Width/Height of the
  15. /// control to cover a relevant area based on the new direction.
  16. /// </summary>
  17. public Orientation Orientation
  18. {
  19. get => _orientation;
  20. set
  21. {
  22. _orientation = value;
  23. switch (Orientation)
  24. {
  25. case Orientation.Horizontal:
  26. Height = 1;
  27. break;
  28. case Orientation.Vertical:
  29. Width = 1;
  30. break;
  31. }
  32. }
  33. }
  34. /// <inheritdoc/>
  35. public override void SetBorderStyle (LineStyle value)
  36. {
  37. // The default changes the thickness. We don't want that. We just set the style.
  38. Border.LineStyle = value;
  39. }
  40. /// <inheritdoc/>
  41. public override void OnDrawContent (Rectangle viewport)
  42. {
  43. LineCanvas lc = LineCanvas;
  44. if (SuperViewRendersLineCanvas)
  45. {
  46. lc = SuperView.LineCanvas;
  47. }
  48. if (SuperView is Adornment adornment)
  49. {
  50. lc = adornment.Parent.LineCanvas;
  51. }
  52. Point pos = ViewportToScreen (viewport).Location;
  53. int length = Orientation == Orientation.Horizontal ? Frame.Width : Frame.Height;
  54. if (SuperViewRendersLineCanvas && Orientation == Orientation.Horizontal)
  55. {
  56. pos.Offset (-SuperView.Border.Thickness.Left, 0);
  57. length += SuperView.Border.Thickness.Horizontal;
  58. }
  59. if (SuperViewRendersLineCanvas && Orientation == Orientation.Vertical)
  60. {
  61. pos.Offset (0, -SuperView.Border.Thickness.Top);
  62. length += SuperView.Border.Thickness.Vertical;
  63. }
  64. lc.AddLine (
  65. pos,
  66. length,
  67. Orientation,
  68. BorderStyle
  69. );
  70. }
  71. }