-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow_state.go
More file actions
53 lines (48 loc) · 1.06 KB
/
Copy pathwindow_state.go
File metadata and controls
53 lines (48 loc) · 1.06 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
package main
import (
"encoding/json"
"os"
"path/filepath"
)
// windowState is the persisted window geometry (shared across platforms,
// stored as JSON in the user config dir).
type windowState struct {
X, Y, W, H int
Maximized bool
}
func stateFilePath() (string, bool) {
dir, err := os.UserConfigDir()
if err != nil {
return "", false
}
return filepath.Join(dir, "msgview", "window.json"), true
}
// readWindowState returns the saved geometry, or ok=false if there is none or
// it is malformed.
func readWindowState() (windowState, bool) {
p, ok := stateFilePath()
if !ok {
return windowState{}, false
}
b, err := os.ReadFile(p)
if err != nil {
return windowState{}, false
}
var s windowState
if err := json.Unmarshal(b, &s); err != nil || s.W <= 0 || s.H <= 0 {
return windowState{}, false
}
return s, true
}
func writeWindowState(s windowState) {
p, ok := stateFilePath()
if !ok {
return
}
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
return
}
if b, err := json.Marshal(s); err == nil {
os.WriteFile(p, b, 0o644)
}
}