Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface AppSettings {
nightLightSceneAssignments?: Record<string, string>;
lastView?: "list" | "settings" | "scenes";
confirmOnOffPressActions?: Record<string, boolean>;
windowBounds?: { width: number; height: number; x: number; y: number };
}

const schema = {
Expand Down Expand Up @@ -49,6 +50,15 @@ const schema = {
default: {},
additionalProperties: { type: "boolean" },
},
windowBounds: {
type: "object",
properties: {
width: { type: "number" },
height: { type: "number" },
x: { type: "number" },
y: { type: "number" },
},
},
};

// Migrate config from v1.1.0 ("app/switchbot-client-config.json") to v1.2.0.
Expand Down Expand Up @@ -79,9 +89,13 @@ const store = new Store<AppSettings>({


function createWindow(): BrowserWindow {
const savedBounds = store.get("windowBounds") as AppSettings["windowBounds"];

const mainWindow = new BrowserWindow({
width: 1000, // Increased width a bit
height: 700, // Increased height a bit
width: savedBounds?.width ?? 1000,
height: savedBounds?.height ?? 700,
x: savedBounds?.x,
y: savedBounds?.y,
Comment on lines +97 to +98

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate restored window position against active displays

Restoring x/y unconditionally can make the app reopen off-screen when the monitor layout changes (for example, a laptop was last used with an external display and then restarted without it). In that case Electron will honor the stale coordinates and users may see no window at startup, which is a functional regression from the previous centered default behavior.

Useful? React with 👍 / 👎.

webPreferences: {
preload: path.join(__dirname, "../preload/preload.js"),
contextIsolation: true,
Expand All @@ -90,6 +104,19 @@ function createWindow(): BrowserWindow {
autoHideMenuBar: true, // Hide the default menu bar
});

// Save window bounds on move and resize (debounced)
let saveTimeout: ReturnType<typeof setTimeout>;
const saveBounds = () => {
clearTimeout(saveTimeout);
saveTimeout = setTimeout(() => {
if (!mainWindow.isDestroyed() && !mainWindow.isMinimized() && !mainWindow.isMaximized()) {
store.set("windowBounds", mainWindow.getBounds());
}
}, 500);
};
Comment on lines +109 to +116

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

ウィンドウの resizemove イベントはドラッグやリサイズ中に頻繁に発生するため、そのたびに store.set(ディスク I/O)を呼び出すとパフォーマンスに悪影響を与える可能性があります。setTimeout を使用して処理をデバウンス(間引き)することをお勧めします。また、タイマー実行時にウィンドウが既に閉じられている場合の例外を防ぐため、!mainWindow.isDestroyed() のチェックを追加するのが安全です。

  let saveTimeout: ReturnType<typeof setTimeout>;
  const saveBounds = () => {
    clearTimeout(saveTimeout);
    saveTimeout = setTimeout(() => {
      if (!mainWindow.isDestroyed() && !mainWindow.isMinimized() && !mainWindow.isMaximized()) {
        store.set("windowBounds", mainWindow.getBounds());
      }
    }, 500);
  };

mainWindow.on("resize", saveBounds);
mainWindow.on("move", saveBounds);

if (process.env.NODE_ENV === "development") {
mainWindow.loadURL("http://localhost:5173"); // Vite dev server URL
mainWindow.webContents.openDevTools();
Expand Down
Loading