@theia/core 1.77.0-next.88 → 1.77.0-next.89

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.
Files changed (134) hide show
  1. package/lib/browser/catalog.json +12 -0
  2. package/lib/browser/color-application-contribution.d.ts +2 -2
  3. package/lib/browser/color-application-contribution.d.ts.map +1 -1
  4. package/lib/browser/color-application-contribution.js +7 -8
  5. package/lib/browser/color-application-contribution.js.map +1 -1
  6. package/lib/browser/common-frontend-contribution.d.ts.map +1 -1
  7. package/lib/browser/common-frontend-contribution.js +7 -0
  8. package/lib/browser/common-frontend-contribution.js.map +1 -1
  9. package/lib/browser/frontend-application-module.d.ts.map +1 -1
  10. package/lib/browser/frontend-application-module.js +6 -0
  11. package/lib/browser/frontend-application-module.js.map +1 -1
  12. package/lib/browser/quick-input/quick-command-service.d.ts +2 -0
  13. package/lib/browser/quick-input/quick-command-service.d.ts.map +1 -1
  14. package/lib/browser/quick-input/quick-command-service.js +10 -2
  15. package/lib/browser/quick-input/quick-command-service.js.map +1 -1
  16. package/lib/browser/saveable-service.d.ts +4 -0
  17. package/lib/browser/saveable-service.d.ts.map +1 -1
  18. package/lib/browser/saveable-service.js +7 -0
  19. package/lib/browser/saveable-service.js.map +1 -1
  20. package/lib/browser/secondary-window-handler.d.ts +126 -3
  21. package/lib/browser/secondary-window-handler.d.ts.map +1 -1
  22. package/lib/browser/secondary-window-handler.js +354 -30
  23. package/lib/browser/secondary-window-handler.js.map +1 -1
  24. package/lib/browser/shell/application-shell-mouse-tracker.d.ts +2 -0
  25. package/lib/browser/shell/application-shell-mouse-tracker.d.ts.map +1 -1
  26. package/lib/browser/shell/application-shell-mouse-tracker.js +10 -0
  27. package/lib/browser/shell/application-shell-mouse-tracker.js.map +1 -1
  28. package/lib/browser/shell/application-shell.d.ts +11 -0
  29. package/lib/browser/shell/application-shell.d.ts.map +1 -1
  30. package/lib/browser/shell/application-shell.js +33 -4
  31. package/lib/browser/shell/application-shell.js.map +1 -1
  32. package/lib/browser/shell/index.d.ts +1 -0
  33. package/lib/browser/shell/index.d.ts.map +1 -1
  34. package/lib/browser/shell/index.js +1 -0
  35. package/lib/browser/shell/index.js.map +1 -1
  36. package/lib/browser/shell/native-tab-drag-service.d.ts +141 -0
  37. package/lib/browser/shell/native-tab-drag-service.d.ts.map +1 -0
  38. package/lib/browser/shell/native-tab-drag-service.js +349 -0
  39. package/lib/browser/shell/native-tab-drag-service.js.map +1 -0
  40. package/lib/browser/shell/native-tab-drag-service.spec.d.ts +2 -0
  41. package/lib/browser/shell/native-tab-drag-service.spec.d.ts.map +1 -0
  42. package/lib/browser/shell/native-tab-drag-service.spec.js +71 -0
  43. package/lib/browser/shell/native-tab-drag-service.spec.js.map +1 -0
  44. package/lib/browser/shell/shell-layout-restorer.d.ts +3 -0
  45. package/lib/browser/shell/shell-layout-restorer.d.ts.map +1 -1
  46. package/lib/browser/shell/shell-layout-restorer.js +20 -0
  47. package/lib/browser/shell/shell-layout-restorer.js.map +1 -1
  48. package/lib/browser/shell/shell-layout-restorer.spec.js +31 -0
  49. package/lib/browser/shell/shell-layout-restorer.spec.js.map +1 -1
  50. package/lib/browser/shell/side-panel-handler.d.ts +8 -0
  51. package/lib/browser/shell/side-panel-handler.d.ts.map +1 -1
  52. package/lib/browser/shell/side-panel-handler.js +14 -0
  53. package/lib/browser/shell/side-panel-handler.js.map +1 -1
  54. package/lib/browser/shell/tab-bars.d.ts +39 -0
  55. package/lib/browser/shell/tab-bars.d.ts.map +1 -1
  56. package/lib/browser/shell/tab-bars.js +127 -2
  57. package/lib/browser/shell/tab-bars.js.map +1 -1
  58. package/lib/browser/shell/tab-bars.spec.js +48 -0
  59. package/lib/browser/shell/tab-bars.spec.js.map +1 -1
  60. package/lib/browser/shell/tab-drop-helper.d.ts +32 -0
  61. package/lib/browser/shell/tab-drop-helper.d.ts.map +1 -0
  62. package/lib/browser/shell/tab-drop-helper.js +75 -0
  63. package/lib/browser/shell/tab-drop-helper.js.map +1 -0
  64. package/lib/browser/shell/theia-dock-panel.d.ts +25 -0
  65. package/lib/browser/shell/theia-dock-panel.d.ts.map +1 -1
  66. package/lib/browser/shell/theia-dock-panel.js +107 -11
  67. package/lib/browser/shell/theia-dock-panel.js.map +1 -1
  68. package/lib/browser/styling-service.d.ts +2 -2
  69. package/lib/browser/styling-service.d.ts.map +1 -1
  70. package/lib/browser/styling-service.js +7 -8
  71. package/lib/browser/styling-service.js.map +1 -1
  72. package/lib/browser/widget-open-handler.d.ts +10 -0
  73. package/lib/browser/widget-open-handler.d.ts.map +1 -1
  74. package/lib/browser/widget-open-handler.js +24 -1
  75. package/lib/browser/widget-open-handler.js.map +1 -1
  76. package/lib/browser/window/default-secondary-window-service.d.ts +17 -3
  77. package/lib/browser/window/default-secondary-window-service.d.ts.map +1 -1
  78. package/lib/browser/window/default-secondary-window-service.js +76 -23
  79. package/lib/browser/window/default-secondary-window-service.js.map +1 -1
  80. package/lib/browser/window/default-secondary-window-service.spec.d.ts +2 -0
  81. package/lib/browser/window/default-secondary-window-service.spec.d.ts.map +1 -0
  82. package/lib/browser/window/default-secondary-window-service.spec.js +49 -0
  83. package/lib/browser/window/default-secondary-window-service.spec.js.map +1 -0
  84. package/lib/browser/window/secondary-window-close-contribution.d.ts +23 -0
  85. package/lib/browser/window/secondary-window-close-contribution.d.ts.map +1 -0
  86. package/lib/browser/window/secondary-window-close-contribution.js +39 -0
  87. package/lib/browser/window/secondary-window-close-contribution.js.map +1 -0
  88. package/lib/browser/window/secondary-window-service.d.ts +24 -1
  89. package/lib/browser/window/secondary-window-service.d.ts.map +1 -1
  90. package/lib/browser/window/secondary-window-service.js.map +1 -1
  91. package/lib/browser/window/window-focus-service.d.ts +4 -0
  92. package/lib/browser/window/window-focus-service.d.ts.map +1 -1
  93. package/lib/browser/window/window-focus-service.js +8 -2
  94. package/lib/browser/window/window-focus-service.js.map +1 -1
  95. package/lib/common/core-preferences.d.ts +2 -0
  96. package/lib/common/core-preferences.d.ts.map +1 -1
  97. package/lib/common/core-preferences.js +15 -0
  98. package/lib/common/core-preferences.js.map +1 -1
  99. package/lib/electron-browser/menu/electron-context-menu-renderer.d.ts.map +1 -1
  100. package/lib/electron-browser/menu/electron-context-menu-renderer.js +2 -1
  101. package/lib/electron-browser/menu/electron-context-menu-renderer.js.map +1 -1
  102. package/lib/electron-browser/window/electron-secondary-window-service.d.ts.map +1 -1
  103. package/lib/electron-browser/window/electron-secondary-window-service.js +1 -0
  104. package/lib/electron-browser/window/electron-secondary-window-service.js.map +1 -1
  105. package/package.json +4 -4
  106. package/src/browser/color-application-contribution.ts +6 -7
  107. package/src/browser/common-frontend-contribution.ts +8 -0
  108. package/src/browser/frontend-application-module.ts +6 -0
  109. package/src/browser/quick-input/quick-command-service.ts +8 -2
  110. package/src/browser/saveable-service.ts +8 -0
  111. package/src/browser/secondary-window-handler.ts +397 -34
  112. package/src/browser/shell/application-shell-mouse-tracker.ts +10 -0
  113. package/src/browser/shell/application-shell.ts +38 -5
  114. package/src/browser/shell/index.ts +1 -0
  115. package/src/browser/shell/native-tab-drag-service.spec.ts +86 -0
  116. package/src/browser/shell/native-tab-drag-service.ts +402 -0
  117. package/src/browser/shell/shell-layout-restorer.spec.ts +42 -0
  118. package/src/browser/shell/shell-layout-restorer.ts +20 -0
  119. package/src/browser/shell/side-panel-handler.ts +15 -0
  120. package/src/browser/shell/tab-bars.spec.ts +56 -2
  121. package/src/browser/shell/tab-bars.ts +146 -3
  122. package/src/browser/shell/tab-drop-helper.ts +79 -0
  123. package/src/browser/shell/theia-dock-panel.ts +122 -11
  124. package/src/browser/style/dockpanel.css +9 -0
  125. package/src/browser/styling-service.ts +6 -7
  126. package/src/browser/widget-open-handler.ts +24 -3
  127. package/src/browser/window/default-secondary-window-service.spec.ts +54 -0
  128. package/src/browser/window/default-secondary-window-service.ts +78 -26
  129. package/src/browser/window/secondary-window-close-contribution.ts +47 -0
  130. package/src/browser/window/secondary-window-service.ts +20 -1
  131. package/src/browser/window/window-focus-service.ts +9 -2
  132. package/src/common/core-preferences.ts +19 -0
  133. package/src/electron-browser/menu/electron-context-menu-renderer.ts +2 -1
  134. package/src/electron-browser/window/electron-secondary-window-service.ts +1 -0
@@ -21,7 +21,7 @@ import { ColorRegistry } from './color-registry';
21
21
  import { DecorationStyle } from './decoration-style';
22
22
  import { FrontendApplicationContribution } from './frontend-application-contribution';
23
23
  import { ThemeService } from './theming';
24
- import { SecondaryWindowHandler } from './secondary-window-handler';
24
+ import { SecondaryWindowService } from './window/secondary-window-service';
25
25
 
26
26
  export const StylingParticipant = Symbol('StylingParticipant');
27
27
 
@@ -52,15 +52,14 @@ export class StylingService implements FrontendApplicationContribution {
52
52
  @inject(ContributionProvider) @named(StylingParticipant)
53
53
  protected readonly themingParticipants: ContributionProvider<StylingParticipant>;
54
54
 
55
- @inject(SecondaryWindowHandler)
56
- protected readonly secondaryWindowHandler: SecondaryWindowHandler;
55
+ @inject(SecondaryWindowService)
56
+ protected readonly secondaryWindowService: SecondaryWindowService;
57
57
 
58
58
  onStart(): void {
59
59
  this.registerWindow(window);
60
- this.secondaryWindowHandler.onWillAddWidget(([widget, window]) => {
61
- this.registerWindow(window);
62
- });
63
- this.secondaryWindowHandler.onWillRemoveWidget(([widget, window]) => {
60
+ // registered per window rather than per widget, as a window may contain several widgets
61
+ this.secondaryWindowService.onWindowLoaded(win => this.registerWindow(win));
62
+ this.secondaryWindowService.onWindowClosed(window => {
64
63
  this.cssElements.delete(window);
65
64
  });
66
65
 
@@ -14,13 +14,15 @@
14
14
  // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
15
  // *****************************************************************************
16
16
 
17
- import { inject, postConstruct, injectable } from 'inversify';
17
+ import { inject, optional, postConstruct, injectable } from 'inversify';
18
18
  import URI from '../common/uri';
19
19
  import { MaybePromise, Emitter, Event } from '../common';
20
- import { BaseWidget } from './widgets';
20
+ import { BaseWidget, ExtractableWidget } from './widgets';
21
21
  import { ApplicationShell } from './shell';
22
22
  import { OpenHandler, OpenerOptions } from './opener-service';
23
23
  import { WidgetManager } from './widget-manager';
24
+ import { extractSecondaryWindow } from './secondary-window-handler';
25
+ import { WindowFocusService } from './window/window-focus-service';
24
26
 
25
27
  export type WidgetOpenMode = 'open' | 'reveal' | 'activate';
26
28
  /**
@@ -54,6 +56,9 @@ export abstract class WidgetOpenHandler<W extends BaseWidget> implements OpenHan
54
56
  @inject(WidgetManager)
55
57
  protected readonly widgetManager: WidgetManager;
56
58
 
59
+ @inject(WindowFocusService) @optional()
60
+ protected readonly windowFocusService: WindowFocusService | undefined;
61
+
57
62
  protected readonly onCreatedEmitter = new Emitter<W>();
58
63
  /**
59
64
  * Emit when a new widget is created.
@@ -99,7 +104,7 @@ export abstract class WidgetOpenHandler<W extends BaseWidget> implements OpenHan
99
104
  ...options
100
105
  };
101
106
  if (!widget.isAttached) {
102
- await this.shell.addWidget(widget, op.widgetOptions || { area: 'main' });
107
+ await this.shell.addWidget(widget, op.widgetOptions || this.getDefaultWidgetOptions(widget));
103
108
  }
104
109
  if (op.mode === 'activate') {
105
110
  await this.shell.activateWidget(widget.id);
@@ -108,6 +113,22 @@ export abstract class WidgetOpenHandler<W extends BaseWidget> implements OpenHan
108
113
  }
109
114
  }
110
115
 
116
+ /**
117
+ * The options to add a widget with if the opener options don't specify any: widgets are added to the main area,
118
+ * unless the user works in a secondary window. Then widgets that can be moved to secondary windows are added there,
119
+ * next to the current widget, like editors are opened in the active editor group in VS Code.
120
+ *
121
+ * @param widget the widget to add, if known already
122
+ */
123
+ protected getDefaultWidgetOptions(widget?: W): ApplicationShell.WidgetOptions {
124
+ const current = this.shell.currentWidget;
125
+ const secondaryWindow = current && extractSecondaryWindow(current);
126
+ if (secondaryWindow && secondaryWindow === this.windowFocusService?.focusedWindow && (!widget || ExtractableWidget.is(widget))) {
127
+ return { area: 'secondaryWindow', ref: current };
128
+ }
129
+ return { area: 'main' };
130
+ }
131
+
111
132
  /**
112
133
  * Tries to get an existing widget for the given uri.
113
134
  * @param uri the uri of the widget.
@@ -0,0 +1,54 @@
1
+ // *****************************************************************************
2
+ // Copyright (C) 2026 EclipseSource and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import { enableJSDOM } from '../test/jsdom';
18
+ let disableJSDOM = enableJSDOM();
19
+ import { expect } from 'chai';
20
+
21
+ import { Widget } from '@lumino/widgets';
22
+ import { PreferenceService } from '../../common';
23
+ // load the shell first, loading it through the secondary window handler hits a circular import
24
+ import { ApplicationShell } from '../shell/application-shell';
25
+ import { ExtractableWidget } from '../widgets';
26
+ import { DefaultSecondaryWindowService } from './default-secondary-window-service';
27
+
28
+ disableJSDOM();
29
+
30
+ describe('DefaultSecondaryWindowService', () => {
31
+
32
+ before(() => {
33
+ disableJSDOM = enableJSDOM();
34
+ });
35
+
36
+ after(() => {
37
+ disableJSDOM();
38
+ });
39
+
40
+ it('should return undefined when the browser blocks the window', () => {
41
+ const service = new DefaultSecondaryWindowService();
42
+ (service as unknown as { preferenceService: Partial<PreferenceService> }).preferenceService = { get: () => undefined };
43
+ const open = window.open;
44
+ window.open = () => null; // eslint-disable-line no-null/no-null
45
+ try {
46
+ const widget = new Widget() as unknown as ExtractableWidget;
47
+ const options = { position: { x: 0, y: 0 }, size: { width: 100, height: 100 } };
48
+ const shell = Object.create(ApplicationShell.prototype) as ApplicationShell;
49
+ expect(service.createSecondaryWindow(widget, shell, options)).to.be.undefined;
50
+ } finally {
51
+ window.open = open;
52
+ }
53
+ });
54
+ });
@@ -14,15 +14,16 @@
14
14
  // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
15
  // *****************************************************************************
16
16
  import { inject, injectable, postConstruct, named } from 'inversify';
17
- import { SecondaryWindow, SecondaryWindowService } from './secondary-window-service';
17
+ import { SecondaryWindow, SecondaryWindowOptions, SecondaryWindowService } from './secondary-window-service';
18
18
  import { WindowService } from './window-service';
19
19
  import { ExtractableWidget, Widget } from '../widgets';
20
20
  import { ApplicationShell } from '../shell';
21
- import { Saveable } from '../saveable';
22
21
  import { Emitter, environment, Event, PreferenceService, ILogger } from '../../common';
23
22
  import { SaveableService } from '../saveable-service';
24
- import { getAllWidgetsFromSecondaryWindow, getDefaultRestoreArea } from '../secondary-window-handler';
23
+ import { getAllWidgetsFromSecondaryWindow, getDefaultRestoreArea, SecondaryWindowHandler } from '../secondary-window-handler';
25
24
  import { WindowFocusService } from './window-focus-service';
25
+ import { SecondaryWindowCloseContribution } from './secondary-window-close-contribution';
26
+ import { ContributionProvider } from '../../common/contribution-provider';
26
27
 
27
28
  @injectable()
28
29
  export class DefaultSecondaryWindowService implements SecondaryWindowService {
@@ -47,6 +48,12 @@ export class DefaultSecondaryWindowService implements SecondaryWindowService {
47
48
 
48
49
  protected secondaryWindows: Window[] = [];
49
50
 
51
+ /**
52
+ * Whether the main window is shutting down, which closes all secondary windows. Such a close is not handled like a close by the user:
53
+ * the widgets stay in their windows, so that the windows are stored with the layout and restored on the next start.
54
+ */
55
+ protected shuttingDown = false;
56
+
50
57
  @inject(WindowService)
51
58
  protected readonly windowService: WindowService;
52
59
 
@@ -62,6 +69,9 @@ export class DefaultSecondaryWindowService implements SecondaryWindowService {
62
69
  @inject(ILogger) @named('core:DefaultSecondaryWindowService')
63
70
  protected readonly logger: ILogger;
64
71
 
72
+ @inject(ContributionProvider) @named(SecondaryWindowCloseContribution)
73
+ protected readonly closeContributions: ContributionProvider<SecondaryWindowCloseContribution>;
74
+
65
75
  @postConstruct()
66
76
  init(): void {
67
77
  // Set up messaging with secondary windows
@@ -97,6 +107,7 @@ export class DefaultSecondaryWindowService implements SecondaryWindowService {
97
107
  protected registerShutdownListeners(): void {
98
108
  // Close all open windows when the main window is closed.
99
109
  this.windowService.onUnload(() => {
110
+ this.shuttingDown = true;
100
111
  // Iterate backwards because calling window.close might remove the window from the array
101
112
  for (let i = this.secondaryWindows.length - 1; i >= 0; i--) {
102
113
  this.secondaryWindows[i].close();
@@ -104,13 +115,18 @@ export class DefaultSecondaryWindowService implements SecondaryWindowService {
104
115
  });
105
116
  }
106
117
 
107
- createSecondaryWindow(widget: ExtractableWidget, shell: ApplicationShell): Window | SecondaryWindow | undefined {
108
- const [height, width, left, top] = this.findSecondaryWindowCoordinates(widget);
109
- let options = `popup=1,width=${width},height=${height},left=${left},top=${top}`;
118
+ createSecondaryWindow(widget: ExtractableWidget, shell: ApplicationShell, options?: SecondaryWindowOptions): Window | SecondaryWindow | undefined {
119
+ let [height, width, left, top] = options?.position
120
+ ? [widget.node.clientHeight, widget.node.clientWidth, options.position.x, options.position.y]
121
+ : this.findSecondaryWindowCoordinates(widget);
122
+ if (options?.size) {
123
+ ({ width, height } = options.size);
124
+ }
125
+ let features = `popup=1,width=${width},height=${height},left=${left},top=${top}`;
110
126
  if (this.preferenceService.get('window.secondaryWindowAlwaysOnTop')) {
111
- options += ',alwaysOnTop=true';
127
+ features += ',alwaysOnTop=true';
112
128
  }
113
- const newWindow = window.open(DefaultSecondaryWindowService.SECONDARY_WINDOW_URL, this.nextWindowId(), options) ?? undefined;
129
+ const newWindow = window.open(DefaultSecondaryWindowService.SECONDARY_WINDOW_URL, this.nextWindowId(), features) ?? undefined;
114
130
  if (newWindow) {
115
131
  this.secondaryWindows.push(newWindow);
116
132
  this.onWindowOpenedEmitter.fire(newWindow);
@@ -121,11 +137,13 @@ export class DefaultSecondaryWindowService implements SecondaryWindowService {
121
137
  this.onWindowLoadedEmitter.fire(newWindow);
122
138
  const focusRegistration = this.windowFocusService.registerWindow(newWindow);
123
139
  newWindow.addEventListener('beforeunload', evt => {
140
+ if (this.shuttingDown) {
141
+ // the main window has asked about unsaved changes already
142
+ return;
143
+ }
124
144
  const widgets = getAllWidgetsFromSecondaryWindow(newWindow) ?? [widget];
125
145
  for (const w of widgets) {
126
- const saveable = Saveable.get(w);
127
- const wouldLoseState = !!saveable && saveable.dirty && this.saveResourceService.autoSave === 'off';
128
- if (wouldLoseState) {
146
+ if (this.saveResourceService.wouldLoseChangesOnClose(w)) {
129
147
  evt.returnValue = '';
130
148
  evt.preventDefault();
131
149
  return 'non-empty';
@@ -143,8 +161,8 @@ export class DefaultSecondaryWindowService implements SecondaryWindowService {
143
161
  });
144
162
  this.windowCreated(newWindow, widget, shell);
145
163
  });
164
+ (newWindow as SecondaryWindow).rootWidget = undefined;
146
165
  }
147
- (newWindow as SecondaryWindow).rootWidget = undefined;
148
166
  return newWindow;
149
167
  }
150
168
 
@@ -217,30 +235,64 @@ export class DefaultSecondaryWindowService implements SecondaryWindowService {
217
235
  return `${this.prefix}-secondaryWindow-${this.nextId++}`;
218
236
  }
219
237
 
238
+ async restoreWidget(widget: Widget, secondaryWindow: Window, shell: ApplicationShell): Promise<void> {
239
+ const area = SecondaryWindowHandler.getMainWindowArea(ExtractableWidget.is(widget) ? widget.previousArea : getDefaultRestoreArea(secondaryWindow));
240
+ // fire removed event before adding it to shell
241
+ this.beforeWidgetRestoreEmitter.fire([widget, secondaryWindow]);
242
+ // reset ExtractableWidget properties before moving back so that handler evaluation is correct immediately
243
+ if (ExtractableWidget.is(widget)) {
244
+ widget.secondaryWindow = undefined;
245
+ widget.previousArea = undefined;
246
+ }
247
+ await shell.addWidget(widget, { area });
248
+ // Don't wait for the widget to accept focus: while the secondary window is being closed, the main window may not
249
+ // have focus yet, so waiting would delay closing the window until the activation times out.
250
+ shell.activateWidget(widget.id);
251
+ }
252
+
220
253
  /**
221
- * Restore the widgets back to the main window. SecondaryWindowHandler needs to get informated about this.
254
+ * @returns whether the given widget is to be moved back to the main window instead of being closed with its secondary window
222
255
  */
223
- protected async restoreWidgets(newWindow: Window, extractableWidget: ExtractableWidget, shell: ApplicationShell): Promise<boolean> {
224
- const widgets = getAllWidgetsFromSecondaryWindow(newWindow) ?? new Set([extractableWidget]);
225
- const defaultRestoreArea = getDefaultRestoreArea(newWindow);
256
+ protected async shouldRestoreOnClose(widget: Widget): Promise<boolean> {
257
+ for (const contribution of this.closeContributions.getContributions()) {
258
+ try {
259
+ if (await contribution.shouldRestoreOnClose(widget)) {
260
+ return true;
261
+ }
262
+ } catch (e) {
263
+ this.logger.error('Failed to determine whether to move a widget back to the main window', e);
264
+ return true;
265
+ }
266
+ }
267
+ return false;
268
+ }
226
269
 
270
+ /**
271
+ * Restore the widgets back to the main window, or close them, depending on the `window.secondaryWindowCloseBehavior` preference.
272
+ * SecondaryWindowHandler needs to get informated about this.
273
+ */
274
+ protected async restoreWidgets(newWindow: Window, extractableWidget: ExtractableWidget, shell: ApplicationShell): Promise<boolean> {
275
+ if (this.shuttingDown) {
276
+ // The widgets stay in the window, so that it is stored with the layout and restored with its widgets on the next start.
277
+ return true;
278
+ }
279
+ // Copy, as moving a widget back removes it from the secondary window's widgets.
280
+ // A window closed before it has been populated has no root widget, its widgets are still where they were.
281
+ const widgets = [...getAllWidgetsFromSecondaryWindow(newWindow) ?? []];
282
+ const closeWidgets = this.preferenceService.get('window.secondaryWindowCloseBehavior', 'close') === 'close';
227
283
  let allMovedOrDisposed = true;
228
284
  for (const widget of widgets) {
229
285
  if (widget.isDisposed) {
230
286
  continue;
231
287
  }
232
288
  try {
233
- const preferredRestoreArea = ExtractableWidget.is(widget) ? widget.previousArea : defaultRestoreArea;
234
- const area = (preferredRestoreArea === undefined || preferredRestoreArea === 'top' || preferredRestoreArea === 'secondaryWindow') ? 'main' : preferredRestoreArea;
235
- // fire removed event before adding it to shell
236
- this.beforeWidgetRestoreEmitter.fire([widget, newWindow]);
237
- // reset ExtractableWidget properties before moving back so that handler evaluation is correct immediately
238
- if (ExtractableWidget.is(widget)) {
239
- widget.secondaryWindow = undefined;
240
- widget.previousArea = undefined;
289
+ if (closeWidgets && !await this.shouldRestoreOnClose(widget)) {
290
+ await shell.closeWidget(widget.id);
291
+ }
292
+ // widgets that are not disposed when closed would be lost with the window
293
+ if (!widget.isDisposed) {
294
+ await this.restoreWidget(widget, newWindow, shell);
241
295
  }
242
- await shell.addWidget(widget, { area });
243
- await shell.activateWidget(widget.id);
244
296
  } catch (e) {
245
297
  // we can't move back, close instead
246
298
  // otherwise the window will just stay open with no way to close it
@@ -0,0 +1,47 @@
1
+ // *****************************************************************************
2
+ // Copyright (C) 2026 EclipseSource and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import { inject, injectable } from 'inversify';
18
+ import { MaybePromise } from '../../common/types';
19
+ import { Widget } from '../widgets';
20
+ import { SaveableService } from '../saveable-service';
21
+
22
+ export const SecondaryWindowCloseContribution = Symbol('SecondaryWindowCloseContribution');
23
+ /**
24
+ * Decides which widgets are moved back to the main window when their secondary window is closed,
25
+ * if the `window.secondaryWindowCloseBehavior` preference is `close`. All other widgets are closed.
26
+ */
27
+ export interface SecondaryWindowCloseContribution {
28
+ /**
29
+ * @returns `true` if the widget would lose state if it was closed, so that it is moved back to the main window instead,
30
+ * `false` or `undefined` if it may be closed as far as this contribution is concerned
31
+ */
32
+ shouldRestoreOnClose(widget: Widget): MaybePromise<boolean | undefined>;
33
+ }
34
+
35
+ /**
36
+ * Moves widgets with unsaved changes back to the main window, unless they are saved automatically.
37
+ */
38
+ @injectable()
39
+ export class SaveableSecondaryWindowCloseContribution implements SecondaryWindowCloseContribution {
40
+
41
+ @inject(SaveableService)
42
+ protected readonly saveableService: SaveableService;
43
+
44
+ shouldRestoreOnClose(widget: Widget): boolean {
45
+ return this.saveableService.wouldLoseChangesOnClose(widget);
46
+ }
47
+ }
@@ -38,6 +38,18 @@ export function isSecondaryWindow(window: unknown): window is SecondaryWindow {
38
38
  return typeof window === 'object' && 'rootWidget' in window;
39
39
  }
40
40
 
41
+ export interface SecondaryWindowOptions {
42
+ /**
43
+ * The screen position of the top left corner of the window. If omitted, the window is placed
44
+ * according to the `window.secondaryWindowPlacement` preference.
45
+ */
46
+ position?: { x: number, y: number };
47
+ /**
48
+ * The size of the window. If omitted, the size is derived from the widget and the `window.secondaryWindowPlacement` preference.
49
+ */
50
+ size?: { width: number, height: number };
51
+ }
52
+
41
53
  export const SecondaryWindowService = Symbol('SecondaryWindowService');
42
54
 
43
55
  /**
@@ -51,9 +63,10 @@ export interface SecondaryWindowService {
51
63
  * The created window is closed automatically when the current theia instance is closed.
52
64
  *
53
65
  * @param onClose optional callback that is invoked when the secondary window is closed
66
+ * @param options optional options, e.g. the screen position at which to open the window
54
67
  * @returns the created window or `undefined` if it could not be created
55
68
  */
56
- createSecondaryWindow(widget: ExtractableWidget, shell: ApplicationShell): SecondaryWindow | Window | undefined;
69
+ createSecondaryWindow(widget: ExtractableWidget, shell: ApplicationShell, options?: SecondaryWindowOptions): SecondaryWindow | Window | undefined;
57
70
  readonly onWindowOpened: Event<Window>;
58
71
  /**
59
72
  * Emitted once a secondary window's final document has finished loading. Listeners can safely
@@ -65,6 +78,12 @@ export interface SecondaryWindowService {
65
78
  readonly onWindowClosed: Event<Window>;
66
79
  readonly beforeWidgetRestore: Event<[Widget, Window]>;
67
80
 
81
+ /**
82
+ * Moves the given widget from the given secondary window back to the application shell.
83
+ * The secondary window is closed once it does not contain any widgets anymore.
84
+ */
85
+ restoreWidget(widget: Widget, secondaryWindow: Window, shell: ApplicationShell): Promise<void>;
86
+
68
87
  /** Handles focussing the given secondary window in the browser and on Electron. */
69
88
  focus(win: Window): void;
70
89
  getWindows(): Window[];
@@ -118,12 +118,19 @@ export class WindowFocusService implements Disposable {
118
118
  * Whether any registered window currently has focus.
119
119
  */
120
120
  get hasFocus(): boolean {
121
+ return this.focusedWindow !== undefined;
122
+ }
123
+
124
+ /**
125
+ * The registered window that currently has focus, if any.
126
+ */
127
+ get focusedWindow(): Window | undefined {
121
128
  for (const win of this.windowStates.keys()) {
122
129
  if (this.windowHasFocus(win)) {
123
- return true;
130
+ return win;
124
131
  }
125
132
  }
126
- return false;
133
+ return undefined;
127
134
  }
128
135
 
129
136
  protected windowHasFocus(win: Window): boolean {
@@ -150,6 +150,23 @@ export const corePreferenceSchema: PreferenceSchema = {
150
150
  default: false,
151
151
  description: nls.localize('theia/core/secondaryWindow/alwaysOnTop', 'When enabled, the secondary window stays above all other windows, including those of different applications.'),
152
152
  },
153
+ 'window.secondaryWindowCloseBehavior': {
154
+ type: 'string',
155
+ enum: ['close', 'moveToMainWindow'],
156
+ enumDescriptions: [
157
+ nls.localize('theia/core/secondaryWindow/closeBehavior/close',
158
+ 'The views of the secondary window are closed. Views that would lose state, e.g. editors with unsaved changes, are moved back to the main window.'),
159
+ nls.localize('theia/core/secondaryWindow/closeBehavior/moveToMainWindow', 'All views of the secondary window are moved back to the main window.'),
160
+ ],
161
+ default: 'close',
162
+ description: nls.localize('theia/core/secondaryWindow/closeBehavior', 'Controls what happens to the views of a secondary window when it is closed.'),
163
+ },
164
+ 'window.restoreSecondaryWindows': {
165
+ type: 'boolean',
166
+ default: true,
167
+ description: nls.localize('theia/core/secondaryWindow/restore',
168
+ 'Controls whether secondary windows are restored on startup. When disabled, their views are restored in the main window instead.'),
169
+ },
153
170
  'http.proxy': {
154
171
  type: 'string',
155
172
  pattern: '^https?://([^:]*(:[^@]*)?@)?([^:]+|\\[[:0-9a-fA-F]+\\])(:\\d+)?/?$|^$',
@@ -319,6 +336,8 @@ export interface CoreConfiguration {
319
336
  'window.title': string;
320
337
  'window.titleSeparator': string;
321
338
  'window.tabCloseIconPlacement': 'end' | 'start';
339
+ 'window.secondaryWindowCloseBehavior': 'close' | 'moveToMainWindow';
340
+ 'window.restoreSecondaryWindows': boolean;
322
341
  'workbench.list.openMode': 'singleClick' | 'doubleClick';
323
342
  'workbench.commandPalette.history': number;
324
343
  'workbench.editor.highlightModifiedTabs': boolean;
@@ -122,7 +122,8 @@ export class ElectronContextMenuRenderer extends BrowserContextMenuRenderer {
122
122
  const contextMenu = this.electronMenuFactory.createElectronContextMenu(params.menuPath, params.menu, params.contextMatcher, params.args, params.context);
123
123
  const { x, y } = coordinateFromAnchor(params.anchor);
124
124
 
125
- const windowName = params.context?.ownerDocument.defaultView?.Window.name;
125
+ // the name of the window, e.g. of a secondary window, in which the menu is to be shown; empty for the main window
126
+ const windowName = params.context?.ownerDocument.defaultView?.name;
126
127
 
127
128
  const menuHandle = window.electronTheiaCore.popup(contextMenu, x, y, () => {
128
129
  if (params.onHide) {
@@ -56,6 +56,7 @@ export class ElectronSecondaryWindowService extends DefaultSecondaryWindowServic
56
56
  override registerShutdownListeners(): void {
57
57
  // Close all open windows when the main window is closed.
58
58
  (this.windowService as ElectronWindowService).onWillShutDown(() => {
59
+ this.shuttingDown = true;
59
60
  const promises = [];
60
61
  // Iterate backwards because calling window.close might remove the window from the array
61
62
  for (let i = this.secondaryWindows.length - 1; i >= 0; i--) {