forked from fyne-io/apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
welcome.go
238 lines (205 loc) · 5.8 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
package main
import (
"fmt"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"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
}
canvas.Refresh(img)
}
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 := ioutil.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(objects []fyne.CanvasObject) fyne.Size {
return i.content.MinSize()
}
func loadWelcome(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 := fyne.NewContainerWithLayout(layout.NewGridLayout(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 := fyne.NewContainerWithLayout(&iconHoverLayout{content: form, icon: w.icon}, form, w.icon)
list := widget.NewList(func() int {
return len(apps)
},
func() fyne.CanvasObject {
return widget.NewLabel("A longish app name")
},
func(id int, obj fyne.CanvasObject) {
obj.(*widget.Label).SetText(apps[id].Name)
})
list.OnSelected = func(id int) {
w.loadAppDetail(apps[id])
}
w.install = widget.NewButton("Install", func() {
prog := dialog.NewProgressInfinite("Downloading...", "Please wait while the app is installed", win)
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()
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]))
if len(apps) > 0 {
w.loadAppDetail(apps[0])
}
content := container.NewBorder(details, nil, nil, nil, w.screenScroll)
return container.NewBorder(nil, nil, list, nil,
container.NewBorder(nil, buttons, nil, nil, content))
}
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 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 := ioutil.ReadAll(req.Body)
if err != nil {
fyne.LogError("Failed tread icon data", err)
return ""
}
err = ioutil.WriteFile(tmp, data, 0666)
if err != nil {
fyne.LogError("Failed to get write icon to: "+tmp, err)
return ""
}
return tmp
}