-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshadow.go
86 lines (76 loc) · 2.15 KB
/
shadow.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
package dos
import "github.com/gdamore/tcell/v2"
// A Shadow draws a shadow covering one cell below, and two cells right of its
// bounding box. This is useful for dialog windows or buttons that need depth.
type Shadow struct {
Child Widget
Style tcell.Style
MakeSmall bool
}
func (s *Shadow) HandleMouse(currentRect Rect, ev *tcell.EventMouse) bool {
if s.Child != nil {
return s.Child.HandleMouse(currentRect, ev)
}
return false
}
func (s *Shadow) HandleKey(ev *tcell.EventKey) bool {
if s.Child != nil {
return s.Child.HandleKey(ev)
}
return false
}
func (s *Shadow) SetFocused(b bool) {
if s.Child != nil {
s.Child.SetFocused(b)
}
}
func (s *Shadow) DisplaySize(boundsW, boundsH int) (w, h int) {
if s.Child != nil {
return s.Child.DisplaySize(boundsW, boundsH)
}
return 0, 0
}
func (s *Shadow) shadowCell(x, y int, screen tcell.Screen) (width int) {
r, combc, _, width := screen.GetContent(x, y)
screen.SetContent(x, y, r, combc, s.Style)
return width
}
// Draw causes the Shadow to intentionally set cells outside its provided rect.
// The provided rect is passed directly to the child.
func (s *Shadow) Draw(rect Rect, screen tcell.Screen) {
// TODO: make Shadow extents configurable
reversedStyle := s.Style.Reverse(true)
// Right side
width := 2
if s.MakeSmall {
// Draw this in the top right corner
screen.SetContent(rect.X+rect.W, rect.Y, '▄', nil, reversedStyle)
width = 1
}
for row := rect.Y + 1; row < rect.Y+rect.H; row++ {
// Draw side two columns wide
for col := rect.X + rect.W; col < rect.X+rect.W+width; col++ {
width := s.shadowCell(col, row, screen)
if width > 1 {
// If we are in the first iteration of the col loop, this will
// prevent us from accessing the next col of an east-asian rune.
col++
}
}
}
// Bottom side
for col := rect.X + 1; col < rect.X+rect.W+width; col++ {
if s.MakeSmall {
screen.SetContent(col, rect.Y+rect.H, '▀', nil, reversedStyle)
} else {
width := s.shadowCell(col, rect.Y+rect.H, screen)
if width > 1 {
col++ // Step over additional cell of east-asian characters
}
}
}
// Draw child
if s.Child != nil {
s.Child.Draw(rect, screen)
}
}