From f5f3448fb9ba0eedb5d260930b0e6ba8442ef595 Mon Sep 17 00:00:00 2001 From: Brice Dutheil Date: Thu, 14 Nov 2024 04:07:52 +0100 Subject: [PATCH] Handle ignore unignore URL actions (#1505) * Handle ignore unignore URL actions * Handle ignore unignore URL actions via AppDelegate * Restore sotryboard from main * Add support for app-bundle-id to ignore/unignore url tasks * PR Review * Check if app-bundle-id parameter is empty for ignore/unignore url --- Rectangle/AppDelegate.swift | 43 +++++++++++++++++++++++++------ Rectangle/ApplicationToggle.swift | 13 +++++----- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/Rectangle/AppDelegate.swift b/Rectangle/AppDelegate.swift index 7d341694..6871e023 100644 --- a/Rectangle/AppDelegate.swift +++ b/Rectangle/AppDelegate.swift @@ -239,9 +239,9 @@ class AppDelegate: NSObject, NSApplicationDelegate { @IBAction func ignoreFrontMostApp(_ sender: NSMenuItem) { if sender.state == .on { - applicationToggle.enableFrontApp() + applicationToggle.enableApp() } else { - applicationToggle.disableFrontApp() + applicationToggle.disableApp() } } @@ -552,20 +552,47 @@ extension AppDelegate { prevActiveApp?.activate() } DispatchQueue.main.async { + func getUrlName(_ name: String) -> String { return name.map { $0.isUppercase ? "-" + $0.lowercased() : String($0) }.joined() } + + func extractBundleIdParameter(fromComponents components: URLComponents) -> String? { + (components.queryItems?.first { $0.name == "app-bundle-id" })?.value + } + + func isValidParameter(bundleId: String?) -> Bool { + let isValid = bundleId?.isEmpty != true + if !isValid { + Logger.log("Received an empty app-bundle-id parameter. Either pass a valid app bundle id or remove the parameter.") + } + return isValid + } + for url in urls { - guard + guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true), - components.host == "execute-action", - components.path.isEmpty, - let name = (components.queryItems?.first { $0.name == "name" })?.value, - let action = (WindowAction.active.first { getUrlName($0.name) == name }) + components.path.isEmpty else { continue } - action.postUrl() + + let name = (components.queryItems?.first { $0.name == "name" })?.value + switch (components.host, name) { + case ("execute-action", _): + let action = (WindowAction.active.first { getUrlName($0.name) == name }) + action?.postUrl() + case ("execute-task", "ignore-app"): + let bundleId = extractBundleIdParameter(fromComponents: components) + guard isValidParameter(bundleId: bundleId) else { continue } + self.applicationToggle.disableApp(appBundleId: bundleId) + case ("execute-task", "unignore-app"): + let bundleId = extractBundleIdParameter(fromComponents: components) + guard isValidParameter(bundleId: bundleId) else { continue } + self.applicationToggle.enableApp(appBundleId: bundleId) + default: + continue + } } } } diff --git a/Rectangle/ApplicationToggle.swift b/Rectangle/ApplicationToggle.swift index 12db1f5b..7c05875a 100644 --- a/Rectangle/ApplicationToggle.swift +++ b/Rectangle/ApplicationToggle.swift @@ -73,17 +73,17 @@ class ApplicationToggle: NSObject { } } - public func disableFrontApp() { - if let frontAppId = Self.frontAppId { - disabledApps.insert(frontAppId) + public func disableApp(appBundleId: String? = frontAppId) { + if let appBundleId { + disabledApps.insert(appBundleId) saveDisabledApps() disableShortcuts() } } - public func enableFrontApp() { - if let frontAppId = Self.frontAppId { - disabledApps.remove(frontAppId) + public func enableApp(appBundleId: String? = frontAppId) { + if let appBundleId { + disabledApps.remove(appBundleId) saveDisabledApps() enableShortcuts() } @@ -118,7 +118,6 @@ class ApplicationToggle: NSObject { } } } - } // todo mode