-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfocus-subsystem.go
52 lines (48 loc) · 1.22 KB
/
focus-subsystem.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
// Copyright © 2018 Anton Bekker. All rights reserved.
// This file is part of the AGui.
// License information can be found in the LICENSE file.
package gui
// Returns false if "c" does not belong to "w".
func (w *Window) SetFocus(newFocus Control) (ok bool) {
ok = newFocus.Window() == w
if ok {
prevFocus := w.focusedControl
prevFocus.OnFocus(FocusEvent{false, newFocus})
w.focusedControl = newFocus
newFocus.OnFocus(FocusEvent{true, prevFocus})
}
return
}
func (w *Window) ShiftFocus(reverse bool) {
current := w.focusedControl
where := current
for {
candidate := where.FocusCandidate(reverse, current)
switch candidate {
case where:
// Control has decided to become a focus Control.
w.SetFocus(candidate)
return
case nil:
// There is no candidate in "where" and his children.
// Go to upper level and look for next or previous candidates.
current = where
where = where.Parent()
if where == nil {
w.SetFocus(w)
return
}
default:
// Some child is a candidate.
// Try to find first or last candidate in it.
current = nil
where = candidate
}
}
}
func (w *Window) FocusCandidate(_ bool, current Control) Control {
if current == w {
return w.child
}
return w
}