Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116import ApplicationServicespublic import CoreGraphics
struct SpaceInputState: Sendable { var modifierFlags: CGEventFlags = [] var keyHeld = false var secondsSinceKeyDown = Double.infinity var secondsSinceKeyUp = Double.infinity var secondsSinceFlagsChanged = Double.infinity var mouseButtonHeld = false
static func read() -> Self { Self( modifierFlags: CGEventSource.flagsState(.combinedSessionState), keyHeld: (0..<128).contains { CGEventSource.keyState(.combinedSessionState, key: CGKeyCode($0)) }, secondsSinceKeyDown: CGEventSource.secondsSinceLastEventType(.combinedSessionState, eventType: .keyDown), secondsSinceKeyUp: CGEventSource.secondsSinceLastEventType(.combinedSessionState, eventType: .keyUp), secondsSinceFlagsChanged: CGEventSource.secondsSinceLastEventType( .combinedSessionState, eventType: .flagsChanged), mouseButtonHeld: [CGMouseButton.left, .right, .center].contains { CGEventSource.buttonState(.combinedSessionState, button: $0) }) }
func validate() throws { guard !mouseButtonHeld else { throw SpaceCommandError.pointerBusy } let heldModifiers: CGEventFlags = [ .maskShift, .maskControl, .maskAlternate, .maskCommand, .maskSecondaryFn, .maskHelp, ] guard modifierFlags.intersection(heldModifiers).isEmpty, !keyHeld, secondsSinceKeyDown >= 0.3, secondsSinceKeyUp >= 0.3, secondsSinceFlagsChanged >= 0.3 else { throw SpaceCommandError.inputBusy } }}
public enum SpaceCursorError: Error, Equatable, Sendable { case positionUnavailable case restoreFailed(CGError) case restoreNotVerified}
public struct SystemSpaceEventPoster: SpaceEventPosting { private let accessibilityTrusted: @Sendable () -> Bool private let inputState: @Sendable () -> SpaceInputState
public init() { self.init(accessibilityTrusted: { AXIsProcessTrusted() }, inputState: { SpaceInputState.read() }) }
init( accessibilityTrusted: @escaping @Sendable () -> Bool, inputState: @escaping @Sendable () -> SpaceInputState ) { self.accessibilityTrusted = accessibilityTrusted self.inputState = inputState }
public func prepare() throws { guard accessibilityTrusted() else { throw InputError.accessibilityRequired } try inputState().validate() }
public func validateTarget(_ window: CGWindowID, at point: CGPoint) throws { let windows = try WindowServer().windows(options: .optionOnScreenOnly) guard windows.first(where: { $0.layer == 0 && $0.bounds.contains(point) })?.number == window else { throw SpaceCommandError.windowUnavailable } }
public func hotKey(_ key: HotKey) throws { try HotKeyPoster.post(key) }
public func mouse(_ sample: SpaceMouseEvent) throws { guard AXIsProcessTrusted() else { throw InputError.accessibilityRequired } let event = try Self.event(for: sample) event.post(tap: .cghidEventTap) }
public static func event(for sample: SpaceMouseEvent) throws -> CGEvent { let type: CGEventType let point: CGPoint switch sample { case .down(let location): (type, point) = (.leftMouseDown, location) case .drag(let location): (type, point) = (.leftMouseDragged, location) case .up(let location): (type, point) = (.leftMouseUp, location) } guard let event = CGEvent(mouseEventSource: nil, mouseType: type, mouseCursorPosition: point, mouseButton: .left) else { throw InputError.eventCreationFailed } event.flags = [] event.setIntegerValueField(.mouseEventClickState, value: 1) return event }
public func cursorPosition() throws -> CGPoint { guard let position = CGEvent(source: nil)?.location, position.x.isFinite, position.y.isFinite else { throw SpaceCursorError.positionUnavailable } return position }
public func restoreCursor(_ point: CGPoint) throws { guard accessibilityTrusted() else { throw InputError.accessibilityRequired } let result = CGWarpMouseCursorPosition(point) guard result == .success else { throw SpaceCursorError.restoreFailed(result) } guard try cursorPosition() == point else { throw SpaceCursorError.restoreNotVerified } }
public func windowFrame(_ window: CGWindowID) throws -> CGRect { guard let record = try WindowServer().windows(options: .optionIncludingWindow, relativeTo: window) .first(where: { $0.number == window }), record.isOnscreen, record.layer == 0 else { throw SpaceCommandError.windowUnavailable } return record.bounds }}