/
focusable_window.go
69 lines (55 loc) · 1.36 KB
/
focusable_window.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
package windeau
import "github.com/nsf/termbox-go"
type WindowState struct {
FgColor termbox.Attribute
BgColor termbox.Attribute
}
type FocusableWindow struct {
Parent *Window
FocusOn, FocusOff WindowState
Focused bool
}
func MakeFocusableWindow(x, y, w, h int, on, off WindowState, border WindowBorder) *FocusableWindow {
parent := &Window{X: x, Y: y, Width: w, Height: h, Border: border}
return &FocusableWindow{parent, on, off, false}
}
func (c *FocusableWindow) ToggleFocus() {
c.Focused = !c.Focused
}
func (c *FocusableWindow) Draw() {
c.setColors()
c.Parent.Draw()
}
func (c *FocusableWindow) SetParent(parent Drawable) {
window, ok := parent.(*Window)
if ok {
c.Parent = window
}
}
func (c *FocusableWindow) GetColors() (termbox.Attribute, termbox.Attribute) {
return c.Parent.GetColors()
}
func (c *FocusableWindow) IsRoot() bool {
return false
}
func (c *FocusableWindow) IsFocused() bool {
return c.Focused
}
func (c *FocusableWindow) SetFocused(focused bool) {
c.Focused = focused
}
func (c *FocusableWindow) GetRect() Rect {
return c.Parent.GetRect()
}
func (c *FocusableWindow) WithinBox(x, y int) bool {
c.Focused = c.Parent.WithinBox(x, y)
return c.Focused
}
func (c *FocusableWindow) setColors() {
color := c.FocusOff
if c.Focused {
color = c.FocusOn
}
c.Parent.Fg = color.FgColor
c.Parent.Bg = color.BgColor
}