Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012bGzgsxrmbBhDzeMV2L57D
77 lines
3.5 KiB
Swift
77 lines
3.5 KiB
Swift
import AppKit
|
||
import LichtkastenCore
|
||
|
||
/// Global key handling for culling (works regardless of SwiftUI focus, ignores text fields).
|
||
@MainActor
|
||
final class KeyboardMonitor {
|
||
private var monitor: Any?
|
||
weak var state: AppState?
|
||
|
||
func install(state: AppState) {
|
||
self.state = state
|
||
guard monitor == nil else { return }
|
||
monitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self] event in
|
||
guard let self, let state = self.state else { return event }
|
||
return self.handle(event, state: state) ? nil : event
|
||
}
|
||
}
|
||
|
||
/// Only handle keys in the browser window and never while typing in a text field.
|
||
private func isTyping(_ event: NSEvent, state: AppState) -> Bool {
|
||
guard let window = event.window, window === state.browserWindow else { return true }
|
||
if window.attachedSheet != nil { return true }
|
||
return window.firstResponder is NSText
|
||
}
|
||
|
||
private func handle(_ event: NSEvent, state: AppState) -> Bool {
|
||
if isTyping(event, state: state) { return false }
|
||
let mods = event.modifierFlags.intersection(.deviceIndependentFlagsMask)
|
||
let shift = mods.contains(.shift)
|
||
if mods.contains(.command) || mods.contains(.control) { return false }
|
||
|
||
switch event.keyCode {
|
||
case 123: state.moveCursor(by: -1, extend: shift); return true // ←
|
||
case 124: state.moveCursor(by: 1, extend: shift); return true // →
|
||
case 126: state.moveCursor(by: state.previewMode ? -1 : -state.gridColumns, extend: shift); return true // ↑
|
||
case 125: state.moveCursor(by: state.previewMode ? 1 : state.gridColumns, extend: shift); return true // ↓
|
||
case 49, 36, 76: state.previewMode.toggle(); return true // Space / Return
|
||
case 53: if state.previewMode { state.previewMode = false; return true }; return false // Esc
|
||
case 115: state.moveCursor(by: -100_000); return true // Home
|
||
case 119: state.moveCursor(by: 100_000); return true // End
|
||
default: break
|
||
}
|
||
|
||
guard let ch = event.charactersIgnoringModifiers?.lowercased(), let c = ch.first else { return false }
|
||
if c == "t" { state.toggleTag(); return true }
|
||
if c == "i" { state.openWindow("iptc"); return true }
|
||
if state.previewMode {
|
||
switch c {
|
||
case "z": state.zoom(state.zoom100 ? .fit : .actual); return true
|
||
case "+", "=": state.zoom(.zoomIn); return true
|
||
case "-": state.zoom(.zoomOut); return true
|
||
default: break
|
||
}
|
||
}
|
||
guard let digit = c.wholeNumberValue else { return false }
|
||
|
||
switch state.settings.keyScheme {
|
||
case .photoMechanic:
|
||
// PM: 1–8 = color class, 0 = none · ⌥1–5 = stars, ⌥0 = no stars
|
||
if mods.contains(.option) {
|
||
guard digit <= 5 else { return false }
|
||
state.setRating(digit)
|
||
} else if digit == 0 {
|
||
state.setLabel(nil)
|
||
} else if let l = ColorLabel(pmColorClass: digit) {
|
||
state.setLabel(l)
|
||
} else { return false }
|
||
case .lightroom:
|
||
if digit <= 5 { state.setRating(digit); return true }
|
||
let map: [Int: ColorLabel] = [6: .red, 7: .yellow, 8: .green, 9: .blue]
|
||
guard let l = map[digit] else { return false }
|
||
state.setLabel(l)
|
||
}
|
||
return true
|
||
}
|
||
}
|