/
osx_notify.rb
37 lines (28 loc) · 915 Bytes
/
osx_notify.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Yanked from http://github.com/evanphx/osx-notify/tree/master
module OSX
class Notify < OSX::NSObject
@@notifications = []
def self.on(name, &block)
notify = new()
@@notifications << notify
notify.name = name
notify.block = block
c = OSX::NSDistributedNotificationCenter.defaultCenter
c.addObserver_selector_name_object notify, "call:", name, nil
return notify
end
def self.send(name, opts)
c = OSX::NSDistributedNotificationCenter.defaultCenter
c.postNotificationName_object_userInfo_deliverImmediately_ name, nil, opts, true
end
attr_accessor :block, :name
def call(notification)
@block.call(notification.userInfo)
end
def delete!
c = OSX::NSDistributedNotificationCenter.defaultCenter
c.removeObserver_name_object_ self, @name, nil
@@notifications.delete self
end
end
end