Skip to content

Commit

Permalink
support workspace identifier in broadcasting API
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Jul 12, 2017
1 parent de02dda commit a5f3583
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 17 deletions.
21 changes: 10 additions & 11 deletions src/vs/code/electron-main/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ import { IWindowsMainService } from "vs/platform/windows/electron-main/windows";
import { IHistoryMainService } from "vs/platform/history/common/history";
import { isUndefinedOrNull } from 'vs/base/common/types';
import { CodeWindow } from "vs/code/electron-main/window";
import { isParent } from 'vs/platform/files/common/files';
import { isEqual } from 'vs/base/common/paths';
import { KeyboardLayoutMonitor } from "vs/code/electron-main/keyboard";
import URI from 'vs/base/common/uri';
import { WorkspacesChannel } from "vs/platform/workspaces/common/workspacesIpc";
import { IWorkspacesMainService } from "vs/platform/workspaces/common/workspaces";
import { IWorkspacesMainService, IWorkspaceIdentifier } from "vs/platform/workspaces/common/workspaces";
import { findWindowOnWorkspaceOrFolder } from "vs/code/node/windowsFinder";

export class CodeApplication {
private toDispose: IDisposable[];
Expand Down Expand Up @@ -205,24 +204,24 @@ export class CodeApplication {
});
});

ipc.on('vscode:broadcast', (event, windowId: number, target: string, broadcast: { channel: string; payload: any; }) => {
ipc.on('vscode:broadcast', (event, windowId: number, target: IWorkspaceIdentifier | string, broadcast: { channel: string; payload: any; }) => {
if (this.windowsMainService && broadcast.channel && !isUndefinedOrNull(broadcast.payload)) {
this.logService.log('IPC#vscode:broadcast', target, broadcast.channel, broadcast.payload);

// Handle specific events on main side
this.onBroadcast(broadcast.channel, broadcast.payload);

// Send to windows
// Send to specific window if target is provided
if (target) {
const otherWindowsWithTarget = this.windowsMainService.getWindows().filter(w => w.id !== windowId && typeof w.openedFolderPath === 'string');
const directTargetMatch = otherWindowsWithTarget.filter(w => isEqual(target, w.openedFolderPath, !platform.isLinux /* ignorecase */));
const parentTargetMatch = otherWindowsWithTarget.filter(w => isParent(target, w.openedFolderPath, !platform.isLinux /* ignorecase */));

const targetWindow = directTargetMatch.length ? directTargetMatch[0] : parentTargetMatch[0]; // prefer direct match over parent match
const otherWindowsWithTarget = this.windowsMainService.getWindows().filter(w => w.id !== windowId && (w.openedWorkspace || w.openedFolderPath));
const targetWindow = findWindowOnWorkspaceOrFolder(otherWindowsWithTarget, target);
if (targetWindow) {
targetWindow.send('vscode:broadcast', broadcast);
}
} else {
}

// Otherwise send to all windows
else {
this.windowsMainService.sendToAll('vscode:broadcast', broadcast, [windowId]);
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/vs/code/node/windowsFinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as platform from 'vs/base/common/platform';
import * as paths from 'vs/base/common/paths';
import { OpenContext } from 'vs/platform/windows/common/windows';
import { IWorkspaceIdentifier } from "vs/platform/workspaces/common/workspaces";
import { isParent } from "vs/platform/files/common/files";

export interface ISimpleWindow {
openedWorkspace?: IWorkspaceIdentifier;
Expand Down Expand Up @@ -165,4 +166,24 @@ export function findExtensionDevelopmentWindow<W extends ISimpleWindow>(windows:
}

return null;
}

export function findWindowOnWorkspaceOrFolder<W extends ISimpleWindow>(windows: W[], target: IWorkspaceIdentifier | string): W {
const directTargetMatch = windows.filter(w => {
if (typeof target === 'string') {
return paths.isEqual(target, w.openedFolderPath, !platform.isLinux /* ignorecase */);
}

return w.openedWorkspace && w.openedWorkspace.id === target.id;
});

const parentTargetMatch = windows.filter(w => {
if (typeof target === 'string') {
return isParent(target, w.openedFolderPath, !platform.isLinux /* ignorecase */);
}

return false; // not supported for workspace target
});

return directTargetMatch.length ? directTargetMatch[0] : parentTargetMatch[0]; // prefer direct match over parent match
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createDecorator } from 'vs/platform/instantiation/common/instantiation'
import Event, { Emitter } from 'vs/base/common/event';

import { ipcRenderer as ipc } from 'electron';
import { IWorkspaceIdentifier } from "vs/platform/workspaces/common/workspaces";

export const IBroadcastService = createDecorator<IBroadcastService>('broadcastService');

Expand All @@ -20,7 +21,7 @@ export interface IBroadcast {
export interface IBroadcastService {
_serviceBrand: any;

broadcast(b: IBroadcast, target?: string): void;
broadcast(b: IBroadcast, target?: IWorkspaceIdentifier | string): void;

onBroadcast: Event<IBroadcast>;
}
Expand All @@ -46,7 +47,7 @@ export class BroadcastService implements IBroadcastService {
return this._onBroadcast.event;
}

public broadcast(b: IBroadcast, target?: string): void {
public broadcast(b: IBroadcast, target?: IWorkspaceIdentifier | string): void {
ipc.send('vscode:broadcast', this.windowId, target, {
channel: b.channel,
payload: b.payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class FileWatcher {
return;
}

// Emit through broadcast service
// Emit through event emitter
if (events.length > 0) {
this.onFileChanges(toFileChangesEvent(events));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class FileWatcher {
return;
}

// Emit through broadcast service
// Emit through event emitter
if (events.length > 0) {
this.onFileChanges(toFileChangesEvent(events));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class FileWatcher {
return;
}

// Emit through broadcast service
// Emit through event emitter
if (events.length > 0) {
this.onFileChanges(toFileChangesEvent(events));
}
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/services/files/test/node/watcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TestFileWatcher {
// Normalize
let normalizedEvents = normalize(events);

// Emit through broadcast service
// Emit through event emitter
if (normalizedEvents.length > 0) {
this._onFileChanges.fire(toFileChangesEvent(normalizedEvents));
}
Expand Down

0 comments on commit a5f3583

Please sign in to comment.