-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
111 lines (92 loc) · 2.52 KB
/
app.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
package main
import (
"context"
"encoding/base64"
"log"
"univboard/models"
watcher "univboard/modules/clipboard"
"univboard/modules/emitter"
"univboard/modules/store"
common "univboard/modules/utils"
"golang.design/x/clipboard"
)
// App struct
type App struct {
ctx context.Context
}
var config *models.Config
var localHistory *models.History
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called at application startup
func (a *App) startup(ctx context.Context) {
// Perform your setup here
a.ctx = ctx
// Initialize the store
if err := store.Init(); err != nil {
log.Println(err)
emitter.Error(ctx, err)
}
// Load the configuration
if cfg, err := store.Load[models.Config](store.CONFIG_FILE); err != nil {
emitter.Error(ctx, err)
} else {
config = cfg
}
// Load the clipboard history
if history, err := store.Load[store.Data](store.DATA_FILE); err != nil {
emitter.Error(ctx, err)
} else {
localHistory = history
}
// Initialize the clipboard and watchers
if err := clipboard.Init(); err != nil {
log.Println(err)
emitter.Error(ctx, err)
}
watcher.Init(ctx, []watcher.WatcherConfig{
{
Format: clipboard.FmtText,
EventName: common.TEXT_COPIED,
DataProcessor: func(data []byte) interface{} {
return string(data)
},
},
{
Format: clipboard.FmtImage,
EventName: common.IMAGE_COPIED,
DataProcessor: func(data []byte) interface{} {
return base64.StdEncoding.EncodeToString(data)
},
},
}, localHistory)
}
// domReady is called after front-end resources have been loaded
func (a App) domReady(ctx context.Context) {
// Add your action here
}
// beforeClose is called when the application is about to quit,
// either by clicking the window close button or calling runtime.Quit.
// Returning true will cause the application to continue, false will continue shutdown as normal.
func (a *App) beforeClose(ctx context.Context) (prevent bool) {
// Expire items older than user specified limit
common.Expire(config.HistoryLimit, localHistory)
// Save the updated clipboard history
if err := store.Save(store.DATA_FILE, localHistory); err != nil {
emitter.Error(ctx, err)
return true
}
return false
}
// shutdown is called at application termination
func (a *App) shutdown(ctx context.Context) {
// Perform your teardown here
// cancel the context to close all running ops
a.ctx.Done()
}
// Returns the stringified clipboard history
func (a *App) LoadHistory() string {
return common.ToJson[models.History](*localHistory)
}