Skip to content

Commit 32eb7f3

Browse files
feat: only jiggle after user idle for configured interval
Reset idle countdown on every user mouse movement. Jiggle fires only when mouse has been stationary for the full configured interval. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
1 parent 4ce6be9 commit 32eb7f3

1 file changed

Lines changed: 25 additions & 9 deletions

File tree

Sources/ScreenCursor/main.swift

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ final class SettingsStore {
203203
}
204204

205205
final class JiggleController {
206-
private var timer: Timer?
206+
private var pollTimer: Timer?
207+
private var lastMousePosition: NSPoint = .zero
208+
private var lastActivityAt: Date = Date()
207209

208210
init() {
209211
NotificationCenter.default.addObserver(
@@ -215,27 +217,41 @@ final class JiggleController {
215217
}
216218

217219
func start() {
220+
lastMousePosition = NSEvent.mouseLocation
221+
lastActivityAt = Date()
218222
applySettings()
219223
}
220224

221225
func stop() {
222-
timer?.invalidate()
223-
timer = nil
226+
pollTimer?.invalidate()
227+
pollTimer = nil
224228
}
225229

226230
@objc private func settingsChanged() {
227231
applySettings()
228232
}
229233

230234
private func applySettings() {
231-
timer?.invalidate()
232-
timer = nil
235+
pollTimer?.invalidate()
236+
pollTimer = nil
233237
guard SettingsStore.shared.jiggleEnabled else { return }
234-
let interval = TimeInterval(SettingsStore.shared.jiggleInterval)
235-
timer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { [weak self] _ in
236-
self?.performJiggle()
238+
pollTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { [weak self] _ in
239+
self?.checkAndJiggle()
237240
}
238-
RunLoop.main.add(timer!, forMode: .common)
241+
RunLoop.main.add(pollTimer!, forMode: .common)
242+
}
243+
244+
private func checkAndJiggle() {
245+
let current = NSEvent.mouseLocation
246+
if current != lastMousePosition {
247+
lastMousePosition = current
248+
lastActivityAt = Date()
249+
return
250+
}
251+
let idle = Date().timeIntervalSince(lastActivityAt)
252+
guard idle >= TimeInterval(SettingsStore.shared.jiggleInterval) else { return }
253+
lastActivityAt = Date()
254+
performJiggle()
239255
}
240256

241257
private func performJiggle() {

0 commit comments

Comments
 (0)