Swiftpack.co - Package - Swifteroid/Observatory

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.

Swifteroid/Observatory

Cocoa and Carbon event, notification and hotkey observing framework 🔭 in pure Swift 💯

Observatory

Observatory is a micro-framework for easier event, notification and hotkey management in Swift.

  • Standardised approach for event, notification and hotkey observing.
  • Simple enabling and disabling of observers.
  • Rich choice of handler signatures.
  • Handle local / global / both events.
  • Chaining support.

Observe global hotkeys.

let observer: HotkeyObserver = HotkeyObserver(active: true)
let fooHotkey: KeyboardHotkey = KeyboardHotkey(key: KeyboardKey.five, modifier: [.commandKey, .shiftKey])
let barHotkey: KeyboardHotkey = KeyboardHotkey(key: KeyboardKey.six, modifier: [.commandKey, .shiftKey])

observer
    .add(hotkey: fooHotkey) { Swift.print("Such foo…") }
    .add(hotkey: barHotkey) { Swift.print("So bar…") }

Observe notifications, chose between plain () -> () or standard (Notification) -> () signatures.

let observer: NotificationObserver = NotificationObserver(active: true)
let observee: AnyObject = NSObject()

observer
    .add(name: Notification.Name("foo"), observee: observee) { Swift.print("Foo captain!") }
    .add(names: [Notification.Name("bar"), Notification.Name("baz")], observee: observee) { Swift.print("Yes \($0.name)!") }

Observe events, like with notifications, you can chose between plain () -> () and standard local (NSEvent) -> NSEvent? or global (NSEvent) -> () signatures.

let observer: EventObserver = EventObserver(active: true)

observer
    .add(mask: .any, handler: { Swift.print("Any is better than none.") })
    .add(mask: [.leftMouseDown, .leftMouseUp], handler: { Swift.print("It's a \($0.type) event!") })

Checkout Observatory.playground for information and examples.

Github

link
Stars: 7
Last commit: 1 week ago

Dependencies

Releases

- 2020-07-14T17:23:22

This is a breaking change release:

  • HotkeyCommand and HotkeyCenter was replaced with Shortcut and ShortcutCenter, see unit tests for details and examples.
  • Renamed error enums.