All files window-state.ts

92.3% Statements 12/13
100% Branches 4/4
100% Functions 3/3
92.3% Lines 12/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                          5x 5x       3x 3x 2x 2x         2x       2x 2x 2x 1x   2x            
import { app } from 'electron';
import * as fs from 'fs';
import * as path from 'path';
 
export interface WindowState {
  x: number;
  y: number;
  width: number;
  height: number;
  isMaximized: boolean;
  displayId: number;
}
 
/**
 * WindowStateStore persists window bounds and display across sessions.
 * Stores at: ~/.valksor/mehrhof/window-state.json
 */
export class WindowStateStore {
  private storePath: string;
 
  constructor() {
    const homeDir = app.getPath('home');
    this.storePath = path.join(homeDir, '.valksor', 'mehrhof', 'window-state.json');
  }
 
  load(): WindowState | null {
    try {
      if (fs.existsSync(this.storePath)) {
        const content = fs.readFileSync(this.storePath, 'utf-8');
        return JSON.parse(content) as WindowState;
      }
    } catch {
      // First run or corrupted file
    }
    return null;
  }
 
  save(state: WindowState): void {
    try {
      const dir = path.dirname(this.storePath);
      if (!fs.existsSync(dir)) {
        fs.mkdirSync(dir, { recursive: true });
      }
      fs.writeFileSync(this.storePath, JSON.stringify(state, null, 2));
    } catch (error) {
      console.error('Failed to save window state:', error);
    }
  }
}