Skip to content

Commit

Permalink
Merge pull request #1352 from decodism/main-2
Browse files Browse the repository at this point in the history
Try to deactivate Rectangle when executing action by URL
  • Loading branch information
rxhanson authored Mar 27, 2024
2 parents c548726 + 136c5e7 commit 277cadc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Drag a window to the edge of the screen. When the mouse cursor reaches the edge

## Execute an action by URL

Open the URL `rectangle://execute-action?name=[name]`. Do not activate Rectangle.
Open the URL `rectangle://execute-action?name=[name]`. Do not activate Rectangle if possible.

Available values for `[name]`: `left-half`, `right-half`, `center-half`, `top-half`, `bottom-half`, `top-left`, `top-right`, `bottom-left`, `bottom-right`, `first-third`, `center-third`, `last-third`, `first-two-thirds`, `last-two-thirds`, `maximize`, `almost-maximize`, `maximize-height`, `smaller`, `larger`, `center`, `restore`, `next-display`, `previous-display`, `move-left`, `move-right`, `move-up`, `move-down`, `first-fourth`, `second-fourth`, `third-fourth`, `last-fourth`, `first-three-fourths`, `last-three-fourths`, `top-left-sixth`, `top-center-sixth`, `top-right-sixth`, `bottom-left-sixth`, `bottom-center-sixth`, `bottom-right-sixth`, `specified`, `reverse-all`, `top-left-ninth`, `top-center-ninth`, `top-right-ninth`, `middle-left-ninth`, `middle-center-ninth`, `middle-right-ninth`, `bottom-left-ninth`, `bottom-center-ninth`, `bottom-right-ninth`, `top-left-third`, `top-right-third`, `bottom-left-third`, `bottom-right-third`, `top-left-eighth`, `top-center-left-eighth`, `top-center-right-eighth`, `top-right-eighth`, `bottom-left-eighth`, `bottom-center-left-eighth`, `bottom-center-right-eighth`, `bottom-right-eighth`, `tile-all`, `cascade-all`

Expand Down
34 changes: 25 additions & 9 deletions Rectangle/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {

private var prefsWindowController: NSWindowController?

private var prevActiveAppObservation: NSKeyValueObservation?
private var prevActiveApp: NSRunningApplication?

@IBOutlet weak var mainStatusMenu: NSMenu!
@IBOutlet weak var unauthorizedMenu: NSMenu!
@IBOutlet weak var ignoreMenuItem: NSMenuItem!
Expand Down Expand Up @@ -94,6 +97,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
Notification.Name.todoMenuToggled.onPost(using: { _ in
self.initializeTodo(false)
})

prevActiveAppObservation = NSWorkspace.shared.observe(\.frontmostApplication, options: .old) { workspace, change in
self.prevActiveApp = change.oldValue ?? nil
}
}

func applicationWillBecomeActive(_ notification: Notification) {
Expand Down Expand Up @@ -536,16 +543,25 @@ extension AppDelegate: NSWindowDelegate {

extension AppDelegate {
func application(_ application: NSApplication, open urls: [URL]) {
for url in urls {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else { continue }
if components.host == "execute-action" && components.path.isEmpty {
guard let name = (components.queryItems?.first { $0.name == "name" })?.value else { continue }
if let action = (WindowAction.active.first { urlName($0.name) == name }) { action.postUrl() }
if NSWorkspace.shared.frontmostApplication == NSRunningApplication.current {
prevActiveApp?.activate()
}
DispatchQueue.main.async {
func getUrlName(_ name: String) -> String {
return name.map { $0.isUppercase ? "-" + $0.lowercased() : String($0) }.joined()
}
for url in urls {
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 })
else {
continue
}
action.postUrl()
}
}
}

private func urlName(_ name: String) -> String {
return name.map { $0.isUppercase ? "-" + $0.lowercased() : String($0) }.joined()
}
}

0 comments on commit 277cadc

Please sign in to comment.