-
Notifications
You must be signed in to change notification settings - Fork 21
/
notifications.clj
241 lines (177 loc) · 11 KB
/
notifications.clj
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
(ns chromex.ext.notifications
"Use the chrome.notifications API to create rich notifications
using templates and show these notifications to users in the system tray.
* available since Chrome 36
* https://developer.chrome.com/extensions/notifications"
(:refer-clojure :only [defmacro defn apply declare meta let partial])
(:require [chromex.wrapgen :refer [gen-wrap-helper]]
[chromex.callgen :refer [gen-call-helper gen-tap-all-events-call]]))
(declare api-table)
(declare gen-call)
; -- functions --------------------------------------------------------------------------------------------------------------
(defmacro create
"Creates and displays a notification.
|notification-id| - Identifier of the notification. If not set or empty, an ID will automatically be generated. If it
matches an existing notification, this method first clears that notification before proceeding with
the create operation. The identifier may not be longer than 500 characters.The notificationId
parameter is required before Chrome 42.
|options| - Contents of the notification.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [notification-id] where:
|notification-id| - https://developer.chrome.com/extensions/notifications#property-callback-notificationId.
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/extensions/notifications#method-create."
([notification-id options] (gen-call :function ::create &form notification-id options)))
(defmacro update
"Updates an existing notification.
|notification-id| - The id of the notification to be updated. This is returned by 'notifications.create' method.
|options| - Contents of the notification to update to.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [was-updated] where:
|was-updated| - https://developer.chrome.com/extensions/notifications#property-callback-wasUpdated.
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/extensions/notifications#method-update."
([notification-id options] (gen-call :function ::update &form notification-id options)))
(defmacro clear
"Clears the specified notification.
|notification-id| - The id of the notification to be cleared. This is returned by 'notifications.create' method.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [was-cleared] where:
|was-cleared| - https://developer.chrome.com/extensions/notifications#property-callback-wasCleared.
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/extensions/notifications#method-clear."
([notification-id] (gen-call :function ::clear &form notification-id)))
(defmacro get-all
"Retrieves all the notifications of this app or extension.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [notifications] where:
|notifications| - https://developer.chrome.com/extensions/notifications#property-callback-notifications.
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/extensions/notifications#method-getAll."
([] (gen-call :function ::get-all &form)))
(defmacro get-permission-level
"Retrieves whether the user has enabled notifications from this app or extension.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [level] where:
|level| - https://developer.chrome.com/extensions/notifications#property-callback-level.
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/extensions/notifications#method-getPermissionLevel."
([] (gen-call :function ::get-permission-level &form)))
; -- events -----------------------------------------------------------------------------------------------------------------
;
; docs: https://github.com/binaryage/chromex/#tapping-events
(defmacro tap-on-closed-events
"The notification closed, either by the system or by user action.
Events will be put on the |channel| with signature [::on-closed [notification-id by-user]] where:
|notification-id| - https://developer.chrome.com/extensions/notifications#property-onClosed-notificationId.
|by-user| - https://developer.chrome.com/extensions/notifications#property-onClosed-byUser.
Note: |args| will be passed as additional parameters into Chrome event's .addListener call.
https://developer.chrome.com/extensions/notifications#event-onClosed."
([channel & args] (apply gen-call :event ::on-closed &form channel args)))
(defmacro tap-on-clicked-events
"The user clicked in a non-button area of the notification.
Events will be put on the |channel| with signature [::on-clicked [notification-id]] where:
|notification-id| - https://developer.chrome.com/extensions/notifications#property-onClicked-notificationId.
Note: |args| will be passed as additional parameters into Chrome event's .addListener call.
https://developer.chrome.com/extensions/notifications#event-onClicked."
([channel & args] (apply gen-call :event ::on-clicked &form channel args)))
(defmacro tap-on-button-clicked-events
"The user pressed a button in the notification.
Events will be put on the |channel| with signature [::on-button-clicked [notification-id button-index]] where:
|notification-id| - https://developer.chrome.com/extensions/notifications#property-onButtonClicked-notificationId.
|button-index| - https://developer.chrome.com/extensions/notifications#property-onButtonClicked-buttonIndex.
Note: |args| will be passed as additional parameters into Chrome event's .addListener call.
https://developer.chrome.com/extensions/notifications#event-onButtonClicked."
([channel & args] (apply gen-call :event ::on-button-clicked &form channel args)))
(defmacro tap-on-permission-level-changed-events
"The user changes the permission level. As of Chrome 47, only ChromeOS has UI that dispatches this event.
Events will be put on the |channel| with signature [::on-permission-level-changed [level]] where:
|level| - https://developer.chrome.com/extensions/notifications#property-onPermissionLevelChanged-level.
Note: |args| will be passed as additional parameters into Chrome event's .addListener call.
https://developer.chrome.com/extensions/notifications#event-onPermissionLevelChanged."
([channel & args] (apply gen-call :event ::on-permission-level-changed &form channel args)))
(defmacro tap-on-show-settings-events
"The user clicked on a link for the app's notification settings. As of Chrome 47, only ChromeOS has UI that dispatches this
event. As of Chrome 65, that UI has been removed from ChromeOS, too.
Events will be put on the |channel| with signature [::on-show-settings []].
Note: |args| will be passed as additional parameters into Chrome event's .addListener call.
https://developer.chrome.com/extensions/notifications#event-onShowSettings."
([channel & args] (apply gen-call :event ::on-show-settings &form channel args)))
; -- convenience ------------------------------------------------------------------------------------------------------------
(defmacro tap-all-events
"Taps all valid non-deprecated events in chromex.ext.notifications namespace."
[chan]
(gen-tap-all-events-call api-table (meta &form) chan))
; ---------------------------------------------------------------------------------------------------------------------------
; -- API TABLE --------------------------------------------------------------------------------------------------------------
; ---------------------------------------------------------------------------------------------------------------------------
(def api-table
{:namespace "chrome.notifications",
:since "36",
:functions
[{:id ::create,
:name "create",
:callback? true,
:params
[{:name "notification-id", :optional? true, :type "string"}
{:name "options", :type "notifications.NotificationOptions"}
{:name "callback",
:optional? true,
:type :callback,
:callback {:params [{:name "notification-id", :type "string"}]}}]}
{:id ::update,
:name "update",
:callback? true,
:params
[{:name "notification-id", :type "string"}
{:name "options", :type "notifications.NotificationOptions"}
{:name "callback",
:optional? true,
:type :callback,
:callback {:params [{:name "was-updated", :type "boolean"}]}}]}
{:id ::clear,
:name "clear",
:callback? true,
:params
[{:name "notification-id", :type "string"}
{:name "callback",
:optional? true,
:type :callback,
:callback {:params [{:name "was-cleared", :type "boolean"}]}}]}
{:id ::get-all,
:name "getAll",
:callback? true,
:params [{:name "callback", :type :callback, :callback {:params [{:name "notifications", :type "object"}]}}]}
{:id ::get-permission-level,
:name "getPermissionLevel",
:callback? true,
:params
[{:name "callback",
:type :callback,
:callback {:params [{:name "level", :type "notifications.PermissionLevel"}]}}]}],
:events
[{:id ::on-closed,
:name "onClosed",
:params [{:name "notification-id", :type "string"} {:name "by-user", :type "boolean"}]}
{:id ::on-clicked, :name "onClicked", :params [{:name "notification-id", :type "string"}]}
{:id ::on-button-clicked,
:name "onButtonClicked",
:params [{:name "notification-id", :type "string"} {:name "button-index", :type "integer"}]}
{:id ::on-permission-level-changed,
:name "onPermissionLevelChanged",
:params [{:name "level", :type "notifications.PermissionLevel"}]}
{:id ::on-show-settings,
:name "onShowSettings",
:since "65",
:deprecated "Custom notification settings button is no longer supported."}]})
; -- helpers ----------------------------------------------------------------------------------------------------------------
; code generation for native API wrapper
(defmacro gen-wrap [kind item-id config & args]
(apply gen-wrap-helper api-table kind item-id config args))
; code generation for API call-site
(def gen-call (partial gen-call-helper api-table))