Skip to content

Commit

Permalink
feat: added hybrid polling method for macOS (#95)
Browse files Browse the repository at this point in the history
* feat: added hybrid polling method for macOS

* fix: added 1s to pulsetimes to fix events not merging
  • Loading branch information
ErikBjare authored Dec 26, 2023
1 parent 310ce78 commit af38b35
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions aw_watcher_window/macos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ func start() {
)

main.focusedAppChanged()

// Start the polling timer
main.pollingTimer = Timer.scheduledTimer(timeInterval: 10.0, target: main, selector: #selector(main.pollActiveWindow), userInfo: nil, repeats: true)
}

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

try await sendHeartbeatSingle(refreshedOldHeartbeat, pulsetime: timeSinceLastHeartbeat)
try await sendHeartbeatSingle(refreshedOldHeartbeat, pulsetime: timeSinceLastHeartbeat + 1)
} catch {
log("Failed to send old heartbeat: \(error)")
return
}
}

do {
let since_last_seconds = heartbeat.timestamp.timeIntervalSince(heartbeat.timestamp) + 1
try await sendHeartbeatSingle(heartbeat, pulsetime: since_last_seconds)
let since_last_seconds = oldHeartbeat != nil ? heartbeat.timestamp.timeIntervalSince(oldHeartbeat!.timestamp) : 0
try await sendHeartbeatSingle(heartbeat, pulsetime: since_last_seconds + 1)
} catch {
log("Failed to send heartbeat: \(error)")
return
Expand Down Expand Up @@ -267,6 +270,7 @@ func sendHeartbeatSingle(_ heartbeat: Heartbeat, pulsetime: Double) async throws
class MainThing {
var observer: AXObserver?
var oldWindow: AXUIElement?
var pollingTimer: Timer?

// list of chrome equivalent browsers
let CHROME_BROWSERS = [
Expand All @@ -276,6 +280,29 @@ class MainThing {
"Brave Browser",
]

@objc func pollActiveWindow() {
debug("Polling active window")

guard let frontmost = NSWorkspace.shared.frontmostApplication else {
log("Failed to get frontmost application from polling")
return
}

let pid = frontmost.processIdentifier
let focusedApp = AXUIElementCreateApplication(pid)

var focusedWindow: AnyObject?
AXUIElementCopyAttributeValue(focusedApp, kAXFocusedWindowAttribute as CFString, &focusedWindow)

if focusedWindow != nil {
focusedWindowChanged(observer!, window: focusedWindow as! AXUIElement)
}
}

deinit {
pollingTimer?.invalidate()
}

func windowTitleChanged(
_ axObserver: AXObserver,
axElement: AXUIElement,
Expand Down

0 comments on commit af38b35

Please sign in to comment.