From 986934405efbb62eb7e5059a22bba5202b2839bd Mon Sep 17 00:00:00 2001 From: jasonm23 Date: Thu, 5 Oct 2023 14:48:30 +0700 Subject: [PATCH] Allow test/mocking of extension methods To facilitate tests/mocking, magnet should not use the `final` modifier. This PR only removes the `final` keyword, and nothing else. --- Lib/Magnet/HotKey.swift | 8 ++++---- Lib/Magnet/HotKeyCenter.swift | 10 +++++----- Lib/Magnet/KeyCombo.swift | 4 ++-- Lib/Magnet/ModifierEventHandler.swift | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Lib/Magnet/HotKey.swift b/Lib/Magnet/HotKey.swift index b6ef931..5fc4250 100644 --- a/Lib/Magnet/HotKey.swift +++ b/Lib/Magnet/HotKey.swift @@ -11,7 +11,7 @@ import Cocoa import Carbon -public final class HotKey: NSObject { +open class HotKey: NSObject { // MARK: - Properties public let identifier: String @@ -65,7 +65,7 @@ public final class HotKey: NSObject { } // MARK: - Invoke -public extension HotKey { +open extension HotKey { func invoke() { guard let callback = self.callback else { guard let target = self.target as? NSObject, let selector = self.action else { return } @@ -84,7 +84,7 @@ public extension HotKey { } // MARK: - Register & UnRegister -public extension HotKey { +open extension HotKey { @discardableResult func register() -> Bool { return HotKeyCenter.shared.register(with: self) @@ -96,7 +96,7 @@ public extension HotKey { } // MARK: - override isEqual -public extension HotKey { +open extension HotKey { override func isEqual(_ object: Any?) -> Bool { guard let hotKey = object as? HotKey else { return false } diff --git a/Lib/Magnet/HotKeyCenter.swift b/Lib/Magnet/HotKeyCenter.swift index a4d8d8c..aef180c 100644 --- a/Lib/Magnet/HotKeyCenter.swift +++ b/Lib/Magnet/HotKeyCenter.swift @@ -11,7 +11,7 @@ import Cocoa import Carbon -public final class HotKeyCenter { +open class HotKeyCenter { // MARK: - Properties public static let shared = HotKeyCenter() @@ -37,7 +37,7 @@ public final class HotKeyCenter { } // MARK: - Register & Unregister -public extension HotKeyCenter { +open extension HotKeyCenter { @discardableResult func register(with hotKey: HotKey) -> Bool { guard !hotKeys.keys.contains(hotKey.identifier) else { return false } @@ -100,7 +100,7 @@ public extension HotKeyCenter { } // MARK: - Terminate -extension HotKeyCenter { +open extension HotKeyCenter { private func observeApplicationTerminate() { notificationCenter.addObserver(self, selector: #selector(HotKeyCenter.applicationWillTerminate), @@ -114,7 +114,7 @@ extension HotKeyCenter { } // MARK: - HotKey Events -private extension HotKeyCenter { +open extension HotKeyCenter { func installHotKeyPressedEventHandler() { var pressedEventType = EventTypeSpec() pressedEventType.eventClass = OSType(kEventClassKeyboard) @@ -151,7 +151,7 @@ private extension HotKeyCenter { } // MARK: - Double Tap Modifier Event -private extension HotKeyCenter { +open extension HotKeyCenter { func installModifiersChangedEventHandlerIfNeeded() { NSEvent.addGlobalMonitorForEvents(matching: .flagsChanged) { [weak self] event in self?.modifierEventHandler.handleModifiersEvent(with: event.modifierFlags, timestamp: event.timestamp) diff --git a/Lib/Magnet/KeyCombo.swift b/Lib/Magnet/KeyCombo.swift index 6e08fad..3f075b8 100644 --- a/Lib/Magnet/KeyCombo.swift +++ b/Lib/Magnet/KeyCombo.swift @@ -12,7 +12,7 @@ import Cocoa import Carbon import Sauce -public final class KeyCombo: NSObject, NSCopying, NSCoding, Codable { +open class KeyCombo: NSObject, NSCopying, NSCoding, Codable { // MARK: - Properties public let key: Key @@ -175,6 +175,6 @@ public final class KeyCombo: NSObject, NSCopying, NSCoding, Codable { } // MARK: - Error -public extension KeyCombo { +open extension KeyCombo { struct InitializeError: Error {} } diff --git a/Lib/Magnet/ModifierEventHandler.swift b/Lib/Magnet/ModifierEventHandler.swift index e568c6e..0bdd4e3 100644 --- a/Lib/Magnet/ModifierEventHandler.swift +++ b/Lib/Magnet/ModifierEventHandler.swift @@ -10,7 +10,7 @@ import Cocoa -public final class ModifierEventHandler { +open class ModifierEventHandler { // MARK: - Properties public var doubleTapped: ((NSEvent.ModifierFlags) -> Void)? @@ -30,7 +30,7 @@ public final class ModifierEventHandler { } // MARK: - Handling -public extension ModifierEventHandler { +open extension ModifierEventHandler { func handleModifiersEvent(with modifierFlags: NSEvent.ModifierFlags, timestamp: TimeInterval) { guard lastHandledEventTimestamp != timestamp else { return } lastHandledEventTimestamp = timestamp