LineView.cs 2.4 KB

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