Skip to content

Commit bced83b

Browse files
committed
fix(capture): focus captured window on recording start and improve toolbar window ordering.
1 parent 9f1e205 commit bced83b

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

Reframed/State/SessionState.swift

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ final class SessionState {
170170

171171
func showToolbar() {
172172
if let existing = toolbarWindow {
173-
existing.makeKeyAndOrderFront(nil)
173+
existing.orderFrontRegardless()
174174
return
175175
}
176176

@@ -180,7 +180,7 @@ final class SessionState {
180180
}
181181
}
182182
toolbarWindow = window
183-
window.makeKeyAndOrderFront(nil)
183+
window.orderFrontRegardless()
184184
}
185185

186186
func hideToolbar() {
@@ -639,6 +639,11 @@ final class SessionState {
639639
webcamPreviewWindow?.hide()
640640
}
641641
showToolbar()
642+
if case .window(let win) = captureTarget,
643+
let pid = win.owningApplication?.processID
644+
{
645+
focusWindow(pid: pid, frame: win.frame)
646+
}
642647
case .paused:
643648
if options.hideCameraPreviewWhileRecording {
644649
webcamPreviewWindow?.unhide()
@@ -650,6 +655,40 @@ final class SessionState {
650655
}
651656
}
652657

658+
private func focusWindow(pid: pid_t, frame: CGRect) {
659+
let axApp = AXUIElementCreateApplication(pid)
660+
var windowsRef: CFTypeRef?
661+
AXUIElementCopyAttributeValue(axApp, kAXWindowsAttribute as CFString, &windowsRef)
662+
if let windows = windowsRef as? [AXUIElement] {
663+
for window in windows {
664+
var positionRef: CFTypeRef?
665+
var sizeRef: CFTypeRef?
666+
AXUIElementCopyAttributeValue(window, kAXPositionAttribute as CFString, &positionRef)
667+
AXUIElementCopyAttributeValue(window, kAXSizeAttribute as CFString, &sizeRef)
668+
669+
var pos = CGPoint.zero
670+
var size = CGSize.zero
671+
if let posRef = positionRef, CFGetTypeID(posRef) == AXValueGetTypeID() {
672+
AXValueGetValue(posRef as! AXValue, .cgPoint, &pos)
673+
}
674+
if let sRef = sizeRef, CFGetTypeID(sRef) == AXValueGetTypeID() {
675+
AXValueGetValue(sRef as! AXValue, .cgSize, &size)
676+
}
677+
678+
if abs(pos.x - frame.origin.x) < 20
679+
&& abs(pos.y - frame.origin.y) < 20
680+
&& abs(size.width - frame.width) < 20
681+
&& abs(size.height - frame.height) < 20
682+
{
683+
AXUIElementPerformAction(window, kAXRaiseAction as CFString)
684+
NSRunningApplication(processIdentifier: pid)?.activate()
685+
return
686+
}
687+
}
688+
}
689+
NSRunningApplication(processIdentifier: pid)?.activate()
690+
}
691+
653692
private func updateStatusIcon() {
654693
let iconName: String =
655694
switch state {

0 commit comments

Comments
 (0)