-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.go
146 lines (117 loc) · 3.31 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
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
package main
import (
"context"
"embed"
"os"
"path/filepath"
"time"
"github.com/ad-on-is/resticity/internal"
"github.com/adrg/xdg"
"github.com/charmbracelet/log"
"github.com/energye/systray"
"github.com/go-co-op/gocron/v2"
"github.com/google/uuid"
"github.com/thoas/go-funk"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type App struct {
ctx context.Context
scheduler *internal.Scheduler
restic *internal.Restic
settings *internal.Settings
assets *embed.FS
}
// NewApp creates a new App application struct
func NewApp(
restic *internal.Restic,
scheduler *internal.Scheduler,
settings *internal.Settings,
assets *embed.FS,
) *App {
return &App{restic: restic, scheduler: scheduler, settings: settings}
}
func (a *App) SaveIcon(icon []byte, file string) {
path := filepath.Join(xdg.CacheHome, "resticity")
os.WriteFile(filepath.Join(path, file), icon, 0644)
}
func (a *App) toggleSysTrayIcon() {
default_icon, err := assets.ReadFile(
"frontend/.output/public/appicon.png",
)
if err != nil {
log.Error(err)
} else {
a.SaveIcon(default_icon, "appicon.png")
}
active_icon, err := assets.ReadFile(
"frontend/.output/public/appicon_active.png",
)
if err != nil {
log.Error(err)
} else {
a.SaveIcon(active_icon, "appicon_active.png")
}
_, err = a.scheduler.Gocron.NewJob(
gocron.DurationJob(500*time.Millisecond),
gocron.NewTask(func() {
running := funk.Filter(
a.scheduler.Jobs,
func(j internal.Job) bool { return j.Running == true },
).([]internal.Job)
if len(running) > 0 {
systray.SetIcon(active_icon)
} else {
systray.SetIcon(default_icon)
}
}),
)
if err != nil {
log.Error("Error creating job", err)
}
}
func (a *App) systemTray() {
systray.CreateMenu()
systray.SetTitle("resticity")
systray.SetTooltip("Resticity")
show := systray.AddMenuItem("Open resticity", "Show the main window")
systray.AddSeparator()
exit := systray.AddMenuItem("Quit", "Quit resticity")
show.Click(func() {
runtime.WindowShow(a.ctx)
})
exit.Click(func() { os.Exit(0) })
systray.SetOnClick(func(menu systray.IMenu) { runtime.WindowShow(a.ctx) })
// systray.SetOnRClick(func(menu systray.IMenu) { menu.ShowMenu() })
systray.SetOnRClick(func(menu systray.IMenu) { runtime.WindowHide(a.ctx) })
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
a.toggleSysTrayIcon()
go systray.Run(a.systemTray, func() {})
}
func (a *App) StopBackup(id uuid.UUID) {
// a.scheduler.RemoveJob(id)
// a.RescheduleBackups()
}
func (a *App) SelectDirectory(title string) string {
if dir, err := runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{
Title: title,
}); err == nil {
return dir
}
return ""
}
func (a *App) SelectFile(title string) string {
if dir, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
Title: title,
}); err == nil {
return dir
}
return ""
}
func (a *App) FakeCreateForModels() (internal.SnapshotGroup, internal.Repository, internal.Backup, internal.Config, internal.Schedule, internal.FileDescriptor, internal.ScheduleObject) {
return internal.SnapshotGroup{}, internal.Repository{}, internal.Backup{}, internal.Config{}, internal.Schedule{}, internal.FileDescriptor{}, internal.ScheduleObject{}
}