Skip to content

Commit

Permalink
fix: windows maximize will lost frame and background material
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Aug 23, 2024
1 parent 97779a0 commit 2bd0e78
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
"@vercel/ncc": "0.38.1",
"@vitejs/plugin-react": "^4.3.1",
"drizzle-orm": "0.32.2",
"electron": "^30.4.0",
"electron": "32.0.1",
"electron-devtools-installer": "3.2.0",
"electron-packager-languages": "0.5.0",
"electron-vite": "^2.3.0",
Expand Down
28 changes: 14 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 59 additions & 1 deletion src/main/tipc/app.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { getRendererHandlers } from "@egoist/tipc/main"
import { callGlobalContextMethod } from "@shared/bridge"
import type { BrowserWindow } from "electron"
import { clipboard, dialog } from "electron"
import { clipboard, dialog, screen } from "electron"

import { isWindows11 } from "../env"
import { downloadFile } from "../lib/download"
import { cleanAuthSessionToken, cleanUser } from "../lib/user"
import type { RendererHandlers } from "../renderer-handlers"
Expand Down Expand Up @@ -65,17 +66,74 @@ export const appRoute = {
window.minimize()
break
}

case "maximum": {
// FIXME: this is a electron bug, see https://github.com/RSSNext/Follow/issues/231
// So in this way we use a workaround to fix it, that is manually resize the window
if (isWindows11) {
const size = screen.getDisplayMatching(
window.getBounds(),
).workAreaSize

const isMaximized = size.height === window.getSize()[1]

const storeKey = Symbol.for("maximized")
if (isMaximized) {
const stored = window[storeKey]
if (!stored) return
window.setSize(stored.size[0], stored.size[1])
window.setPosition(stored.position[0], stored.position[1])

delete window[storeKey]
} else {
const currentWindowSize = window.getSize()
const currentWindowPosition = window.getPosition()
window[storeKey] = {
size: currentWindowSize,
position: currentWindowPosition,
}
// Maually Resize
window.setSize(size.width, size.height)
window.setPosition(0, 0)
}

return
}

if (window.isMaximized()) {
window.unmaximize()
} else {
window.maximize()
}

break
}
}
}
}),
getWindowIsMaximized: t.procedure
.input<void>()
.action(async ({ context }) => {
const window: BrowserWindow | null = (
context.sender as Sender
).getOwnerBrowserWindow()

if (isWindows11 && window) {
const size = screen.getDisplayMatching(window.getBounds()).workAreaSize

const windowSize = window.getSize()
const windowPosition = window.getPosition()
const isMaximized =
windowSize[0] === size.width &&
windowSize[1] === size.height &&
windowPosition[0] === 0 &&
windowPosition[1] === 0

return !!isMaximized
}

return window?.isMaximized()
}),
quitAndInstall: t.procedure.action(async () => {
quitAndInstall()
}),
Expand Down
12 changes: 0 additions & 12 deletions src/main/tipc/setting.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createRequire } from "node:module"

import type { BrowserWindow } from "electron"
import { app, nativeTheme } from "electron"

import { setDockCount } from "../lib/dock"
Expand Down Expand Up @@ -47,16 +46,5 @@ export const settingRoute = {
setDockBadge: t.procedure.input<number>().action(async ({ input }) => {
setDockCount(input)
}),
getWindowIsMaximized: t.procedure
.input<void>()
.action(async ({ context }) => {
const window: BrowserWindow | null = (
context.sender as Sender
).getOwnerBrowserWindow()
return window?.isMaximized()
}),
}

interface Sender extends Electron.WebContents {
getOwnerBrowserWindow: () => Electron.BrowserWindow | null
}
21 changes: 20 additions & 1 deletion src/main/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ export function createWindow(
case "win32": {
Object.assign(baseWindowConfig, {
icon: getIconPath(),
backgroundMaterial: isWindows11 ? "mica" : undefined,

titleBarStyle: "hidden",
backgroundMaterial: "mica",
frame: true,
maximizable: false,
backgroundColor: "#00000000",
} as Electron.BrowserWindowConstructorOptions)
break
}
Expand Down Expand Up @@ -217,6 +221,21 @@ export const createMainWindow = () => {
})

window.on("close", () => {
if (isWindows11) {
const windowStoreKey = Symbol.for("maximized")
if (window[windowStoreKey]) {
const stored = window[windowStoreKey]
store.set(storeKey, {
width: stored.size[0],
height: stored.size[1],
x: stored.position[0],
y: stored.position[1],
})

return
}
}

const bounds = window.getBounds()
store.set(storeKey, {
width: bounds.width,
Expand Down

0 comments on commit 2bd0e78

Please sign in to comment.