Skip to content

Commit

Permalink
fix #6039: implement vscode.env.openExternal API
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
  • Loading branch information
akosyakov committed Sep 2, 2019
1 parent 595d784 commit e0654a6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
7 changes: 6 additions & 1 deletion packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,10 @@ export enum TreeViewItemCollapsibleState {
Expanded = 2
}

export interface WindowMain {
$openUri(uri: UriComponents): Promise<boolean>;
}

export interface WindowStateExt {
$onWindowStateChanged(focus: boolean): void;
}
Expand Down Expand Up @@ -1269,7 +1273,8 @@ export const PLUGIN_RPC_CONTEXT = {
DEBUG_MAIN: createProxyIdentifier<DebugMain>('DebugMain'),
FILE_SYSTEM_MAIN: createProxyIdentifier<FileSystemMain>('FileSystemMain'),
SCM_MAIN: createProxyIdentifier<ScmMain>('ScmMain'),
DECORATIONS_MAIN: createProxyIdentifier<DecorationsMain>('DecorationsMain')
DECORATIONS_MAIN: createProxyIdentifier<DecorationsMain>('DecorationsMain'),
WINDOW_MAIN: createProxyIdentifier<WindowMain>('WindowMain')
};

export const MAIN_RPC_CONTEXT = {
Expand Down
4 changes: 3 additions & 1 deletion packages/plugin-ext/src/main/browser/main-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export function setUpPluginApi(rpc: RPCProtocol, container: interfaces.Container
rpc.set(PLUGIN_RPC_CONTEXT.PREFERENCE_REGISTRY_MAIN, preferenceRegistryMain);

/* tslint:disable */
new WindowStateMain(rpc);
new EditorsAndDocumentsMain(rpc, container);
/* tslint:enable */

Expand Down Expand Up @@ -111,4 +110,7 @@ export function setUpPluginApi(rpc: RPCProtocol, container: interfaces.Container

const decorationsMain = new DecorationsMainImpl(rpc, container);
rpc.set(PLUGIN_RPC_CONTEXT.DECORATIONS_MAIN, decorationsMain);

const windowMain = new WindowStateMain(rpc, container);
rpc.set(PLUGIN_RPC_CONTEXT.WINDOW_MAIN, windowMain);
}
26 changes: 22 additions & 4 deletions packages/plugin-ext/src/main/browser/window-state-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,22 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { WindowStateExt, MAIN_RPC_CONTEXT } from '../../common/plugin-api-rpc';
import URI from 'vscode-uri';
import { interfaces } from 'inversify';
import { WindowStateExt, MAIN_RPC_CONTEXT, WindowMain } from '../../common/plugin-api-rpc';
import { RPCProtocol } from '../../common/rpc-protocol';
import { UriComponents } from '../../common/uri-components';
import { WindowService } from '@theia/core/lib/browser/window/window-service';

export class WindowStateMain {
export class WindowStateMain implements WindowMain {

private proxy: WindowStateExt;
private readonly proxy: WindowStateExt;

constructor(rpc: RPCProtocol) {
private readonly windowService: WindowService;

constructor(rpc: RPCProtocol, container: interfaces.Container) {
this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.WINDOW_STATE_EXT);
this.windowService = container.get(WindowService);

window.addEventListener('focus', () => this.onFocusChanged(true));
window.addEventListener('blur', () => this.onFocusChanged(false));
Expand All @@ -32,4 +39,15 @@ export class WindowStateMain {
this.proxy.$onWindowStateChanged(focused);
}

async $openUri(uriComponent: UriComponents): Promise<boolean> {
const uri = URI.revive(uriComponent);
const url = encodeURI(uri.toString(true));
try {
this.windowService.openNewWindow(url, { external: true });
return true;
} catch (e) {
return false;
}
}

}
6 changes: 4 additions & 2 deletions packages/plugin-ext/src/plugin/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export function createAPIFactory(
const commandRegistry = rpc.set(MAIN_RPC_CONTEXT.COMMAND_REGISTRY_EXT, new CommandRegistryImpl(rpc));
const quickOpenExt = rpc.set(MAIN_RPC_CONTEXT.QUICK_OPEN_EXT, new QuickOpenExtImpl(rpc));
const dialogsExt = new DialogsExtImpl(rpc);
const windowStateExt = rpc.set(MAIN_RPC_CONTEXT.WINDOW_STATE_EXT, new WindowStateExtImpl());
const windowStateExt = rpc.set(MAIN_RPC_CONTEXT.WINDOW_STATE_EXT, new WindowStateExtImpl(rpc));
const notificationExt = rpc.set(MAIN_RPC_CONTEXT.NOTIFICATION_EXT, new NotificationExtImpl(rpc));
const editors = rpc.set(MAIN_RPC_CONTEXT.TEXT_EDITORS_EXT, new TextEditorsExtImpl(rpc, editorsAndDocumentsExt));
const documents = rpc.set(MAIN_RPC_CONTEXT.DOCUMENTS_EXT, new DocumentsExtImpl(rpc, editorsAndDocumentsExt));
Expand Down Expand Up @@ -501,8 +501,10 @@ export function createAPIFactory(
},
getClientOperatingSystem(): PromiseLike<theia.OperatingSystem> {
return envExt.getClientOperatingSystem();
},
openExternal(uri: theia.Uri): PromiseLike<boolean> {
return windowStateExt.openUri(uri);
}

});

const languageServer: typeof theia.languageServer = {
Expand Down
13 changes: 11 additions & 2 deletions packages/plugin-ext/src/plugin/window-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import URI from 'vscode-uri';
import { WindowState } from '@theia/plugin';
import { WindowStateExt } from '../common/plugin-api-rpc';
import { WindowStateExt, WindowMain, PLUGIN_RPC_CONTEXT } from '../common/plugin-api-rpc';
import { Event, Emitter } from '@theia/core/lib/common/event';
import { RPCProtocol } from '../common/rpc-protocol';

export class WindowStateExtImpl implements WindowStateExt {

Expand All @@ -25,7 +27,10 @@ export class WindowStateExtImpl implements WindowStateExt {
private windowStateChangedEmitter = new Emitter<WindowState>();
public readonly onDidChangeWindowState: Event<WindowState> = this.windowStateChangedEmitter.event;

constructor() {
private readonly proxy: WindowMain;

constructor(rpc: RPCProtocol) {
this.proxy = rpc.getProxy(PLUGIN_RPC_CONTEXT.WINDOW_MAIN);
this.windowStateCached = { focused: true }; // supposed tab is active on start
}

Expand All @@ -43,4 +48,8 @@ export class WindowStateExtImpl implements WindowStateExt {
this.windowStateChangedEmitter.fire(state);
}

openUri(uri: URI): Promise<boolean> {
return this.proxy.$openUri(uri);
}

}
12 changes: 12 additions & 0 deletions packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4744,6 +4744,18 @@ declare module '@theia/plugin' {
*/
export const sessionId: string;

/**
* Opens an *external* item, e.g. a http(s) or mailto-link, using the
* default application.
*
* *Note* that [`showTextDocument`](#window.showTextDocument) is the right
* way to open a text document inside the editor, not this function.
*
* @param target The uri that should be opened.
* @returns A promise indicating if open was successful.
*/
export function openExternal(target: Uri): PromiseLike<boolean>;

}

/**
Expand Down

0 comments on commit e0654a6

Please sign in to comment.