Skip to content

Commit 404f5e8

Browse files
committed
Add loading image
1 parent e377959 commit 404f5e8

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

loading.png

6.9 KB
Loading

main.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package main
22

33
import (
4+
"bytes"
5+
_ "embed"
46
"flag"
57
"fmt"
68
"image"
9+
"image/png"
710
"log"
811
"os"
912
"os/signal"
@@ -25,9 +28,19 @@ const (
2528
)
2629

2730
var (
28-
emptyImg = image.NewRGBA(image.Rect(0, 0, 1, 1))
31+
//go:embed loading.png
32+
loadingImgBytes []byte
33+
loadingImg image.Image
2934
)
3035

36+
func init() {
37+
var err error
38+
loadingImg, err = png.Decode(bytes.NewReader(loadingImgBytes))
39+
if err != nil {
40+
panic(err)
41+
}
42+
}
43+
3144
type PageGrid struct {
3245
labels []*widget.Label
3346
thumbs []*canvas.Image
@@ -43,7 +56,7 @@ func NewPageGrid(pageCount int, tapHandler func(page int), clearHandler func(pag
4356
for i := range labels {
4457
labels[i] = widget.NewLabel(strconv.Itoa(i + 1))
4558
labels[i].Alignment = fyne.TextAlignCenter
46-
thumbs[i] = canvas.NewImageFromImage(emptyImg)
59+
thumbs[i] = canvas.NewImageFromImage(loadingImg)
4760
thumbs[i].FillMode = canvas.ImageFillContain
4861
func(i int) { clears[i] = widget.NewButton("clear annots", func() { clearHandler(i) }) }(i)
4962
clears[i].Hide()

session/session.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"regexp"
1313
"strconv"
1414
"strings"
15+
"sync"
1516
"text/template"
1617
)
1718

@@ -79,6 +80,7 @@ type Session struct {
7980
path string
8081
pageCount int
8182
tmpDir string
83+
mu sync.Mutex
8284
annotated map[int]struct{}
8385
}
8486

@@ -109,7 +111,12 @@ func New(path string) (*Session, error) {
109111
return nil, err
110112
}
111113

112-
return &Session{copyPath, p, tmpDir, map[int]struct{}{}}, nil
114+
return &Session{
115+
path: copyPath,
116+
pageCount: p,
117+
tmpDir: tmpDir,
118+
annotated: map[int]struct{}{},
119+
}, nil
113120
}
114121

115122
func (s *Session) PageCount() int {
@@ -214,7 +221,9 @@ func (s *Session) Annotate(page int) (bool, error) {
214221
modified := afterEditStat.ModTime() != beforeEditStat.ModTime()
215222
if modified {
216223
_ = os.Remove(s.thumbPath(page))
224+
s.mu.Lock()
217225
s.annotated[page] = struct{}{}
226+
s.mu.Unlock()
218227
}
219228
return modified, nil
220229
}
@@ -235,11 +244,15 @@ func (s *Session) IsAnnotated(page int) bool {
235244
if page < 0 || page >= s.pageCount {
236245
panic("invalid page number")
237246
}
247+
s.mu.Lock()
238248
_, ok := s.annotated[page]
249+
s.mu.Unlock()
239250
return ok
240251
}
241252

242253
func (s *Session) HasAnnotations() bool {
254+
s.mu.Lock()
255+
defer s.mu.Unlock()
243256
return len(s.annotated) > 0
244257
}
245258

@@ -249,16 +262,21 @@ func (s *Session) Clear(page int) {
249262
}
250263
_ = os.Remove(s.annotPath(page))
251264
_ = os.Remove(s.thumbPath(page))
265+
s.mu.Lock()
252266
delete(s.annotated, page)
267+
s.mu.Unlock()
253268
}
254269

255270
func (s *Session) Save(path string) error {
256271

257272
// Shortcut for when no page is annotated
258273

274+
s.mu.Lock()
259275
if len(s.annotated) == 0 {
276+
s.mu.Unlock()
260277
return fileCopy(s.path, path)
261278
}
279+
s.mu.Unlock()
262280

263281
// Covert all annotated pages to PDF
264282

@@ -330,7 +348,9 @@ func (s *Session) Close() {
330348
_ = os.Remove(filepath.Join(s.tmpDir, f.Name()))
331349
}
332350
_ = os.Remove(s.tmpDir)
351+
s.mu.Lock()
333352
s.annotated = nil
353+
s.mu.Unlock()
334354
s.tmpDir = ""
335355
s.pageCount = -1
336356
s.path = ""

0 commit comments

Comments
 (0)