-
Notifications
You must be signed in to change notification settings - Fork 0
/
menubar.go
377 lines (335 loc) · 9.57 KB
/
menubar.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package main
import (
"fmt"
"runtime"
"strconv"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"fyne.io/fyne/v2/cmd/fyne_settings/settings"
ilp "github.com/BieHDC/fic/imagelistplayer"
)
type Menubar struct {
imgplayer *ilp.ImagePlayer
selectedfolder string
}
type contextMenuButton struct {
widget.Button
menu *fyne.Menu
}
func (b *contextMenuButton) Tapped(pe *fyne.PointEvent) {
//we add 1unit down so clicking again closes the menu instead of invoking the first action
widget.ShowPopUpMenuAtPosition(b.menu, fyne.CurrentApp().Driver().CanvasForObject(b), pe.AbsolutePosition.AddXY(0, 1))
}
func newContextMenuButton(label string, menu *fyne.Menu) *contextMenuButton {
b := &contextMenuButton{menu: menu}
b.Text = label
b.ExtendBaseWidget(b)
return b
}
func (v *Viewer) makeMenu(w fyne.Window) fyne.CanvasObject {
setNewFolder := func(lu fyne.ListableURI) {
v.InvalidateImageCache()
v.rootdir = lu
v.refreshFileTree(v.rootdir)
v.filetree.OpenBranch(v.rootdir.String())
v.filetree.ScrollToTop()
v.filetree.Select(v.rootdir.String())
}
openfolder := widget.NewButtonWithIcon("Open Folder", theme.FolderOpenIcon(), func() {
fo := dialog.NewFolderOpen(func(lu fyne.ListableURI, err error) {
if err != nil {
v.setStatus(err.Error())
return
}
if lu == nil {
v.setStatus("opening cancelled")
return
}
setNewFolder(lu)
}, w)
fo.SetLocation(v.rootdir)
fo.Show()
fo.Resize(fo.MinSize().Add(fo.MinSize()))
})
w.SetOnDropped(func(_ fyne.Position, urix []fyne.URI) {
if len(urix) < 1 {
return
}
uri := urix[0]
if uri == nil {
return
}
isDir, _ := storage.CanList(uri)
if !isDir {
parent := parentfromfile(uri)
if parent == nil {
//fmt.Println("cannot open parent for file:", uri.String())
return
}
defer v.imgplayer.SeekToData(uri.String())
uri = parent
}
lu, _ := storage.ListerForURI(uri)
setNewFolder(lu)
})
subfolders := widget.NewCheck("", func(_ bool) {})
threads := newNumEntry()
workers := func() uint {
asuint, err := strconv.Atoi(threads.Text)
if err != nil {
return DefaultSettings.maxworkers
}
return uint(max(1, asuint))
}
maxfilesize := newNumEntry()
filesize := func() uint {
asuint, err := strconv.Atoi(maxfilesize.Text)
if err != nil {
return DefaultSettings.maxfilesize
}
return uint(max(1, asuint))
}
ficsettings := widget.NewForm(
NewFormItemWithHintText("Include Subfolders", subfolders, "Used when selecting a folder"),
NewFormItemWithHintText("Max Worker Threads", threads, "How many threads are loading images"),
NewFormItemWithHintText("Max File Size in MB", maxfilesize, "Do not accidentally load too big images"),
)
resetSettingWidgetsValues := func() {
subfolders.Checked = v.includesubfolders
threads.Text = fmt.Sprintf("%d", v.maxworkers)
maxfilesize.Text = fmt.Sprintf("%d", v.maxfilesize)
}
resetSettingWidgetsValues()
menu := newContextMenuButton("Options", fyne.NewMenu("",
fyne.NewMenuItem("Open Folder", func() { openfolder.Tapped(&fyne.PointEvent{}) }),
fyne.NewMenuItem("Clear Cache", func() {
v.InvalidateImageCache()
runtime.GC()
v.setStatus("Cache has been cleared")
}),
fyne.NewMenuItemSeparator(),
fyne.NewMenuItem("Fic Settings", func() {
dialog.ShowCustomConfirm("Fic Settings", "Save", "Defaults", ficsettings,
func(save bool) {
if save {
v.ApplySettings(workers(), filesize(), subfolders.Checked)
v.SetNewFolder(v.selectedfolder, true, false) //dont seek when the flag is switched
if subfolders.Checked {
v.setStatus("Subfolders will be included")
} else {
v.setStatus("Subfolders will be ignored")
}
} else {
// load the defaults on dismiss
v.LoadDefaults()
resetSettingWidgetsValues()
}
}, w)
}),
fyne.NewMenuItem("Appearance", func() {
w := fyne.CurrentApp().NewWindow("Fyne Settings")
settings := settings.NewSettings().LoadAppearanceScreen(w)
w.SetContent(settings)
minsz := settings.MinSize()
w.Resize(minsz.AddWidthHeight(minsz.Width/3, minsz.Height/2))
w.Show()
}),
))
return menu
}
func (v *Viewer) makeMenubar(w fyne.Window) fyne.CanvasObject {
prev := widget.NewButtonWithIcon("Previous", theme.NavigateBackIcon(), func() { v.imgplayer.Previous() })
next := widget.NewButtonWithIcon("Next", theme.NavigateNextIcon(), func() { v.imgplayer.Next() })
v.imgplayer.SetOnPlayFunc(func() {
go v.CacheTask(v.filestringsToURI(v.imgplayer.List()),
func(s string, _ bool) {
v.setStatus(s)
}, int64(v.maxworkers), int64(v.maxfilesize))
})
var playpause *widget.Button
playpause = widget.NewButtonWithIcon("Play", theme.MediaPlayIcon(), func() {
if v.imgplayer.PlayPause() {
//is playing
playpause.SetText("Pause")
playpause.SetIcon(theme.MediaPauseIcon())
} else {
//is stopped
playpause.SetText("Play")
playpause.SetIcon(theme.MediaPlayIcon())
}
})
stop := widget.NewButtonWithIcon("Stop", theme.MediaStopIcon(), func() {
go func() {
// do not freeze the ui while we wait for the player to stop
if v.imgplayer.Stop() {
playpause.SetText("Play")
playpause.SetIcon(theme.MediaPlayIcon())
}
}()
})
var precache *widget.Button
precache = widget.NewButtonWithIcon("Cache Folder", theme.MediaRecordIcon(), func() {
if v.IsCaching() {
precache.SetIcon(theme.MediaRecordIcon())
precache.SetText("Start Caching")
//kill caching
go v.CancelCurrentCachetask()
} else {
precache.SetIcon(theme.MediaStopIcon())
precache.SetText("Stop Caching")
go v.CacheTask(v.filestringsToURI(v.imgplayer.List()),
func(s string, done bool) {
v.setStatus(s)
if done {
precache.SetIcon(theme.MediaRecordIcon())
precache.SetText("Start Caching")
}
}, int64(v.maxworkers), int64(v.maxfilesize))
}
})
seeker := widget.NewSlider(0, 0)
seeker.Step = 1
seeker.OnChanged = func(f float64) {
v.imgplayer.SeekTo(int(f))
}
speed := widget.NewSlider(20, 2000)
speed.Step = 5
speed.SetValue(200) //fixme remember from default settings
speedasstring := widget.NewLabel("")
speedasstring.SetText(fmt.Sprintf("%04.0f", speed.Value))
estimatedplaytime := widget.NewLabel("")
estimatedplaytimeupdate := func() {
estimate := (float64(v.imgplayer.Len()) * speed.Value) / 1000
estimatedplaytime.SetText(fmt.Sprintf("%0.2f seconds", estimate))
}
speed.OnChanged = func(f float64) {
estimatedplaytimeupdate()
speedasstring.SetText(fmt.Sprintf("%04.0f", f))
}
v.imgplayer.SetOnFrameFunc(func(index int, data []string, block bool) {
if index >= len(data) {
index = 0
}
seeker.SetValue(float64(index))
seeker.Refresh()
//we unselect, so we can reclick the folder to display the preview
v.filetree.UnselectAll()
uri, ok := v.filetreedata.Values[data[index]]
if !ok {
v.setStatus(data[index] + " failed: does not exist")
return
}
li := v.filetree.IsBranch(uri.String())
if li {
defer v.displayLoadingScreen("Generating previews")()
v.displayPreview(v.filetreedata.Ids[data[index]])
} else {
err := v.displayImage(uri)
if err != nil {
v.setStatus(data[index] + " failed: " + err.Error())
//move on quicker if the image was not an image
block = false
}
}
v.setFileNumber(index, v.imgplayer.Len())
if block {
time.Sleep(time.Duration(speed.Value) * time.Millisecond)
}
})
v.imgplayer.SetOnDataChangedFunc(func() {
low, high := v.imgplayer.GetSeekerBounds()
seeker.Min = float64(low)
seeker.Max = float64(high)
estimatedplaytimeupdate()
v.filetree.OnSelected(v.selected)
})
ticker := time.NewTicker(5 * time.Second)
go func() {
for {
<-ticker.C
ramusedpercent := v.refreshMemoryUsage()
if ramusedpercent > 80 {
precache.Importance = widget.DangerImportance
runtime.GC() // lets pressure the gc a little
} else {
precache.Importance = widget.MediumImportance
}
precache.Refresh()
}
}()
return container.NewVBox(
container.NewBorder(
nil, nil,
container.NewHBox(
v.makeMenu(w),
precache,
prev,
next,
playpause,
stop,
speedasstring,
),
estimatedplaytime,
speed,
),
seeker,
)
}
func (v *Viewer) filestringsToURI(files []string) []fyne.URI {
uris := make([]fyne.URI, 0, len(files))
for _, index := range files {
uri, ok := v.filetreedata.Values[index]
if !ok {
continue
}
uris = append(uris, uri)
}
return uris
}
func (v *Viewer) walksubfolder(child string) []string {
result := make([]string, 0, len(v.filetreedata.Ids[child]))
for _, file := range v.filetreedata.Ids[child] {
if len(v.filetreedata.Ids[file]) > 0 {
//folder inside folder
result = append(result, v.walksubfolder(file)...)
continue
}
result = append(result, file)
}
return result
}
func (v *Viewer) SetNewFolder(selectedfolder string, force bool, seek bool) {
if (selectedfolder == v.selectedfolder) && !force {
return
}
v.selectedfolder = selectedfolder
// it is most of the time around the selected folder len
filelist := make([]string, 0, len(v.filetreedata.Ids[v.selectedfolder]))
internaloffset := v.imgplayer.Cursor()
newoffset := 0
for offset, file := range v.filetreedata.Ids[v.selectedfolder] {
clen := len(v.filetreedata.Ids[file])
if v.includesubfolders && clen > 0 {
//is a folder, walk it
subfiles := v.walksubfolder(file)
filelist = append(filelist, subfiles...)
if internaloffset > offset {
newoffset += len(subfiles)
}
continue
}
if clen > 0 {
continue
}
filelist = append(filelist, file)
}
v.imgplayer.SetNewData(filelist)
if seek {
v.imgplayer.SeekTo(newoffset)
}
}