Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliu-dev committed Sep 17, 2024
1 parent 7482af8 commit 3a4e355
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 171 deletions.
19 changes: 10 additions & 9 deletions main/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import {
BrowserWindow,
ipcMain,
Menu,
utilityProcess,
utilityProcess,
UtilityProcess,
} from "electron";
} from "electron";
import serve from "electron-serve";
import { createWindow } from "./helpers";
import { initMenu } from "./helpers/menu";

const isProd = process.env.NODE_ENV === "production";
const userDataPath = app.getPath("userData");
const schemaPath = isProd?path.join(userDataPath, "schema.gql"):"graphql-server/schema.gql";
const schemaPath = isProd
? path.join(userDataPath, "schema.gql")
: "graphql-server/schema.gql";
process.env.SCHEMA_PATH = schemaPath;
process.env.NEXT_PUBLIC_FOR_ELECTRON = "true";

Expand Down Expand Up @@ -79,13 +81,12 @@ app.on("ready", () => {

function updateMenu(databaseName?: string) {
const hasChosenDatabase = !!databaseName;
const menu = initMenu(mainWindow, isProd,hasChosenDatabase);

Menu.setApplicationMenu(menu);

const menu = initMenu(mainWindow, isProd, hasChosenDatabase);

Menu.setApplicationMenu(menu);
}

ipcMain.on('update-menu', (_event, databaseName?: string) => {
ipcMain.on("update-menu", (_event, databaseName?: string) => {
updateMenu(databaseName);
});

Expand Down
66 changes: 33 additions & 33 deletions main/helpers/create-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,72 @@ import {
BrowserWindow,
BrowserWindowConstructorOptions,
Rectangle,
} from 'electron'
import Store from 'electron-store'
} from "electron";
import Store from "electron-store";

export const createWindow = (
windowName: string,
options: BrowserWindowConstructorOptions
options: BrowserWindowConstructorOptions,
): BrowserWindow => {
const key = 'window-state'
const name = `window-state-${windowName}`
const store = new Store<Rectangle>({ name })
const key = "window-state";
const name = `window-state-${windowName}`;
const store = new Store<Rectangle>({ name });
const defaultSize = {
width: options.width,
height: options.height,
}
let state = {}
};
let state = {};

const restore = () => store.get(key, defaultSize)
const restore = () => store.get(key, defaultSize);

const getCurrentPosition = () => {
const position = win.getPosition()
const size = win.getSize()
const position = win.getPosition();
const size = win.getSize();
return {
x: position[0],
y: position[1],
width: size[0],
height: size[1],
}
}
};
};

const windowWithinBounds = (windowState, bounds) => {
return (
windowState.x >= bounds.x &&
windowState.y >= bounds.y &&
windowState.x + windowState.width <= bounds.x + bounds.width &&
windowState.y + windowState.height <= bounds.y + bounds.height
)
}
);
};

const resetToDefaults = () => {
const bounds = screen.getPrimaryDisplay().bounds
const bounds = screen.getPrimaryDisplay().bounds;
return Object.assign({}, defaultSize, {
x: (bounds.width - defaultSize.width) / 2,
y: (bounds.height - defaultSize.height) / 2,
})
}
});
};

const ensureVisibleOnSomeDisplay = (windowState) => {
const visible = screen.getAllDisplays().some((display) => {
return windowWithinBounds(windowState, display.bounds)
})
const ensureVisibleOnSomeDisplay = windowState => {
const visible = screen.getAllDisplays().some(display => {
return windowWithinBounds(windowState, display.bounds);
});
if (!visible) {
// Window is partially or fully not visible now.
// Reset it to safe defaults.
return resetToDefaults()
return resetToDefaults();
}
return windowState
}
return windowState;
};

const saveState = () => {
if (!win.isMinimized() && !win.isMaximized()) {
Object.assign(state, getCurrentPosition())
Object.assign(state, getCurrentPosition());
}
store.set(key, state)
}
store.set(key, state);
};

state = ensureVisibleOnSomeDisplay(restore())
state = ensureVisibleOnSomeDisplay(restore());

const win = new BrowserWindow({
...state,
Expand All @@ -78,9 +78,9 @@ export const createWindow = (
contextIsolation: true,
...options.webPreferences,
},
})
});

win.on('close', saveState)
win.on("close", saveState);

return win
}
return win;
};
2 changes: 1 addition & 1 deletion main/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './create-window'
export * from "./create-window";
Loading

0 comments on commit 3a4e355

Please sign in to comment.