Repository navigation
Expand file tree
/
Copy pathbutton.go
More file actions
158 lines (132 loc) · 3.78 KB
/
Copy pathbutton.go
File metadata and controls
158 lines (132 loc) · 3.78 KB
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
package main
import (
"image"
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
api "github.com/unix-streamdeck/api/v2"
)
type button struct {
widget.BaseWidget
editor *editor
keyID int
// key points into editor.currentDeviceConfig.Pages[page].Keys[keyID].Application[""].
// May be nil if the slot hasn't been initialized yet.
key *api.KeyConfigV3
}
func newButton(key *api.KeyConfigV3, id int, e *editor) *button {
b := &button{key: key, keyID: id, editor: e}
b.ExtendBaseWidget(b)
return b
}
// ensureKeyConfig returns a non-nil *KeyConfigV3 for the default ("") application
// of the given KeyV3, allocating the Application map and entry if needed.
func ensureKeyConfig(k *api.KeyV3) *api.KeyConfigV3 {
if k.Application == nil {
k.Application = map[string]*api.KeyConfigV3{}
}
cfg, ok := k.Application[""]
if !ok || cfg == nil {
cfg = &api.KeyConfigV3{}
k.Application[""] = cfg
}
return cfg
}
func (b *button) CreateRenderer() fyne.WidgetRenderer {
icon := canvas.NewImageFromFile(b.iconPath())
text := &canvas.Image{}
border := canvas.NewRectangle(color.Transparent)
border.StrokeWidth = 2
border.SetMinSize(fyne.NewSize(float32(b.editor.currentDevice.IconSize), float32(b.editor.currentDevice.IconSize)))
bg := canvas.NewRectangle(color.Black)
render := &buttonRenderer{border: border, text: text, icon: icon, bg: bg,
objects: []fyne.CanvasObject{bg, icon, text, border}, b: b}
render.Refresh()
return render
}
func (b *button) Tapped(ev *fyne.PointEvent) {
b.editor.editButton(b)
}
func (b *button) iconPath() string {
if b.key == nil {
return ""
}
return b.key.Icon
}
// updateKey reflects the current button's edits back into the active config.
// In v2 the button's key field is a pointer into the live config map, so
// field mutations already flow through; this only cleans up handler markers.
func (b *button) updateKey() {
if b.key == nil {
return
}
if b.key.IconHandler == "Default" {
b.key.IconHandler = ""
}
if b.key.KeyHandler == "Default" {
b.key.KeyHandler = ""
}
}
const (
buttonInset = 2
)
type buttonRenderer struct {
border, bg *canvas.Rectangle
icon, text *canvas.Image
objects []fyne.CanvasObject
b *button
}
func (r *buttonRenderer) Layout(s fyne.Size) {
size := s.Subtract(fyne.NewSize(buttonInset*2, buttonInset*2))
offset := fyne.NewPos(buttonInset, buttonInset)
for _, obj := range r.objects {
obj.Move(offset)
obj.Resize(size)
}
}
func (r *buttonRenderer) MinSize() fyne.Size {
iconSize := fyne.NewSize(float32(r.b.editor.currentDevice.IconSize), float32(r.b.editor.currentDevice.IconSize))
return iconSize.Add(fyne.NewSize(buttonInset*2, buttonInset*2))
}
func (r *buttonRenderer) Refresh() {
if r.b.editor.currentButton == r.b {
r.border.StrokeColor = theme.FocusColor()
} else {
r.border.StrokeColor = &color.Gray{128}
}
r.text.Image = r.textToImage()
r.text.Refresh()
iconPath := r.b.iconPath()
if iconPath != r.icon.File {
r.icon.File = iconPath
go r.icon.Refresh()
}
r.border.Refresh()
}
func (r *buttonRenderer) BackgroundColor() color.Color {
return color.Transparent
}
func (r *buttonRenderer) Objects() []fyne.CanvasObject {
return r.objects
}
func (r *buttonRenderer) Destroy() {
// nothing
}
func (r *buttonRenderer) textToImage() image.Image {
textImg := image.NewNRGBA(image.Rect(0, 0, r.b.editor.currentDevice.IconSize, r.b.editor.currentDevice.IconSize))
if r.b.key == nil || r.b.key.Text == "" {
return textImg
}
img, err := api.DrawText(textImg, r.b.key.Text, api.DrawTextOptions{
FontSize: int64(r.b.key.TextSize),
VerticalAlignment: r.b.key.TextAlignment,
FontFace: r.b.key.FontFace,
Colour: r.b.key.TextColour,
})
if err != nil {
fyne.LogError("Failed to draw text to image", err)
}
return img
}