Repository navigation
Expand file tree
/
Copy pathnotifications.lua
More file actions
320 lines (294 loc) · 13 KB
/
Copy pathnotifications.lua
File metadata and controls
320 lines (294 loc) · 13 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
-- plugins/notifications.lua
-- Notifications: cards stacked in a corner of the screen.
--
-- local notifications = plugin("@boring/notifications"):mount({
-- corner = "top-right", -- or top-left, bottom-right, bottom-left
-- width = 360,
-- timeout = 5000, -- ms, when the sender doesn't say
-- max_visible = 4,
-- history = 100, -- how many to remember
-- dbus = "sd-bus", -- force a D-Bus library (default: whichever works)
-- theme = { ... }, -- library/ui theme overrides
-- })
-- ["Mod4+n"] = notifications.actions.dismiss_all,
-- ["Mod4+Shift+n"] = notifications.actions.toggle_dnd, -- do not disturb
--
-- With `screenshots` (default true), saved screenshots show a card too.
--
-- Applications reach it over D-Bus (org.freedesktop.Notifications, e.g.
-- `notify-send`), through whichever D-Bus library is installed
-- (shared/dbus). Without one, or without a session bus, it warns and only
-- shows what plugins send with `notifications.notify({ title, body, ... })`.
--
-- Clicking a card runs its default action (or just dismisses it); its
-- buttons run the other actions. Critical notifications stay until
-- dismissed; with do not disturb on, others go straight to the history
-- (`notifications.history()`, also over ipc).
local ui = require("@boring/library/ui")
local animate = require("@boring/library/animate")
local desktop = require("@boring/library/desktop")
local scene = require("@boring/library/scene")
local dbus = require("shared.dbus")
local plugin = { name = "notifications" }
local PATH = "/org/freedesktop/Notifications"
local IFACE = "org.freedesktop.Notifications"
local EXPIRED, DISMISSED, CLOSED = 1, 2, 3
-- Notification bodies may carry a little markup; cards show plain text.
local function plain(s)
s = (s or ""):gsub("<[^>]*>", "")
s = s:gsub("<", "<"):gsub(">", ">"):gsub(""", '"'):gsub("'", "'"):gsub("&", "&")
return s
end
local function icon_path(name, size)
if not name or name == "" then return nil end
name = name:gsub("^file://", "")
return desktop.icon(name, size)
end
function plugin:mount(config, ctx)
local theme = ui.theme(config.theme)
local width = config.width or 360
local corner = config.corner or "top-right"
local default_timeout = config.timeout or 5000
local max_visible = config.max_visible or 4
local keep = config.history or 100
local margin, gap = 12, 10
ctx.state.next_id = ctx.state.next_id or 1
ctx.state.history = ctx.state.history or {}
local history = ctx.state.history
local shown = {} -- cards on screen, newest first
local by_id = {} -- id -> card
local dnd = ctx.state.dnd or false
local bus = nil
local function output()
local w = boring.focused()
if w then
local b = w:bounds()
local o = boring.output_at(b.x + b.width / 2, b.y + b.height / 2)
if o then return o end
end
return boring.output_at(boring.cursor()) or boring.outputs()[1]
end
-- Where each card goes, newest nearest the corner.
local function layout()
local o = output()
if not o then return end
local area = o.usable
local right = corner:find("right") ~= nil
local bottom = corner:find("bottom") ~= nil
local x = right and area.x + area.width - width - margin or area.x + margin
local offset = margin
for i, card in ipairs(shown) do
local visible = i <= max_visible
local y = bottom and area.y + area.height - offset - card.view.height or area.y + offset
card.target = { x, y }
card.offscreen = { right and area.x + area.width + 20 or area.x - width - 20, y }
if visible then offset = offset + card.view.height + gap end
if card.position then
animate.to(card.position, visible and card.target or card.offscreen, { duration = 220 })
else
card.position = animate.bind(nil, animate.ease({ from = card.offscreen, to = card.target,
duration = 260, easing = "out-back" }), function(p)
if card.view.tree then card.view.tree:set_position(p[1], p[2]) end
end)
end
end
end
local close
local function remember(n)
table.insert(history, 1, n)
while #history > keep do table.remove(history) end
end
local function invoke(card, key)
if bus then bus:emit(PATH, IFACE, "ActionInvoked", "us", card.n.id, key) end
if card.n.on_action then
local ok, err = xpcall(card.n.on_action, debug.traceback, key)
if not ok then boring.log.error("notifications: %s", tostring(err)) end
end
close(card.n.id, DISMISSED)
end
local function render(card)
local n = card.n
local icon = icon_path(n.icon, 40)
local header = ui.row({
ui.text(n.app ~= "" and n.app or "Notification", { style = "muted", max_width = width - 140 }),
ui.text(os.date("%H:%M", n.time), { style = "muted" }),
}, { gap = 8 })
local text = { header, ui.text(n.title, { style = "title", max_width = width - 90 }) }
if n.body ~= "" then text[#text + 1] = ui.text(n.body, { wrap = width - 90 }) end
local buttons = {}
for _, a in ipairs(n.actions) do
if a.key ~= "default" then
buttons[#buttons + 1] = ui.button(a.label, function() invoke(card, a.key) end)
end
end
if #buttons > 0 then text[#text + 1] = ui.row(buttons, { gap = 6 }) end
local body = ui.column(text, { gap = 4, align = "start" })
local content = icon and ui.row({ ui.image(icon, { size = 40 }), body }, { gap = 12, align = "start" }) or body
return ui.box(content, {
width = width, padding = 14, radius = theme.radius + 4,
background = n.urgency == 2 and (config.critical_background or "#3b0d0df0") or theme.background,
border = theme.border,
})
end
-- Clicks: buttons first, then the card's default action. The view's
-- tree is new after every update, so this is redone then.
local function listen(card)
local n = card.n
if card.off_click then card.off_click() end
card.off_click = scene.on_click(card.view.tree, function(_, button)
local x, y = boring.cursor()
if button == 0x111 then return close(n.id, DISMISSED) end -- right click
if not card.view:click(x, y, button) then
local default = nil
for _, a in ipairs(n.actions) do if a.key == "default" then default = a end end
if default then invoke(card, "default") else close(n.id, DISMISSED) end
end
end)
end
local function refresh(card)
card.view:update()
listen(card)
local p = card.position and card.position.current
if p then card.view.tree:set_position(p[1], p[2]) end
end
local function show(n)
local card = { n = n }
card.view = ui.view({ theme = theme, layer = "overlay", render = function() return render(card) end,
place = function() return -10000, -10000 end })
listen(card)
by_id[n.id] = card
table.insert(shown, 1, card)
if n.timeout > 0 then
card.cancel_expiry = boring.after(n.timeout, function() close(n.id, EXPIRED) end)
end
layout()
end
-- Take a card off the screen (sliding out), and tell the sender.
close = function(id, reason)
local card = by_id[id]
if not card then return false end
by_id[id] = nil
for i, c in ipairs(shown) do
if c == card then table.remove(shown, i) break end
end
if card.cancel_expiry then card.cancel_expiry() end
card.off_click()
if card.position then animate.stop(card.position) end
local view = card.view
local from = card.target or { -10000, -10000 }
animate.run(nil, animate.ease({ from = 0, to = 1, duration = 180 }), function(p)
if view.tree then
view.tree:set_position(from[1] + (card.offscreen[1] - from[1]) * p, from[2])
view:set_opacity(1 - p)
end
end, function() view:close() end)
if bus then bus:emit(PATH, IFACE, "NotificationClosed", "uu", id, reason or CLOSED) end
layout()
return true
end
--- Show a notification: { title, body, app, icon, urgency (0-2),
--- timeout (ms; 0 = until dismissed), actions = { { key, label } },
--- replaces (an id), on_action = fn(key) }. Returns its id.
local function notify(opts)
local id = opts.replaces and opts.replaces > 0 and by_id[opts.replaces] and opts.replaces or nil
if not id then
id = ctx.state.next_id
ctx.state.next_id = id + 1
end
local urgency = opts.urgency or 1
local timeout = opts.timeout
if timeout == nil or timeout < 0 then timeout = default_timeout end
if urgency == 2 then timeout = 0 end
local n = {
id = id, app = opts.app or "", title = plain(opts.title), body = plain(opts.body),
icon = opts.icon, urgency = urgency, timeout = timeout, actions = opts.actions or {},
time = os.time(), on_action = opts.on_action,
}
remember(n)
local existing = by_id[id]
if existing then
existing.n = n
refresh(existing)
if existing.cancel_expiry then existing.cancel_expiry() end
existing.cancel_expiry = n.timeout > 0 and boring.after(n.timeout, function() close(id, EXPIRED) end) or nil
layout()
elseif not dnd or urgency == 2 then
show(n)
end
return id
end
-- D-Bus --------------------------------------------------------------------
local conn, err = dbus.session({ backend = config.dbus, watch = boring.on_readable })
if not conn then
boring.log.warn("notifications: D-Bus isn't available (%s); applications can't send notifications", err)
else
local ok, why = conn:own(IFACE)
if not ok then
boring.log.warn("notifications: can't own %s (%s); is another notification daemon running?", IFACE, why)
conn:close()
else
bus = conn
boring.log.info("notifications: on D-Bus through %s", conn.backend)
conn:export(PATH, IFACE, {
GetCapabilities = { "", "as", function()
return { "body", "actions", "icon-static", "persistence" }
end },
GetServerInformation = { "", "ssss", function()
return "boring", "boring", "1", "1.2"
end },
CloseNotification = { "u", "", function(id) close(id, CLOSED) end },
Notify = { "susssasa{sv}i", "u", function(app, replaces, icon, summary, body, actions, hints, timeout)
local list = {}
for i = 1, #actions - 1, 2 do list[#list + 1] = { key = actions[i], label = actions[i + 1] } end
hints = hints or {}
local hint_icon = hints["image-path"] or hints["image_path"]
if (not icon or icon == "") and hints["desktop-entry"] then icon = hints["desktop-entry"] end
return notify({
app = app, replaces = replaces, icon = hint_icon or icon, title = summary, body = body,
actions = list, urgency = tonumber(hints.urgency) or 1, timeout = timeout,
})
end },
})
end
end
-- Saved screenshots, with the picture as the icon.
if config.screenshots ~= false then
ctx.scope:subscribe(boring.events, "screenshot:taken", function(path)
notify({ app = "Screenshot", title = path and "Screenshot saved" or "Screenshot copied",
body = path or "", icon = path, timeout = 3000 })
end)
end
ctx.scope:defer(function()
local ids = {}
for id in pairs(by_id) do ids[#ids + 1] = id end
for _, id in ipairs(ids) do close(id, CLOSED) end
if bus then bus:close() end
bus = nil
end)
local actions = {}
function actions.dismiss() if shown[1] then close(shown[1].n.id, DISMISSED) end end
function actions.dismiss_all()
local ids = {}
for id in pairs(by_id) do ids[#ids + 1] = id end
for _, id in ipairs(ids) do close(id, DISMISSED) end
end
function actions.toggle_dnd()
dnd = not dnd
ctx.state.dnd = dnd
if dnd then actions.dismiss_all() end
end
return {
actions = actions,
notify = notify,
close = function(id) return close(id, CLOSED) end,
history = function() return history end,
shown = function()
local out = {}
for i, c in ipairs(shown) do out[i] = c.n end
return out
end,
dnd = function() return dnd end,
backend = bus and bus.backend or nil,
}
end
return plugin