// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ui.jcef;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.util.Disposer;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author tav
 */
public class JCEFHtmlPanel extends JBCefBrowser {
  private static final JBCefClient ourCefClient = JBCefApp.getInstance().createClient();

  @NotNull
  private final String myUrl;

  static {
    Disposer.register(ApplicationManager.getApplication(), () -> {
      ourCefClient.getCefClient().dispose();
    });
  }

  public JCEFHtmlPanel(@Nullable String url) {
    this(ourCefClient, url);
  }

  public JCEFHtmlPanel(JBCefClient client, String url) {
    super(client, null); // should no pass url to ctor
    myUrl = ObjectUtils.notNull(url, "about:blank");
    if (client != ourCefClient) {
      Disposer.register(this, client);
    }
  }

  public void setHtml(@NotNull String html) {
    final String htmlToRender = prepareHtml(html);
    loadHTML(htmlToRender, myUrl);
  }

  @NotNull
  protected String prepareHtml(@NotNull String html) {
    return html;
  }
}

@@ -292,109 +292,596 @@
 lij.
-util.ObjectUtils;%0Aimport org.jetbrains.annotations.NotNull;%0Aimport org.jetbrains.annotations.Nullable
+openapi.wm.impl.IdeFrameImpl;%0Aimport com.intellij.util.ObjectUtils;%0Aimport org.cef.browser.CefBrowser;%0Aimport org.cef.browser.CefFrame;%0Aimport org.cef.callback.CefContextMenuParams;%0Aimport org.cef.callback.CefMenuModel;%0Aimport org.cef.handler.CefContextMenuHandler;%0Aimport org.cef.handler.CefContextMenuHandlerAdapter;%0Aimport org.jetbrains.annotations.NotNull;%0Aimport org.jetbrains.annotations.Nullable;%0A%0Aimport javax.swing.*;%0A%0Aimport java.awt.*;%0Aimport java.awt.event.WindowAdapter;%0Aimport java.awt.event.WindowEvent;%0A%0Aimport static org.cef.callback.CefMenuModel.MenuId.MENU_ID_USER_LAST
 ;%0A%0A/
@@ -1082,16 +1082,144 @@
 g myUrl;
+%0A  protected CefContextMenuHandler myDefaultContextMenuHandler;%0A  private static final int DEBUG_COMMAND_ID = MENU_ID_USER_LAST;
 %0A%0A  stat
@@ -1673,16 +1673,1650 @@
 ;%0A    %7D%0A
+    boolean isInternal = ApplicationManager.getApplication().isInternal();%0A    myDefaultContextMenuHandler = new CefContextMenuHandlerAdapter() %7B%0A      @Override%0A      public void onBeforeContextMenu(CefBrowser browser, CefFrame frame, CefContextMenuParams params, CefMenuModel model) %7B%0A        model.clear();%0A        if (isInternal) %7B%0A          model.addItem(DEBUG_COMMAND_ID, %22Open DevTools%22);%0A        %7D%0A      %7D%0A%0A      @Override%0A      public boolean onContextMenuCommand(CefBrowser browser, CefFrame frame, CefContextMenuParams params, int commandId, int eventFlags) %7B%0A        if (commandId == DEBUG_COMMAND_ID) %7B%0A          openDevtools(browser);%0A          return true;%0A        %7D%0A        return false;%0A      %7D%0A    %7D;%0A    client.addContextMenuHandler(myDefaultContextMenuHandler, this.getCefBrowser());%0A  %7D%0A%0A  private void openDevtools(CefBrowser browser) %7B%0A    Window activeFrame = IdeFrameImpl.getActiveFrame();%0A    if (activeFrame == null) return;%0A    Rectangle bounds = activeFrame.getGraphicsConfiguration().getBounds();%0A    JFrame frame = new IdeFrameImpl();%0A%0A    frame.setTitle(%22JCEF DevTools%22);%0A    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);%0A    frame.setBounds(bounds.width / 4, bounds.height / 4, bounds.width / 2, bounds.height / 2);%0A    frame.setLayout(new BorderLayout());%0A    JBCefBrowser devTools = new JBCefBrowser(browser.getDevTools());%0A    frame.add(devTools.getComponent(), BorderLayout.CENTER);%0A    frame.addWindowListener(new WindowAdapter() %7B%0A      @Override%0A      public void windowClosed(WindowEvent e) %7B%0A        Disposer.dispose(devTools);%0A      %7D%0A    %7D);%0A    frame.setVisible(true);%0A
   %7D%0A%0A  p

add default context menu for JCEFHtmlPanel