From c3b8317edb1369d2968f20283eba4b3c7cce3425 Mon Sep 17 00:00:00 2001
From: meeh <meeh@mail.i2p>
Date: Sat, 8 Dec 2018 09:16:28 +0000
Subject: [PATCH] Mac OS X Launcher: Adding optional Dock Icon functionality

---
 .../I2PLauncher/SwiftMainDelegate.swift       | 36 ++++++++++++++++---
 1 file changed, 31 insertions(+), 5 deletions(-)

diff --git a/launchers/macosx/I2PLauncher/SwiftMainDelegate.swift b/launchers/macosx/I2PLauncher/SwiftMainDelegate.swift
index 719e0ed012..188425ee29 100644
--- a/launchers/macosx/I2PLauncher/SwiftMainDelegate.swift
+++ b/launchers/macosx/I2PLauncher/SwiftMainDelegate.swift
@@ -52,9 +52,8 @@ class Logger {
   } // End of init()
   
   @objc func findInstalledI2PVersion() {
-    var i2pPath = NSHomeDirectory()
-    i2pPath += "/Library/I2P"
-    let jExecPath:String = "/usr/libexec/java_home -v 1.7+ --exec java "
+    var i2pPath = Preferences.shared().i2pBaseDirectory
+    let jExecPath:String = Preferences.shared().javaCommandPath
     
     let jarPath = i2pPath + "/lib/i2p.jar"
     
@@ -81,7 +80,35 @@ class Logger {
     }
   }
   
+  func triggerDockIconShowHide(showIcon state: Bool) -> Bool {
+    var result: Bool
+    if state {
+      result = NSApp.setActivationPolicy(NSApplicationActivationPolicy.regular)
+    } else {
+      result = NSApp.setActivationPolicy(NSApplicationActivationPolicy.accessory)
+    }
+    return result
+  }
+  
+  func getDockIconStateIsShowing() -> Bool {
+    if NSApp.activationPolicy() == NSApplicationActivationPolicy.regular {
+      return true
+    } else {
+      return false
+    }
+  }
+  
   @objc func applicationDidFinishLaunching() {
+    switch Preferences.shared().showAsIconMode {
+    case .bothIcon, .dockIcon:
+      if (!getDockIconStateIsShowing()) {
+        triggerDockIconShowHide(showIcon: true)
+      }
+    default:
+      if (getDockIconStateIsShowing()) {
+        triggerDockIconShowHide(showIcon: false)
+      }
+    }
   }
   
   @objc func listenForEvent(eventName: String, callbackActionFn: @escaping ((Any?)->()) ) {
@@ -99,8 +126,7 @@ class Logger {
   
   @objc func applicationWillTerminate() {
     // Shutdown stuff
-    let userPreferences = UserDefaults.standard
-    if (!userPreferences.bool(forKey: "letRouterLiveEvenLauncherDied")) {
+    if (Preferences.shared().stopRouterOnLauncherShutdown) {
       RouterManager.shared().routerRunner.TeardownLaunchd()
       sleep(2)
       let status: AgentStatus? = RouterRunner.launchAgent?.status()
-- 
GitLab