LineView.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 
  2. namespace Terminal.Gui.Views;
  3. /// <summary>A straight line control either horizontal or vertical</summary>
  4. public class LineView : View
  5. {
  6. /// <summary>Creates a horizontal line</summary>
  7. public LineView () : this (Orientation.Horizontal) { }
  8. /// <summary>Creates a horizontal or vertical line based on <paramref name="orientation"/></summary>
  9. public LineView (Orientation orientation)
  10. {
  11. CanFocus = false;
  12. switch (orientation)
  13. {
  14. case Orientation.Horizontal:
  15. Height = Dim.Auto (minimumContentDim: 1);
  16. Width = Dim.Fill ();
  17. LineRune = Glyphs.HLine;
  18. break;
  19. case Orientation.Vertical:
  20. Height = Dim.Fill ();
  21. Width = Dim.Auto (minimumContentDim: 1);
  22. LineRune = Glyphs.VLine;
  23. break;
  24. default:
  25. throw new ArgumentException ($"Unknown Orientation {orientation}");
  26. }
  27. Orientation = orientation;
  28. }
  29. /// <summary>
  30. /// The rune to display at the end of the line (right end of horizontal line or bottom end of vertical). If not
  31. /// specified then <see cref="LineRune"/> is used
  32. /// </summary>
  33. public Rune? EndingAnchor { get; set; }
  34. /// <summary>The symbol to use for drawing the line</summary>
  35. public Rune LineRune { get; set; }
  36. /// <summary>
  37. /// The direction of the line. If you change this you will need to manually update the Width/Height of the
  38. /// control to cover a relevant area based on the new direction.
  39. /// </summary>
  40. public Orientation Orientation { get; set; }
  41. /// <summary>
  42. /// The rune to display at the start of the line (left end of horizontal line or top end of vertical) If not
  43. /// specified then <see cref="LineRune"/> is used
  44. /// </summary>
  45. public Rune? StartingAnchor { get; set; }
  46. /// <summary>Draws the line including any starting/ending anchors</summary>
  47. protected override bool OnDrawingContent ()
  48. {
  49. Move (0, 0);
  50. SetAttribute (GetAttributeForRole (VisualRole.Normal));
  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. return true;
  75. }
  76. }