From 24d7882f0b879847461f424982fff7c0a9f83381 Mon Sep 17 00:00:00 2001 From: Vincent Fugnitto Date: Thu, 23 Feb 2023 12:32:37 -0500 Subject: [PATCH] webview: add support for `activeWebviewPanelId` (#12182) The commit adds support for the `activeWebviewPanelId` context when-clause. Signed-off-by: vince-fugnitto Co-authored-by: Paul Marechal --- .../browser/plugin-ext-frontend-module.ts | 3 ++ .../browser/webview/webview-context-keys.ts | 49 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 packages/plugin-ext/src/main/browser/webview/webview-context-keys.ts diff --git a/packages/plugin-ext/src/main/browser/plugin-ext-frontend-module.ts b/packages/plugin-ext/src/main/browser/plugin-ext-frontend-module.ts index 2d72b2c6bd39c..411fdd04e8f91 100644 --- a/packages/plugin-ext/src/main/browser/plugin-ext-frontend-module.ts +++ b/packages/plugin-ext/src/main/browser/plugin-ext-frontend-module.ts @@ -82,6 +82,7 @@ import { PluginMenuCommandAdapter } from './menus/plugin-menu-command-adapter'; import './theme-icon-override'; import { PluginTerminalRegistry } from './plugin-terminal-registry'; import { DnDFileContentStore } from './view/dnd-file-content-store'; +import { WebviewContextKeys } from './webview/webview-context-keys'; export default new ContainerModule((bind, unbind, isBound, rebind) => { @@ -177,6 +178,8 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => { bind(WebviewWidget).toSelf(); bind(WebviewWidgetFactory).toDynamicValue(ctx => new WebviewWidgetFactory(ctx.container)).inSingletonScope(); bind(WidgetFactory).toService(WebviewWidgetFactory); + bind(WebviewContextKeys).toSelf().inSingletonScope(); + bind(FrontendApplicationContribution).toService(WebviewContextKeys); bind(CustomEditorContribution).toSelf().inSingletonScope(); bind(CommandContribution).toService(CustomEditorContribution); diff --git a/packages/plugin-ext/src/main/browser/webview/webview-context-keys.ts b/packages/plugin-ext/src/main/browser/webview/webview-context-keys.ts new file mode 100644 index 0000000000000..36c2ebbd1d239 --- /dev/null +++ b/packages/plugin-ext/src/main/browser/webview/webview-context-keys.ts @@ -0,0 +1,49 @@ +// ***************************************************************************** +// Copyright (C) 2023 Ericsson and others. +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License v. 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0. +// +// This Source Code may also be made available under the following Secondary +// Licenses when the conditions for such availability set forth in the Eclipse +// Public License v. 2.0 are satisfied: GNU General Public License, version 2 +// with the GNU Classpath Exception which is available at +// https://www.gnu.org/software/classpath/license.html. +// +// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 +// ***************************************************************************** + +import { inject, injectable, postConstruct } from '@theia/core/shared/inversify'; +import { ContextKey, ContextKeyService } from '@theia/core/lib/browser/context-key-service'; +import { ApplicationShell, FocusTracker, Widget } from '@theia/core/lib/browser'; +import { WebviewWidget } from './webview'; + +@injectable() +export class WebviewContextKeys { + + /** + * Context key representing the `viewType` of the active `WebviewWidget`, if any. + */ + activeWebviewPanelId: ContextKey; + + @inject(ApplicationShell) + protected applicationShell: ApplicationShell; + + @inject(ContextKeyService) + protected contextKeyService: ContextKeyService; + + @postConstruct() + protected postConstruct(): void { + this.activeWebviewPanelId = this.contextKeyService.createKey('activeWebviewPanelId', ''); + this.applicationShell.onDidChangeCurrentWidget(this.handleDidChangeCurrentWidget, this); + } + + protected handleDidChangeCurrentWidget(change: FocusTracker.IChangedArgs): void { + if (change.newValue instanceof WebviewWidget) { + this.activeWebviewPanelId.set(change.newValue.viewType); + } else { + this.activeWebviewPanelId.set(''); + } + } +}