Skip to content

Commit 29ca95b

Browse files
author
Benjamin Pasero
committed
Hitting a breakpoint doesn't active window (fix microsoft#105913)
1 parent 5dc6727 commit 29ca95b

10 files changed

Lines changed: 37 additions & 28 deletions

File tree

src/vs/code/electron-main/window.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,18 @@ export class CodeWindow extends Disposable implements ICodeWindow {
336336
return !!this.documentEdited;
337337
}
338338

339-
focus(): void {
339+
focus(options?: { force: boolean }): void {
340+
// macOS: Electron >6.x changed its behaviour to not
341+
// bring the application to the foreground when a window
342+
// is focused programmatically. Only via `app.focus` and
343+
// the option `steal: true` can you get the previous
344+
// behaviour back. The only reason to use this option is
345+
// when a window is getting focused while the application
346+
// is not in the foreground.
347+
if (isMacintosh && options?.force) {
348+
app.focus({ steal: true });
349+
}
350+
340351
if (!this._win) {
341352
return;
342353
}
@@ -719,7 +730,7 @@ export class CodeWindow extends Disposable implements ICodeWindow {
719730
this.showTimeoutHandle = setTimeout(() => {
720731
if (this._win && !this._win.isVisible() && !this._win.isMinimized()) {
721732
this._win.show();
722-
this._win.focus();
733+
this.focus({ force: true });
723734
this._win.webContents.openDevTools();
724735
}
725736
}, 10000);

src/vs/platform/electron/common/electron.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@ export interface ICommonElectronService {
4444
unmaximizeWindow(): Promise<void>;
4545
minimizeWindow(): Promise<void>;
4646

47-
focusWindow(options?: { windowId?: number }): Promise<void>;
47+
/**
48+
* Make the window focused.
49+
*
50+
* @param options Pass `force: true` if you want to make the window take
51+
* focus even if the application does not have focus currently. This option
52+
* should only be used if it is necessary to steal focus from the current
53+
* focused application which may not be VSCode.
54+
*/
55+
focusWindow(options?: { windowId?: number, force?: boolean }): Promise<void>;
4856

4957
// Dialogs
5058
showMessageBox(options: MessageBoxOptions): Promise<MessageBoxReturnValue>;

src/vs/platform/electron/electron-main/electronMainService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,14 @@ export class ElectronMainService implements IElectronMainService {
172172
}
173173
}
174174

175-
async focusWindow(windowId: number | undefined, options?: { windowId?: number; }): Promise<void> {
175+
async focusWindow(windowId: number | undefined, options?: { windowId?: number; force?: boolean; }): Promise<void> {
176176
if (options && typeof options.windowId === 'number') {
177177
windowId = options.windowId;
178178
}
179179

180180
const window = this.windowById(windowId);
181181
if (window) {
182-
window.focus();
182+
window.focus({ force: options?.force ?? false });
183183
}
184184
}
185185

src/vs/platform/windows/electron-main/windows.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export interface ICodeWindow extends IDisposable {
6161
load(config: INativeWindowConfiguration, isReload?: boolean): void;
6262
reload(configuration?: INativeWindowConfiguration, cli?: ParsedArgs): void;
6363

64-
focus(): void;
64+
focus(options?: { force: boolean }): void;
6565
close(): void;
6666

6767
getBounds(): Rectangle;

src/vs/platform/windows/electron-main/windowsMainService.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,6 @@ export class WindowsMainService extends Disposable implements IWindowsMainServic
167167
private readonly _onWindowReady = this._register(new Emitter<ICodeWindow>());
168168
readonly onWindowReady = this._onWindowReady.event;
169169

170-
private readonly _onWindowClose = this._register(new Emitter<number>());
171-
readonly onWindowClose = this._onWindowClose.event;
172-
173170
private readonly _onWindowsCountChanged = this._register(new Emitter<IWindowsCountChangedEvent>());
174171
readonly onWindowsCountChanged = this._onWindowsCountChanged.event;
175172

@@ -1626,18 +1623,6 @@ export class WindowsMainService extends Disposable implements IWindowsMainServic
16261623
return state;
16271624
}
16281625

1629-
focusLastActive(cli: ParsedArgs, context: OpenContext): ICodeWindow {
1630-
const lastActive = this.getLastActiveWindow();
1631-
if (lastActive) {
1632-
lastActive.focus();
1633-
1634-
return lastActive;
1635-
}
1636-
1637-
// No window - open new empty one
1638-
return this.open({ context, cli, forceEmpty: true })[0];
1639-
}
1640-
16411626
getLastActiveWindow(): ICodeWindow | undefined {
16421627
return getLastActiveWindow(WindowsMainService.WINDOWS);
16431628
}
@@ -1695,6 +1680,5 @@ export class WindowsMainService extends Disposable implements IWindowsMainServic
16951680

16961681
// Emit
16971682
this._onWindowsCountChanged.fire({ oldCount: WindowsMainService.WINDOWS.length + 1, newCount: WindowsMainService.WINDOWS.length });
1698-
this._onWindowClose.fire(win.id);
16991683
}
17001684
}

src/vs/workbench/contrib/debug/browser/debugSession.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ export class DebugSession implements IDebugSession {
813813
}
814814

815815
if (this.configurationService.getValue<IDebugConfiguration>('debug').focusWindowOnBreak) {
816-
this.hostService.focus();
816+
this.hostService.focus({ force: true /* Application may not be active */ });
817817
}
818818
}
819819
}

src/vs/workbench/services/host/browser/host.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@ export interface IHostService {
3232

3333
/**
3434
* Attempt to bring the window to the foreground and focus it.
35+
*
36+
* @param options Pass `force: true` if you want to make the window take
37+
* focus even if the application does not have focus currently. This option
38+
* should only be used if it is necessary to steal focus from the current
39+
* focused application which may not be VSCode. It may not be supported
40+
* in all environments.
3541
*/
36-
focus(): Promise<void>;
42+
focus(options?: { force: boolean }): Promise<void>;
3743

3844
//#endregion
3945

src/vs/workbench/services/host/electron-sandbox/nativeHostService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ export class NativeHostService extends Disposable implements IHostService {
8282
return this.electronService.toggleFullScreen();
8383
}
8484

85-
focus(): Promise<void> {
86-
return this.electronService.focusWindow();
85+
focus(options?: { force: boolean }): Promise<void> {
86+
return this.electronService.focusWindow(options);
8787
}
8888

8989
restart(): Promise<void> {

src/vs/workbench/services/url/electron-sandbox/urlService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class RelayURLService extends NativeURLService implements IURLHandler, IO
6666
const result = await super.open(uri, options);
6767

6868
if (result) {
69-
await this.electronService.focusWindow();
69+
await this.electronService.focusWindow({ force: true /* Application may not be active */ });
7070
}
7171

7272
return result;

src/vs/workbench/test/browser/workbenchTestServices.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ export class TestHostService implements IHostService {
10321032
async restart(): Promise<void> { }
10331033
async reload(): Promise<void> { }
10341034

1035-
async focus(): Promise<void> { }
1035+
async focus(options?: { force: boolean }): Promise<void> { }
10361036

10371037
async openWindow(arg1?: IOpenEmptyWindowOptions | IWindowOpenable[], arg2?: IOpenWindowOptions): Promise<void> { }
10381038

0 commit comments

Comments
 (0)