This repository has been archived by the owner on Oct 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
wrap.go
208 lines (169 loc) · 4.05 KB
/
wrap.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
package main
import (
"fmt"
"image/color"
"reflect"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
var current fyne.CanvasObject
type jsonResource struct {
fyne.Resource `json:"-"`
}
func (r *jsonResource) MarshalJSON() ([]byte, error) {
icon := "\"" + iconReverse[fmt.Sprintf("%p", r.Resource)] + "\""
return []byte(icon), nil
}
type overlayContainer struct {
widget.BaseWidget
c, parent *fyne.Container
}
func (o *overlayContainer) CreateRenderer() fyne.WidgetRenderer {
border := canvas.NewRectangle(color.Transparent)
border.StrokeWidth = 4
return &overRender{p: o, c: o.c, r: border}
}
func (o *overlayContainer) GoString() string {
return widgets["*fyne.Container"].gostring(o.c)
}
func (o *overlayContainer) MinSize() fyne.Size {
min := o.c.MinSize()
if min.IsZero() {
return fyne.NewSize(theme.IconInlineSize(), theme.IconInlineSize())
}
return min
}
func (o *overlayContainer) Move(p fyne.Position) {
o.c.Move(p)
o.BaseWidget.Move(p)
}
func (o *overlayContainer) Refresh() {
o.BaseWidget.Refresh()
o.c.Refresh()
}
func (o *overlayContainer) Resize(s fyne.Size) {
o.c.Resize(s)
o.BaseWidget.Resize(s)
}
func (o *overlayContainer) Tapped(e *fyne.PointEvent) {
setCurrent(o)
choose(o.c)
}
func (o *overlayContainer) Object() fyne.CanvasObject {
return o.c
}
type overlayWidget struct {
widget.BaseWidget
child fyne.Widget
parent *fyne.Container
}
func (w *overlayWidget) CreateRenderer() fyne.WidgetRenderer {
border := canvas.NewRectangle(color.Transparent)
border.StrokeWidth = 4
return &overRender{p: w, r: border}
}
func (w *overlayWidget) GoString() string {
name := reflect.TypeOf(w.child).String()
if widgets[name].gostring != nil {
return widgets[name].gostring(w.child)
}
return fmt.Sprintf("%#v", w.child)
}
func (w *overlayWidget) Object() fyne.CanvasObject {
return w.child
}
func (w *overlayWidget) Packages() []string {
name := reflect.TypeOf(w.child).String()
if widgets[name].packages != nil {
return widgets[name].packages(w.child)
}
return []string{"widget"}
}
func (w *overlayWidget) Refresh() {
w.BaseWidget.Refresh()
w.child.Refresh()
}
func (w *overlayWidget) Tapped(e *fyne.PointEvent) {
setCurrent(w)
choose(w.child)
}
type overRender struct {
p fyne.CanvasObject
c *fyne.Container
r *canvas.Rectangle
}
func (o overRender) BackgroundColor() color.Color {
return color.Transparent
}
func (o overRender) Destroy() {
}
func (o overRender) Layout(s fyne.Size) {
o.r.Resize(s)
}
func (o overRender) MinSize() fyne.Size {
return fyne.Size{}
}
func (o overRender) Objects() []fyne.CanvasObject {
if o.c == nil {
return []fyne.CanvasObject{o.r}
}
return append([]fyne.CanvasObject{o.r}, o.c.Objects...)
}
func (o overRender) Refresh() {
if o.p == current {
o.r.StrokeColor = theme.PrimaryColor()
} else {
o.r.StrokeColor = color.Transparent
}
o.r.Refresh()
}
func setCurrent(o fyne.CanvasObject) {
old := current
current = o
if old != nil {
old.Refresh()
}
current.Refresh()
}
func wrapContent(o fyne.CanvasObject, parent *fyne.Container) fyne.CanvasObject {
switch obj := o.(type) {
case *fyne.Container:
var c *fyne.Container
if obj.Layout == nil {
c = container.NewWithoutLayout()
} else {
c = container.New(obj.Layout)
}
items := make([]fyne.CanvasObject, len(obj.Objects))
for i, child := range obj.Objects {
items[i] = wrapContent(child, c)
}
c.Objects = items
o := &overlayContainer{c: c, parent: parent}
layoutProps[o.c] = map[string]string{"layout": "VBox"}
o.ExtendBaseWidget(o)
return o
case fyne.Widget:
return wrapWidget(obj, parent)
}
return nil //?
}
func wrapWidget(w fyne.Widget, parent *fyne.Container) fyne.CanvasObject {
switch t := w.(type) {
case *widget.Icon:
t.Resource = wrapResource(t.Resource)
case *widget.Button:
if t.Icon != nil {
t.Icon = wrapResource(t.Icon)
}
}
o := &overlayWidget{child: w, parent: parent}
o.ExtendBaseWidget(o)
return container.NewMax(w, o)
}
func wrapResource(r fyne.Resource) fyne.Resource {
return &jsonResource{r}
}