LineView.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. namespace Terminal.Gui;
  2. /// <summary>A straight line control either horizontal or vertical</summary>
  3. public class LineView : View
  4. {
  5. /// <summary>Creates a horizontal line</summary>
  6. public LineView () : this (Orientation.Horizontal) { }
  7. /// <summary>Creates a horizontal or vertical line based on <paramref name="orientation"/></summary>
  8. public LineView (Orientation orientation)
  9. {
  10. CanFocus = false;
  11. switch (orientation)
  12. {
  13. case Orientation.Horizontal:
  14. Height = Dim.Auto (minimumContentDim: 1);
  15. Width = Dim.Fill ();
  16. LineRune = Glyphs.HLine;
  17. break;
  18. case Orientation.Vertical:
  19. Height = Dim.Fill ();
  20. Width = Dim.Auto (minimumContentDim: 1);
  21. LineRune = Glyphs.VLine;
  22. break;
  23. default:
  24. throw new ArgumentException ($"Unknown Orientation {orientation}");
  25. }
  26. Orientation = orientation;
  27. }
  28. /// <summary>
  29. /// The rune to display at the end of the line (right end of horizontal line or bottom end of vertical). If not
  30. /// specified then <see cref="LineRune"/> is used
  31. /// </summary>
  32. public Rune? EndingAnchor { get; set; }
  33. /// <summary>The symbol to use for drawing the line</summary>
  34. public Rune LineRune { get; set; }
  35. /// <summary>
  36. /// The direction of the line. If you change this you will need to manually update the Width/Height of the
  37. /// control to cover a relevant area based on the new direction.
  38. /// </summary>
  39. public Orientation Orientation { get; set; }
  40. /// <summary>
  41. /// The rune to display at the start of the line (left end of horizontal line or top end of vertical) If not
  42. /// specified then <see cref="LineRune"/> is used
  43. /// </summary>
  44. public Rune? StartingAnchor { get; set; }
  45. /// <summary>Draws the line including any starting/ending anchors</summary>
  46. public override void OnDrawContent (Rectangle viewport)
  47. {
  48. base.OnDrawContent (viewport);
  49. Move (0, 0);
  50. Driver.SetAttribute (GetNormalColor ());
  51. int hLineWidth = Math.Max (1, Glyphs.HLine.GetColumns ());
  52. int dEnd = Orientation == Orientation.Horizontal ? Viewport.Width : Viewport.Height;
  53. for (var d = 0; d < dEnd; d += hLineWidth)
  54. {
  55. if (Orientation == Orientation.Horizontal)
  56. {
  57. Move (d, 0);
  58. }
  59. else
  60. {
  61. Move (0, d);
  62. }
  63. Rune rune = LineRune;
  64. if (d == 0)
  65. {
  66. rune = StartingAnchor ?? LineRune;
  67. }
  68. else if (d == dEnd - 1)
  69. {
  70. rune = EndingAnchor ?? LineRune;
  71. }
  72. Driver.AddRune (rune);
  73. }
  74. }
  75. }