Skip to content

Commit

Permalink
Call AllowSetForegroundWindow before sending IPC to the running insta…
Browse files Browse the repository at this point in the history
…nce on Windows (Fixes microsoft#929)
  • Loading branch information
the-ress committed Oct 7, 2016
1 parent 069984e commit 90770d2
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions build/gulpfile.vscode.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ function packageTask(platform, arch, opts) {
.pipe(util.cleanNodeModule('oniguruma', ['binding.gyp', 'build/**', 'src/**', 'deps/**'], ['**/*.node']))
.pipe(util.cleanNodeModule('windows-mutex', ['binding.gyp', 'build/**', 'src/**'], ['**/*.node']))
.pipe(util.cleanNodeModule('native-keymap', ['binding.gyp', 'build/**', 'src/**', 'deps/**'], ['**/*.node']))
.pipe(util.cleanNodeModule('windows-foreground-love', ['binding.gyp', 'build/**', 'src/**'], ['**/*.node']))
.pipe(util.cleanNodeModule('gc-signals', ['binding.gyp', 'build/**', 'src/**', 'deps/**'], ['**/*.node', 'src/index.js']))
.pipe(util.cleanNodeModule('pty.js', ['binding.gyp', 'build/**', 'src/**', 'deps/**'], ['build/Release/**']));

Expand Down
5 changes: 5 additions & 0 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
}
},
"optionalDependencies": {
"windows-foreground-love": "0.1.0",
"windows-mutex": "^0.2.0",
"fsevents": "0.3.8"
}
Expand Down
10 changes: 10 additions & 0 deletions src/typings/windows-foreground-love.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

declare module 'windows-foreground-love' {

export function allowSetForegroundWindow(pid?: number): boolean;

}
20 changes: 17 additions & 3 deletions src/vs/code/electron-main/launch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ export interface IStartArguments {

export interface ILaunchService {
start(args: ParsedArgs, userEnv: IProcessEnvironment): TPromise<void>;
getMainProcessId(): TPromise<number>;
}

export interface ILaunchChannel extends IChannel {
call(command: 'start', arg: IStartArguments): TPromise<void>;
call(command: 'get-main-process-id', arg: null): TPromise<any>;
call(command: string, arg: any): TPromise<any>;
}

Expand All @@ -33,10 +35,13 @@ export class LaunchChannel implements ILaunchChannel {
constructor(private service: ILaunchService) { }

call(command: string, arg: any): TPromise<any> {
const { args, userEnv } = arg as IStartArguments;

switch (command) {
case 'start': return this.service.start(args, userEnv);
case 'start':
const { args, userEnv } = arg as IStartArguments;
return this.service.start(args, userEnv);

case 'get-main-process-id':
return this.service.getMainProcessId();
}
}
}
Expand All @@ -48,6 +53,10 @@ export class LaunchChannelClient implements ILaunchService {
start(args: ParsedArgs, userEnv: IProcessEnvironment): TPromise<void> {
return this.channel.call('start', { args, userEnv });
}

getMainProcessId(): TPromise<number> {
return this.channel.call('get-main-process-id', null);
}
}

export class LaunchService implements ILaunchService {
Expand Down Expand Up @@ -105,4 +114,9 @@ export class LaunchService implements ILaunchService {

return TPromise.as(null);
}

getMainProcessId(): TPromise<number> {
this.logService.log('Received request for process ID from other instance.');
return TPromise.as(process.pid);
}
}
13 changes: 12 additions & 1 deletion src/vs/code/electron-main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { AskpassChannel } from 'vs/workbench/parts/git/common/gitIpc';
import { GitAskpassService } from 'vs/workbench/parts/git/electron-main/askpassService';
import { spawnSharedProcess } from 'vs/code/node/sharedProcess';
import { Mutex } from 'windows-mutex';
import { allowSetForegroundWindow } from 'windows-foreground-love';
import { LaunchService, ILaunchChannel, LaunchChannel, LaunchChannelClient } from './launch';
import { ServicesAccessor, IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { InstantiationService } from 'vs/platform/instantiation/common/instantiationService';
Expand Down Expand Up @@ -299,7 +300,17 @@ function setupIPC(accessor: ServicesAccessor): TPromise<Server> {
const channel = client.getChannel<ILaunchChannel>('launch');
const service = new LaunchChannelClient(channel);

return service.start(environmentService.args, process.env)
let promise = TPromise.as(null);
if (platform.isWindows) {
promise = service.getMainProcessId()
.then(processId => {
logService.log('Sending some foreground love to the running instance:', processId);
allowSetForegroundWindow(processId);
});
}

return promise
.then(() => service.start(environmentService.args, process.env))
.then(() => client.dispose())
.then(() => TPromise.wrapError('Sent env to running instance. Terminating...'));
},
Expand Down

0 comments on commit 90770d2

Please sign in to comment.