From cf2cdd3da4e86d55dfccdc6e1f038ff50612414a Mon Sep 17 00:00:00 2001 From: Philip Langer Date: Wed, 26 Apr 2023 12:13:12 +0200 Subject: [PATCH] Fix context menu for Edge in Windows (#77) * Add browser function that fires on context menu request * Add example for custom context menu in workflow diagram Fixes https://github.com/eclipse-glsp/glsp/issues/978 Fixes https://github.com/eclipse-glsp/glsp/issues/969 Contributed on behalf of STMicroelectronics --- .../plugin.xml | 9 +++- .../ui/BrowserContextMenuInstaller.java | 48 +++++++++++++++++++ .../glsp/ide/editor/ui/GLSPDiagramEditor.java | 4 +- 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 server/plugins/org.eclipse.glsp.ide.editor/src/org/eclipse/glsp/ide/editor/ui/BrowserContextMenuInstaller.java diff --git a/server/example/org.eclipse.glsp.ide.workflow.editor/plugin.xml b/server/example/org.eclipse.glsp.ide.workflow.editor/plugin.xml index ea41830..0356eba 100644 --- a/server/example/org.eclipse.glsp.ide.workflow.editor/plugin.xml +++ b/server/example/org.eclipse.glsp.ide.workflow.editor/plugin.xml @@ -26,7 +26,7 @@ allPopups="false" locationURI="popup:org.eclipse.glsp.workflow.editor"> + name="org.eclipse.glsp.integration.workflow.editor.edit_start" visible="true"> + + + + diff --git a/server/plugins/org.eclipse.glsp.ide.editor/src/org/eclipse/glsp/ide/editor/ui/BrowserContextMenuInstaller.java b/server/plugins/org.eclipse.glsp.ide.editor/src/org/eclipse/glsp/ide/editor/ui/BrowserContextMenuInstaller.java new file mode 100644 index 0000000..f6dc8e2 --- /dev/null +++ b/server/plugins/org.eclipse.glsp.ide.editor/src/org/eclipse/glsp/ide/editor/ui/BrowserContextMenuInstaller.java @@ -0,0 +1,48 @@ +/******************************************************************************** + * Copyright (c) 2023 EclipseSource 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 + * https://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 + ********************************************************************************/ +package org.eclipse.glsp.ide.editor.ui; + +import java.util.Optional; + +import org.eclipse.swt.browser.Browser; +import org.eclipse.swt.browser.BrowserFunction; + +public class BrowserContextMenuInstaller implements BrowserFunctionInstaller { + + private static final String FUNCTION_NAME = "requestContextMenu"; + private static final String FUNCTION_INSTALLER = "document.addEventListener(\"contextmenu\", e => { requestContextMenu(); e.preventDefault(); });"; + + @Override + public Optional install(final Browser browser) { + BrowserFunction browserFunction = new BrowserFunction(browser, FUNCTION_NAME) { + @Override + public Object function(final Object[] arguments) { + browser.getDisplay().asyncExec(() -> requestContextMenu(browser)); + return null; + } + }; + browser.execute(FUNCTION_INSTALLER); + return Optional.of(browserFunction); + } + + protected void requestContextMenu(final Browser browser) { + if (browser.isDisposed()) { + return; + } + browser.getMenu().setEnabled(true); + browser.getMenu().setVisible(true); + } +} diff --git a/server/plugins/org.eclipse.glsp.ide.editor/src/org/eclipse/glsp/ide/editor/ui/GLSPDiagramEditor.java b/server/plugins/org.eclipse.glsp.ide.editor/src/org/eclipse/glsp/ide/editor/ui/GLSPDiagramEditor.java index c140d1f..a844034 100644 --- a/server/plugins/org.eclipse.glsp.ide.editor/src/org/eclipse/glsp/ide/editor/ui/GLSPDiagramEditor.java +++ b/server/plugins/org.eclipse.glsp.ide.editor/src/org/eclipse/glsp/ide/editor/ui/GLSPDiagramEditor.java @@ -361,9 +361,8 @@ protected void syncMarkers(final GModelState modelState) { .ifPresent(toDispose::add); } - @SuppressWarnings("deprecation") protected Browser createBrowser(final Composite parent) { - Browser browser = new FocusAwareBrowser(parent, SWT.NO_SCROLL | SWT.EDGE | SWT.CHROMIUM); + Browser browser = new FocusAwareBrowser(parent, SWT.NO_SCROLL | SWT.EDGE); browser.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true)); toDispose.add(browser::dispose); return browser; @@ -405,6 +404,7 @@ protected void installBrowserFunctions() { // browser functions are automatically disposed with the browser new BrowserKeyBindingForwarderInstaller(getSite()).install(browser); new BrowserFocusControlInstaller().install(browser); + new BrowserContextMenuInstaller().install(browser); } protected String getBaseUrl() { return "diagram.html"; }