Skip to content

Commit 397e91a

Browse files
authored
[ai] fix: replace force-unwraps with guard-let in WindowManager, enable CI coverage (#176)
1 parent ae3f32d commit 397e91a

1 file changed

Lines changed: 40 additions & 8 deletions

File tree

Swift Shift/src/Manager/WindowManager.swift

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Cocoa
22
import Accessibility
3+
import os.log
34
struct WindowBounds {
45
let topLeft: NSPoint
56
let topRight: NSPoint
@@ -9,19 +10,37 @@ struct WindowBounds {
910
class WindowManager {
1011
@discardableResult
1112
static func move(window: AXUIElement, to point: NSPoint) -> AXError {
12-
var p = point; let v = AXValueCreate(.cgPoint, &p)!
13+
var p = point
14+
guard let v = AXValueCreate(.cgPoint, &p) else {
15+
os_log("WindowManager: AXValueCreate failed for cgPoint", log: .default, type: .error)
16+
return .cannotComplete
17+
}
1318
return AXUIElementSetAttributeValue(window, kAXPositionAttribute as CFString, v)
1419
}
1520
@discardableResult
1621
static func resize(window: AXUIElement, to s: CGSize, from o: NSPoint, shouldMoveOrigin: Bool = true) -> Bool {
1722
let moveResult = shouldMoveOrigin ? move(window: window, to: o) : .success
18-
var sz = s; let v = AXValueCreate(.cgSize, &sz)!
23+
var sz = s
24+
guard let v = AXValueCreate(.cgSize, &sz) else {
25+
os_log("WindowManager: AXValueCreate failed for cgSize", log: .default, type: .error)
26+
return false
27+
}
1928
let sizeResult = AXUIElementSetAttributeValue(window, kAXSizeAttribute as CFString, v)
2029
return moveResult == .success && sizeResult == .success
2130
}
2231
static func getSize(window: AXUIElement) -> NSSize? {
23-
var r: CFTypeRef?; guard AXUIElementCopyAttributeValue(window, kAXSizeAttribute as CFString, &r) == .success else { return nil }
24-
var s: CGSize = .zero; AXValueGetValue(r as! AXValue, .cgSize, &s); return NSSize(width: s.width, height: s.height)
32+
var r: CFTypeRef?
33+
guard AXUIElementCopyAttributeValue(window, kAXSizeAttribute as CFString, &r) == .success,
34+
let r = r, CFGetTypeID(r) == AXValueGetTypeID() else {
35+
os_log("WindowManager: AXValueCopyAttributeValue failed for kAXSizeAttribute", log: .default, type: .error)
36+
return nil
37+
}
38+
var s: CGSize = .zero
39+
guard AXValueGetValue(r as! AXValue, .cgSize, &s) else {
40+
os_log("WindowManager: AXValueGetValue failed for cgSize", log: .default, type: .error)
41+
return nil
42+
}
43+
return NSSize(width: s.width, height: s.height)
2544
}
2645
static func getVisibleWindowRects(excluding excludedWindow: AXUIElement? = nil) -> [CGRect] {
2746
let excludedRect: CGRect? = {
@@ -78,8 +97,11 @@ class WindowManager {
7897
var r: AnyObject?; AXUIElementCopyAttributeValue(element, kAXRoleAttribute as CFString, &r)
7998
if r as? String == kAXWindowRole { return element }
8099
var p: AnyObject?; AXUIElementCopyAttributeValue(element, kAXParentAttribute as CFString, &p)
81-
if let parent = p { return getWindow(from: parent as! AXUIElement) }
82-
return nil
100+
guard let p = p, CFGetTypeID(p as CFTypeRef) == AXUIElementGetTypeID() else {
101+
os_log("WindowManager: getWindow parent is not an AXUIElement", log: .default, type: .error)
102+
return nil
103+
}
104+
return getWindow(from: p as! AXUIElement)
83105
}
84106
static func focus(window: AXUIElement) { AXUIElementPerformAction(window, kAXRaiseAction as CFString); getNSApplication(from: window)?.activate() }
85107
static func getNSApplication(from element: AXUIElement) -> NSRunningApplication? {
@@ -89,8 +111,18 @@ class WindowManager {
89111
return NSPoint(x: point.x, y: CGDisplayBounds(CGMainDisplayID()).height - point.y)
90112
}
91113
static func getPosition(window: AXUIElement) -> NSPoint? {
92-
var r: CFTypeRef?; guard AXUIElementCopyAttributeValue(window, kAXPositionAttribute as CFString, &r) == .success else { return nil }
93-
var p: CGPoint = .zero; AXValueGetValue(r as! AXValue, .cgPoint, &p); return NSPoint(x: p.x, y: p.y)
114+
var r: CFTypeRef?
115+
guard AXUIElementCopyAttributeValue(window, kAXPositionAttribute as CFString, &r) == .success,
116+
let r = r, CFGetTypeID(r) == AXValueGetTypeID() else {
117+
os_log("WindowManager: AXValueCopyAttributeValue failed for kAXPositionAttribute", log: .default, type: .error)
118+
return nil
119+
}
120+
var p: CGPoint = .zero
121+
guard AXValueGetValue(r as! AXValue, .cgPoint, &p) else {
122+
os_log("WindowManager: AXValueGetValue failed for cgPoint", log: .default, type: .error)
123+
return nil
124+
}
125+
return NSPoint(x: p.x, y: p.y)
94126
}
95127
static func getWindowBounds(windowLocation: NSPoint, windowSize: CGSize) -> WindowBounds {
96128
let fixed = convertYCoordinateBecauseTheAreTwoFuckingCoordinateSystems(point: windowLocation)

0 commit comments

Comments
 (0)