Skip to content

Commit af38b35

Browse files
authored
feat: added hybrid polling method for macOS (#95)
* feat: added hybrid polling method for macOS * fix: added 1s to pulsetimes to fix events not merging
1 parent 310ce78 commit af38b35

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

aw_watcher_window/macos.swift

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ func start() {
177177
)
178178

179179
main.focusedAppChanged()
180+
181+
// Start the polling timer
182+
main.pollingTimer = Timer.scheduledTimer(timeInterval: 10.0, target: main, selector: #selector(main.pollActiveWindow), userInfo: nil, repeats: true)
180183
}
181184

182185
// TODO might be better to have the python wrapper create this before launching the swift application
@@ -224,20 +227,20 @@ func sendHeartbeat(_ heartbeat: Heartbeat) {
224227
// more info: https://github.com/ActivityWatch/aw-watcher-window/pull/69
225228
// we don't *think* this millisecond subtraction is necessary, but it may be:
226229
// https://github.com/ActivityWatch/aw-watcher-window/pull/69#discussion_r987064282
227-
timestamp: heartbeat.timestamp - 1.0/1000.0,
230+
timestamp: heartbeat.timestamp - 0.001,
228231
data: oldHeartbeat!.data
229232
)
230233

231-
try await sendHeartbeatSingle(refreshedOldHeartbeat, pulsetime: timeSinceLastHeartbeat)
234+
try await sendHeartbeatSingle(refreshedOldHeartbeat, pulsetime: timeSinceLastHeartbeat + 1)
232235
} catch {
233236
log("Failed to send old heartbeat: \(error)")
234237
return
235238
}
236239
}
237240

238241
do {
239-
let since_last_seconds = heartbeat.timestamp.timeIntervalSince(heartbeat.timestamp) + 1
240-
try await sendHeartbeatSingle(heartbeat, pulsetime: since_last_seconds)
242+
let since_last_seconds = oldHeartbeat != nil ? heartbeat.timestamp.timeIntervalSince(oldHeartbeat!.timestamp) : 0
243+
try await sendHeartbeatSingle(heartbeat, pulsetime: since_last_seconds + 1)
241244
} catch {
242245
log("Failed to send heartbeat: \(error)")
243246
return
@@ -267,6 +270,7 @@ func sendHeartbeatSingle(_ heartbeat: Heartbeat, pulsetime: Double) async throws
267270
class MainThing {
268271
var observer: AXObserver?
269272
var oldWindow: AXUIElement?
273+
var pollingTimer: Timer?
270274

271275
// list of chrome equivalent browsers
272276
let CHROME_BROWSERS = [
@@ -276,6 +280,29 @@ class MainThing {
276280
"Brave Browser",
277281
]
278282

283+
@objc func pollActiveWindow() {
284+
debug("Polling active window")
285+
286+
guard let frontmost = NSWorkspace.shared.frontmostApplication else {
287+
log("Failed to get frontmost application from polling")
288+
return
289+
}
290+
291+
let pid = frontmost.processIdentifier
292+
let focusedApp = AXUIElementCreateApplication(pid)
293+
294+
var focusedWindow: AnyObject?
295+
AXUIElementCopyAttributeValue(focusedApp, kAXFocusedWindowAttribute as CFString, &focusedWindow)
296+
297+
if focusedWindow != nil {
298+
focusedWindowChanged(observer!, window: focusedWindow as! AXUIElement)
299+
}
300+
}
301+
302+
deinit {
303+
pollingTimer?.invalidate()
304+
}
305+
279306
func windowTitleChanged(
280307
_ axObserver: AXObserver,
281308
axElement: AXUIElement,

0 commit comments

Comments
 (0)