@lobehub/lobehub 2.0.0-next.215 → 2.0.0-next.216

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,31 @@
2
2
 
3
3
  # Changelog
4
4
 
5
+ ## [Version 2.0.0-next.216](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.215...v2.0.0-next.216)
6
+
7
+ <sup>Released on **2026-01-05**</sup>
8
+
9
+ #### 🐛 Bug Fixes
10
+
11
+ - **misc**: Restore window position safely.
12
+
13
+ <br/>
14
+
15
+ <details>
16
+ <summary><kbd>Improvements and Fixes</kbd></summary>
17
+
18
+ #### What's fixed
19
+
20
+ - **misc**: Restore window position safely ([e0b555e](https://github.com/lobehub/lobe-chat/commit/e0b555e))
21
+
22
+ </details>
23
+
24
+ <div align="right">
25
+
26
+ [![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)
27
+
28
+ </div>
29
+
5
30
  ## [Version 2.0.0-next.215](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.214...v2.0.0-next.215)
6
31
 
7
32
  <sup>Released on **2026-01-05**</sup>
@@ -42,6 +42,13 @@ export interface BrowserWindowOpts extends BrowserWindowConstructorOptions {
42
42
  width?: number;
43
43
  }
44
44
 
45
+ interface WindowState {
46
+ height?: number;
47
+ width?: number;
48
+ x?: number;
49
+ y?: number;
50
+ }
51
+
45
52
  export default class Browser {
46
53
  private app: App;
47
54
  private _browserWindow?: BrowserWindow;
@@ -152,6 +159,46 @@ export default class Browser {
152
159
  this._browserWindow.setTitleBarOverlay(config.titleBarOverlay);
153
160
  }
154
161
 
162
+ private clampNumber(value: number, min: number, max: number) {
163
+ return Math.min(Math.max(value, min), max);
164
+ }
165
+
166
+ private resolveWindowState(
167
+ savedState: WindowState | undefined,
168
+ fallbackState: { height?: number; width?: number },
169
+ ): WindowState {
170
+ const width = savedState?.width ?? fallbackState.width;
171
+ const height = savedState?.height ?? fallbackState.height;
172
+ const resolvedState: WindowState = { height, width };
173
+
174
+ const hasPosition = Number.isFinite(savedState?.x) && Number.isFinite(savedState?.y);
175
+ if (!hasPosition) return resolvedState;
176
+
177
+ const x = savedState?.x as number;
178
+ const y = savedState?.y as number;
179
+
180
+ const targetDisplay = screen.getDisplayMatching({
181
+ height: height ?? 0,
182
+ width: width ?? 0,
183
+ x,
184
+ y,
185
+ });
186
+
187
+ const workArea = targetDisplay?.workArea ?? screen.getPrimaryDisplay().workArea;
188
+ const resolvedWidth = typeof width === 'number' ? Math.min(width, workArea.width) : width;
189
+ const resolvedHeight = typeof height === 'number' ? Math.min(height, workArea.height) : height;
190
+
191
+ const maxX = workArea.x + Math.max(0, workArea.width - (resolvedWidth ?? 0));
192
+ const maxY = workArea.y + Math.max(0, workArea.height - (resolvedHeight ?? 0));
193
+
194
+ return {
195
+ height: resolvedHeight,
196
+ width: resolvedWidth,
197
+ x: this.clampNumber(x, workArea.x, maxX),
198
+ y: this.clampNumber(y, workArea.y, maxY),
199
+ };
200
+ }
201
+
155
202
  private cleanupThemeListener(): void {
156
203
  if (this.themeListenerSetup) {
157
204
  // Note: nativeTheme listeners are global, consider using a centralized theme manager
@@ -323,12 +370,17 @@ export default class Browser {
323
370
 
324
371
  // Load window state
325
372
  const savedState = this.app.storeManager.get(this.windowStateKey as any) as
326
- | { height?: number; width?: number }
327
- | undefined; // Keep type for now, but only use w/h
373
+ | WindowState
374
+ | undefined;
328
375
  logger.info(`Creating new BrowserWindow instance: ${this.identifier}`);
329
376
  logger.debug(`[${this.identifier}] Options for new window: ${JSON.stringify(this.options)}`);
330
377
  logger.debug(
331
- `[${this.identifier}] Saved window state (only size used): ${JSON.stringify(savedState)}`,
378
+ `[${this.identifier}] Saved window state: ${JSON.stringify(savedState)}`,
379
+ );
380
+
381
+ const resolvedState = this.resolveWindowState(savedState, { height, width });
382
+ logger.debug(
383
+ `[${this.identifier}] Resolved window state: ${JSON.stringify(resolvedState)}`,
332
384
  );
333
385
 
334
386
  const browserWindow = new BrowserWindow({
@@ -337,7 +389,7 @@ export default class Browser {
337
389
  backgroundColor: '#00000000',
338
390
  darkTheme: this.isDarkMode,
339
391
  frame: false,
340
- height: savedState?.height || height,
392
+ height: resolvedState.height,
341
393
  show: false,
342
394
  title,
343
395
  vibrancy: 'sidebar',
@@ -348,7 +400,9 @@ export default class Browser {
348
400
  preload: join(preloadDir, 'index.js'),
349
401
  sandbox: false,
350
402
  },
351
- width: savedState?.width || width,
403
+ width: resolvedState.width,
404
+ x: resolvedState.x,
405
+ y: resolvedState.y,
352
406
  ...this.getPlatformThemeConfig(),
353
407
  });
354
408
 
@@ -405,12 +459,17 @@ export default class Browser {
405
459
  logger.debug(`[${this.identifier}] App is quitting, allowing window to close naturally.`);
406
460
  // Save state before quitting
407
461
  try {
408
- const { width, height } = browserWindow.getBounds(); // Get only width and height
409
- const sizeState = { height, width };
462
+ const bounds = browserWindow.getBounds();
463
+ const sizeState = {
464
+ height: bounds.height,
465
+ width: bounds.width,
466
+ x: bounds.x,
467
+ y: bounds.y,
468
+ };
410
469
  logger.debug(
411
- `[${this.identifier}] Saving window size on quit: ${JSON.stringify(sizeState)}`,
470
+ `[${this.identifier}] Saving window state on quit: ${JSON.stringify(sizeState)}`,
412
471
  );
413
- this.app.storeManager.set(this.windowStateKey as any, sizeState); // Save only size
472
+ this.app.storeManager.set(this.windowStateKey as any, sizeState);
414
473
  } catch (error) {
415
474
  logger.error(`[${this.identifier}] Failed to save window state on quit:`, error);
416
475
  }
@@ -437,15 +496,20 @@ export default class Browser {
437
496
  } else {
438
497
  // Window is actually closing (not keepAlive)
439
498
  logger.debug(
440
- `[${this.identifier}] keepAlive is false, allowing window to close. Saving size...`, // Updated log message
499
+ `[${this.identifier}] keepAlive is false, allowing window to close. Saving state...`,
441
500
  );
442
501
  try {
443
- const { width, height } = browserWindow.getBounds(); // Get only width and height
444
- const sizeState = { height, width };
502
+ const bounds = browserWindow.getBounds();
503
+ const sizeState = {
504
+ height: bounds.height,
505
+ width: bounds.width,
506
+ x: bounds.x,
507
+ y: bounds.y,
508
+ };
445
509
  logger.debug(
446
- `[${this.identifier}] Saving window size on close: ${JSON.stringify(sizeState)}`,
510
+ `[${this.identifier}] Saving window state on close: ${JSON.stringify(sizeState)}`,
447
511
  );
448
- this.app.storeManager.set(this.windowStateKey as any, sizeState); // Save only size
512
+ this.app.storeManager.set(this.windowStateKey as any, sizeState);
449
513
  } catch (error) {
450
514
  logger.error(`[${this.identifier}] Failed to save window state on close:`, error);
451
515
  }
@@ -56,9 +56,15 @@ const { mockBrowserWindow, mockNativeTheme, mockIpcMain, mockScreen, MockBrowser
56
56
  themeSource: 'system',
57
57
  },
58
58
  mockScreen: {
59
+ getDisplayMatching: vi.fn().mockReturnValue({
60
+ workArea: { height: 1080, width: 1920, x: 0, y: 0 },
61
+ }),
59
62
  getDisplayNearestPoint: vi.fn().mockReturnValue({
60
63
  workArea: { height: 1080, width: 1920, x: 0, y: 0 },
61
64
  }),
65
+ getPrimaryDisplay: vi.fn().mockReturnValue({
66
+ workArea: { height: 1080, width: 1920, x: 0, y: 0 },
67
+ }),
62
68
  },
63
69
  };
64
70
  });
@@ -240,6 +246,47 @@ describe('Browser', () => {
240
246
  );
241
247
  });
242
248
 
249
+ it('should restore window position from store and clamp within display', () => {
250
+ mockStoreManagerGet.mockImplementation((key: string) => {
251
+ if (key === 'windowSize_test-window') {
252
+ return { height: 700, width: 900, x: 1800, y: 900 };
253
+ }
254
+ return undefined;
255
+ });
256
+
257
+ new Browser(defaultOptions, mockApp);
258
+
259
+ expect(MockBrowserWindow).toHaveBeenCalledWith(
260
+ expect.objectContaining({
261
+ height: 700,
262
+ width: 900,
263
+ x: 1020,
264
+ y: 380,
265
+ }),
266
+ );
267
+ });
268
+
269
+ it('should clamp saved size when it exceeds current display bounds', () => {
270
+ mockScreen.getDisplayMatching.mockReturnValueOnce({
271
+ workArea: { height: 800, width: 1200, x: 0, y: 0 },
272
+ });
273
+ mockStoreManagerGet.mockImplementation((key: string) => {
274
+ if (key === 'windowSize_test-window') {
275
+ return { height: 1200, width: 2000, x: 0, y: 0 };
276
+ }
277
+ return undefined;
278
+ });
279
+
280
+ new Browser(defaultOptions, mockApp);
281
+
282
+ expect(MockBrowserWindow).toHaveBeenCalledWith(
283
+ expect.objectContaining({
284
+ height: 800,
285
+ width: 1200,
286
+ }),
287
+ );
288
+ });
289
+
243
290
  it('should use default size when no saved state', () => {
244
291
  mockStoreManagerGet.mockReturnValue(undefined);
245
292
 
@@ -541,6 +588,8 @@ describe('Browser', () => {
541
588
  expect(mockStoreManagerSet).toHaveBeenCalledWith('windowSize_test-window', {
542
589
  height: 600,
543
590
  width: 800,
591
+ x: 0,
592
+ y: 0,
544
593
  });
545
594
  expect(mockEvent.preventDefault).not.toHaveBeenCalled();
546
595
  });
@@ -572,6 +621,8 @@ describe('Browser', () => {
572
621
  expect(mockStoreManagerSet).toHaveBeenCalledWith('windowSize_test-window', {
573
622
  height: 600,
574
623
  width: 800,
624
+ x: 0,
625
+ y: 0,
575
626
  });
576
627
  });
577
628
  });
package/changelog/v1.json CHANGED
@@ -1,4 +1,13 @@
1
1
  [
2
+ {
3
+ "children": {
4
+ "fixes": [
5
+ "Restore window position safely."
6
+ ]
7
+ },
8
+ "date": "2026-01-05",
9
+ "version": "2.0.0-next.216"
10
+ },
2
11
  {
3
12
  "children": {
4
13
  "fixes": [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/lobehub",
3
- "version": "2.0.0-next.215",
3
+ "version": "2.0.0-next.216",
4
4
  "description": "LobeHub - an open-source,comprehensive AI Agent framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.",
5
5
  "keywords": [
6
6
  "framework",