-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
105 lines (85 loc) · 2.21 KB
/
settings.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
package main
import (
"encoding/json"
"os"
"os/exec"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
clog "github.com/charmbracelet/log"
"github.com/davecgh/go-spew/spew"
)
type SettingsSelection struct {
MinimizeToTray bool
AllowUnload bool
}
var (
settingsSelection = SettingsSelection{}
unloadCheck *widget.Check
sysTrayCheck *widget.Check
settings *fyne.Container
settingsTab *fyne.Container
)
func initSettings() {
unloadCheck = widget.NewCheck("Allow Unloading (may cause memory leaks and crashes)", func(checked bool) {
settingsSelection.AllowUnload = checked
settingsSelection.saveSettingsToFile()
})
sysTrayCheck = widget.NewCheck("Minimize Kinjector to system tray on app closing?", func(checked bool) {
settingsSelection.MinimizeToTray = checked
settingsSelection.saveSettingsToFile()
})
settings = container.NewVBox(
sysTrayCheck,
unloadCheck,
widget.NewButtonWithIcon("Open config directory", theme.FolderIcon(), func() {
exec.Command("explorer", configDir).Start()
}),
)
settingsTab = container.NewVBox(
widget.NewLabelWithStyle("Kinjector settings", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
settings,
)
// Load settings from file
err := settingsSelection.loadSettingsFromFile()
if err != nil {
// Handle error (e.g., log it or show a message to the user)
clog.Error("Error loading settings", "err", err)
}
// Update checkbox states
updateCheckboxStates()
if *dbg {
spew.Dump(settingsSelection)
}
}
func (s *SettingsSelection) saveSettingsToFile() error {
data, err := json.Marshal(settingsSelection)
if err != nil {
return err
}
err = os.WriteFile(settingsFile, data, 0600)
if err != nil {
return err
}
return nil
}
func (s *SettingsSelection) loadSettingsFromFile() error {
data, err := os.ReadFile(settingsFile)
if err != nil {
if os.IsNotExist(err) {
// File doesn't exist, which is fine for first run
return nil
}
return err
}
err = json.Unmarshal(data, &settingsSelection)
if err != nil {
return err
}
return nil
}
func updateCheckboxStates() {
unloadCheck.SetChecked(settingsSelection.AllowUnload)
sysTrayCheck.SetChecked(settingsSelection.MinimizeToTray)
}