forked from adamk33n3r/GoBorderless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.go
More file actions
160 lines (142 loc) · 3.81 KB
/
settings.go
File metadata and controls
160 lines (142 loc) · 3.81 KB
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
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"slices"
"strings"
)
var (
appFolder = filepath.Join(getDocumentsFolder(), APP_NAME)
settingsPath = filepath.Join(appFolder, "settings.json")
)
type MatchType int
const (
MatchWindowTitle MatchType = iota
MatchExePath
MatchBoth
MatchEither
)
var matchTypes = []string{"Window Title", "Exe Path", "Both", "Either"}
func (m MatchType) String() string {
switch m {
case MatchWindowTitle:
return matchTypes[0]
case MatchExePath:
return matchTypes[1]
case MatchBoth:
return matchTypes[2]
case MatchEither:
return matchTypes[3]
default:
return "Unknown"
}
}
func GetMatchTypeFromString(s string) MatchType {
switch s {
case matchTypes[0]:
return MatchWindowTitle
case matchTypes[1]:
return MatchExePath
case matchTypes[2]:
return MatchBoth
case matchTypes[3]:
return MatchEither
default:
return MatchWindowTitle // Default to MatchWindowTitle if unknown
}
}
type AppSetting struct {
WindowName string `json:"windowName"`
ExePath string `json:"exePath"`
MatchType MatchType `json:"matchType"`
AutoApply bool `json:"autoApply"`
Monitor int `json:"monitor"`
OffsetX int32 `json:"offsetX"`
OffsetY int32 `json:"offsetY"`
Width int32 `json:"width"`
Height int32 `json:"height"`
PreOffsetX int32 `json:"preOffsetX"`
PreOffsetY int32 `json:"preOffsetY"`
PreWidth int32 `json:"preWidth"`
PreHeight int32 `json:"preHeight"`
}
func (as AppSetting) Display() string {
return fmt.Sprintf("%s | %s", as.WindowName, as.ExePath)
}
type AppSettingDefaults struct {
Monitor int `json:"monitor"`
MatchType MatchType `json:"matchType"`
OffsetX int32 `json:"offsetX"`
OffsetY int32 `json:"offsetY"`
Width int32 `json:"width"`
Height int32 `json:"height"`
}
type Settings struct {
Apps []AppSetting `json:"apps"`
Theme string `json:"theme"`
StartWithWindows bool `json:"startWithWindows"`
CloseToTray bool `json:"closeToTray"`
MinimizeToTray bool `json:"minimizeToTray"`
StartMinimized bool `json:"startMinimized"`
Defaults AppSettingDefaults `json:"defaults"`
}
func newSettings() *Settings {
return &Settings{
Apps: make([]AppSetting, 0),
Theme: "System",
StartWithWindows: false,
}
}
func loadSettings() (*Settings, error) {
os.MkdirAll(appFolder, os.ModeDir)
bytes, err := os.ReadFile(settingsPath)
// No settings file found, create default settings
if err != nil {
return newSettings(), nil
}
var settings *Settings
if err := json.Unmarshal(bytes, &settings); err != nil {
return newSettings(), err
}
settings.sortApps()
return settings, nil
}
func (settings *Settings) sortApps() {
slices.SortFunc(settings.Apps, func(a AppSetting, b AppSetting) int {
return strings.Compare(a.WindowName, b.WindowName)
})
}
func (settings *Settings) Save() error {
bytes, err := json.MarshalIndent(settings, "", " ")
if err != nil {
return err
}
return os.WriteFile(settingsPath, bytes, 0666)
}
func backUpSettingsFile() error {
baseBackupPath := settingsPath + ".bak"
backupPath := baseBackupPath
i := 1
for {
if _, err := os.Stat(backupPath); os.IsNotExist(err) {
break
}
backupPath = fmt.Sprintf("%s.%d", baseBackupPath, i)
i++
}
if _, err := os.Stat(settingsPath); err == nil {
if err := os.Rename(settingsPath, backupPath); err != nil {
return fmt.Errorf("failed to back up settings: %w", err)
}
}
return nil
}
func (settings *Settings) AddApp(app AppSetting) {
settings.Apps = append(settings.Apps, app)
settings.sortApps()
}
func (settings *Settings) RemoveApp(appSettingIdx int) {
settings.Apps = slices.Delete(settings.Apps, appSettingIdx, appSettingIdx+1)
}