LineView.cs 2.5 KB

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