1
1
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' ;
3
9
import os from 'node:os' ;
4
10
import { join } from 'node:path' ;
5
11
@@ -19,6 +25,7 @@ export interface BrowserWindowOpts extends BrowserWindowConstructorOptions {
19
25
*/
20
26
identifier : string ;
21
27
keepAlive ?: boolean ;
28
+ parentIdentifier ?: string ;
22
29
path : string ;
23
30
showOnInit ?: boolean ;
24
31
title ?: string ;
@@ -145,9 +152,40 @@ export default class Browser {
145
152
146
153
show ( ) {
147
154
logger . debug ( `Showing window: ${ this . identifier } ` ) ;
155
+ this . determineWindowPosition ( ) ;
148
156
this . browserWindow . show ( ) ;
149
157
}
150
158
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
+
151
189
hide ( ) {
152
190
logger . debug ( `Hiding window: ${ this . identifier } ` ) ;
153
191
this . browserWindow . hide ( ) ;
0 commit comments