TidGi-Desktop/src/services/windows/handleCreateBasicWindow.ts
lin onetwo b76fc17794
Chore/upgrade (#646)
* docs: deps

* Update dependencies and type usage for AI features

Upgraded multiple dependencies in package.json and pnpm-lock.yaml, including @ai-sdk, @mui, react, and others for improved compatibility and performance. Changed type usage from CoreMessage to ModelMessage in mockOpenAI.test.ts to align with updated ai package. No functional changes to application logic.

* feat: i18n

* feat: test oauth login and use PKCE

* fix: use ollama-ai-provider-v2

* test: github and mock oauth2 login

* test: gitea login

* Refactor context menu cleanup and error message

Moved context menu cleanup for OAuth window to a single closed event handler in Authentication service. Simplified error message formatting in ContextService for missing keys.

* lint: AI fix

* Add tsx as a dev dependency and update scripts

Replaced usage of 'pnpm dlx tsx' with direct 'tsx' command in development and test scripts for improved reliability. Added 'tsx' to devDependencies in package.json.
2025-10-23 23:42:06 +08:00

83 lines
3.7 KiB
TypeScript

import { isTest } from '@/constants/environment';
import type { IAuthenticationService } from '@services/auth/interface';
import { container } from '@services/container';
import type { IMenuService } from '@services/menu/interface';
import serviceIdentifier from '@services/serviceIdentifier';
import type { IThemeService } from '@services/theme/interface';
import { app, BrowserWindow, BrowserWindowConstructorOptions } from 'electron';
import type { IWindowOpenConfig, IWindowService } from './interface';
import { getMainWindowEntry } from './viteEntry';
import { WindowMeta, WindowNames } from './WindowProperties';
export async function handleCreateBasicWindow<N extends WindowNames>(
windowName: N,
windowConfig: BrowserWindowConstructorOptions,
windowMeta: WindowMeta[N] = {} as WindowMeta[N],
config?: IWindowOpenConfig<N>,
): Promise<BrowserWindow> {
const menuService = container.get<IMenuService>(serviceIdentifier.MenuService);
const windowService = container.get<IWindowService>(serviceIdentifier.Window);
const newWindow = new BrowserWindow(windowConfig);
const newWindowURL = (windowMeta !== undefined && 'uri' in windowMeta ? windowMeta.uri : undefined) ?? getMainWindowEntry();
if (config?.multiple !== true) {
windowService.set(windowName, newWindow);
}
const unregisterContextMenu = await menuService.initContextMenuForWindowWebContents(newWindow.webContents);
newWindow.on('closed', () => {
windowService.set(windowName, undefined);
unregisterContextMenu();
});
// Handle OAuth redirect for preferences/addWorkspace windows
if (windowName === WindowNames.preferences || windowName === WindowNames.addWorkspace) {
const authService = container.get<IAuthenticationService>(serviceIdentifier.Authentication);
authService.setupOAuthRedirectHandler(newWindow, getMainWindowEntry, WindowNames.preferences);
}
let webContentLoadingPromise: Promise<void> | undefined;
if (windowName === WindowNames.main) {
// handle window show and Webview/browserView show
webContentLoadingPromise = new Promise<void>((resolve, reject) => {
newWindow.once('ready-to-show', () => {
const mainWindow = windowService.get(WindowNames.main);
if (mainWindow === undefined) {
reject(new Error("Main window is undefined in newWindow.once('ready-to-show'"));
return;
}
// eslint-disable-next-line @typescript-eslint/no-deprecated
const { wasOpenedAsHidden } = app.getLoginItemSettings();
// Don't bring up window when running e2e test, otherwise it will annoy the developer who is doing other things.
if (!wasOpenedAsHidden && !isTest) {
mainWindow.show();
}
// ensure redux is loaded first
// if not, redux might not be able catch changes sent from ipcMain
if (!mainWindow.webContents.isLoading()) {
resolve();
return;
}
mainWindow.webContents.once('did-stop-loading', () => {
resolve();
});
});
});
}
await updateWindowBackground(newWindow);
// Not loading main window (like sidebar and background) here. Only load wiki in browserView in the secondary window. Secondary window will use a WebContentsView to load content, and without main content like sidebar and Guide.
const isWindowToLoadURL = windowName !== WindowNames.secondary;
if (isWindowToLoadURL) {
// This loading will wait for a while
await newWindow.loadURL(newWindowURL);
}
await webContentLoadingPromise;
return newWindow;
}
async function updateWindowBackground(newWindow: BrowserWindow): Promise<void> {
const themeService = container.get<IThemeService>(serviceIdentifier.ThemeService);
if (await themeService.shouldUseDarkColors()) {
newWindow.setBackgroundColor('#000000');
}
}