/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ 'use strict'; import { TPromise } from 'vs/base/common/winjs.base'; import { Event, buffer } from 'vs/base/common/event'; import { IChannel, eventToCall, eventFromCall } from 'vs/base/parts/ipc/common/ipc'; import { IWindowsService, INativeOpenDialogOptions, IEnterWorkspaceResult, CrashReporterStartOptions, IMessageBoxResult, MessageBoxOptions, SaveDialogOptions, OpenDialogOptions, IDevToolsOptions } from 'vs/platform/windows/common/windows'; import { IWorkspaceIdentifier, ISingleFolderWorkspaceIdentifier, IWorkspaceFolderCreationData } from 'vs/platform/workspaces/common/workspaces'; import { IRecentlyOpened } from 'vs/platform/history/common/history'; import { ISerializableCommandAction } from 'vs/platform/actions/common/actions'; import URI from 'vs/base/common/uri'; import { ParsedArgs } from 'vs/platform/environment/common/environment'; export interface IWindowsChannel extends IChannel { call(command: 'event:onWindowOpen'): TPromise; call(command: 'event:onWindowFocus'): TPromise; call(command: 'event:onWindowBlur'): TPromise; call(command: 'event:onRecentlyOpenedChange'): TPromise; call(command: 'pickFileFolderAndOpen', arg: INativeOpenDialogOptions): TPromise; call(command: 'pickFileAndOpen', arg: INativeOpenDialogOptions): TPromise; call(command: 'pickFolderAndOpen', arg: INativeOpenDialogOptions): TPromise; call(command: 'pickWorkspaceAndOpen', arg: INativeOpenDialogOptions): TPromise; call(command: 'showMessageBox', arg: [number, MessageBoxOptions]): TPromise; call(command: 'showSaveDialog', arg: [number, SaveDialogOptions]): TPromise; call(command: 'showOpenDialog', arg: [number, OpenDialogOptions]): TPromise; call(command: 'reloadWindow', arg: [number, ParsedArgs]): TPromise; call(command: 'toggleDevTools', arg: number): TPromise; call(command: 'closeWorkspace', arg: number): TPromise; call(command: 'createAndEnterWorkspace', arg: [number, IWorkspaceFolderCreationData[], string]): TPromise; call(command: 'saveAndEnterWorkspace', arg: [number, string]): TPromise; call(command: 'toggleFullScreen', arg: number): TPromise; call(command: 'setRepresentedFilename', arg: [number, string]): TPromise; call(command: 'addRecentlyOpened', arg: string[]): TPromise; call(command: 'removeFromRecentlyOpened', arg: (IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier)[]): TPromise; call(command: 'clearRecentlyOpened'): TPromise; call(command: 'getRecentlyOpened', arg: number): TPromise; call(command: 'showPreviousWindowTab', arg: number): TPromise; call(command: 'showNextWindowTab', arg: number): TPromise; call(command: 'moveWindowTabToNewWindow', arg: number): TPromise; call(command: 'mergeAllWindowTabs', arg: number): TPromise; call(command: 'toggleWindowTabsBar', arg: number): TPromise; call(command: 'updateTouchBar', arg: [number, ISerializableCommandAction[][]]): TPromise; call(command: 'focusWindow', arg: number): TPromise; call(command: 'closeWindow', arg: number): TPromise; call(command: 'isFocused', arg: number): TPromise; call(command: 'isMaximized', arg: number): TPromise; call(command: 'maximizeWindow', arg: number): TPromise; call(command: 'unmaximizeWindow', arg: number): TPromise; call(command: 'minimizeWindow', arg: number): TPromise; call(command: 'onWindowTitleDoubleClick', arg: number): TPromise; call(command: 'setDocumentEdited', arg: [number, boolean]): TPromise; call(command: 'quit'): TPromise; call(command: 'openWindow', arg: [number, string[], { forceNewWindow?: boolean, forceReuseWindow?: boolean, forceOpenWorkspaceAsFile?: boolean }]): TPromise; call(command: 'openNewWindow'): TPromise; call(command: 'showWindow', arg: number): TPromise; call(command: 'getWindows'): TPromise<{ id: number; workspace?: IWorkspaceIdentifier; folderPath?: string; title: string; filename?: string; }[]>; call(command: 'getWindowCount'): TPromise; call(command: 'relaunch', arg: { addArgs?: string[], removeArgs?: string[] }): TPromise; call(command: 'whenSharedProcessReady'): TPromise; call(command: 'toggleSharedProcess'): TPromise; call(command: 'log', arg: [string, string[]]): TPromise; call(command: 'showItemInFolder', arg: string): TPromise; call(command: 'openExternal', arg: string): TPromise; call(command: 'startCrashReporter', arg: CrashReporterStartOptions): TPromise; call(command: 'openAccessibilityOptions'): TPromise; call(command: 'openAboutDialog'): TPromise; call(command: string, arg?: any): TPromise; } export class WindowsChannel implements IWindowsChannel { private onWindowOpen: Event; private onWindowFocus: Event; private onWindowBlur: Event; private onWindowMaximize: Event; private onWindowUnmaximize: Event; private onRecentlyOpenedChange: Event; constructor(private service: IWindowsService) { this.onWindowOpen = buffer(service.onWindowOpen, true); this.onWindowFocus = buffer(service.onWindowFocus, true); this.onWindowBlur = buffer(service.onWindowBlur, true); this.onWindowMaximize = buffer(service.onWindowMaximize, true); this.onWindowUnmaximize = buffer(service.onWindowUnmaximize, true); this.onRecentlyOpenedChange = buffer(service.onRecentlyOpenedChange, true); } call(command: string, arg?: any): TPromise { switch (command) { case 'event:onWindowOpen': return eventToCall(this.onWindowOpen); case 'event:onWindowFocus': return eventToCall(this.onWindowFocus); case 'event:onWindowBlur': return eventToCall(this.onWindowBlur); case 'event:onWindowMaximize': return eventToCall(this.onWindowMaximize); case 'event:onWindowUnmaximize': return eventToCall(this.onWindowUnmaximize); case 'event:onRecentlyOpenedChange': return eventToCall(this.onRecentlyOpenedChange); case 'pickFileFolderAndOpen': return this.service.pickFileFolderAndOpen(arg); case 'pickFileAndOpen': return this.service.pickFileAndOpen(arg); case 'pickFolderAndOpen': return this.service.pickFolderAndOpen(arg); case 'pickWorkspaceAndOpen': return this.service.pickWorkspaceAndOpen(arg); case 'showMessageBox': return this.service.showMessageBox(arg[0], arg[1]); case 'showSaveDialog': return this.service.showSaveDialog(arg[0], arg[1]); case 'showOpenDialog': return this.service.showOpenDialog(arg[0], arg[1]); case 'reloadWindow': return this.service.reloadWindow(arg[0], arg[1]); case 'openDevTools': return this.service.openDevTools(arg[0], arg[1]); case 'toggleDevTools': return this.service.toggleDevTools(arg); case 'closeWorkspace': return this.service.closeWorkspace(arg); case 'createAndEnterWorkspace': { const rawFolders: IWorkspaceFolderCreationData[] = arg[1]; let folders: IWorkspaceFolderCreationData[]; if (Array.isArray(rawFolders)) { folders = rawFolders.map(rawFolder => { return { uri: URI.revive(rawFolder.uri), // convert raw URI back to real URI name: rawFolder.name } as IWorkspaceFolderCreationData; }); } return this.service.createAndEnterWorkspace(arg[0], folders, arg[2]); } case 'saveAndEnterWorkspace': return this.service.saveAndEnterWorkspace(arg[0], arg[1]); case 'toggleFullScreen': return this.service.toggleFullScreen(arg); case 'setRepresentedFilename': return this.service.setRepresentedFilename(arg[0], arg[1]); case 'addRecentlyOpened': return this.service.addRecentlyOpened(arg); case 'removeFromRecentlyOpened': return this.service.removeFromRecentlyOpened(arg); case 'clearRecentlyOpened': return this.service.clearRecentlyOpened(); case 'showPreviousWindowTab': return this.service.showPreviousWindowTab(); case 'showNextWindowTab': return this.service.showNextWindowTab(); case 'moveWindowTabToNewWindow': return this.service.moveWindowTabToNewWindow(); case 'mergeAllWindowTabs': return this.service.mergeAllWindowTabs(); case 'toggleWindowTabsBar': return this.service.toggleWindowTabsBar(); case 'updateTouchBar': return this.service.updateTouchBar(arg[0], arg[1]); case 'getRecentlyOpened': return this.service.getRecentlyOpened(arg); case 'focusWindow': return this.service.focusWindow(arg); case 'closeWindow': return this.service.closeWindow(arg); case 'isFocused': return this.service.isFocused(arg); case 'isMaximized': return this.service.isMaximized(arg); case 'maximizeWindow': return this.service.maximizeWindow(arg); case 'unmaximizeWindow': return this.service.unmaximizeWindow(arg); case 'minimizeWindow': return this.service.minimizeWindow(arg); case 'onWindowTitleDoubleClick': return this.service.onWindowTitleDoubleClick(arg); case 'setDocumentEdited': return this.service.setDocumentEdited(arg[0], arg[1]); case 'openWindow': return this.service.openWindow(arg[0], arg[1], arg[2]); case 'openNewWindow': return this.service.openNewWindow(); case 'showWindow': return this.service.showWindow(arg); case 'getWindows': return this.service.getWindows(); case 'getWindowCount': return this.service.getWindowCount(); case 'relaunch': return this.service.relaunch(arg[0]); case 'whenSharedProcessReady': return this.service.whenSharedProcessReady(); case 'toggleSharedProcess': return this.service.toggleSharedProcess(); case 'quit': return this.service.quit(); case 'log': return this.service.log(arg[0], arg[1]); case 'showItemInFolder': return this.service.showItemInFolder(arg); case 'openExternal': return this.service.openExternal(arg); case 'startCrashReporter': return this.service.startCrashReporter(arg); case 'openAccessibilityOptions': return this.service.openAccessibilityOptions(); case 'openAboutDialog': return this.service.openAboutDialog(); } return undefined; } } export class WindowsChannelClient implements IWindowsService { _serviceBrand: any; constructor(private channel: IWindowsChannel) { } private _onWindowOpen: Event = eventFromCall(this.channel, 'event:onWindowOpen'); get onWindowOpen(): Event { return this._onWindowOpen; } private _onWindowFocus: Event = eventFromCall(this.channel, 'event:onWindowFocus'); get onWindowFocus(): Event { return this._onWindowFocus; } private _onWindowBlur: Event = eventFromCall(this.channel, 'event:onWindowBlur'); get onWindowBlur(): Event { return this._onWindowBlur; } private _onWindowMaximize: Event = eventFromCall(this.channel, 'event:onWindowMaximize'); get onWindowMaximize(): Event { return this._onWindowMaximize; } private _onWindowUnmaximize: Event = eventFromCall(this.channel, 'event:onWindowUnmaximize'); get onWindowUnmaximize(): Event { return this._onWindowUnmaximize; } private _onRecentlyOpenedChange: Event = eventFromCall(this.channel, 'event:onRecentlyOpenedChange'); get onRecentlyOpenedChange(): Event { return this._onRecentlyOpenedChange; } pickFileFolderAndOpen(options: INativeOpenDialogOptions): TPromise { return this.channel.call('pickFileFolderAndOpen', options); } pickFileAndOpen(options: INativeOpenDialogOptions): TPromise { return this.channel.call('pickFileAndOpen', options); } pickFolderAndOpen(options: INativeOpenDialogOptions): TPromise { return this.channel.call('pickFolderAndOpen', options); } pickWorkspaceAndOpen(options: INativeOpenDialogOptions): TPromise { return this.channel.call('pickWorkspaceAndOpen', options); } showMessageBox(windowId: number, options: MessageBoxOptions): TPromise { return this.channel.call('showMessageBox', [windowId, options]); } showSaveDialog(windowId: number, options: SaveDialogOptions): TPromise { return this.channel.call('showSaveDialog', [windowId, options]); } showOpenDialog(windowId: number, options: OpenDialogOptions): TPromise { return this.channel.call('showOpenDialog', [windowId, options]); } reloadWindow(windowId: number, args?: ParsedArgs): TPromise { return this.channel.call('reloadWindow', [windowId, args]); } openDevTools(windowId: number, options?: IDevToolsOptions): TPromise { return this.channel.call('openDevTools', [windowId, options]); } toggleDevTools(windowId: number): TPromise { return this.channel.call('toggleDevTools', windowId); } closeWorkspace(windowId: number): TPromise { return this.channel.call('closeWorkspace', windowId); } createAndEnterWorkspace(windowId: number, folders?: IWorkspaceFolderCreationData[], path?: string): TPromise { return this.channel.call('createAndEnterWorkspace', [windowId, folders, path]); } saveAndEnterWorkspace(windowId: number, path: string): TPromise { return this.channel.call('saveAndEnterWorkspace', [windowId, path]); } toggleFullScreen(windowId: number): TPromise { return this.channel.call('toggleFullScreen', windowId); } setRepresentedFilename(windowId: number, fileName: string): TPromise { return this.channel.call('setRepresentedFilename', [windowId, fileName]); } addRecentlyOpened(files: string[]): TPromise { return this.channel.call('addRecentlyOpened', files); } removeFromRecentlyOpened(paths: string[]): TPromise { return this.channel.call('removeFromRecentlyOpened', paths); } clearRecentlyOpened(): TPromise { return this.channel.call('clearRecentlyOpened'); } getRecentlyOpened(windowId: number): TPromise { return this.channel.call('getRecentlyOpened', windowId); } showPreviousWindowTab(): TPromise { return this.channel.call('showPreviousWindowTab'); } showNextWindowTab(): TPromise { return this.channel.call('showNextWindowTab'); } moveWindowTabToNewWindow(): TPromise { return this.channel.call('moveWindowTabToNewWindow'); } mergeAllWindowTabs(): TPromise { return this.channel.call('mergeAllWindowTabs'); } toggleWindowTabsBar(): TPromise { return this.channel.call('toggleWindowTabsBar'); } focusWindow(windowId: number): TPromise { return this.channel.call('focusWindow', windowId); } closeWindow(windowId: number): TPromise { return this.channel.call('closeWindow', windowId); } isFocused(windowId: number): TPromise { return this.channel.call('isFocused', windowId); } isMaximized(windowId: number): TPromise { return this.channel.call('isMaximized', windowId); } maximizeWindow(windowId: number): TPromise { return this.channel.call('maximizeWindow', windowId); } unmaximizeWindow(windowId: number): TPromise { return this.channel.call('unmaximizeWindow', windowId); } minimizeWindow(windowId: number): TPromise { return this.channel.call('minimizeWindow', windowId); } onWindowTitleDoubleClick(windowId: number): TPromise { return this.channel.call('onWindowTitleDoubleClick', windowId); } setDocumentEdited(windowId: number, flag: boolean): TPromise { return this.channel.call('setDocumentEdited', [windowId, flag]); } quit(): TPromise { return this.channel.call('quit'); } relaunch(options: { addArgs?: string[], removeArgs?: string[] }): TPromise { return this.channel.call('relaunch', [options]); } whenSharedProcessReady(): TPromise { return this.channel.call('whenSharedProcessReady'); } toggleSharedProcess(): TPromise { return this.channel.call('toggleSharedProcess'); } openWindow(windowId: number, paths: string[], options?: { forceNewWindow?: boolean, forceReuseWindow?: boolean, forceOpenWorkspaceAsFile?: boolean }): TPromise { return this.channel.call('openWindow', [windowId, paths, options]); } openNewWindow(): TPromise { return this.channel.call('openNewWindow'); } showWindow(windowId: number): TPromise { return this.channel.call('showWindow', windowId); } getWindows(): TPromise<{ id: number; workspace?: IWorkspaceIdentifier; folderPath?: string; title: string; filename?: string; }[]> { return this.channel.call('getWindows'); } getWindowCount(): TPromise { return this.channel.call('getWindowCount'); } log(severity: string, ...messages: string[]): TPromise { return this.channel.call('log', [severity, messages]); } showItemInFolder(path: string): TPromise { return this.channel.call('showItemInFolder', path); } openExternal(url: string): TPromise { return this.channel.call('openExternal', url); } startCrashReporter(config: CrashReporterStartOptions): TPromise { return this.channel.call('startCrashReporter', config); } updateTouchBar(windowId: number, items: ISerializableCommandAction[][]): TPromise { return this.channel.call('updateTouchBar', [windowId, items]); } openAccessibilityOptions(): TPromise { return this.channel.call('openAccessibilityOptions'); } openAboutDialog(): TPromise { return this.channel.call('openAboutDialog'); } }