/*
 * 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.application.ApplicationListener;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.SystemInfo;
import com.teamdev.jxbrowser.engine.Engine;
import com.teamdev.jxbrowser.engine.EngineOptions;
import com.teamdev.jxbrowser.engine.PasswordStore;
import io.flutter.FlutterInitializer;

import java.io.File;
import java.nio.file.Paths;

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

public class EmbeddedBrowserEngine {
  private static final Logger LOG = Logger.getInstance(EmbeddedBrowserEngine.class);
  private final Engine engine;

  public static EmbeddedBrowserEngine getInstance() {
    return ServiceManager.getService(EmbeddedBrowserEngine.class);
  }

  public EmbeddedBrowserEngine() {
    final String dataPath = JxBrowserManager.DOWNLOAD_PATH + File.separatorChar + "user-data";
    LOG.info("JxBrowser user data path: " + dataPath);

    final EngineOptions options =
      EngineOptions.newBuilder(SystemInfo.isWindows ? OFF_SCREEN : HARDWARE_ACCELERATED)
        .userDataDir(Paths.get(dataPath))
        .passwordStore(PasswordStore.BASIC)
        .addSwitch("--disable-features=NativeNotifications")
        .build();

    Engine temp;
    try {
      temp = Engine.newInstance(options);
    } catch (Exception ex) {
      temp = null;
      LOG.info(ex);
      FlutterInitializer.getAnalytics().sendExpectedException("jxbrowser-engine", ex);
    }
    engine = temp;

    ApplicationManager.getApplication().addApplicationListener(new ApplicationListener() {
      @Override
      public boolean canExitApplication() {
        try {
          if (engine != null && !engine.isClosed()) {
            engine.close();
          }
        } catch (Exception ex) {
          LOG.info(ex);
          FlutterInitializer.getAnalytics().sendExpectedException("jxbrowswer-engine-close", ex);
        }
        return true;
      }
    });
  }

  public Engine getEngine() {
    return engine;
  }
}

@@ -1341,16 +1341,24 @@
 ions
+.Builder
  options
  =%0A 
@@ -1353,16 +1353,23 @@
  options
+Builder
  =%0A     
@@ -1598,25 +1598,169 @@
 ations%22)
+;%0A
 %0A    
-    
+if (SystemInfo.isLinux) %7B%0A      optionsBuilder.addSwitch(%22--force-device-scale-factor=1%22);%0A    %7D%0A%0A    final EngineOptions options = optionsBuilder
 .build()

Add force scale option for linux (#5618)