Skip to content

Commit 7a638f7

Browse files
authored
🐛 fix: enhance the multi-display window opening experience (#8176)
1 parent 015b597 commit 7a638f7

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

apps/desktop/src/main/appBrowsers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const appBrowsers = {
2626
identifier: 'devtools',
2727
maximizable: false,
2828
minWidth: 400,
29+
parentIdentifier: 'chat',
2930
path: '/desktop/devtools',
3031
titleBarStyle: 'hiddenInset',
3132
vibrancy: 'under-window',
@@ -37,6 +38,7 @@ export const appBrowsers = {
3738
identifier: 'settings',
3839
keepAlive: true,
3940
minWidth: 600,
41+
parentIdentifier: 'chat',
4042
path: '/settings',
4143
titleBarStyle: 'hidden',
4244
vibrancy: 'under-window',

apps/desktop/src/main/core/Browser.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { MainBroadcastEventKey, MainBroadcastParams } from '@lobechat/electron-client-ipc';
2-
import { BrowserWindow, BrowserWindowConstructorOptions, ipcMain, nativeTheme } from 'electron';
2+
import {
3+
BrowserWindow,
4+
BrowserWindowConstructorOptions,
5+
ipcMain,
6+
nativeTheme,
7+
screen,
8+
} from 'electron';
39
import os from 'node:os';
410
import { join } from 'node:path';
511

@@ -19,6 +25,7 @@ export interface BrowserWindowOpts extends BrowserWindowConstructorOptions {
1925
*/
2026
identifier: string;
2127
keepAlive?: boolean;
28+
parentIdentifier?: string;
2229
path: string;
2330
showOnInit?: boolean;
2431
title?: string;
@@ -145,9 +152,40 @@ export default class Browser {
145152

146153
show() {
147154
logger.debug(`Showing window: ${this.identifier}`);
155+
this.determineWindowPosition();
148156
this.browserWindow.show();
149157
}
150158

159+
private determineWindowPosition() {
160+
const { parentIdentifier } = this.options;
161+
162+
if (parentIdentifier) {
163+
// todo: fix ts type
164+
const parentWin = this.app.browserManager.retrieveByIdentifier(parentIdentifier as any);
165+
if (parentWin) {
166+
logger.debug(`[${this.identifier}] Found parent window: ${parentIdentifier}`);
167+
168+
const display = screen.getDisplayNearestPoint(parentWin.browserWindow.getContentBounds());
169+
if (display) {
170+
const {
171+
workArea: { x, y, width: displayWidth, height: displayHeight },
172+
} = display;
173+
174+
const { width, height } = this._browserWindow.getContentBounds();
175+
logger.debug(
176+
`[${this.identifier}] Display bounds: x=${x}, y=${y}, width=${displayWidth}, height=${displayHeight}`,
177+
);
178+
179+
// Calculate new position
180+
const newX = Math.floor(Math.max(x + (displayWidth - width) / 2, x));
181+
const newY = Math.floor(Math.max(y + (displayHeight - height) / 2, y));
182+
logger.debug(`[${this.identifier}] Calculated position: x=${newX}, y=${newY}`);
183+
this._browserWindow.setPosition(newX, newY, false);
184+
}
185+
}
186+
}
187+
}
188+
151189
hide() {
152190
logger.debug(`Hiding window: ${this.identifier}`);
153191
this.browserWindow.hide();

0 commit comments

Comments
 (0)