Line.cs 2.2 KB

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