-
Notifications
You must be signed in to change notification settings - Fork 7
/
welcome.go
334 lines (285 loc) · 7.51 KB
/
welcome.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"fmt"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"sort"
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/cmd/fyne/commands"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
type welcome struct {
shownApp App
name, summary, date *widget.Label
developer, version *widget.Label
link *widget.Hyperlink
icon *canvas.Image
screenshots [5]*canvas.Image
screenScroll *container.Scroll
install *widget.Button
}
func (w *welcome) loadAppDetail(app App) {
w.shownApp = app
w.name.SetText(app.Name)
w.developer.SetText(app.Developer)
w.version.SetText(app.Version)
w.date.SetText(app.Date.Format("02 Jan 2006"))
w.summary.SetText(app.Summary)
w.icon.Resource = nil
go setImageFromURL(w.icon, app.Icon)
for i := 0; i < len(w.screenshots); i++ {
w.screenshots[i].Resource = nil
if i < len(app.Screenshots) {
w.screenshots[i].Show()
go setImageFromURL(w.screenshots[i], app.Screenshots[i].Image)
} else {
w.screenshots[i].Hide()
}
w.screenshots[i].Refresh()
}
w.screenScroll.ScrollToTop()
parsed, err := url.Parse(app.Website)
if err != nil {
w.link.SetText("")
return
}
w.link.SetText(parsed.Host)
w.link.SetURL(parsed)
installedVer := installedVersion(app)
installed := installedVer != "" && installedVer == app.Version
if installed || app.Source.Package == "fyne.io/apps" {
w.install.SetText("Installed")
w.install.Disable()
} else if installedVer == "" {
w.install.SetText("Install")
w.install.Enable()
} else {
w.install.SetText("Upgrade")
w.install.Enable()
}
}
func setImageFromURL(img *canvas.Image, location string) {
if location == "" {
return
}
res, err := loadResourceFromURL(location)
if err != nil {
img.Resource = theme.WarningIcon()
} else {
img.Resource = res
}
img.Refresh()
}
func loadResourceFromURL(urlStr string) (fyne.Resource, error) {
res, err := http.Get(urlStr)
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status code: %d", res.StatusCode)
}
bytes, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
parsed, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
name := filepath.Base(parsed.Path)
return fyne.NewStaticResource(name, bytes), nil
}
// iconHoverLayout specifies a layout that floats an icon image top right over other content
type iconHoverLayout struct {
content, icon fyne.CanvasObject
}
func (i *iconHoverLayout) Layout(_ []fyne.CanvasObject, size fyne.Size) {
i.content.Resize(size)
i.icon.Resize(fyne.NewSize(64, 64))
i.icon.Move(fyne.NewPos(size.Width-i.icon.Size().Width, 0))
}
func (i *iconHoverLayout) MinSize(_ []fyne.CanvasObject) fyne.Size {
return i.content.MinSize()
}
func loadUI(apps AppList, win fyne.Window) fyne.CanvasObject {
w := &welcome{}
w.name = widget.NewLabel("")
w.developer = widget.NewLabel("")
w.link = widget.NewHyperlink("", nil)
w.summary = widget.NewLabel("")
w.summary.Wrapping = fyne.TextWrapWord
w.version = widget.NewLabel("")
w.date = widget.NewLabel("")
w.icon = &canvas.Image{}
w.icon.FillMode = canvas.ImageFillContain
makeScreenshots(w)
dateAndVersion := container.NewGridWithColumns(2, w.date,
widget.NewForm(&widget.FormItem{Text: "Version", Widget: w.version}))
form := widget.NewForm(
&widget.FormItem{Text: "Name", Widget: w.name},
&widget.FormItem{Text: "Developer", Widget: w.developer},
&widget.FormItem{Text: "Website", Widget: w.link},
&widget.FormItem{Text: "Summary", Widget: w.summary},
&widget.FormItem{Text: "Date", Widget: dateAndVersion},
)
details := container.New(&iconHoverLayout{content: form, icon: w.icon}, form, w.icon)
var app, featured *fyne.Container
nodes := mapAppList(apps)
tree := widget.NewTree(
func(id widget.TreeNodeID) []widget.TreeNodeID {
return nodes[id]
},
func(id widget.TreeNodeID) bool {
if id == "" {
return true
}
if id == "featured" {
return false
}
for _, n := range nodes[""] {
if n == id {
return true
}
}
return false
},
func(_ bool) fyne.CanvasObject {
return widget.NewLabel(" -> A longish app name")
},
func(id widget.TreeNodeID, branch bool, obj fyne.CanvasObject) {
if id == "featured" {
obj.(*widget.Label).SetText("Featured")
return
}
if branch {
title := id
title = strings.ToUpper(id[:1]) + title[1:]
obj.(*widget.Label).SetText(title)
return
}
obj.(*widget.Label).SetText(apps[id].Name)
})
selectApp := func(id string) {
selected := apps[id]
tree.OpenBranch(selected.Category)
tree.Select(id)
app.Show()
featured.Hide()
w.loadAppDetail(selected)
}
tree.Select("featured")
tree.OnSelected = func(id widget.TreeNodeID) {
if id == "featured" {
featured.Show()
app.Hide()
return
}
for _, n := range nodes[""] {
if n == id {
tree.OpenBranch(id)
return
}
}
selectApp(id)
}
w.install = widget.NewButton("Install", func() {
bar := widget.NewProgressBarInfinite()
content := container.NewVBox(widget.NewLabel("Please wait while the app is installed"), bar)
prog := dialog.NewCustomWithoutButtons("Downloading...", content, win)
bar.Start()
prog.Show()
get := commands.NewGetter()
tmpIcon := downloadIcon(w.shownApp.Icon)
get.SetIcon(tmpIcon)
get.SetAppID(w.shownApp.ID)
err := get.Get(w.shownApp.Source.Package)
prog.Hide()
bar.Stop()
if err != nil {
dialog.ShowError(err, win)
} else {
dialog.ShowInformation("Installed", "App was installed successfully :)", win)
markInstalled(w.shownApp)
w.loadAppDetail(w.shownApp)
}
_ = os.Remove(tmpIcon)
})
buttons := container.NewHBox(
layout.NewSpacer(),
w.install,
)
w.screenScroll = container.NewHScroll(container.NewHBox(
w.screenshots[0], w.screenshots[1], w.screenshots[2], w.screenshots[3], w.screenshots[4]))
content := container.NewBorder(details, nil, nil, nil, w.screenScroll)
app = container.NewBorder(nil, buttons, nil, nil, content)
featured = makeFeatured(apps, selectApp)
app.Hide()
return container.NewBorder(nil, nil, tree, nil,
container.NewStack(featured, app))
}
func makeScreenshots(w *welcome) {
for i := 0; i < len(w.screenshots); i++ {
img := &canvas.Image{}
img.SetMinSize(fyne.NewSize(320, 240))
img.FillMode = canvas.ImageFillContain
w.screenshots[i] = img
}
}
func mapAppList(list AppList) map[string][]string {
ret := make(map[string][]string)
for id, a := range list {
cat := a.Category
if cat == "" {
continue
}
similar, ok := ret[cat]
if !ok {
similar = []string{}
}
similar = append(similar, id)
ret[cat] = similar
}
var cats []string
for cat, ids := range ret {
sort.Slice(ids, func(i, j int) bool {
return strings.Compare(strings.ToLower(list[ids[i]].Name), strings.ToLower(list[ids[j]].Name)) < 0
})
cats = append(cats, cat)
}
sort.Slice(cats, func(i, j int) bool {
return strings.Compare(cats[i], cats[j]) < 0
})
ret[""] = append([]string{"featured"}, cats...)
return ret
}
func downloadIcon(url string) string {
req, err := http.Get(url)
if err != nil {
fyne.LogError("Failed to access icon url: "+url, err)
return ""
}
tmp := filepath.Join(os.TempDir(), "Fyne-Icon.png")
data, err := io.ReadAll(req.Body)
if err != nil {
fyne.LogError("Failed tread icon data", err)
return ""
}
err = os.WriteFile(tmp, data, 0666)
if err != nil {
fyne.LogError("Failed to get write icon to: "+tmp, err)
return ""
}
return tmp
}