forked from google/gxui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfocusable_part.go
87 lines (66 loc) · 2.09 KB
/
focusable_part.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
// 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 gxui
// Focusable is the optional interface implmented by controls that have the ability to acquire focus.
// A control with focus will receive keyboard input first.
type Focusable interface {
Control
// IsFocusable returns true if the control is currently in a state where it can acquire focus.
IsFocusable() bool
// HasFocus returns true when the control has focus.
HasFocus() bool
// GainedFocus is called when the Focusable gains focus.
// This method is called by the FocusManager should not be called by the user.
GainedFocus()
// LostFocus is called when the Focusable loses focus.
// This method is called by the FocusManager should not be called by the user.
LostFocus()
// OnGainedFocus subscribes f to be called whenever the control gains focus.
OnGainedFocus(callback func()) EventSubscription
// OnLostFocus subscribes f to be called whenever the control loses focus.
OnLostFocus(callback func()) EventSubscription
}
type FocusablePart struct {
onGainedFocus Event
onLostFocus Event
hasFocus bool
focusable bool
}
func (f *FocusablePart) Init() {
f.focusable = true
}
// gxui.Control compliance
func (f *FocusablePart) IsFocusable() bool {
return f.focusable
}
func (f *FocusablePart) HasFocus() bool {
return f.hasFocus
}
func (f *FocusablePart) SetFocusable(bool) {
f.focusable = true
}
func (f *FocusablePart) OnGainedFocus(callback func()) EventSubscription {
if f.onGainedFocus == nil {
f.onGainedFocus = CreateEvent(f.GainedFocus)
}
return f.onGainedFocus.Listen(callback)
}
func (f *FocusablePart) OnLostFocus(callback func()) EventSubscription {
if f.onLostFocus == nil {
f.onLostFocus = CreateEvent(f.LostFocus)
}
return f.onLostFocus.Listen(callback)
}
func (f *FocusablePart) GainedFocus() {
f.hasFocus = true
if f.onGainedFocus != nil {
f.onGainedFocus.Emit()
}
}
func (f *FocusablePart) LostFocus() {
f.hasFocus = false
if f.onLostFocus != nil {
f.onLostFocus.Emit()
}
}