Skip to content

Commit

Permalink
[vscode] fix #4339: focus and reveal webviews properly
Browse files Browse the repository at this point in the history
  • Loading branch information
akosyakov committed Apr 5, 2019
1 parent 4de9282 commit 3493775
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
7 changes: 2 additions & 5 deletions packages/plugin-ext/src/main/browser/webview/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class WebviewWidget extends BaseWidget {

constructor(title: string, private options: WebviewWidgetOptions, private eventDelegate: WebviewEvents) {
super();
this.node.tabIndex = 0;
this.id = WebviewWidget.ID.nextId();
this.title.closable = true;
this.title.label = title;
Expand Down Expand Up @@ -132,17 +133,13 @@ export class WebviewWidget extends BaseWidget {
this.loadTimeout = undefined;
onLoad(e.target, newFrame.contentWindow);
}
});
}, { once: true });
newFrame.contentDocument!.write(newDocument!.documentElement!.innerHTML);
newFrame.contentDocument!.close();

this.updateSandboxAttribute(newFrame);
}

focus() {
this.iframe.contentWindow!.focus();
}

protected onActivateRequest(msg: Message): void {
super.onActivateRequest(msg);
this.node.focus();
Expand Down
17 changes: 15 additions & 2 deletions packages/plugin-ext/src/main/browser/webviews-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,20 @@ export class WebviewsMainImpl implements WebviewsMain {
this.onCloseView(viewId);
});
this.views.set(viewId, view);
this.shell.addWidget(view, { area: showOptions.area ? showOptions.area : 'main' });
this.shell.activateWidget(view.id);
const widgetOptions: ApplicationShell.WidgetOptions = { area: showOptions.area ? showOptions.area : 'main' };
// FIXME translate all view columns properly
if (showOptions.viewColumn === -2) {
const ref = this.shell.currentWidget;
if (ref && this.shell.getAreaFor(ref) === widgetOptions.area) {
Object.assign(widgetOptions, { ref, mode: 'open-to-right' });
}
}
this.shell.addWidget(view, widgetOptions);
if (showOptions.preserveFocus) {
this.shell.revealWidget(view.id);
} else {
this.shell.activateWidget(view.id);
}
}
$disposeWebview(handle: string): void {
const view = this.views.get(handle);
Expand All @@ -99,6 +111,7 @@ export class WebviewsMainImpl implements WebviewsMain {
if (webview.isDisposed) {
return;
}
// FIXME handle view column here too!
if (showOptions.preserveFocus) {
this.shell.revealWidget(webview.id);
} else {
Expand Down
22 changes: 19 additions & 3 deletions packages/plugin-ext/src/plugin/webviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,28 @@ export class WebviewPanelImpl implements theia.WebviewPanel {
this._visible = value;
}

reveal(area?: WebviewPanelTargetArea, viewColumn?: theia.ViewColumn, preserveFocus?: boolean): void {
reveal(arg0?: theia.ViewColumn | WebviewPanelTargetArea, arg1?: theia.ViewColumn | boolean, arg2?: boolean): void {
let area: WebviewPanelTargetArea | undefined = undefined;
let viewColumn: theia.ViewColumn | undefined = undefined;
let preserveFocus: boolean | undefined = undefined;
if (typeof arg0 === 'number') {
viewColumn = arg0;
} else {
area = arg0;
}
if (typeof arg1 === 'number') {
viewColumn = arg1;
} else {
preserveFocus = arg1;
}
if (typeof arg2 === 'boolean') {
preserveFocus = arg2;
}
this.checkIsDisposed();
this.proxy.$reveal(this.viewId, {
area: area,
area,
viewColumn: viewColumn ? fromViewColumn(viewColumn) : undefined,
preserveFocus: !!preserveFocus
preserveFocus
});
}

Expand Down
11 changes: 11 additions & 0 deletions packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2663,6 +2663,17 @@ declare module '@theia/plugin' {
*/
readonly onDidDispose: Event<void>;

/**
* Show the webview panel in a given column.
*
* A webview panel may only show in a single column at a time. If it is already showing, this
* method moves it to a new column.
*
* @param viewColumn View column to show the panel in. Shows in the current `viewColumn` if undefined.
* @param preserveFocus When `true`, the webview will not take focus.
*/
reveal(viewColumn?: ViewColumn, preserveFocus?: boolean): void;

/**
* Show the webview panel according to a given options.
*
Expand Down

0 comments on commit 3493775

Please sign in to comment.