Skip to content

Commit f5f3448

Browse files
authored
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
1 parent 8e8a16d commit f5f3448

2 files changed

Lines changed: 41 additions & 15 deletions

File tree

Rectangle/AppDelegate.swift

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {
239239

240240
@IBAction func ignoreFrontMostApp(_ sender: NSMenuItem) {
241241
if sender.state == .on {
242-
applicationToggle.enableFrontApp()
242+
applicationToggle.enableApp()
243243
} else {
244-
applicationToggle.disableFrontApp()
244+
applicationToggle.disableApp()
245245
}
246246
}
247247

@@ -552,20 +552,47 @@ extension AppDelegate {
552552
prevActiveApp?.activate()
553553
}
554554
DispatchQueue.main.async {
555+
555556
func getUrlName(_ name: String) -> String {
556557
return name.map { $0.isUppercase ? "-" + $0.lowercased() : String($0) }.joined()
557558
}
559+
560+
func extractBundleIdParameter(fromComponents components: URLComponents) -> String? {
561+
(components.queryItems?.first { $0.name == "app-bundle-id" })?.value
562+
}
563+
564+
func isValidParameter(bundleId: String?) -> Bool {
565+
let isValid = bundleId?.isEmpty != true
566+
if !isValid {
567+
Logger.log("Received an empty app-bundle-id parameter. Either pass a valid app bundle id or remove the parameter.")
568+
}
569+
return isValid
570+
}
571+
558572
for url in urls {
559-
guard
573+
guard
560574
let components = URLComponents(url: url, resolvingAgainstBaseURL: true),
561-
components.host == "execute-action",
562-
components.path.isEmpty,
563-
let name = (components.queryItems?.first { $0.name == "name" })?.value,
564-
let action = (WindowAction.active.first { getUrlName($0.name) == name })
575+
components.path.isEmpty
565576
else {
566577
continue
567578
}
568-
action.postUrl()
579+
580+
let name = (components.queryItems?.first { $0.name == "name" })?.value
581+
switch (components.host, name) {
582+
case ("execute-action", _):
583+
let action = (WindowAction.active.first { getUrlName($0.name) == name })
584+
action?.postUrl()
585+
case ("execute-task", "ignore-app"):
586+
let bundleId = extractBundleIdParameter(fromComponents: components)
587+
guard isValidParameter(bundleId: bundleId) else { continue }
588+
self.applicationToggle.disableApp(appBundleId: bundleId)
589+
case ("execute-task", "unignore-app"):
590+
let bundleId = extractBundleIdParameter(fromComponents: components)
591+
guard isValidParameter(bundleId: bundleId) else { continue }
592+
self.applicationToggle.enableApp(appBundleId: bundleId)
593+
default:
594+
continue
595+
}
569596
}
570597
}
571598
}

Rectangle/ApplicationToggle.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,17 @@ class ApplicationToggle: NSObject {
7373
}
7474
}
7575

76-
public func disableFrontApp() {
77-
if let frontAppId = Self.frontAppId {
78-
disabledApps.insert(frontAppId)
76+
public func disableApp(appBundleId: String? = frontAppId) {
77+
if let appBundleId {
78+
disabledApps.insert(appBundleId)
7979
saveDisabledApps()
8080
disableShortcuts()
8181
}
8282
}
8383

84-
public func enableFrontApp() {
85-
if let frontAppId = Self.frontAppId {
86-
disabledApps.remove(frontAppId)
84+
public func enableApp(appBundleId: String? = frontAppId) {
85+
if let appBundleId {
86+
disabledApps.remove(appBundleId)
8787
saveDisabledApps()
8888
enableShortcuts()
8989
}
@@ -118,7 +118,6 @@ class ApplicationToggle: NSObject {
118118
}
119119
}
120120
}
121-
122121
}
123122

124123
// todo mode

0 commit comments

Comments
 (0)