forked from fyne-io/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
218 lines (175 loc) · 4.79 KB
/
main.go
File metadata and controls
218 lines (175 loc) · 4.79 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
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
package main
import (
"flag"
"fmt"
"image/color"
"strings"
"fyne.io/fyne"
"fyne.io/fyne/app"
"fyne.io/fyne/canvas"
"fyne.io/fyne/layout"
"fyne.io/fyne/theme"
"fyne.io/fyne/widget"
"github.com/fyne-io/examples/bugs"
"github.com/fyne-io/examples/calculator"
"github.com/fyne-io/examples/clock"
"github.com/fyne-io/examples/fractal"
"github.com/fyne-io/examples/img/icon"
"github.com/fyne-io/examples/life"
"github.com/fyne-io/examples/solitaire"
"github.com/fyne-io/examples/sudoku"
"github.com/fyne-io/examples/xkcd"
)
type appInfo struct {
name string
icon fyne.Resource
canv bool
run func(fyne.App)
}
var apps []appInfo
func welcome(app fyne.App) {
w := app.NewWindow("Examples")
grid := fyne.NewContainerWithLayout(layout.NewGridLayout(2))
group1 := widget.NewGroup("Canvas")
grid.AddObject(group1)
group2 := widget.NewGroup("Widgets")
grid.AddObject(group2)
for _, launch := range apps {
list := group1
if !launch.canv {
list = group2
}
list.Append(newExampleButton(launch, app))
}
w.SetContent(grid)
w.Show()
}
func main() {
apps = append(apps, appInfo{"Calculator", icon.CalculatorBitmap, false, calculator.Show})
apps = append(apps, appInfo{"Bugs", icon.BugBitmap, false, bugs.Show})
apps = append(apps, appInfo{"Sudoku", icon.SudokuBitmap, false, sudoku.Show})
apps = append(apps, appInfo{"XKCD", icon.XKCDBitmap, false, xkcd.Show})
apps = append(apps, appInfo{"Clock", icon.ClockBitmap, true, clock.Show})
apps = append(apps, appInfo{"Fractal", icon.FractalBitmap, true, fractal.Show})
apps = append(apps, appInfo{"Life", icon.Life, true, life.Show})
apps = append(apps, appInfo{"Solitaire", icon.SolitaireBitmap, true, solitaire.Show})
flags := make(map[string]*bool)
for _, launch := range apps {
name := strings.ToLower(launch.name)
flags[name] = flag.Bool(name, false, fmt.Sprintf("Launch %s app directly", name))
}
flag.Parse()
launch := welcome
for ex, set := range flags {
if *set {
for _, a := range apps {
if strings.ToLower(a.name) == ex {
launch = a.run
}
}
break
}
}
ex := app.New()
launch(ex)
ex.Run()
}
type exampleButtonRenderer struct {
icon *canvas.Image
label *canvas.Text
objects []fyne.CanvasObject
button *exampleButton
}
func (b *exampleButtonRenderer) MinSize() fyne.Size {
baseSize := b.label.MinSize()
baseSize = baseSize.Add(fyne.NewSize(24, 24))
return baseSize.Add(fyne.NewSize(theme.Padding()*4, theme.Padding()*2))
}
func (b *exampleButtonRenderer) Layout(size fyne.Size) {
inner := size.Subtract(fyne.NewSize(theme.Padding()*4, theme.Padding()*2))
inner = inner.Subtract(fyne.NewSize(24, 24))
height := b.button.size.Height
b.label.Resize(inner)
b.label.Move(fyne.NewPos(theme.Padding()*2+24, theme.Padding()+24))
if b.icon != nil {
b.icon.Resize(fyne.NewSize(height, height))
b.icon.Move(fyne.NewPos(0, 0))
}
}
func (b *exampleButtonRenderer) ApplyTheme() {
b.label.Color = theme.TextColor()
b.label.TextSize = theme.TextSize() * 2
b.Refresh()
}
func (b *exampleButtonRenderer) BackgroundColor() color.Color {
return theme.ButtonColor()
}
func (b *exampleButtonRenderer) Refresh() {
b.Layout(b.button.Size())
canvas.Refresh(b.button)
}
func (b *exampleButtonRenderer) Objects() []fyne.CanvasObject {
return b.objects
}
func (b *exampleButtonRenderer) Destroy() {
}
type exampleButton struct {
Text string
Icon fyne.Resource
OnTapped func()
size fyne.Size
pos fyne.Position
}
func (b *exampleButton) Size() fyne.Size {
return b.size
}
func (b *exampleButton) Resize(size fyne.Size) {
b.size = size
widget.Renderer(b).Layout(size)
}
func (b *exampleButton) Position() fyne.Position {
return b.pos
}
func (b *exampleButton) Move(pos fyne.Position) {
b.pos = pos
widget.Renderer(b).Refresh()
}
func (b *exampleButton) MinSize() fyne.Size {
if widget.Renderer(b) == nil {
return fyne.NewSize(0, 0)
}
return widget.Renderer(b).MinSize()
}
func (b *exampleButton) Show() {
}
func (b *exampleButton) Hide() {
}
func (b *exampleButton) Visible() bool {
return true
}
func (b *exampleButton) Tapped(*fyne.PointEvent) {
b.OnTapped()
}
func (b *exampleButton) TappedSecondary(*fyne.PointEvent) {
}
func (b *exampleButton) CreateRenderer() fyne.WidgetRenderer {
var objects []fyne.CanvasObject
var img *canvas.Image
if b.Icon != nil {
img = canvas.NewImageFromResource(b.Icon)
img.Translucency = 0.25
objects = append(objects, img)
}
text := canvas.NewText(b.Text, theme.TextColor())
text.TextSize = theme.TextSize() * 2
text.Alignment = fyne.TextAlignTrailing
objects = append(objects, text)
return &exampleButtonRenderer{img, text, objects, b}
}
func newExampleButton(info appInfo, app fyne.App) *exampleButton {
button := &exampleButton{Text: info.name, Icon: info.icon, OnTapped: func() {
info.run(app)
}}
widget.Renderer(button).Layout(button.MinSize())
return button
}