LineView.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. protected override bool OnDrawingContent (Rectangle viewport)
  47. {
  48. Move (0, 0);
  49. SetAttribute (GetNormalColor ());
  50. int hLineWidth = Math.Max (1, Glyphs.HLine.GetColumns ());
  51. int dEnd = Orientation == Orientation.Horizontal ? Viewport.Width : Viewport.Height;
  52. for (var d = 0; d < dEnd; d += hLineWidth)
  53. {
  54. if (Orientation == Orientation.Horizontal)
  55. {
  56. Move (d, 0);
  57. }
  58. else
  59. {
  60. Move (0, d);
  61. }
  62. Rune rune = LineRune;
  63. if (d == 0)
  64. {
  65. rune = StartingAnchor ?? LineRune;
  66. }
  67. else if (d == dEnd - 1)
  68. {
  69. rune = EndingAnchor ?? LineRune;
  70. }
  71. Driver?.AddRune (rune);
  72. }
  73. return true;
  74. }
  75. }