-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptag.go
404 lines (379 loc) · 9.83 KB
/
ptag.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"image"
"image/color"
"os"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/driver/desktop"
"github.com/davidbyttow/govips/v2/vips"
)
// newPtag creates a new Ptag app
func newPtag(width, height, preload int) (*Ptag, error) {
a := app.New()
// Create a top level window to hold the elements of the app.
win := a.NewWindow("ptag")
win.SetMaster()
if *fullscreen {
win.SetFullScreen(true)
} else {
win.Resize(fyne.NewSize(float32(width), float32(height)))
}
return &Ptag{app: a, win: win, preload: preload, loaded: map[int]nothing{}}, nil
}
// start initialises the app and starts it.
func (a *Ptag) start(f []string) {
// Make vips less noisy.
vips.LoggingSettings(nil, vips.LogLevelError)
vips.Startup(nil)
// Create some containers for the layout.
a.build()
// Create a Pict object for every image
for i, file := range f {
p := NewPict(file, i)
p.SetTitle(fmt.Sprintf("%s (%d/%d)", p.Name(), i+1, len(f)))
a.picts = append(a.picts, p)
}
// Show the main window.
a.win.Show()
go a.resizeWatcher()
// Main runloop.
a.app.Run()
}
// Show the current image.
func (a *Ptag) show() {
p := a.picts[a.index]
defer a.win.SetTitle(p.Title())
if err := p.Draw(a.iDraw); err != nil {
fmt.Fprintf(os.Stderr, "%s: draw: %v", p.Name(), err)
return
}
a.iCanvas.Refresh()
if *verbose {
fmt.Printf("%s (%d): Showing image, size %g, %g\n", p.Name(), a.index, a.iCanvas.Size().Width, a.iCanvas.Size().Height)
}
// If Draw worked, no error will be returned from Caption()
capt, _ := p.Caption()
if len(capt) != 0 {
if *verbose {
fmt.Printf("Initialising caption text to <%s>\n", capt)
}
a.caption.SetText(capt)
} else {
a.caption.SetText("")
a.caption.SetPlaceHolder("Caption")
}
a.displayRating()
}
// build creates the elements that comprise the main window.
func (a *Ptag) build() {
a.rating = canvas.NewText("Rating: -", color.Black)
a.caption = &CaptionEntry{app: a}
a.caption.ExtendBaseWidget(a.caption)
a.caption.SetPlaceHolder("Caption")
a.caption.OnChanged = a.caption.OnChange
// Initially set up a dummy canvas in order for the
// window to be shown and the sizes determined.
// The resize watcher will detect when the window is
// shown so that the first image can be loaded.
a.iCanvas = canvas.NewRectangle(color.Black)
a.top = container.NewBorder(nil, nil, a.rating, nil, a.caption)
a.win.SetContent(container.NewBorder(a.top, nil, nil, nil, a.iCanvas))
// Add key handlers
if deskCanvas, ok := a.win.Canvas().(desktop.Canvas); ok {
deskCanvas.SetOnKeyDown(func(key *fyne.KeyEvent) {
if *verbose {
fmt.Printf("Key: %s\n", key.Name)
}
switch key.Name {
case fyne.KeyUp, fyne.KeyPageUp:
a.setIndex(a.index - 10)
case fyne.KeyDown, fyne.KeyPageDown:
a.setIndex(a.index + 10)
case "N", fyne.KeyRight, fyne.KeySpace:
a.setIndex(a.index + 1)
case "P", fyne.KeyLeft, fyne.KeyBackspace:
a.setIndex(a.index - 1)
case fyne.KeyHome:
a.setIndex(0)
case fyne.KeyEnd:
a.setIndex(len(a.picts) - 1)
case "M":
a.mirror()
case "R":
a.rotate()
case "F":
a.fullScreen()
case "Q":
a.quit()
case fyne.KeyMinus:
a.rate(-1)
case fyne.Key0:
a.rate(0)
case fyne.Key1:
a.rate(1)
case fyne.Key2:
a.rate(2)
case fyne.Key3:
a.rate(3)
case fyne.Key4:
a.rate(4)
case fyne.Key5:
a.rate(5)
}
})
}
}
// Updated flags that the EXIF data may have changed.
func (a *Ptag) Updated() {
a.updated = true
if *verbose {
p := a.picts[a.index]
fmt.Printf("%s (%d): Updated flag set\n", p.Name(), a.index)
}
}
// Sync writes the caption if it has changed.
func (a *Ptag) Sync() {
if a.updated {
a.updated = false
p := a.picts[a.index]
c, err := p.Caption()
if err == nil {
if c != a.caption.Text {
if *verbose {
fmt.Printf("%s (%d): update caption to <%s>\n", p.Name(), a.index, a.caption.Text)
}
p.SetCaption(a.caption.Text)
}
}
}
}
// Window has been resized, so rescale all the images and redisplay the current one.
func (a *Ptag) resize() {
sz := a.iCanvas.Size()
scale := a.win.Canvas().Scale()
if *verbose {
fmt.Printf("resize to %g, %g, scale %g\n", sz.Width, sz.Height, scale)
}
// Create a new raster canvas for displaying the image. A raster canvas
// maps the image pixels 1-1 to the canvas pixels.
// This canvas is then used as the target for the image drawing.
a.iDraw = image.NewRGBA(image.Rect(0, 0, int(sz.Width*scale), int(sz.Height*scale)))
a.iCanvas = canvas.NewRasterFromImage(a.iDraw)
a.win.SetContent(container.NewBorder(a.top, nil, nil, nil, a.iCanvas))
// The first image to be displayed shows the window.
if !a.active {
a.active = true
// Preload other images
defer a.cacheUpdate()
} else {
a.flushCache()
}
a.redisplay()
}
// fullScreen toggles full screen mode
func (a *Ptag) fullScreen() {
a.win.SetFullScreen(!a.win.FullScreen())
}
// quit exits the app
func (a *Ptag) quit() {
a.Sync()
vips.Shutdown()
a.app.Quit()
}
// rotate the image 90 degrees clockwise
func (a *Ptag) rotate() {
a.adjustOrientation(map[string]string{
"": "6", // No existing orientation
"1": "6",
"2": "5",
"3": "8",
"4": "7",
"5": "4",
"6": "3",
"7": "2",
"8": "1"})
}
// mirror the image
func (a *Ptag) mirror() {
a.adjustOrientation(map[string]string{
"": "2", // No existing orientation
"1": "2",
"2": "1",
"3": "4",
"4": "3",
"5": "6",
"6": "5",
"7": "8",
"8": "7"})
}
// adjustOrientation selects a new orientation value using
// the current orientation as the key.
func (a *Ptag) adjustOrientation(adj map[string]string) {
p := a.picts[a.index]
if current, err := p.Orientation(); err != nil {
fmt.Fprintf(os.Stderr, "%s: current orientation: %v", p.Name(), err)
} else {
newO, ok := adj[current]
if !ok {
fmt.Fprintf(os.Stderr, "%s: unknown orientation: %s", p.Name(), current)
} else {
p.SetOrientation(newO)
a.redisplay()
if *verbose {
fmt.Printf("%s: old orientation %s, new orientation: %s\n", p.Name(), current, newO)
}
}
}
}
// rate sets the rating on the current picture.
func (a *Ptag) rate(rating int) {
p := a.picts[a.index]
if err := p.SetRating(rating); err != nil {
fmt.Fprintf(os.Stderr, "%s: Failed to set rating: %v", p.Name(), err)
} else {
a.displayRating()
}
}
// displayRating updates the rating display
func (a *Ptag) displayRating() {
p := a.picts[a.index]
rating, err := p.Rating()
if err != nil {
fmt.Fprintf(os.Stderr, "%s: rating: %v", p.Name(), err)
}
// Display rating.
if *verbose {
fmt.Printf("Displaying rating as %d\n", rating)
}
if rating < 0 {
a.rating.Text = fmt.Sprintf("Rating: -")
} else {
a.rating.Text = fmt.Sprintf("Rating: %d", rating)
}
a.rating.Refresh()
}
// redisplay the current image, usually because something has changed
// such as orientation or size.
func (a *Ptag) redisplay() {
a.removeCache(a.index)
a.setIndex(a.index)
}
// setIndex selects the image to display.
func (a *Ptag) setIndex(newIndex int) {
a.Sync()
if newIndex < 0 {
newIndex = 0
}
if newIndex >= len(a.picts) {
newIndex = len(a.picts) - 1
}
a.index = newIndex
a.addCache(a.index)
a.show()
a.cacheUpdate()
}
// cacheUpdate updates the cached set of images
func (a *Ptag) cacheUpdate() {
// Map of items to cache
nc := map[int]nothing{}
// List of new items
var newEntries []int
count := a.preload + 1
if count > len(a.picts) {
count = len(a.picts)
}
f := func(index int) {
if index >= 0 && index < len(a.picts) && count > 0 {
if _, ok := a.loaded[index]; !ok {
// new entry
newEntries = append(newEntries, index)
}
// Flag item for caching
nc[index] = nothing{}
count--
}
}
// Bias the preload going forwards
start := a.index + a.preload/4
before := start
after := start + 1
for count > 0 {
f(before)
f(after)
before--
after++
}
// Unload any items not in the new cache
for k, _ := range a.loaded {
if _, ok := nc[k]; !ok {
a.removeCache(k)
}
}
// Begin loading new items - we do this after unloading the expired entries
for _, index := range newEntries {
a.addCache(index)
}
}
// flushCache removes all the items from the cache.
func (a *Ptag) flushCache() {
for k, _ := range a.loaded {
a.removeCache(k)
}
}
// removeCache removes one image from the cache.
func (a *Ptag) removeCache(index int) {
if _, ok := a.loaded[index]; ok {
delete(a.loaded, index)
a.picts[index].Unload()
}
}
// addCache adds this image to the cache and initiates loading it.
func (a *Ptag) addCache(index int) {
if _, ok := a.loaded[index]; !ok {
a.loaded[index] = nothing{}
a.picts[index].StartLoad(a.iDraw.Bounds().Max.X, a.iDraw.Bounds().Max.Y)
}
}
// resizeWatcher tracks the actual size of the image canvas,
// and will force the images to be resized and redisplayed once
// the window has been resized.
func (a *Ptag) resizeWatcher() {
sl := time.Millisecond * 50
changed := 0
lastC := a.iCanvas.Size()
current := lastC
for {
time.Sleep(sl)
if a.iCanvas.Size() != lastC {
lastC = a.iCanvas.Size()
// 250 ms delay before actioning resize
changed = 5
}
if changed != 0 {
changed--
if changed == 0 {
if *verbose {
fmt.Printf("Canvas resize to %g, %g from %g, %g\n", a.iCanvas.Size().Width, a.iCanvas.Size().Height, current.Width, current.Height)
}
a.resize()
current = lastC
}
}
}
}