Skip to content

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

License

Notifications You must be signed in to change notification settings

Swifteroid/Observatory

Repository files navigation

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.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published