feat: remember main window position and size (#44)

* feat: remember window position and size

Signed-off-by: Innei <i@innei.in>

* chore: cleanup

Signed-off-by: Innei <i@innei.in>

---------

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-06-06 16:57:37 +08:00
committed by GitHub
parent 9deadc61fd
commit 6dba13d751
8 changed files with 278 additions and 86 deletions
+10 -75
View File
@@ -5,11 +5,14 @@ import path from "node:path"
import { registerIpcMain } from "@egoist/tipc/main"
import { electronApp, optimizer } from "@electron-toolkit/utils"
import { extractElectronWindowOptions } from "@shared/electron"
import { app, BrowserWindow, Menu } from "electron"
import { app, BrowserWindow } from "electron"
import { registerAppMenu } from "./menu"
import { router } from "./tipc"
import { createWindow } from "./window"
import { createMainWindow, createWindow } from "./window"
let mainWindow: BrowserWindow
export const getMainWindow = () => mainWindow
registerIpcMain(router)
if (process.defaultApp) {
@@ -24,10 +27,6 @@ if (process.defaultApp) {
app.dock.setIcon(path.join(__dirname, "../../resources/icon.png"))
let mainWindow: BrowserWindow
export const getMainWindow = () => mainWindow
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
@@ -42,13 +41,13 @@ app.whenReady().then(() => {
optimizer.watchWindowShortcuts(window)
})
mainWindow = createWindow()
mainWindow = createMainWindow()
app.on("activate", () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
mainWindow = createWindow()
mainWindow = createMainWindow()
}
})
@@ -69,15 +68,14 @@ app.whenReady().then(() => {
mainWindow.reload()
}
} else {
console.log("open-url", url)
const options = extractElectronWindowOptions(url)
const { height, resizeable, width } = options || {}
const { height, resizable, width } = options || {}
createWindow({
extraPath: url.replace("follow:/", ""),
width: width ?? 800,
height: height ?? 600,
resizeable,
resizable,
})
}
}
@@ -112,67 +110,4 @@ app.on("window-all-closed", () => {
// In this file you can include the rest of your app"s specific main process
// code. You can also put them in separate files and require them here.
Menu.setApplicationMenu(
Menu.buildFromTemplate([
{
role: "appMenu",
submenu: [
{ role: "about" },
{ type: "separator" },
{
label: "Settings...",
accelerator: "CmdOrCtrl+,",
click: () => {
createWindow({
extraPath: "/settings",
width: 800,
height: 600,
})
},
},
{ type: "separator" },
{ role: "services" },
{ type: "separator" },
{ role: "hide" },
{ role: "hideOthers" },
{ type: "separator" },
{ role: "quit" },
],
},
{ role: "fileMenu" },
{ role: "editMenu" },
{ role: "viewMenu" },
{ role: "windowMenu" },
{ role: "help" },
{
label: "Dev",
submenu: [
{
label: "follow https://rsshub.app/twitter/user/DIYgod",
click: () => {
createWindow({
extraPath: `/add?url=${encodeURIComponent(
"https://rsshub.app/twitter/user/DIYgod",
)}`,
width: 800,
height: 600,
})
},
},
{
label: "follow https://diygod.me/feed",
click: () => {
createWindow({
extraPath: `/add?url=${encodeURIComponent(
"https://diygod.me/feed",
)}`,
width: 800,
height: 600,
})
},
},
],
},
]),
)
registerAppMenu()
+72
View File
@@ -0,0 +1,72 @@
import type { MenuItem, MenuItemConstructorOptions } from "electron"
import { Menu } from "electron"
import { createWindow } from "./window"
export const registerAppMenu = () => {
const menus: Array<MenuItemConstructorOptions | MenuItem> = [
{
role: "appMenu",
submenu: [
{ role: "about" },
{ type: "separator" },
{
label: "Settings...",
accelerator: "CmdOrCtrl+,",
click: () => {
createWindow({
extraPath: "/settings",
width: 800,
height: 600,
})
},
},
{ type: "separator" },
{ role: "services" },
{ type: "separator" },
{ role: "hide" },
{ role: "hideOthers" },
{ type: "separator" },
{ role: "quit" },
],
},
{ role: "fileMenu" },
{ role: "editMenu" },
{ role: "viewMenu" },
{ role: "windowMenu" },
{ role: "help" },
]
if (import.meta.env.DEV) {
menus.push({
label: "Dev",
submenu: [
{
label: "follow https://rsshub.app/twitter/user/DIYgod",
click: () => {
createWindow({
extraPath: `/add?url=${encodeURIComponent(
"https://rsshub.app/twitter/user/DIYgod",
)}`,
width: 800,
height: 600,
})
},
},
{
label: "follow https://diygod.me/feed",
click: () => {
createWindow({
extraPath: `/add?url=${encodeURIComponent(
"https://diygod.me/feed",
)}`,
width: 800,
height: 600,
})
},
},
],
})
}
Menu.setApplicationMenu(Menu.buildFromTemplate(menus))
}
+3
View File
@@ -0,0 +1,3 @@
import Store from "electron-store"
export const store = new Store()
+45 -9
View File
@@ -1,22 +1,26 @@
import path from "node:path"
import { is } from "@electron-toolkit/utils"
import type { BrowserWindowConstructorOptions } from "electron"
import { BrowserWindow, shell } from "electron"
import icon from "../../resources/icon.png?asset"
import { store } from "./store"
export function createWindow(options?: {
extraPath?: string
width?: number
height?: number
resizeable?: boolean
}) {
export function createWindow(
options: {
extraPath?: string
height: number
width: number
} & BrowserWindowConstructorOptions,
) {
const { extraPath, height, width, ...configs } = options
// Create the browser window.
const window = new BrowserWindow({
width: options?.width || 1200,
height: options?.height || 900,
width,
height,
show: false,
resizable: options?.resizeable ?? true,
resizable: configs?.resizable ?? true,
autoHideMenuBar: true,
...(process.platform === "linux" ? { icon } : {}),
webPreferences: {
@@ -34,6 +38,7 @@ export function createWindow(options?: {
backgroundColor: "#00000000", // transparent hexadecimal or anything with transparency,
vibrancy: "sidebar",
visualEffectState: "active",
...configs,
})
window.on("ready-to-show", () => {
@@ -91,3 +96,34 @@ export function createWindow(options?: {
return window
}
export const createMainWindow = () => {
const storeKey = "windowState"
const windowState = store.get(storeKey) as {
height: number
width: number
x: number
y: number
} | null
const window = createWindow({
width: windowState?.width || 1200,
height: windowState?.height || 900,
x: windowState?.x,
y: windowState?.y,
minWidth: 1024,
minHeight: 500,
})
window.on("close", () => {
const bounds = window.getBounds()
store.set(storeKey, {
width: bounds.width,
height: bounds.height,
x: bounds.x,
y: bounds.y,
})
})
return window
}
@@ -195,7 +195,7 @@ export function DiscoverForm({ type }: { type: string }) {
`?url=${item.feed.url}`
}`,
{
resizeable: false,
resizable: false,
height: 550,
},
)
+1 -1
View File
@@ -2,7 +2,7 @@ import { DEEPLINK_SCHEME } from "@shared/constants"
const ELECTRON_QUERY_KEY = "__electron"
export interface OpenElectronWindowOptions {
resizeable?: boolean
resizable?: boolean
height?: number
width?: number
}