diff --git a/SecretAgent/Assets.xcassets/AppIcon.appiconset/Contents.json b/SecretAgent/Assets.xcassets/AppIcon.appiconset/Contents.json index 71f386d..c14ca73 100644 --- a/SecretAgent/Assets.xcassets/AppIcon.appiconset/Contents.json +++ b/SecretAgent/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -31,13 +31,13 @@ "size" : "128x128" }, { - "filename" : "Icon 2@1x.png", + "filename" : "Mac Icon.png", "idiom" : "mac", "scale" : "1x", "size" : "256x256" }, { - "filename" : "Icon 2@2x.png", + "filename" : "Mac Icon@0.25x.png", "idiom" : "mac", "scale" : "2x", "size" : "256x256" diff --git a/SecretAgent/Assets.xcassets/AppIcon.appiconset/Icon 2@1x.png b/SecretAgent/Assets.xcassets/AppIcon.appiconset/Icon 2@1x.png deleted file mode 100644 index c1f6cc1..0000000 Binary files a/SecretAgent/Assets.xcassets/AppIcon.appiconset/Icon 2@1x.png and /dev/null differ diff --git a/SecretAgent/Assets.xcassets/AppIcon.appiconset/Icon 2@2x.png b/SecretAgent/Assets.xcassets/AppIcon.appiconset/Icon 2@2x.png deleted file mode 100644 index b3b5339..0000000 Binary files a/SecretAgent/Assets.xcassets/AppIcon.appiconset/Icon 2@2x.png and /dev/null differ diff --git a/SecretAgent/Assets.xcassets/AppIcon.appiconset/Mac Icon.png b/SecretAgent/Assets.xcassets/AppIcon.appiconset/Mac Icon.png new file mode 100644 index 0000000..99a172b Binary files /dev/null and b/SecretAgent/Assets.xcassets/AppIcon.appiconset/Mac Icon.png differ diff --git a/SecretAgent/Assets.xcassets/AppIcon.appiconset/Mac Icon@0.25x.png b/SecretAgent/Assets.xcassets/AppIcon.appiconset/Mac Icon@0.25x.png new file mode 100644 index 0000000..8b7b7ae Binary files /dev/null and b/SecretAgent/Assets.xcassets/AppIcon.appiconset/Mac Icon@0.25x.png differ diff --git a/Secretive/App.swift b/Secretive/App.swift index 3ade857..ab0bd93 100644 --- a/Secretive/App.swift +++ b/Secretive/App.swift @@ -28,9 +28,19 @@ struct Secretive: App { .onAppear { if !hasRunSetup { showingSetup = true - } else if agentStatusChecker.running && justUpdatedChecker.justUpdated { - // Relaunch the agent, since it'll be running from earlier update still - _ = LaunchAgentController().install() + } else { + if agentStatusChecker.running && justUpdatedChecker.justUpdated { + // Relaunch the agent, since it'll be running from earlier update still + _ = LaunchAgentController().install() + } + } + } + .onReceive(NotificationCenter.default.publisher(for: NSApplication.willBecomeActiveNotification)) { _ in + agentStatusChecker.check() + if hasRunSetup && !agentStatusChecker.running { + // We've run setup, we didn't just update, launchd is just not doing it's thing. + // Force a launch directly. + LaunchAgentController().forceLaunch() } } } diff --git a/Secretive/Controllers/AgentStatusChecker.swift b/Secretive/Controllers/AgentStatusChecker.swift index 02b7f84..d09cd32 100644 --- a/Secretive/Controllers/AgentStatusChecker.swift +++ b/Secretive/Controllers/AgentStatusChecker.swift @@ -15,11 +15,24 @@ class AgentStatusChecker: ObservableObject, AgentStatusCheckerProtocol { } func check() { - running = secretAgentProcess != nil + running = instanceSecretAgentProcess != nil } - var secretAgentProcess: NSRunningApplication? { - NSRunningApplication.runningApplications(withBundleIdentifier: Constants.secretAgentAppID).first + // All processes, including ones from older versions, etc + var secretAgentProcesses: [NSRunningApplication] { + NSRunningApplication.runningApplications(withBundleIdentifier: Constants.secretAgentAppID) + } + + // The process corresponding to this instance of Secretive + var instanceSecretAgentProcess: NSRunningApplication? { + let agents = secretAgentProcesses + for agent in agents { + guard let url = agent.bundleURL else { continue } + if url.absoluteString.hasPrefix(Bundle.main.bundleURL.absoluteString) { + return agent + } + } + return nil } } diff --git a/Secretive/Controllers/LaunchAgentController.swift b/Secretive/Controllers/LaunchAgentController.swift index 10b9150..9c000eb 100644 --- a/Secretive/Controllers/LaunchAgentController.swift +++ b/Secretive/Controllers/LaunchAgentController.swift @@ -1,13 +1,28 @@ import Foundation import ServiceManagement +import AppKit +import OSLog struct LaunchAgentController { func install() -> Bool { + Logger().debug("Installing agent") _ = setEnabled(false) return setEnabled(true) } + func forceLaunch() { + Logger().debug("Agent is not running, attempting to force launch") + let url = Bundle.main.bundleURL.appendingPathComponent("Contents/Library/LoginItems/SecretAgent.app") + NSWorkspace.shared.openApplication(at: url, configuration: NSWorkspace.OpenConfiguration()) { app, error in + if let error = error { + Logger().error("Error force launching \(error.localizedDescription)") + } else { + Logger().debug("Agent force launched") + } + } + } + private func setEnabled(_ enabled: Bool) -> Bool { SMLoginItemSetEnabled("com.maxgoedjen.Secretive.SecretAgent" as CFString, enabled) }