116 lines
2.7 KiB
Go
116 lines
2.7 KiB
Go
package window
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"sync"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Frame is a window's position and size in the platform's own screen
|
||
|
|
// coordinates (macOS: points, origin bottom left; Windows: pixels of the
|
||
|
|
// virtual screen, origin top left). It is only ever read back on the
|
||
|
|
// platform that wrote it. Maximized (macOS: zoomed) keeps the normal
|
||
|
|
// frame underneath, so un-maximizing returns to it.
|
||
|
|
type Frame struct {
|
||
|
|
X int `json:"x"`
|
||
|
|
Y int `json:"y"`
|
||
|
|
Width int `json:"width"`
|
||
|
|
Height int `json:"height"`
|
||
|
|
Maximized bool `json:"maximized,omitempty"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// minFrame is the smallest frame restored; anything smaller is a broken
|
||
|
|
// file, not a window someone made.
|
||
|
|
const minFrame = 200
|
||
|
|
|
||
|
|
func (f Frame) usable() bool { return f.Width >= minFrame && f.Height >= minFrame }
|
||
|
|
|
||
|
|
// loadFrame reads a frame file written by frameSaver. A missing,
|
||
|
|
// unreadable or implausible file is no frame: the window opens with its
|
||
|
|
// default size, centered.
|
||
|
|
func loadFrame(path string) (Frame, bool) {
|
||
|
|
if path == "" {
|
||
|
|
return Frame{}, false
|
||
|
|
}
|
||
|
|
data, err := os.ReadFile(path)
|
||
|
|
if err != nil {
|
||
|
|
return Frame{}, false
|
||
|
|
}
|
||
|
|
var f Frame
|
||
|
|
if json.Unmarshal(data, &f) != nil || !f.usable() {
|
||
|
|
return Frame{}, false
|
||
|
|
}
|
||
|
|
return f, true
|
||
|
|
}
|
||
|
|
|
||
|
|
// frameSaver writes the window's frame to its file, a moment after the
|
||
|
|
// last move or resize (a drag reports many), and once more at Destroy.
|
||
|
|
type frameSaver struct {
|
||
|
|
path string
|
||
|
|
mu sync.Mutex
|
||
|
|
last Frame
|
||
|
|
dirty bool
|
||
|
|
timer *time.Timer
|
||
|
|
}
|
||
|
|
|
||
|
|
const frameSaveDelay = 400 * time.Millisecond
|
||
|
|
|
||
|
|
func (s *frameSaver) changed(f Frame) {
|
||
|
|
if s == nil || !f.usable() {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
s.mu.Lock()
|
||
|
|
defer s.mu.Unlock()
|
||
|
|
if f == s.last && !s.dirty {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
s.last, s.dirty = f, true
|
||
|
|
if s.timer == nil {
|
||
|
|
s.timer = time.AfterFunc(frameSaveDelay, s.flush)
|
||
|
|
} else {
|
||
|
|
s.timer.Reset(frameSaveDelay)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// flush writes a pending frame (temp file + rename, so a crash never
|
||
|
|
// leaves half a file). Errors are not fatal: the next start just opens
|
||
|
|
// with the default frame.
|
||
|
|
func (s *frameSaver) flush() {
|
||
|
|
if s == nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
s.mu.Lock()
|
||
|
|
if s.timer != nil {
|
||
|
|
s.timer.Stop()
|
||
|
|
}
|
||
|
|
f, dirty := s.last, s.dirty
|
||
|
|
s.dirty = false
|
||
|
|
s.mu.Unlock()
|
||
|
|
if !dirty {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
data, err := json.Marshal(f)
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if err := os.MkdirAll(filepath.Dir(s.path), 0o755); err != nil {
|
||
|
|
debugf("window: frame file: %v", err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
tmp := s.path + ".tmp"
|
||
|
|
if err := os.WriteFile(tmp, append(data, '\n'), 0o644); err != nil {
|
||
|
|
debugf("window: frame file: %v", err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if err := os.Rename(tmp, s.path); err != nil {
|
||
|
|
debugf("window: frame file: %v", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func debugf(format string, args ...any) {
|
||
|
|
if Debugf != nil {
|
||
|
|
Debugf(format, args...)
|
||
|
|
}
|
||
|
|
}
|