Line.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 
  2. namespace Terminal.Gui.Views;
  3. /// <summary>
  4. /// Draws a single line using the <see cref="LineStyle"/> specified by <see cref="View.BorderStyle"/>.
  5. /// </summary>
  6. /// <remarks>
  7. /// </remarks>
  8. public class Line : View, IOrientation
  9. {
  10. private readonly OrientationHelper _orientationHelper;
  11. /// <summary>Constructs a Line object.</summary>
  12. public Line ()
  13. {
  14. CanFocus = false;
  15. base.SuperViewRendersLineCanvas = true;
  16. _orientationHelper = new (this);
  17. _orientationHelper.Orientation = Orientation.Horizontal;
  18. OnOrientationChanged(Orientation);
  19. }
  20. #region IOrientation members
  21. /// <summary>
  22. /// The direction of the line. If you change this you will need to manually update the Width/Height of the
  23. /// control to cover a relevant area based on the new direction.
  24. /// </summary>
  25. public Orientation Orientation
  26. {
  27. get => _orientationHelper.Orientation;
  28. set => _orientationHelper.Orientation = value;
  29. }
  30. #pragma warning disable CS0067 // The event is never used
  31. /// <inheritdoc/>
  32. public event EventHandler<CancelEventArgs<Orientation>> OrientationChanging;
  33. /// <inheritdoc/>
  34. public event EventHandler<EventArgs<Orientation>> OrientationChanged;
  35. #pragma warning restore CS0067 // The event is never used
  36. /// <summary>Called when <see cref="Orientation"/> has changed.</summary>
  37. /// <param name="newOrientation"></param>
  38. public void OnOrientationChanged (Orientation newOrientation)
  39. {
  40. switch (newOrientation)
  41. {
  42. case Orientation.Horizontal:
  43. Height = 1;
  44. Width = Dim.Fill ();
  45. break;
  46. case Orientation.Vertical:
  47. Width = 1;
  48. Height = Dim.Fill ();
  49. break;
  50. }
  51. }
  52. #endregion
  53. /// <inheritdoc/>
  54. protected override bool OnDrawingContent ()
  55. {
  56. Point pos = ViewportToScreen (Viewport).Location;
  57. int length = Orientation == Orientation.Horizontal ? Frame.Width : Frame.Height;
  58. LineCanvas?.AddLine (
  59. pos,
  60. length,
  61. Orientation,
  62. BorderStyle
  63. );
  64. //SuperView?.SetNeedsDraw ();
  65. return true;
  66. }
  67. }