/*
 * Copyright 2020 The Chromium Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
package io.flutter.jxbrowser;

import com.intellij.openapi.wm.ToolWindow;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentManager;
import com.teamdev.jxbrowser.browser.Browser;
import com.teamdev.jxbrowser.engine.Engine;
import com.teamdev.jxbrowser.engine.EngineOptions;
import com.teamdev.jxbrowser.view.swing.BrowserView;
import icons.FlutterIcons;

import java.awt.*;

import static com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED;

public class EmbeddedBrowser {
  public void openPanel(ContentManager contentManager, String tabName, String url) {
    EngineOptions options =
      EngineOptions.newBuilder(HARDWARE_ACCELERATED).build();
    Engine engine = Engine.newInstance(options);
    Browser browser = engine.newBrowser();

    if (contentManager.isDisposed()) {
      return;
    }
    contentManager.removeAllContents(false);
    final Content content = contentManager.getFactory().createContent(null, tabName, false);

    // Creating Swing component for rendering web content
    // loaded in the given Browser instance.
    BrowserView view = BrowserView.newInstance(browser);
    view.setPreferredSize(new Dimension(contentManager.getComponent().getWidth(), contentManager.getComponent().getHeight()));

    content.setComponent(view);
    content.putUserData(ToolWindow.SHOW_CONTENT_ICON, Boolean.TRUE);
    // TODO(helin24): Use differentiated icons for each tab and copy from devtools toolbar.
    content.setIcon(FlutterIcons.Phone);
    contentManager.addContent(content);

    browser.navigation().loadUrl(url);
  }
}

@@ -196,16 +196,76 @@
 owser;%0A%0A
+import com.intellij.openapi.application.ApplicationManager;%0A
 import c
@@ -527,16 +527,76 @@
 ptions;%0A
+import com.teamdev.jxbrowser.navigation.event.LoadFinished;%0A
 import c
@@ -686,16 +686,66 @@
 a.awt.*;
+%0Aimport java.util.concurrent.atomic.AtomicBoolean;
 %0A%0Aimport
@@ -934,24 +934,30 @@
  url) %7B%0A    
+final 
 EngineOption
@@ -1030,24 +1030,30 @@
 uild();%0A    
+final 
 Engine engin
@@ -1085,24 +1085,30 @@
 tions);%0A    
+final 
 Browser brow
@@ -1134,17 +1134,476 @@
 wser();%0A
-%0A
+    browser.settings().enableTransparentBackground();%0A%0A    // Multiple LoadFinished events can occur, but we only need to add content the first time.%0A    final AtomicBoolean contentLoaded = new AtomicBoolean(false);%0A%0A    browser.navigation().loadUrl(url);%0A    browser.navigation().on(LoadFinished.class, event -%3E %7B%0A      if (!contentLoaded.compareAndSet(false, true)) %7B%0A        return;%0A      %7D%0A      ApplicationManager.getApplication().invokeLater(() -%3E %7B%0A    
     if (
@@ -1635,24 +1635,28 @@
 ()) %7B%0A      
+   
+ 
 return;%0A    
@@ -1655,22 +1655,31 @@
 rn;%0A    
-%7D%0A
+    %7D%0A%0A   
     
+ 
 contentM
@@ -1707,24 +1707,28 @@
 nts(false);%0A
+    
     final Co
@@ -1805,24 +1805,28 @@
 e, false);%0A%0A
+    
     // Creat
@@ -1871,16 +1871,20 @@
 content%0A
+    
     // l
@@ -1924,16 +1924,26 @@
 ce.%0A    
+    final 
 BrowserV
@@ -1987,16 +1987,20 @@
 owser);%0A
+    
     view
@@ -2115,24 +2115,28 @@
 eight()));%0A%0A
+    
     content.
@@ -2155,16 +2155,20 @@
 (view);%0A
+    
     cont
@@ -2232,16 +2232,20 @@
 E);%0A    
+ 
+   
 // TODO(
@@ -2324,16 +2324,20 @@
 oolbar.%0A
+    
     cont
@@ -2369,24 +2369,28 @@
 Phone);%0A    
+  
+  
 contentManag
@@ -2417,45 +2417,23 @@
 t);%0A
-%0A    browser.navigation().loadUrl(url
+      %7D);%0A    %7D
 );%0A 

Fix white screen during embedded browser load (#4787)