Skip to content

Commit

Permalink
API changed for ease of use (#49)
Browse files Browse the repository at this point in the history
* API changed for ease of use

* Update README.md

* swiftlint
  • Loading branch information
Econa77 authored May 30, 2020
1 parent 416e4ea commit d1f7966
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 39 deletions.
27 changes: 14 additions & 13 deletions Example/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
recordView.delegate = self
recordView.clearButtonMode = .whenRecorded
restoreKeyCombo()
window?.makeFirstResponder(recordView)
recordView.beginRecording()
}

func applicationWillTerminate(_ aNotification: Notification) {
Expand All @@ -38,7 +38,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
guard let data = UserDefaults.standard.data(forKey: "keyCombo") else { return }
guard let keyCombo = try? JSONDecoder().decode(KeyCombo.self, from: data) else { return }
recordView.keyCombo = keyCombo
let hotKey = HotKey(identifier: "KeyHolderExample", keyCombo: keyCombo, target: self, action: #selector(AppDelegate.hotkeyCalled))
let hotKey = HotKey(identifier: "KeyHolderExample",
keyCombo: keyCombo,
target: self,
action: #selector(AppDelegate.hotkeyCalled))
hotKey.register()
}

Expand All @@ -60,20 +63,18 @@ extension AppDelegate: RecordViewDelegate {
return true
}

func recordViewDidClearShortcut(_ recordView: RecordView) {
print("clear shortcut")
storeKeyCombo(with: nil)
HotKeyCenter.shared.unregisterHotKey(with: "KeyHolderExample")
func recordView(_ recordView: RecordView, didChangeKeyCombo keyCombo: KeyCombo?) {
storeKeyCombo(with: keyCombo)
HotKeyCenter.shared.unregisterAll()
guard let keyCombo = keyCombo else { return }
let hotKey = HotKey(identifier: "KeyHolderExample",
keyCombo: keyCombo,
target: self,
action: #selector(AppDelegate.hotkeyCalled))
hotKey.register()
}

func recordViewDidEndRecording(_ recordView: RecordView) {
print("end recording")
}

func recordView(_ recordView: RecordView, didChangeKeyCombo keyCombo: KeyCombo) {
HotKeyCenter.shared.unregisterHotKey(with: "KeyHolderExample")
storeKeyCombo(with: keyCombo)
let hotKey = HotKey(identifier: "KeyHolderExample", keyCombo: keyCombo, target: self, action: #selector(AppDelegate.hotkeyCalled))
hotKey.register()
}
}
60 changes: 36 additions & 24 deletions Lib/KeyHolder/RecordView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import Sauce
public protocol RecordViewDelegate: AnyObject {
func recordViewShouldBeginRecording(_ recordView: RecordView) -> Bool
func recordView(_ recordView: RecordView, canRecordKeyCombo keyCombo: KeyCombo) -> Bool
func recordViewDidClearShortcut(_ recordView: RecordView)
func recordView(_ recordView: RecordView, didChangeKeyCombo keyCombo: KeyCombo)
func recordView(_ recordView: RecordView, didChangeKeyCombo keyCombo: KeyCombo?)
func recordViewDidEndRecording(_ recordView: RecordView)
}

Expand Down Expand Up @@ -78,6 +77,9 @@ open class RecordView: NSView {
private var marginX: CGFloat {
return marginY * 1.5
}
private var isFirstResponder: Bool {
return (isEnabled && window?.firstResponder == self && isRecording)
}

// MARK: - Override Properties
open override var isOpaque: Bool {
Expand All @@ -87,7 +89,7 @@ open class RecordView: NSView {
return true
}
open override var focusRingMaskBounds: NSRect {
return (isEnabled && window?.firstResponder == self && isRecording) ? bounds : NSRect.zero
return (isFirstResponder) ? bounds : NSRect.zero
}

// MARK: - Initialize
Expand Down Expand Up @@ -115,6 +117,7 @@ open class RecordView: NSView {
// Double Tap
modifierEventHandler.doubleTapped = { [weak self] modifierFlags in
guard let strongSelf = self else { return }
guard strongSelf.isFirstResponder else { return }
guard let keyCombo = KeyCombo(doubledCocoaModifiers: modifierFlags) else { return }
guard self?.delegate?.recordView(strongSelf, canRecordKeyCombo: keyCombo) ?? true else { return }
self?.keyCombo = keyCombo
Expand All @@ -126,7 +129,7 @@ open class RecordView: NSView {

// MARK: - Draw
open override func drawFocusRingMask() {
guard isEnabled && window?.firstResponder == self && isRecording else { return }
guard isFirstResponder else { return }
NSBezierPath(roundedRect: bounds, xRadius: cornerRadius, yRadius: cornerRadius).fill()
}

Expand Down Expand Up @@ -186,11 +189,11 @@ open class RecordView: NSView {
}

open override func becomeFirstResponder() -> Bool {
return beginRecording()
return focusView()
}

override open func resignFirstResponder() -> Bool {
endRecording()
unfocusView()
return super.resignFirstResponder()
}

Expand All @@ -204,9 +207,7 @@ open class RecordView: NSView {
}

override open func performKeyEquivalent(with theEvent: NSEvent) -> Bool {
guard isEnabled else { return false }
guard isRecording else { return false }
guard window?.firstResponder == self else { return false }
guard isFirstResponder else { return false }
guard let key = Sauce.shared.key(by: Int(theEvent.keyCode)) else { return false }

if theEvent.modifierFlags.carbonModifiers() != 0 {
Expand All @@ -231,8 +232,9 @@ open class RecordView: NSView {
}

override open func flagsChanged(with theEvent: NSEvent) {
guard isRecording else {
guard isFirstResponder else {
inputModifiers = NSEvent.ModifierFlags()
needsDisplay = true
super.flagsChanged(with: theEvent)
return
}
Expand Down Expand Up @@ -268,42 +270,52 @@ private extension RecordView {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byTruncatingTail
paragraphStyle.baseWritingDirection = .leftToRight
let textColor: NSColor
if !isEnabled {
textColor = .disabledControlTextColor
} else {
textColor = tintColor
}
return [.font: NSFont.systemFont(ofSize: floor(fontSize)),
.foregroundColor: tintColor,
.foregroundColor: textColor,
.paragraphStyle: paragraphStyle]
}
}

// MARK: - Recording
public extension RecordView {
extension RecordView {
@discardableResult
func beginRecording() -> Bool {
public func beginRecording() -> Bool {
guard let window = self.window else { return false }
guard isEnabled else { return false }
guard !isRecording else { return true }
guard window.firstResponder != self || !isRecording else { return true }
return window.makeFirstResponder(self)
}

needsDisplay = true
@discardableResult
public func endRecording() -> Bool {
guard let window = self.window else { return true }
guard window.firstResponder == self || isRecording else { return true }
return window.makeFirstResponder(nil)
}

private func focusView() -> Bool {
guard isEnabled else { return false }
if let delegate = delegate, !delegate.recordViewShouldBeginRecording(self) {
NSSound.beep()
return false
}

isRecording = true
needsDisplay = true
updateTrackingAreas()

return true
}

func endRecording() {
guard isRecording else { return }

private func unfocusView() {
inputModifiers = NSEvent.ModifierFlags()

isRecording = false
updateTrackingAreas()
needsDisplay = true

if window?.firstResponder == self && !canBecomeKeyView { window?.makeFirstResponder(nil) }
delegate?.recordViewDidEndRecording(self)
}
}
Expand All @@ -315,7 +327,7 @@ public extension RecordView {
inputModifiers = NSEvent.ModifierFlags()
needsDisplay = true
didChange?(nil)
delegate?.recordViewDidClearShortcut(self)
delegate?.recordView(self, didChangeKeyCombo: nil)
}

@objc func clearAndEndRecording() {
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ Some delegate methods
```swift
func recordViewShouldBeginRecording(_ recordView: RecordView) -> Bool
func recordView(_ recordView: RecordView, canRecordShortcut keyCombo: KeyCombo) -> Bool
func recordViewDidClearShortcut(_ recordView: RecordView)
func recordView(_ recordView: RecordView, didChangeKeyCombo keyCombo: KeyCombo)
func recordView(_ recordView: RecordView, didChangeKeyCombo keyCombo: KeyCombo?)
func recordViewDidEndRecording(_ recordView: RecordView)
```

Expand Down

0 comments on commit d1f7966

Please sign in to comment.