-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbox.go
90 lines (77 loc) · 2.04 KB
/
box.go
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
package dos
import (
"github.com/gdamore/tcell/v2"
)
// The BoxDecoration allows for an individual rune per side and corner of the
// box being drawn. See https://en.wikipedia.org/wiki/Box-drawing_character
type BoxDecoration struct {
Hor, Vert rune // Horizontal and vertical sides
TL, TR, BR, BL rune // Clockwise corners
JointT, JointR rune // Joints (for menus and other divided boxes)
JointB, JointL rune
Style tcell.Style
}
// WithStyle is a helper function to make a copy of the BoxDecoration with a
// different style.
func (b BoxDecoration) WithStyle(style tcell.Style) BoxDecoration {
b.Style = style
return b
}
// A Box draws an enclosed rectangle around its Child. A Box assumes each rune
// has a width of one terminal cell so double-wide characters will not be drawn
// correctly using a Box and BoxDecoration combination.
type Box struct {
Child Widget
Decoration *BoxDecoration
}
var DefaultBoxDecoration = BoxDecoration{
Hor: '─',
Vert: '│',
TL: '┌',
TR: '┐',
BR: '┘',
BL: '└',
JointT: '┬',
JointR: '┤',
JointB: '┴',
JointL: '├',
Style: tcell.StyleDefault,
}
func (b *Box) HandleMouse(currentRect Rect, ev *tcell.EventMouse) bool {
if b.Child != nil {
return b.Child.HandleMouse(currentRect, ev)
}
return false
}
func (b *Box) HandleKey(ev *tcell.EventKey) bool {
if b.Child != nil {
return b.Child.HandleKey(ev)
}
return false
}
func (b *Box) SetFocused(v bool) {
if b.Child != nil {
b.Child.SetFocused(v)
}
}
func (b *Box) DisplaySize(boundsW, boundsH int) (w, h int) {
if b.Child != nil {
childW, childH := b.Child.DisplaySize(boundsW-2, boundsH-2)
return childW + 2, childH + 2
}
return 0, 0
}
func (b *Box) Draw(rect Rect, s tcell.Screen) {
// Do not draw if not even a single cell of room
if rect.W < 1 || rect.H < 1 {
return
}
decoration := b.Decoration
if decoration == nil {
decoration = &DefaultBoxDecoration
}
DrawBox(rect, decoration, s)
if b.Child != nil {
b.Child.Draw(Rect{rect.X + 1, rect.Y + 1, rect.W - 1, rect.H - 1}, s)
}
}