Skip to content

Commit

Permalink
Don't listen to click/drag events when frontmost app is fullscreen
Browse files Browse the repository at this point in the history
  • Loading branch information
rxhanson committed Apr 27, 2022
1 parent 8e4f26a commit 95019b7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
13 changes: 13 additions & 0 deletions Rectangle/AccessibilityElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,19 @@ class AccessibilityElement {
return value(for: .subrole) == kAXSystemDialogSubrole
}

func isFullScreen() -> Bool {
if let window = window() {
if let fullScreenButton: AccessibilityElement = window.value(for: .fullScreenButton) {
if let subrole: String = fullScreenButton.value(for: .subrole) {
if subrole == kAXZoomButtonSubrole {
return true
}
}
}
}
return false
}

func isEnhancedUserInterfaceEnabled() -> Bool? {
var rawValue: AnyObject?
let error = AXUIElementCopyAttributeValue(self.underlyingElement, kAXEnhancedUserInterface as CFString, &rawValue)
Expand Down
43 changes: 35 additions & 8 deletions Rectangle/Snapping/SnappingManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ class SnappingManager {
var windowIdAttempt: Int = 0
var lastWindowIdAttempt: TimeInterval?
var windowMoving: Bool = false
var isFullScreen: Bool = false
var allowListening: Bool = true
var initialWindowRect: CGRect?
var currentSnapArea: SnapArea?

var box: FootprintWindow?

let screenDetection = ScreenDetection()

private let marginTop = CGFloat(Defaults.snapEdgeMarginTop.value)
Expand Down Expand Up @@ -49,17 +51,40 @@ class SnappingManager {
enableSnapping()
}

registerWorkspaceChangeNote()

Notification.Name.windowSnapping.onPost { notification in
guard let enabled = notification.object as? Bool else { return }
if enabled {
if !Defaults.windowSnapping.userDisabled {
self.enableSnapping()
}
} else {
self.disableSnapping()
if let enabled = notification.object as? Bool {
self.allowListening = enabled
}
self.toggleListening()
}
}

func toggleListening() {
if allowListening, !isFullScreen, !Defaults.windowSnapping.userDisabled {
enableSnapping()
} else {
disableSnapping()
}
}

private func registerWorkspaceChangeNote() {
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(receiveWorkspaceNote(_:)), name: NSWorkspace.activeSpaceDidChangeNotification, object: nil)
checkFullScreen()
}

func checkFullScreen() {
let currentFullScreen = AccessibilityElement.frontmostWindow()?.isFullScreen() == true
if isFullScreen != currentFullScreen {
isFullScreen = currentFullScreen
toggleListening()
}
}

@objc func receiveWorkspaceNote(_ notification: Notification) {
checkFullScreen()
}

public func reloadFromDefaults() {
if Defaults.windowSnapping.userDisabled {
Expand All @@ -74,6 +99,7 @@ class SnappingManager {
}

private func enableSnapping() {
print("enable")
if box == nil {
box = FootprintWindow()
}
Expand All @@ -84,6 +110,7 @@ class SnappingManager {
}

private func disableSnapping() {
print("disable")
box = nil
eventMonitor?.stop()
eventMonitor = nil
Expand Down

0 comments on commit 95019b7

Please sign in to comment.