-
Notifications
You must be signed in to change notification settings - Fork 26
/
recent.go
68 lines (60 loc) · 1.27 KB
/
recent.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
package main
import (
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/widget"
)
const preferenceKeyRecent = "recentlist"
func addRecent(u fyne.URI, p fyne.Preferences) {
old := loadRecents(p)
if len(old) > 0 {
if old[0].String() == u.String() {
return
}
for i, u2 := range old {
if u2.String() == u.String() {
if i < len(old)-1 {
old = old[:i]
} else {
old = append(old[:i], old[i+1:]...)
}
}
}
}
all := append([]fyne.URI{u}, old...)
str := ""
for _, u := range all {
str += "|" + u.String()
}
p.SetString(preferenceKeyRecent, str[1:])
}
func loadRecents(p fyne.Preferences) []fyne.URI {
var ret []fyne.URI
val := p.String(preferenceKeyRecent)
for _, s := range strings.Split(val, "|") {
u, err := storage.ParseURI(s)
if err == nil {
ret = append(ret, u)
}
}
return ret
}
func makeRecentList(p fyne.Preferences, f func(fyne.URI)) *widget.List {
recents := loadRecents(p)
r := widget.NewList(
func() int {
return len(recents)
},
func() fyne.CanvasObject {
return widget.NewLabel("")
},
func(id widget.ListItemID, o fyne.CanvasObject) {
name := recents[id].Name()
o.(*widget.Label).SetText(name)
})
r.OnSelected = func(id widget.ListItemID) {
f(recents[id])
}
return r
}