forked from richardwilkes/unison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.go
270 lines (248 loc) · 7.59 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright ©2021-2022 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
package unison
import (
"time"
"github.com/richardwilkes/toolbox"
"github.com/richardwilkes/unison/enums/align"
"github.com/richardwilkes/unison/enums/side"
)
// DefaultButtonTheme holds the default ButtonTheme values for Buttons. Modifying this data will not alter existing
// Buttons, but will alter any Buttons created in the future.
var DefaultButtonTheme = ButtonTheme{
Font: SystemFont,
BackgroundInk: ControlColor,
OnBackgroundInk: OnControlColor,
EdgeInk: ControlEdgeColor,
SelectionInk: SelectionColor,
OnSelectionInk: OnSelectionColor,
Gap: 3,
CornerRadius: 4,
HMargin: 8,
VMargin: 1,
DrawableOnlyHMargin: 3,
DrawableOnlyVMargin: 3,
ClickAnimationTime: 100 * time.Millisecond,
HAlign: align.Middle,
VAlign: align.Middle,
Side: side.Left,
HideBase: false,
}
// DefaultSVGButtonTheme holds the default ButtonTheme values for SVG Buttons. Modifying this data will not alter
// existing Buttons, but will alter any Buttons created in the future.
var DefaultSVGButtonTheme = ButtonTheme{
Font: SystemFont,
BackgroundInk: ControlColor,
OnBackgroundInk: IconButtonColor,
EdgeInk: ControlEdgeColor,
SelectionInk: ControlColor,
OnSelectionInk: IconButtonPressedColor,
RolloverInk: IconButtonRolloverColor,
Gap: 3,
CornerRadius: 4,
HMargin: 8,
VMargin: 1,
DrawableOnlyHMargin: 3,
DrawableOnlyVMargin: 3,
ClickAnimationTime: 100 * time.Millisecond,
HAlign: align.Middle,
VAlign: align.Middle,
Side: side.Left,
HideBase: false,
}
// ButtonTheme holds theming data for a Button.
type ButtonTheme struct {
Font Font
BackgroundInk Ink
OnBackgroundInk Ink
EdgeInk Ink
SelectionInk Ink
OnSelectionInk Ink
RolloverInk Ink
Gap float32
CornerRadius float32
HMargin float32
VMargin float32
DrawableOnlyHMargin float32
DrawableOnlyVMargin float32
ClickAnimationTime time.Duration
HAlign align.Enum
VAlign align.Enum
Side side.Enum
HideBase bool
Sticky bool
}
// Button represents a clickable button.
type Button struct {
GroupPanel
ButtonTheme
ClickCallback func()
Drawable Drawable
Text string
cache TextCache
Pressed bool
rollover bool
}
// NewButton creates a new button.
func NewButton() *Button {
b := &Button{ButtonTheme: DefaultButtonTheme}
b.Self = b
b.SetFocusable(true)
b.SetSizer(b.DefaultSizes)
b.DrawCallback = b.DefaultDraw
b.GainedFocusCallback = b.DefaultFocusGained
b.LostFocusCallback = b.MarkForRedraw
b.MouseDownCallback = b.DefaultMouseDown
b.MouseDragCallback = b.DefaultMouseDrag
b.MouseUpCallback = b.DefaultMouseUp
b.MouseEnterCallback = b.DefaultMouseEnter
b.MouseExitCallback = b.DefaultMouseExit
b.KeyDownCallback = b.DefaultKeyDown
b.UpdateCursorCallback = b.DefaultUpdateCursor
return b
}
// NewSVGButton creates an SVG icon button with a size equal to the default button theme's font baseline.
func NewSVGButton(svg *SVG) *Button {
b := NewButton()
b.ButtonTheme = DefaultSVGButtonTheme
b.HideBase = true
baseline := b.Font.Baseline()
b.Drawable = &DrawableSVG{
SVG: svg,
Size: NewSize(baseline, baseline).Ceil(),
}
return b
}
// DefaultSizes provides the default sizing.
func (b *Button) DefaultSizes(hint Size) (minSize, prefSize, maxSize Size) {
prefSize = LabelSize(b.cache.Text(b.Text, b.Font), b.Drawable, b.Side, b.Gap)
if b.Text == "" && toolbox.IsNil(b.Drawable) {
prefSize.Height = b.Font.LineHeight()
}
if border := b.Border(); border != nil {
prefSize = prefSize.Add(border.Insets().Size())
}
prefSize.Width += b.HorizontalMargin() * 2
prefSize.Height += b.VerticalMargin() * 2
if !b.HideBase {
prefSize.Width += 2
prefSize.Height += 2
}
prefSize = prefSize.Ceil().ConstrainForHint(hint)
return prefSize, prefSize, MaxSize(prefSize)
}
// HorizontalMargin returns the horizontal margin that will be used.
func (b *Button) HorizontalMargin() float32 {
if b.Text == "" && b.Drawable != nil {
return b.DrawableOnlyHMargin
}
return b.HMargin
}
// VerticalMargin returns the vertical margin that will be used.
func (b *Button) VerticalMargin() float32 {
if b.Text == "" && b.Drawable != nil {
return b.DrawableOnlyVMargin
}
return b.VMargin
}
// DefaultFocusGained provides the default focus gained handling.
func (b *Button) DefaultFocusGained() {
b.ScrollIntoView()
b.MarkForRedraw()
}
// DefaultDraw provides the default drawing.
func (b *Button) DefaultDraw(canvas *Canvas, _ Rect) {
var fg, bg Ink
switch {
case b.Pressed || (b.Sticky && b.Selected()):
bg = b.SelectionInk
fg = b.OnSelectionInk
case b.rollover && !b.disabled && b.RolloverInk != nil:
bg = b.BackgroundInk
fg = b.RolloverInk
default:
bg = b.BackgroundInk
fg = b.OnBackgroundInk
}
r := b.ContentRect(false)
if !b.HideBase || b.Focused() {
thickness := float32(1)
if b.Focused() {
thickness++
}
DrawRoundedRectBase(canvas, r, b.CornerRadius, thickness, bg, b.EdgeInk)
r = r.Inset(NewUniformInsets(thickness + 0.5))
}
r = r.Inset(NewSymmetricInsets(b.HorizontalMargin(), b.VerticalMargin()))
DrawLabel(canvas, r, b.HAlign, b.VAlign, b.cache.Text(b.Text, b.Font), fg, b.Drawable, b.Side, b.Gap, !b.Enabled())
}
// Click makes the button behave as if a user clicked on it.
func (b *Button) Click() {
b.SetSelected(true)
pressed := b.Pressed
b.Pressed = true
b.MarkForRedraw()
b.FlushDrawing()
b.Pressed = pressed
time.Sleep(b.ClickAnimationTime)
b.MarkForRedraw()
if b.ClickCallback != nil {
b.ClickCallback()
}
}
// DefaultMouseDown provides the default mouse down handling.
func (b *Button) DefaultMouseDown(_ Point, _, _ int, _ Modifiers) bool {
b.Pressed = true
b.MarkForRedraw()
return true
}
// DefaultMouseDrag provides the default mouse drag handling.
func (b *Button) DefaultMouseDrag(where Point, _ int, _ Modifiers) bool {
if pressed := where.In(b.ContentRect(false)); pressed != b.Pressed {
b.Pressed = pressed
b.MarkForRedraw()
}
return true
}
// DefaultMouseUp provides the default mouse up handling.
func (b *Button) DefaultMouseUp(where Point, _ int, _ Modifiers) bool {
b.Pressed = false
b.MarkForRedraw()
if where.In(b.ContentRect(false)) {
b.SetSelected(true)
if b.ClickCallback != nil {
b.ClickCallback()
}
}
return true
}
// DefaultMouseEnter provides the default mouse enter handling.
func (b *Button) DefaultMouseEnter(_ Point, _ Modifiers) bool {
b.rollover = true
b.MarkForRedraw()
return true
}
// DefaultMouseExit provides the default mouse exit handling.
func (b *Button) DefaultMouseExit() bool {
b.rollover = false
b.MarkForRedraw()
return true
}
// DefaultKeyDown provides the default key down handling.
func (b *Button) DefaultKeyDown(keyCode KeyCode, mod Modifiers, _ bool) bool {
if IsControlAction(keyCode, mod) {
b.Click()
return true
}
return false
}
// DefaultUpdateCursor provides the default cursor for buttons.
func (b *Button) DefaultUpdateCursor(_ Point) *Cursor {
return PointingCursor()
}