Skip to content

Commit

Permalink
Remove final access modifier from public classes (#87)
Browse files Browse the repository at this point in the history
* 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.

* Fix extension access

* coder inits required

* HotKey isEqual must be public

* Testing - Danger - workflow_dispatch

---------

Co-authored-by: Econa77 <[email protected]>
  • Loading branch information
jasonm23 and Econa77 authored Oct 11, 2023
1 parent 87babf2 commit 6ab594e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/Danger.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
name: Danger

on:
pull_request:
push:
branches:
- master

jobs:
danger:
runs-on: macos-13
Expand Down
16 changes: 8 additions & 8 deletions Lib/Magnet/HotKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import Cocoa
import Carbon

public final class HotKey: NSObject {
open class HotKey: NSObject {

// MARK: - Properties
public let identifier: String
Expand Down Expand Up @@ -65,8 +65,8 @@ public final class HotKey: NSObject {
}

// MARK: - Invoke
public extension HotKey {
func invoke() {
extension HotKey {
public func invoke() {
guard let callback = self.callback else {
guard let target = self.target as? NSObject, let selector = self.action else { return }
guard target.responds(to: selector) else { return }
Expand All @@ -84,20 +84,20 @@ public extension HotKey {
}

// MARK: - Register & UnRegister
public extension HotKey {
extension HotKey {
@discardableResult
func register() -> Bool {
public func register() -> Bool {
return HotKeyCenter.shared.register(with: self)
}

func unregister() {
public func unregister() {
return HotKeyCenter.shared.unregister(with: self)
}
}

// MARK: - override isEqual
public extension HotKey {
override func isEqual(_ object: Any?) -> Bool {
extension HotKey {
override public func isEqual(_ object: Any?) -> Bool {
guard let hotKey = object as? HotKey else { return false }

return self.identifier == hotKey.identifier &&
Expand Down
22 changes: 11 additions & 11 deletions Lib/Magnet/HotKeyCenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import Cocoa
import Carbon

public final class HotKeyCenter {
open class HotKeyCenter {

// MARK: - Properties
public static let shared = HotKeyCenter()
Expand All @@ -37,9 +37,9 @@ public final class HotKeyCenter {
}

// MARK: - Register & Unregister
public extension HotKeyCenter {
extension HotKeyCenter {
@discardableResult
func register(with hotKey: HotKey) -> Bool {
public func register(with hotKey: HotKey) -> Bool {
guard !hotKeys.keys.contains(hotKey.identifier) else { return false }
guard !hotKeys.values.contains(hotKey) else { return false }

Expand Down Expand Up @@ -78,7 +78,7 @@ public extension HotKeyCenter {
return true
}

func unregister(with hotKey: HotKey) {
public func unregister(with hotKey: HotKey) {
if let carbonHotKey = hotKey.hotKeyRef {
UnregisterEventHotKey(carbonHotKey)
}
Expand All @@ -88,13 +88,13 @@ public extension HotKeyCenter {
}

@discardableResult
func unregisterHotKey(with identifier: String) -> Bool {
public func unregisterHotKey(with identifier: String) -> Bool {
guard let hotKey = hotKeys[identifier] else { return false }
unregister(with: hotKey)
return true
}

func unregisterAll() {
public func unregisterAll() {
hotKeys.forEach { unregister(with: $1) }
}
}
Expand All @@ -114,8 +114,8 @@ extension HotKeyCenter {
}

// MARK: - HotKey Events
private extension HotKeyCenter {
func installHotKeyPressedEventHandler() {
extension HotKeyCenter {
public func installHotKeyPressedEventHandler() {
var pressedEventType = EventTypeSpec()
pressedEventType.eventClass = OSType(kEventClassKeyboard)
pressedEventType.eventKind = OSType(kEventHotKeyPressed)
Expand All @@ -124,7 +124,7 @@ private extension HotKeyCenter {
}, 1, &pressedEventType, nil, nil)
}

func sendPressedKeyboardEvent(_ event: EventRef) -> OSStatus {
public func sendPressedKeyboardEvent(_ event: EventRef) -> OSStatus {
assert(Int(GetEventClass(event)) == kEventClassKeyboard, "Unknown event class")

var hotKeyId = EventHotKeyID()
Expand All @@ -151,8 +151,8 @@ private extension HotKeyCenter {
}

// MARK: - Double Tap Modifier Event
private extension HotKeyCenter {
func installModifiersChangedEventHandlerIfNeeded() {
extension HotKeyCenter {
public func installModifiersChangedEventHandlerIfNeeded() {
NSEvent.addGlobalMonitorForEvents(matching: .flagsChanged) { [weak self] event in
self?.modifierEventHandler.handleModifiersEvent(with: event.modifierFlags, timestamp: event.timestamp)
}
Expand Down
10 changes: 5 additions & 5 deletions Lib/Magnet/KeyCombo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -92,7 +92,7 @@ public final class KeyCombo: NSObject, NSCopying, NSCoding, Codable {
}

// MARK: - NSCoding
public init?(coder aDecoder: NSCoder) {
public required init?(coder aDecoder: NSCoder) {
self.doubledModifiers = aDecoder.decodeBool(forKey: CodingKeys.doubledModifiers.rawValue)
self.modifiers = aDecoder.decodeInteger(forKey: CodingKeys.modifiers.rawValue)
guard !doubledModifiers else {
Expand Down Expand Up @@ -124,7 +124,7 @@ public final class KeyCombo: NSObject, NSCopying, NSCoding, Codable {
}

// MARK: - Codable
public init(from decoder: Decoder) throws {
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.doubledModifiers = try container.decode(Bool.self, forKey: .doubledModifiers)
self.modifiers = try container.decode(Int.self, forKey: .modifiers)
Expand Down Expand Up @@ -175,6 +175,6 @@ public final class KeyCombo: NSObject, NSCopying, NSCoding, Codable {
}

// MARK: - Error
public extension KeyCombo {
struct InitializeError: Error {}
extension KeyCombo {
public struct InitializeError: Error {}
}
6 changes: 3 additions & 3 deletions Lib/Magnet/ModifierEventHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import Cocoa

public final class ModifierEventHandler {
open class ModifierEventHandler {

// MARK: - Properties
public var doubleTapped: ((NSEvent.ModifierFlags) -> Void)?
Expand All @@ -30,8 +30,8 @@ public final class ModifierEventHandler {
}

// MARK: - Handling
public extension ModifierEventHandler {
func handleModifiersEvent(with modifierFlags: NSEvent.ModifierFlags, timestamp: TimeInterval) {
extension ModifierEventHandler {
public func handleModifiersEvent(with modifierFlags: NSEvent.ModifierFlags, timestamp: TimeInterval) {
guard lastHandledEventTimestamp != timestamp else { return }
lastHandledEventTimestamp = timestamp

Expand Down

0 comments on commit 6ab594e

Please sign in to comment.