11import Cocoa
22
33class KeyboardShortcutRecorderView : NSView {
4- private let textField = NSTextField ( labelWithString: " Click to record shortcut " )
4+ private let instructionLabel = NSTextField ( labelWithString: " Enter Key Combination " )
5+ private let valueLabel = NSTextField ( labelWithString: " waiting... " )
56 private var isRecording = false
67 private let recorder = KeyboardShortcutRecorder ( )
78
8- // Add the callback property
9+ // Add the callback properties
910 @objc var onShortcutChange : RCTBubblingEventBlock ?
11+ @objc var onCancel : RCTBubblingEventBlock ?
1012
1113 override init ( frame frameRect: NSRect ) {
1214 super. init ( frame: frameRect)
@@ -21,37 +23,57 @@ class KeyboardShortcutRecorderView: NSView {
2123 private func setupView( ) {
2224 wantsLayer = true
2325 layer? . backgroundColor = NSColor . windowBackgroundColor. cgColor
24- layer? . cornerRadius = 6
25-
26- textField. alignment = . center
27- textField. translatesAutoresizingMaskIntoConstraints = false
28- addSubview ( textField)
26+ layer? . cornerRadius = 10
27+ layer? . shadowColor = NSColor . black. cgColor
28+ layer? . shadowOpacity = 0.08
29+ layer? . shadowRadius = 4
30+ layer? . shadowOffset = CGSize ( width: 0 , height: 2 )
31+
32+ instructionLabel. alignment = . center
33+ instructionLabel. font = NSFont . boldSystemFont ( ofSize: 13 )
34+ instructionLabel. translatesAutoresizingMaskIntoConstraints = false
35+ addSubview ( instructionLabel)
36+
37+ valueLabel. alignment = . center
38+ valueLabel. font = NSFont . monospacedSystemFont ( ofSize: 13 , weight: . medium)
39+ valueLabel. textColor = NSColor . labelColor
40+ valueLabel. backgroundColor = NSColor . controlBackgroundColor
41+ valueLabel. wantsLayer = true
42+ valueLabel. translatesAutoresizingMaskIntoConstraints = false
43+ valueLabel. drawsBackground = true
44+ addSubview ( valueLabel)
2945
3046 NSLayoutConstraint . activate ( [
31- textField. leadingAnchor. constraint ( equalTo: leadingAnchor, constant: 8 ) ,
32- textField. trailingAnchor. constraint ( equalTo: trailingAnchor, constant: - 8 ) ,
33- textField. centerYAnchor. constraint ( equalTo: centerYAnchor) ,
47+ instructionLabel. topAnchor. constraint ( equalTo: topAnchor, constant: 18 ) ,
48+ instructionLabel. leadingAnchor. constraint ( equalTo: leadingAnchor, constant: 18 ) ,
49+ instructionLabel. trailingAnchor. constraint ( equalTo: trailingAnchor, constant: - 18 ) ,
50+
51+ valueLabel. topAnchor. constraint ( equalTo: instructionLabel. bottomAnchor, constant: 10 ) ,
52+ valueLabel. leadingAnchor. constraint ( equalTo: leadingAnchor, constant: 18 ) ,
53+ valueLabel. trailingAnchor. constraint ( equalTo: trailingAnchor, constant: - 18 ) ,
54+ valueLabel. bottomAnchor. constraint ( equalTo: bottomAnchor, constant: - 18 ) ,
3455 ] )
3556
36- // let clickGesture = NSClickGestureRecognizer(target: self, action: #selector(toggleRecording))
37- // addGestureRecognizer(clickGesture)
38-
3957 recorder. onShortcut = { [ weak self] keys in
40- self ? . textField. stringValue = keys. joined ( separator: " + " )
41- // self?.stopRecording()
42- // Call the callback with the shortcut
58+ self ? . valueLabel. stringValue = keys. joined ( separator: " + " )
4359 if let onShortcutChange = self ? . onShortcutChange {
4460 onShortcutChange ( [
4561 " shortcut " : keys
4662 ] )
4763 }
4864 }
4965
66+ recorder. onCancel = { [ weak self] in
67+ self ? . valueLabel. stringValue = " waiting... "
68+ if let onCancel = self ? . onCancel {
69+ onCancel ( [ : ] )
70+ }
71+ }
72+
5073 startRecording ( )
5174 }
5275
5376 deinit {
54- // CRITICAL: Stop recording when view is deallocated
5577 recorder. stopRecording ( )
5678 }
5779
@@ -63,7 +85,7 @@ class KeyboardShortcutRecorderView: NSView {
6385
6486 private func startRecording( ) {
6587 isRecording = true
66- textField . stringValue = " Recording ..."
88+ valueLabel . stringValue = " waiting ..."
6789 recorder. startRecording ( )
6890 }
6991
0 commit comments