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<EventArgs<Orientation>> OrientationChanged;
  31. /// <summary>Called when <see cref="Orientation"/> has changed.</summary>
  32. /// <param name="newOrientation"></param>
  33. public void OnOrientationChanged (Orientation newOrientation)
  34. {
  35. switch (newOrientation)
  36. {
  37. case Orientation.Horizontal:
  38. Height = 1;
  39. break;
  40. case Orientation.Vertical:
  41. Width = 1;
  42. break;
  43. }
  44. }
  45. #endregion
  46. /// <inheritdoc/>
  47. public override void SetBorderStyle (LineStyle value)
  48. {
  49. // The default changes the thickness. We don't want that. We just set the style.
  50. Border.LineStyle = value;
  51. }
  52. /// <inheritdoc/>
  53. protected override bool OnDrawingContent (Rectangle viewport)
  54. {
  55. LineCanvas lc = LineCanvas;
  56. if (SuperViewRendersLineCanvas)
  57. {
  58. lc = SuperView?.LineCanvas;
  59. }
  60. if (SuperView is Adornment adornment)
  61. {
  62. lc = adornment.Parent?.LineCanvas;
  63. }
  64. Point pos = ViewportToScreen (viewport).Location;
  65. int length = Orientation == Orientation.Horizontal ? Frame.Width : Frame.Height;
  66. if (SuperView is {} && SuperViewRendersLineCanvas && Orientation == Orientation.Horizontal)
  67. {
  68. pos.Offset (-SuperView.Border.Thickness.Left, 0);
  69. length += SuperView.Border.Thickness.Horizontal;
  70. }
  71. if (SuperView is { } && SuperViewRendersLineCanvas && Orientation == Orientation.Vertical)
  72. {
  73. pos.Offset (0, -SuperView.Border.Thickness.Top);
  74. length += SuperView.Border.Thickness.Vertical;
  75. }
  76. lc?.AddLine (
  77. pos,
  78. length,
  79. Orientation,
  80. BorderStyle
  81. );
  82. return true;
  83. }
  84. }