naughty: Add a destroy callback argument to naughty.notify

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2015-01-10 17:32:27 +01:00
parent d5088c5f6f
commit 509bbe7230
1 changed files with 21 additions and 14 deletions

View File

@ -95,7 +95,8 @@ local urgency = {
critical = "\2" critical = "\2"
} }
local notificationClosedReason = { naughty.notificationClosedReason = {
silent = -1,
expired = 1, expired = 1,
dismissedByUser = 2, dismissedByUser = 2,
dismissedByCommand = 3, dismissedByCommand = 3,
@ -220,8 +221,9 @@ end
--- Destroy notification by notification object --- Destroy notification by notification object
-- @param notification Notification object to be destroyed -- @param notification Notification object to be destroyed
-- @param reason One of the reasons from notificationClosedReason
-- @return True if the popup was successfully destroyed, nil otherwise -- @return True if the popup was successfully destroyed, nil otherwise
function naughty.destroy(notification) function naughty.destroy(notification, reason)
if notification and notification.box.visible then if notification and notification.box.visible then
if suspended then if suspended then
for k, v in pairs(naughty.notifications.suspended) do for k, v in pairs(naughty.notifications.suspended) do
@ -238,6 +240,9 @@ function naughty.destroy(notification)
end end
notification.box.visible = false notification.box.visible = false
arrange(scr) arrange(scr)
if notification.destroy_cb and reason ~= naughty.notificationClosedReason.silent then
notification.destroy_cb(reason or naughty.notificationClosedReason.undefined)
end
return true return true
end end
end end
@ -298,6 +303,7 @@ end
-- @param border_color Border color. -- @param border_color Border color.
-- Default: beautiful.border_focus or '#535d6c' -- Default: beautiful.border_focus or '#535d6c'
-- @param run Function to run on left click. Default: nil -- @param run Function to run on left click. Default: nil
-- @param destroy Function to run when notification is destroyed. Default: nil
-- @param preset Table with any of the above parameters. Note: Any parameters -- @param preset Table with any of the above parameters. Note: Any parameters
-- specified directly in args will override ones defined in the preset. -- specified directly in args will override ones defined in the preset.
-- @param replaces_id Replace the notification with the given ID -- @param replaces_id Replace the notification with the given ID
@ -331,6 +337,7 @@ function naughty.notify(args)
local border_width = args.border_width or preset.border_width local border_width = args.border_width or preset.border_width
local position = args.position or preset.position local position = args.position or preset.position
local actions = args.actions local actions = args.actions
local destroy_cb = args.destroy
local escape_pattern = "[<>&]" local escape_pattern = "[<>&]"
local escape_subs = { ['<'] = "&lt;", ['>'] = "&gt;", ['&'] = "&amp;" } local escape_subs = { ['<'] = "&lt;", ['>'] = "&gt;", ['&'] = "&amp;" }
@ -340,14 +347,14 @@ function naughty.notify(args)
local fg = args.fg or preset.fg or beautiful.fg_normal or '#ffffff' local fg = args.fg or preset.fg or beautiful.fg_normal or '#ffffff'
local bg = args.bg or preset.bg or beautiful.bg_normal or '#535d6c' local bg = args.bg or preset.bg or beautiful.bg_normal or '#535d6c'
local border_color = args.border_color or preset.border_color or beautiful.bg_focus or '#535d6c' local border_color = args.border_color or preset.border_color or beautiful.bg_focus or '#535d6c'
local notification = { screen = screen } local notification = { screen = screen, destroy_cb = destroy_cb }
-- replace notification if needed -- replace notification if needed
if args.replaces_id then if args.replaces_id then
local obj = getById(args.replaces_id) local obj = getById(args.replaces_id)
if obj then if obj then
-- destroy this and ... -- destroy this and ...
naughty.destroy(obj) naughty.destroy(obj, naughty.notificationClosedReason.silent)
end end
-- ... may use its ID -- ... may use its ID
if args.replaces_id <= counter then if args.replaces_id <= counter then
@ -368,12 +375,12 @@ function naughty.notify(args)
-- hook destroy -- hook destroy
local die = function (reason) local die = function (reason)
naughty.destroy(notification) naughty.destroy(notification, reason)
sendNotificationClosed(notification.id, reason) sendNotificationClosed(notification.id, reason)
end end
if timeout > 0 then if timeout > 0 then
local timer_die = timer { timeout = timeout } local timer_die = timer { timeout = timeout }
timer_die:connect_signal("timeout", function() die(notificationClosedReason.expired) end) timer_die:connect_signal("timeout", function() die(naughty.notificationClosedReason.expired) end)
if not suspended then if not suspended then
timer_die:start() timer_die:start()
end end
@ -389,17 +396,17 @@ function naughty.notify(args)
if args.run then if args.run then
args.run(notification) args.run(notification)
else else
die(notificationClosedReason.dismissedByUser) die(naughty.notificationClosedReason.dismissedByUser)
end end
end end
local hover_destroy = function () local hover_destroy = function ()
if hover_timeout == 0 then if hover_timeout == 0 then
die(notificationClosedReason.expired) die(naughty.notificationClosedReason.expired)
else else
if notification.timer then notification.timer:stop() end if notification.timer then notification.timer:stop() end
notification.timer = timer { timeout = hover_timeout } notification.timer = timer { timeout = hover_timeout }
notification.timer:connect_signal("timeout", function() die(notificationClosedReason.expired) end) notification.timer:connect_signal("timeout", function() die(naughty.notificationClosedReason.expired) end)
notification.timer:start() notification.timer:start()
end end
end end
@ -467,11 +474,11 @@ function naughty.notify(args)
actionmarginbox:buttons(util.table.join( actionmarginbox:buttons(util.table.join(
button({ }, 1, function() button({ }, 1, function()
sendActionInvoked(notification.id, actionid) sendActionInvoked(notification.id, actionid)
die(notificationClosedReason.dismissedByUser) die(naughty.notificationClosedReason.dismissedByUser)
end), end),
button({ }, 3, function() button({ }, 3, function()
sendActionInvoked(notification.id, actionid) sendActionInvoked(notification.id, actionid)
die(notificationClosedReason.dismissedByUser) die(naughty.notificationClosedReason.dismissedByUser)
end)) end))
) )
actionslayout:add(actionmarginbox) actionslayout:add(actionmarginbox)
@ -596,7 +603,7 @@ function naughty.notify(args)
-- Setup the mouse events -- Setup the mouse events
layout:buttons(util.table.join(button({ }, 1, run), layout:buttons(util.table.join(button({ }, 1, run),
button({ }, 3, function() button({ }, 3, function()
die(notificationClosedReason.dismissedByUser) die(naughty.notificationClosedReason.dismissedByUser)
end))) end)))
-- insert the notification to the table -- insert the notification to the table
@ -708,8 +715,8 @@ if capi.dbus then
elseif data.member == "CloseNotification" then elseif data.member == "CloseNotification" then
local obj = getById(appname) local obj = getById(appname)
if obj then if obj then
sendNotificationClosed(obj.id, notificationClosedReason.dismissedByCommand) sendNotificationClosed(obj.id, naughty.notificationClosedReason.dismissedByCommand)
naughty.destroy(obj) naughty.destroy(obj, naughty.notificationClosedReason.dismissedByCommand)
end end
elseif data.member == "GetServerInfo" or data.member == "GetServerInformation" then elseif data.member == "GetServerInfo" or data.member == "GetServerInformation" then
-- name of notification app, name of vender, version -- name of notification app, name of vender, version