pimp up open-url

This commit is contained in:
Joao Moreno 2016-08-04 18:59:44 +02:00
parent 7803b68ca1
commit 51047443d3
3 changed files with 24 additions and 10 deletions

View file

@ -110,10 +110,7 @@ function main(accessor: ServicesAccessor, mainIpcServer: Server, userEnv: IProce
// Register Electron IPC services
const urlService = instantiationService.createInstance(URLService);
const urlChannel = new URLChannel(urlService, id => {
const window = windowsService.getFocusedWindow() || windowsService.getLastActiveWindow();
return window ? window.id === id : false;
});
const urlChannel = instantiationService.createInstance(URLChannel, urlService);
electronIpcServer.registerChannel('url', urlChannel);
// Used by sub processes to communicate back to the main instance

View file

@ -9,6 +9,7 @@ import { TPromise } from 'vs/base/common/winjs.base';
import { IChannel, eventToCall, eventFromCall } from 'vs/base/parts/ipc/common/ipc';
import { IURLService } from './url';
import Event, { filterEvent } from 'vs/base/common/event';
import { IWindowsService } from 'vs/code/electron-main/windows';
export interface IURLChannel extends IChannel {
call(command: 'event:onOpenURL'): TPromise<void>;
@ -17,21 +18,37 @@ export interface IURLChannel extends IChannel {
export class URLChannel implements IURLChannel {
constructor(private service: IURLService, private eventScope: (id: number) => boolean) { }
constructor(
private service: IURLService,
@IWindowsService private windowsService: IWindowsService
) { }
call(command: string, arg: any): TPromise<any> {
switch (command) {
case 'event:onOpenURL': return eventToCall(filterEvent(this.service.onOpenURL, () => this.eventScope(arg as number)));
case 'event:onOpenURL': return eventToCall(filterEvent(this.service.onOpenURL, () => this.isWindowFocused(arg)));
}
}
/**
* We only want the focused window to get pinged with the onOpenUrl event.
* The idea here is to filter the onOpenUrl event with the knowledge of which
* was the last window to be focused. When first listening to the event,
* each client sends its window ID via the arguments to `call(...)`.
* When the event fires, the server has enough knowledge to filter the event
* and fire it only to the focused window.
*/
private isWindowFocused(windowID: number): boolean {
const window = this.windowsService.getFocusedWindow() || this.windowsService.getLastActiveWindow();
return window ? window.id === windowID : false;
}
}
export class URLChannelClient implements IURLService {
_serviceBrand: any;
constructor(private channel: IChannel, private id: number) { }
constructor(private channel: IChannel, private windowID: number) { }
private _onOpenURL = eventFromCall<string>(this.channel, 'event:onOpenURL', this.id);
private _onOpenURL = eventFromCall<string>(this.channel, 'event:onOpenURL', this.windowID);
get onOpenURL(): Event<string> { return this._onOpenURL; }
}

View file

@ -319,11 +319,11 @@ export class WorkbenchShell {
serviceCollection.set(ICodeEditorService, codeEditorService);
const extensionManagementChannel = getDelayedChannel<IExtensionManagementChannel>(sharedProcess.then(c => c.getChannel('extensions')));
const extensionManagementChannelClient = instantiationService.createInstance(ExtensionManagementChannelClient, extensionManagementChannel);
const extensionManagementChannelClient = new ExtensionManagementChannelClient(extensionManagementChannel);
serviceCollection.set(IExtensionManagementService, extensionManagementChannelClient);
const urlChannel = mainProcessClient.getChannel('url');
const urlChannelClient = instantiationService.createInstance(URLChannelClient, urlChannel, this.windowService.getWindowId());
const urlChannelClient = new URLChannelClient(urlChannel, this.windowService.getWindowId());
serviceCollection.set(IURLService, urlChannelClient);
return [instantiationService, serviceCollection];