LineView.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Text;
  3. namespace Terminal.Gui {
  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 = CM.Glyphs.HLine;
  44. break;
  45. case Orientation.Vertical:
  46. Height = Dim.Fill ();
  47. Width = 1;
  48. LineRune = CM.Glyphs.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. public override void OnDrawContent (Rect contentArea)
  59. {
  60. base.OnDrawContent (contentArea);
  61. Move (0, 0);
  62. Driver.SetAttribute (GetNormalColor ());
  63. var hLineWidth = Math.Max (1, CM.Glyphs.HLine.GetColumns ());
  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. } else {
  71. Move (0, d);
  72. }
  73. Rune rune = LineRune;
  74. if (d == 0) {
  75. rune = StartingAnchor ?? LineRune;
  76. } else
  77. if (d == dEnd - 1) {
  78. rune = EndingAnchor ?? LineRune;
  79. }
  80. Driver.AddRune (rune);
  81. }
  82. }
  83. }
  84. }