| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | |
| 9 | "reasonix/internal/config" |
| 10 | ) |
| 11 | |
| 12 | // DesktopZoomFactor persists the user's WebView2 zoom factor preference across |
| 13 | // restarts. The frontend writes it; main.go reads it before wails.Run() to set |
| 14 | // the Windows ZoomFactor option. |
| 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 | // loadZoomFactor reads the saved zoom factor. The bool is false when no saved |
| 24 | // value exists (first launch, missing file, corrupt JSON). Callers should fall |
| 25 | // back to 1.0 (no zoom) in that case. |
| 26 | func loadZoomFactor() (float64, bool) { |
| 27 | path := zoomFactorPath() |
| 28 | data, err := readFileUTF8(path) |
| 29 | if err != nil { |
| 30 | return 0, false |
| 31 | } |
| 32 | var zf DesktopZoomFactor |
| 33 | if err := json.Unmarshal(data, &zf); err != nil { |
| 34 | return 0, false |
| 35 | } |
| 36 | if zf.ZoomFactor < 0.5 || zf.ZoomFactor > 2.0 { |
| 37 | return 0, false |
| 38 | } |
| 39 | return zf.ZoomFactor, true |
| 40 | } |
| 41 | |
| 42 | // GetDesktopZoomFactor returns the currently persisted restart zoom factor, |
| 43 | // or 1.0 if none is saved. |
| 44 | func (a *App) GetDesktopZoomFactor() float64 { |
| 45 | zf, ok := loadZoomFactor() |
| 46 | if !ok { |
| 47 | return 1.0 |
| 48 | } |
| 49 | return zf |
| 50 | } |
| 51 | |
| 52 | // SetDesktopZoomFactor persists a zoom factor for the next launch. The value |
| 53 | // is clamped to [0.5, 2.0] (50% – 200%) for safety. |
| 54 | func (a *App) SetDesktopZoomFactor(factor float64) error { |
| 55 | if factor < 0.5 { |
| 56 | factor = 0.5 |
| 57 | } |
| 58 | if factor > 2.0 { |
| 59 | factor = 2.0 |
| 60 | } |
| 61 | path := zoomFactorPath() |
| 62 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 63 | return err |
| 64 | } |
| 65 | data, err := json.Marshal(DesktopZoomFactor{ZoomFactor: factor}) |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | return os.WriteFile(path, data, 0o644) |
| 70 | } |
| 71 | |
| 72 | // RestartApplication saves the zoom and restarts the whole process so the new |
| 73 | // ZoomFactor takes effect in the WebView2 window options. |
| 74 | func (a *App) RestartApplication() error { |
| 75 | exe, err := os.Executable() |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | cmd := exec.Command(exe, os.Args[1:]...) |
| 80 | cmd.Stdout = os.Stdout |
| 81 | cmd.Stderr = os.Stderr |
| 82 | if err := cmd.Start(); err != nil { |
| 83 | return err |
| 84 | } |
| 85 | os.Exit(0) |
| 86 | return nil |
| 87 | } |
| 88 |