Swiftpack.co - Swifteroid/Observatory as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by Swifteroid.
Swifteroid/Observatory 0.7.3
Cocoa and Carbon event, notification and hotkey observing framework πŸ”­ in pure Swift πŸ’―
⭐️ 12
πŸ•“ 1 year ago
macOS linux macOS iOS
.package(url: "https://github.com/Swifteroid/Observatory.git", from: "0.7.3")

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: 12
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Dependencies

Release Notes

3 years ago

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.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics