fix(electron): do not restore window on get window (#5163)

fix https://github.com/toeverything/AFFiNE/issues/5161

Looks like I used window.restore incorrectly.
This commit is contained in:
Peng Xiao
2023-12-02 15:13:48 +00:00
parent 1f7654e80f
commit 512504e177
4 changed files with 30 additions and 18 deletions

View File

@@ -5,10 +5,10 @@ import { type App, type BrowserWindow, ipcMain } from 'electron';
import { buildType, CLOUD_BASE_URL, isDev } from './config'; import { buildType, CLOUD_BASE_URL, isDev } from './config';
import { logger } from './logger'; import { logger } from './logger';
import { import {
getOrCreateWindow,
handleOpenUrlInHiddenWindow, handleOpenUrlInHiddenWindow,
mainWindowOrigin, mainWindowOrigin,
removeCookie, removeCookie,
restoreOrCreateWindow,
setCookie, setCookie,
} from './main-window'; } from './main-window';
@@ -39,8 +39,9 @@ export function setupDeepLink(app: App) {
// on windows & linux, we need to listen for the second-instance event // on windows & linux, we need to listen for the second-instance event
app.on('second-instance', (event, commandLine) => { app.on('second-instance', (event, commandLine) => {
restoreOrCreateWindow() getOrCreateWindow()
.then(() => { .then(window => {
window.show();
const url = commandLine.pop(); const url = commandLine.pop();
if (url?.startsWith(`${protocol}://`)) { if (url?.startsWith(`${protocol}://`)) {
event.preventDefault(); event.preventDefault();
@@ -67,7 +68,7 @@ async function handleAffineUrl(url: string) {
async function handleOauthJwt(url: string) { async function handleOauthJwt(url: string) {
if (url) { if (url) {
try { try {
const mainWindow = await restoreOrCreateWindow(); const mainWindow = await getOrCreateWindow();
mainWindow.show(); mainWindow.show();
const urlObj = new URL(url); const urlObj = new URL(url);
const token = urlObj.searchParams.get('token'); const token = urlObj.searchParams.get('token');

View File

@@ -11,7 +11,7 @@ import { registerEvents } from './events';
import { registerHandlers } from './handlers'; import { registerHandlers } from './handlers';
import { ensureHelperProcess } from './helper-process'; import { ensureHelperProcess } from './helper-process';
import { logger } from './logger'; import { logger } from './logger';
import { restoreOrCreateWindow } from './main-window'; import { getOrCreateWindow } from './main-window';
import { registerProtocol } from './protocol'; import { registerProtocol } from './protocol';
import { registerUpdater } from './updater'; import { registerUpdater } from './updater';
@@ -56,7 +56,7 @@ app.on('window-all-closed', () => {
* @see https://www.electronjs.org/docs/v14-x-y/api/app#event-activate-macos Event: 'activate' * @see https://www.electronjs.org/docs/v14-x-y/api/app#event-activate-macos Event: 'activate'
*/ */
app.on('activate', () => { app.on('activate', () => {
restoreOrCreateWindow().catch(e => getOrCreateWindow().catch(e =>
console.error('Failed to restore or create window:', e) console.error('Failed to restore or create window:', e)
); );
}); });
@@ -72,7 +72,7 @@ app
.then(registerHandlers) .then(registerHandlers)
.then(registerEvents) .then(registerEvents)
.then(ensureHelperProcess) .then(ensureHelperProcess)
.then(restoreOrCreateWindow) .then(getOrCreateWindow)
.then(createApplicationMenu) .then(createApplicationMenu)
.then(registerUpdater) .then(registerUpdater)
.catch(e => console.error('Failed create window:', e)); .catch(e => console.error('Failed create window:', e));

View File

@@ -148,16 +148,11 @@ let browserWindow$: Promise<BrowserWindow> | undefined;
/** /**
* Restore existing BrowserWindow or Create new BrowserWindow * Restore existing BrowserWindow or Create new BrowserWindow
*/ */
export async function restoreOrCreateWindow() { export async function getOrCreateWindow() {
if (!browserWindow$ || (await browserWindow$.then(w => w.isDestroyed()))) { if (!browserWindow$ || (await browserWindow$.then(w => w.isDestroyed()))) {
browserWindow$ = createWindow(); browserWindow$ = createWindow();
} }
const mainWindow = await browserWindow$; const mainWindow = await browserWindow$;
if (mainWindow.isMinimized()) {
mainWindow.restore();
logger.info('restore main window');
}
return mainWindow; return mainWindow;
} }
@@ -188,7 +183,11 @@ export async function setCookie(
arg0: CookiesSetDetails | string, arg0: CookiesSetDetails | string,
arg1?: string arg1?: string
) { ) {
const window = await restoreOrCreateWindow(); const window = await browserWindow$;
if (!window) {
// do nothing if window is not ready
return;
}
const details = const details =
typeof arg1 === 'string' && typeof arg0 === 'string' typeof arg1 === 'string' && typeof arg0 === 'string'
? parseCookie(arg0, arg1) ? parseCookie(arg0, arg1)
@@ -204,12 +203,20 @@ export async function setCookie(
} }
export async function removeCookie(url: string, name: string): Promise<void> { export async function removeCookie(url: string, name: string): Promise<void> {
const window = await restoreOrCreateWindow(); const window = await browserWindow$;
if (!window) {
// do nothing if window is not ready
return;
}
await window.webContents.session.cookies.remove(url, name); await window.webContents.session.cookies.remove(url, name);
} }
export async function getCookie(url?: string, name?: string) { export async function getCookie(url?: string, name?: string) {
const window = await restoreOrCreateWindow(); const window = await browserWindow$;
if (!window) {
// do nothing if window is not ready
return;
}
const cookies = await window.webContents.session.cookies.get({ const cookies = await window.webContents.session.cookies.get({
url, url,
name, name,

View File

@@ -119,8 +119,12 @@ export function registerProtocol() {
// if sending request to the cloud, attach the session cookie // if sending request to the cloud, attach the session cookie
if (isNetworkResource(pathname)) { if (isNetworkResource(pathname)) {
const cookie = await getCookie(CLOUD_BASE_URL); const cookie = await getCookie(CLOUD_BASE_URL);
const cookieString = cookie.map(c => `${c.name}=${c.value}`).join('; '); if (cookie) {
details.requestHeaders['cookie'] = cookieString; const cookieString = cookie
.map(c => `${c.name}=${c.value}`)
.join('; ');
details.requestHeaders['cookie'] = cookieString;
}
} }
callback({ callback({
cancel: false, cancel: false,