forked from marcusolsson/tui-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
focus.go
88 lines (78 loc) · 1.98 KB
/
focus.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
package tui
// FocusChain enables custom focus traversal when Tab or Backtab is pressed.
type FocusChain interface {
FocusNext(w Widget) Widget
FocusPrev(w Widget) Widget
FocusDefault() Widget
}
type kbFocusController struct {
focusedWidget Widget
chain FocusChain
}
func (c *kbFocusController) OnKeyEvent(e KeyEvent) {
if c.chain == nil {
return
}
switch e.Key {
case KeyTab:
if c.focusedWidget != nil {
c.focusedWidget.SetFocused(false)
c.focusedWidget = c.chain.FocusNext(c.focusedWidget)
c.focusedWidget.SetFocused(true)
}
case KeyBacktab:
if c.focusedWidget != nil {
c.focusedWidget.SetFocused(false)
c.focusedWidget = c.chain.FocusPrev(c.focusedWidget)
c.focusedWidget.SetFocused(true)
}
}
}
// DefaultFocusChain is the default focus chain.
var DefaultFocusChain = &SimpleFocusChain{
widgets: make([]Widget, 0),
}
// SimpleFocusChain represents a ring of widgets where focus is loops to the
// first widget when it reaches the end.
type SimpleFocusChain struct {
widgets []Widget
}
// Set sets the widgets in the focus chain. Widgets will received focus in the
// order widgets were passed.
func (c *SimpleFocusChain) Set(ws ...Widget) {
c.widgets = ws
}
// FocusNext returns the widget in the ring that is after the given widget.
func (c *SimpleFocusChain) FocusNext(current Widget) Widget {
for i, w := range c.widgets {
if w != current {
continue
}
if i < len(c.widgets)-1 {
return c.widgets[i 1]
}
return c.widgets[0]
}
return nil
}
// FocusPrev returns the widget in the ring that is before the given widget.
func (c *SimpleFocusChain) FocusPrev(current Widget) Widget {
for i, w := range c.widgets {
if w != current {
continue
}
if i <= 0 {
return c.widgets[len(c.widgets)-1]
}
return c.widgets[i-1]
}
return nil
}
// FocusDefault returns the default widget for when there is no widget
// currently focused.
func (c *SimpleFocusChain) FocusDefault() Widget {
if len(c.widgets) == 0 {
return nil
}
return c.widgets[0]
}