forked from google/gxui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.go
116 lines (99 loc) · 2.16 KB
/
button.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// 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 mixins
import (
"github.com/google/gxui"
"github.com/google/gxui/math"
"github.com/google/gxui/mixins/parts"
)
type ButtonOuter interface {
LinearLayoutOuter
IsChecked() bool
SetChecked(bool)
}
type Button struct {
LinearLayout
parts.Focusable
outer ButtonOuter
theme gxui.Theme
label gxui.Label
buttonType gxui.ButtonType
checked bool
}
func (b *Button) Init(outer ButtonOuter, theme gxui.Theme) {
b.LinearLayout.Init(outer, theme)
b.Focusable.Init(outer)
b.buttonType = gxui.PushButton
b.theme = theme
b.outer = outer
// Interface compliance test
_ = gxui.Button(b)
}
func (b *Button) Label() gxui.Label {
return b.label
}
func (b *Button) Text() string {
if b.label != nil {
return b.label.Text()
} else {
return ""
}
}
func (b *Button) SetText(text string) {
if b.Text() == text {
return
}
if text == "" {
if b.label != nil {
b.RemoveChild(b.label)
b.label = nil
}
} else {
if b.label == nil {
b.label = b.theme.CreateLabel()
b.label.SetMargin(math.ZeroSpacing)
b.AddChild(b.label)
}
b.label.SetText(text)
}
}
func (b *Button) Type() gxui.ButtonType {
return b.buttonType
}
func (b *Button) SetType(buttonType gxui.ButtonType) {
if buttonType != b.buttonType {
b.buttonType = buttonType
b.outer.Redraw()
}
}
func (b *Button) IsChecked() bool {
return b.checked
}
func (b *Button) SetChecked(checked bool) {
if checked != b.checked {
b.checked = checked
b.outer.Redraw()
}
}
// InputEventHandler override
func (b *Button) Click(ev gxui.MouseEvent) (consume bool) {
if ev.Button == gxui.MouseButtonLeft {
if b.buttonType == gxui.ToggleButton {
b.outer.SetChecked(!b.outer.IsChecked())
}
b.LinearLayout.Click(ev)
return true
}
return b.LinearLayout.Click(ev)
}
func (b *Button) KeyPress(ev gxui.KeyboardEvent) (consume bool) {
consume = b.LinearLayout.KeyPress(ev)
if ev.Key == gxui.KeySpace || ev.Key == gxui.KeyEnter {
me := gxui.MouseEvent{
Button: gxui.MouseButtonLeft,
}
return b.Click(me)
}
return
}