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