From f8aef780b450c89f75f5a57ae5f821a67ac43c86 Mon Sep 17 00:00:00 2001 From: Or Geva Date: Mon, 17 Jul 2023 11:45:26 +0300 Subject: [PATCH] Webview - Add DevTools window --- .../ide/idea/ui/JfrogContextMenuHandler.java | 44 +++++++++++++++++++ .../ide/idea/ui/webview/WebviewManager.java | 2 + 2 files changed, 46 insertions(+) create mode 100644 src/main/java/com/jfrog/ide/idea/ui/JfrogContextMenuHandler.java diff --git a/src/main/java/com/jfrog/ide/idea/ui/JfrogContextMenuHandler.java b/src/main/java/com/jfrog/ide/idea/ui/JfrogContextMenuHandler.java new file mode 100644 index 00000000..c6a2b593 --- /dev/null +++ b/src/main/java/com/jfrog/ide/idea/ui/JfrogContextMenuHandler.java @@ -0,0 +1,44 @@ +package com.jfrog.ide.idea.ui; + +import org.cef.browser.CefBrowser; +import org.cef.browser.CefFrame; +import org.cef.callback.CefContextMenuParams; +import org.cef.callback.CefMenuModel; +import org.cef.handler.CefContextMenuHandler; + +import javax.swing.*; +import java.awt.*; + +public class JfrogContextMenuHandler implements CefContextMenuHandler { + private static final int DEV_TOOLS_ID = 1; + + @Override + public void onBeforeContextMenu(CefBrowser browser, CefFrame frame, CefContextMenuParams params, CefMenuModel model) { + model.clear(); + model.addItem(DEV_TOOLS_ID, "Inspect"); + } + + @Override + public boolean onContextMenuCommand(CefBrowser browser, CefFrame frame, CefContextMenuParams params, int commandId, int eventFlags) { + if (commandId == DEV_TOOLS_ID) { + openDevTools(browser); + return true; + } + return false; + } + + private void openDevTools(CefBrowser browser) { + SwingUtilities.invokeLater(() -> { + CefBrowser devToolsBrowser = browser.getDevTools(); + JFrame frame = new JFrame("DevTools - JFrog IDE Webview"); + frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); + frame.setSize(800, 600); + frame.getContentPane().add(devToolsBrowser.getUIComponent(), BorderLayout.CENTER); + frame.setVisible(true); + }); + } + + @Override + public void onContextMenuDismissed(CefBrowser browser, CefFrame frame) { + } +} \ No newline at end of file diff --git a/src/main/java/com/jfrog/ide/idea/ui/webview/WebviewManager.java b/src/main/java/com/jfrog/ide/idea/ui/webview/WebviewManager.java index aaa2a471..6257052e 100644 --- a/src/main/java/com/jfrog/ide/idea/ui/webview/WebviewManager.java +++ b/src/main/java/com/jfrog/ide/idea/ui/webview/WebviewManager.java @@ -4,6 +4,7 @@ import com.intellij.openapi.util.Disposer; import com.intellij.ui.jcef.JBCefBrowser; import com.jfrog.ide.idea.log.Logger; +import com.jfrog.ide.idea.ui.JfrogContextMenuHandler; import com.jfrog.ide.idea.ui.jcef.message.MessagePacker; import com.jfrog.ide.idea.ui.jcef.message.MessageType; import org.cef.CefApp; @@ -28,6 +29,7 @@ public CefBrowser createBrowser(Runnable onLoadEnd) { streamConsoleMessagesToLog(); handleLoadEvent(onLoadEnd); messagePacker = new MessagePacker(cefBrowser); + jbCefBrowser.getJBCefClient().addContextMenuHandler(new JfrogContextMenuHandler(), jbCefBrowser.getCefBrowser()); return cefBrowser; }