Line.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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, IOrientation
  4. {
  5. private readonly OrientationHelper _orientationHelper;
  6. /// <summary>Constructs a Line object.</summary>
  7. public Line ()
  8. {
  9. BorderStyle = LineStyle.Single;
  10. Border.Thickness = new Thickness (0);
  11. SuperViewRendersLineCanvas = true;
  12. _orientationHelper = new (this);
  13. _orientationHelper.Orientation = Orientation.Horizontal;
  14. _orientationHelper.OrientationChanging += (sender, e) => OrientationChanging?.Invoke (this, e);
  15. _orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e);
  16. }
  17. #region IOrientation members
  18. /// <summary>
  19. /// The direction of the line. If you change this you will need to manually update the Width/Height of the
  20. /// control to cover a relevant area based on the new direction.
  21. /// </summary>
  22. public Orientation Orientation
  23. {
  24. get => _orientationHelper.Orientation;
  25. set => _orientationHelper.Orientation = value;
  26. }
  27. /// <inheritdoc/>
  28. public event EventHandler<CancelEventArgs<Orientation>> OrientationChanging;
  29. /// <inheritdoc/>
  30. public event EventHandler<CancelEventArgs<Orientation>> OrientationChanged;
  31. /// <summary>Called when <see cref="Orientation"/> has changed.</summary>
  32. /// <param name="oldOrientation"></param>
  33. /// <param name="newOrientation"></param>
  34. public void OnOrientationChanged (Orientation oldOrientation, Orientation newOrientation)
  35. {
  36. switch (Orientation)
  37. {
  38. case Orientation.Horizontal:
  39. Height = 1;
  40. break;
  41. case Orientation.Vertical:
  42. Width = 1;
  43. break;
  44. }
  45. }
  46. #endregion
  47. /// <inheritdoc/>
  48. public override void SetBorderStyle (LineStyle value)
  49. {
  50. // The default changes the thickness. We don't want that. We just set the style.
  51. Border.LineStyle = value;
  52. }
  53. /// <inheritdoc/>
  54. public override void OnDrawContent (Rectangle viewport)
  55. {
  56. LineCanvas lc = LineCanvas;
  57. if (SuperViewRendersLineCanvas)
  58. {
  59. lc = SuperView.LineCanvas;
  60. }
  61. if (SuperView is Adornment adornment)
  62. {
  63. lc = adornment.Parent.LineCanvas;
  64. }
  65. Point pos = ViewportToScreen (viewport).Location;
  66. int length = Orientation == Orientation.Horizontal ? Frame.Width : Frame.Height;
  67. if (SuperViewRendersLineCanvas && Orientation == Orientation.Horizontal)
  68. {
  69. pos.Offset (-SuperView.Border.Thickness.Left, 0);
  70. length += SuperView.Border.Thickness.Horizontal;
  71. }
  72. if (SuperViewRendersLineCanvas && Orientation == Orientation.Vertical)
  73. {
  74. pos.Offset (0, -SuperView.Border.Thickness.Top);
  75. length += SuperView.Border.Thickness.Vertical;
  76. }
  77. lc.AddLine (
  78. pos,
  79. length,
  80. Orientation,
  81. BorderStyle
  82. );
  83. }
  84. }