Skip to content

Commit

Permalink
Fix title bar element detection
Browse files Browse the repository at this point in the history
  • Loading branch information
decodism committed Dec 8, 2023
1 parent 0d1937e commit 89653f2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
28 changes: 28 additions & 0 deletions Rectangle/AccessibilityElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,22 @@ class AccessibilityElement {
return elements
}

func getSelfOrChildElementRecursively(_ position: CGPoint) -> AccessibilityElement? {
func getChildElement() -> AccessibilityElement? {
return element.childElements?
.map {(element: $0, frame: $0.frame)}
.filter { $0.frame.contains(position) }
.min { $0.frame.width * $0.frame.height < $1.frame.width * $1.frame.height }?
.element
}
var element = self
var elements = Set<AccessibilityElement>()
while let childElement = getChildElement(), elements.insert(childElement).inserted {
element = childElement
}
return element
}

var windowId: CGWindowID? {
wrappedElement.getWindowId()
}
Expand Down Expand Up @@ -359,6 +375,18 @@ extension AccessibilityElement {
}
}

extension AccessibilityElement: Equatable {
static func == (lhs: AccessibilityElement, rhs: AccessibilityElement) -> Bool {
return lhs.wrappedElement == rhs.wrappedElement
}
}

extension AccessibilityElement: Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(wrappedElement)
}
}

class StageWindowAccessibilityElement: AccessibilityElement {
private let _windowId: CGWindowID

Expand Down
2 changes: 1 addition & 1 deletion Rectangle/TitleBarManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TitleBarManager {
TitleBarManager.systemSettingDisabled,
let action = WindowAction(rawValue: Defaults.doubleClickTitleBar.value - 1),
case let location = NSEvent.mouseLocation.screenFlipped,
let element = AccessibilityElement(location),
let element = AccessibilityElement(location)?.getSelfOrChildElementRecursively(location),
let windowElement = element.windowElement,
var titleBarFrame = windowElement.titleBarFrame
else {
Expand Down

0 comments on commit 89653f2

Please sign in to comment.