mirror of
https://github.com/FBukovina/Atoll.git
synced 2026-10-27 23:24:32 +00:00
Add keyboard backlight adjustment
This commit is contained in:
parent
38d94e360e
commit
a62de8fbe8
5 changed files with 379 additions and 24 deletions
|
|
@ -0,0 +1,66 @@
|
|||
import Foundation
|
||||
|
||||
extension Notification.Name {
|
||||
static let keyboardBacklightDidChange = Notification.Name("DynamicIsland.keyboardBacklightDidChange")
|
||||
}
|
||||
|
||||
final class SystemKeyboardBacklightController {
|
||||
static let shared = SystemKeyboardBacklightController()
|
||||
|
||||
var onBacklightChange: ((Float) -> Void)?
|
||||
|
||||
private let workerQueue = DispatchQueue(label: "com.atoll.keyboardBacklight", qos: .userInitiated)
|
||||
private let notificationCenter = NotificationCenter.default
|
||||
private var isRunning = false
|
||||
|
||||
private init() {}
|
||||
|
||||
func start() {
|
||||
guard !isRunning else { return }
|
||||
isRunning = true
|
||||
notifyCurrentLevel()
|
||||
}
|
||||
|
||||
func stop() {
|
||||
guard isRunning else { return }
|
||||
isRunning = false
|
||||
}
|
||||
|
||||
func adjust(by delta: Float) {
|
||||
let target = currentLevel + delta
|
||||
setLevel(target)
|
||||
}
|
||||
|
||||
func setLevel(_ value: Float) {
|
||||
let clamped = max(0, min(1, value))
|
||||
workerQueue.async {
|
||||
do {
|
||||
try KeyboardBrightnessSensor.setLevel(clamped)
|
||||
let level = (try? KeyboardBrightnessSensor.currentLevel()) ?? clamped
|
||||
self.emitChange(level: level)
|
||||
} catch {
|
||||
NSLog("⚠️ Failed to set keyboard backlight: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var currentLevel: Float {
|
||||
(try? KeyboardBrightnessSensor.currentLevel()) ?? 0
|
||||
}
|
||||
|
||||
private func notifyCurrentLevel() {
|
||||
workerQueue.async {
|
||||
let level = (try? KeyboardBrightnessSensor.currentLevel()) ?? 0
|
||||
self.emitChange(level: level)
|
||||
}
|
||||
}
|
||||
|
||||
private func emitChange(level: Float) {
|
||||
guard isRunning else { return }
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
guard let self, self.isRunning else { return }
|
||||
self.onBacklightChange?(level)
|
||||
self.notificationCenter.post(name: .keyboardBacklightDidChange, object: nil, userInfo: ["value": level])
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue