Skip to content

Commit

Permalink
Apply yabai's fix for moving windows between spaces in macOS 14.5 (#1648
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ianyh committed May 28, 2024
1 parent e45fbdd commit 23df7fb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Amethyst.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"originHash" : "43f1b1e9bfd866a37b073557f24d496c85ab3a7508ae523528cda0c3afb30aa9",
"pins" : [
{
"identity" : "cartography",
Expand Down Expand Up @@ -127,5 +128,5 @@
}
}
],
"version" : 2
"version" : 3
}
36 changes: 34 additions & 2 deletions Amethyst/Model/Window.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ func SLPSPostEventRecordTo(_ psn: inout ProcessSerialNumber, _ bytes: inout UInt
@_silgen_name("SLSMoveWindowsToManagedSpace")
func SLSMoveWindowsToManagedSpace(_ cid: Int32, _ window_ids: CFArray, _ sid: Int)

@_silgen_name("SLSSpaceSetCompatID")
func SLSSpaceSetCompatID(_ cid: Int32, _ sid: Int, _ workspace: Int32) -> CGError

@_silgen_name("SLSSetWindowListWorkspace")
func SLSSetWindowListWorkspace(_ cid: Int32, _ window_ids: UnsafePointer<UInt32>, _ window_count: Int32, _ workspace: Int32) -> CGError

let kCPSUserGenerated: UInt32 = 0x200
let kCPSNoWindows: UInt32 = 0x400
// swiftlint:enable identifier_name
Expand Down Expand Up @@ -292,7 +298,7 @@ extension AXWindow: WindowType {
What a mess. See: https://github.com/Hammerspoon/hammerspoon/issues/370#issuecomment-545545468
*/
@discardableResult override func focus() -> Bool {
var pid = self.pid()
let pid = self.pid()
var wid = self.cgID()
var psn = ProcessSerialNumber()
let status = GetProcessForPID(pid, &psn)
Expand Down Expand Up @@ -367,6 +373,32 @@ extension AXWindow: WindowType {
}

func move(toSpace spaceID: CGSSpaceID) {
SLSMoveWindowsToManagedSpace(CGSMainConnectionID(), [cgID()] as CFArray, spaceID)
if ProcessInfo.processInfo.isOperatingSystemAtLeast(OperatingSystemVersion(majorVersion: 14, minorVersion: 5, patchVersion: 0)) {
/*
See:
- https://github.com/ianyh/Amethyst/issues/1643
- https://github.com/koekeishiya/yabai/issues/2240
- https://github.com/koekeishiya/yabai/commit/98bbdbd1363f27d35f09338cded0de1ec010d830
*/
var error: CGError = .success

error = SLSSpaceSetCompatID(CGSMainConnectionID(), spaceID, 0x79616265)
defer { _ = SLSSpaceSetCompatID(CGSMainConnectionID(), spaceID, 0x0) }
guard error == .success else {
log.error("failed to set compat aside id: \(error)")
return
}

var id = cgID()
error = withUnsafeMutablePointer(to: &id, { pointer -> CGError in
return SLSSetWindowListWorkspace(CGSMainConnectionID(), pointer, 1, 0x79616265)
})
guard error == .success else {
log.error("failed to throw window: \(error)")
return
}
} else {
SLSMoveWindowsToManagedSpace(CGSMainConnectionID(), [cgID()] as CFArray, spaceID)
}
}
}

0 comments on commit 23df7fb

Please sign in to comment.