| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | |
| 8 | "reasonix/internal/config" |
| 9 | "reasonix/internal/fileutil" |
| 10 | ) |
| 11 | |
| 12 | // DesktopZoomFactor persists the user's zoom factor preference across |
| 13 | // restarts. The frontend writes it; the host RPC Hello handshake reads it to |
| 14 | // set the shell window's zoom factor. |
| 15 | type DesktopZoomFactor struct { |
| 16 | ZoomFactor float64 `json:"zoomFactor"` |
| 17 | } |
| 18 | |
| 19 | func zoomFactorPath() string { |
| 20 | return filepath.Join(config.MemoryUserDir(), "desktop-zoom.json") |
| 21 | } |
| 22 | |
| 23 | func initialDesktopZoomFactor() float64 { |
| 24 | if zf, ok := loadZoomFactor(); ok && zf > 0 { |
| 25 | return zf |
| 26 | } |
| 27 | return 1.0 |
| 28 | } |
| 29 | |
| 30 | // loadZoomFactor reads the saved zoom factor. The bool is false when no saved |
| 31 | // value exists (first launch, missing file, corrupt JSON). Callers should fall |
| 32 | // back to 1.0 (no zoom) in that case. |
| 33 | func loadZoomFactor() (float64, bool) { |
| 34 | path := zoomFactorPath() |
| 35 | data, err := readFileUTF8(path) |
| 36 | if err != nil { |
| 37 | return 0, false |
| 38 | } |
| 39 | var zf DesktopZoomFactor |
| 40 | if err := json.Unmarshal(data, &zf); err != nil { |
| 41 | return 0, false |
| 42 | } |
| 43 | if zf.ZoomFactor < 0.5 || zf.ZoomFactor > 2.0 { |
| 44 | return 0, false |
| 45 | } |
| 46 | return zf.ZoomFactor, true |
| 47 | } |
| 48 | |
| 49 | // GetDesktopZoomFactor returns the currently persisted restart zoom factor, |
| 50 | // or 1.0 if none is saved. |
| 51 | func (a *App) GetDesktopZoomFactor() float64 { |
| 52 | zf, ok := loadZoomFactor() |
| 53 | if !ok { |
| 54 | return 1.0 |
| 55 | } |
| 56 | return zf |
| 57 | } |
| 58 | |
| 59 | // SetDesktopZoomFactor persists a zoom factor for the next launch. The value |
| 60 | // is clamped to [0.5, 2.0] (50% – 200%) for safety. |
| 61 | func (a *App) SetDesktopZoomFactor(factor float64) error { |
| 62 | if factor < 0.5 { |
| 63 | factor = 0.5 |
| 64 | } |
| 65 | if factor > 2.0 { |
| 66 | factor = 2.0 |
| 67 | } |
| 68 | path := zoomFactorPath() |
| 69 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 70 | return err |
| 71 | } |
| 72 | data, err := json.Marshal(DesktopZoomFactor{ZoomFactor: factor}) |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | return fileutil.AtomicWriteFile(path, data, 0o644) |
| 77 | } |
| 78 | |
| 79 | // RestartApplication restarts the whole application so a newly saved zoom |
| 80 | // factor takes effect in the shell window. |
| 81 | func (a *App) RestartApplication() error { |
| 82 | a.relaunchDesktop(true) |
| 83 | return nil |
| 84 | } |
| 85 |