forked from google/gxui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
focusable.go
65 lines (53 loc) · 1.26 KB
/
focusable.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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package parts
import (
"github.com/google/gxui"
)
type FocusableOuter interface{}
type Focusable struct {
outer FocusableOuter
focusable bool
hasFocus bool
onGainedFocus gxui.Event
onLostFocus gxui.Event
}
func (f *Focusable) Init(outer FocusableOuter) {
f.outer = outer
f.focusable = true
}
// gxui.Control compliance
func (f *Focusable) IsFocusable() bool {
return f.focusable
}
func (f *Focusable) HasFocus() bool {
return f.hasFocus
}
func (f *Focusable) SetFocusable(bool) {
f.focusable = true
}
func (f *Focusable) OnGainedFocus(l func()) gxui.EventSubscription {
if f.onGainedFocus == nil {
f.onGainedFocus = gxui.CreateEvent(f.GainedFocus)
}
return f.onGainedFocus.Listen(l)
}
func (f *Focusable) OnLostFocus(l func()) gxui.EventSubscription {
if f.onLostFocus == nil {
f.onLostFocus = gxui.CreateEvent(f.LostFocus)
}
return f.onLostFocus.Listen(l)
}
func (f *Focusable) GainedFocus() {
f.hasFocus = true
if f.onGainedFocus != nil {
f.onGainedFocus.Fire()
}
}
func (f *Focusable) LostFocus() {
f.hasFocus = false
if f.onLostFocus != nil {
f.onLostFocus.Fire()
}
}