Skip to content

Commit

Permalink
Handle ignore unignore URL actions (#1505)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bric3 authored Nov 14, 2024
1 parent 8e8a16d commit f5f3448
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
43 changes: 35 additions & 8 deletions Rectangle/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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
}
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions Rectangle/ApplicationToggle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -118,7 +118,6 @@ class ApplicationToggle: NSObject {
}
}
}

}

// todo mode
Expand Down

0 comments on commit f5f3448

Please sign in to comment.