Line.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #pragma warning disable CS0067 // The event is never used
  30. /// <inheritdoc/>
  31. public event EventHandler<CancelEventArgs<Orientation>> OrientationChanging;
  32. /// <inheritdoc/>
  33. public event EventHandler<EventArgs<Orientation>> OrientationChanged;
  34. #pragma warning restore CS0067 // The event is never used
  35. /// <summary>Called when <see cref="Orientation"/> has changed.</summary>
  36. /// <param name="newOrientation"></param>
  37. public void OnOrientationChanged (Orientation newOrientation)
  38. {
  39. switch (newOrientation)
  40. {
  41. case Orientation.Horizontal:
  42. Height = 1;
  43. Width = Dim.Fill ();
  44. break;
  45. case Orientation.Vertical:
  46. Width = 1;
  47. Height = Dim.Fill ();
  48. break;
  49. }
  50. }
  51. #endregion
  52. /// <inheritdoc/>
  53. protected override bool OnDrawingContent ()
  54. {
  55. Point pos = ViewportToScreen (Viewport).Location;
  56. int length = Orientation == Orientation.Horizontal ? Frame.Width : Frame.Height;
  57. LineCanvas?.AddLine (
  58. pos,
  59. length,
  60. Orientation,
  61. BorderStyle
  62. );
  63. //SuperView?.SetNeedsDraw ();
  64. return true;
  65. }
  66. }