Add reset_timer and replace_text functions

Enables users to replace text and reset expiration timeouts of existing
notifications, which is necessary for OSD creation by means of naughty.

Signed-off-by: taptap <alexey.e.egorov@gmail.com>
This commit is contained in:
taptap 2015-02-27 23:45:03 +03:00 committed by Daniel Hahler
parent 98dd0d6b63
commit 5b81a0db60
1 changed files with 84 additions and 35 deletions

View File

@ -274,6 +274,85 @@ function naughty.getById(id)
end end
end end
--- Install expiration timer for notification object.
-- @tparam notification notification Notification object.
-- @tparam number timeout Time in seconds to be set as expiration timeout.
local function set_timeout(notification, timeout)
local die = function (reason)
naughty.destroy(notification, reason)
end
if timeout > 0 then
local timer_die = timer { timeout = timeout }
timer_die:connect_signal("timeout", function() die(naughty.notificationClosedReason.expired) end)
if not suspended then
timer_die:start()
end
notification.timer = timer_die
end
notification.die = die
end
--- Set new notification timeout.
-- @tparam notification notification Notification object, which timer is to be reset.
-- @tparam number new_timeout Time in seconds after which notification disappears.
-- @return None.
function naughty.reset_timeout(notification, new_timeout)
if notification.timer then notification.timer:stop() end
local timeout = timeout or notification.timeout
set_timeout(notification, timeout)
notification.timeout = timeout
notification.timer:start()
end
--- Escape and set title and text for notification object.
-- @tparam notification notification Notification object.
-- @tparam string title Title of notification.
-- @tparam string text Main text of notification.
-- @return None.
local function set_text(notification, title, text)
local escape_pattern = "[<>&]"
local escape_subs = { ['<'] = "&lt;", ['>'] = "&gt;", ['&'] = "&amp;" }
local textbox = notification.textbox
local function setMarkup(pattern, replacements)
textbox:set_markup(string.format('<b>%s</b>%s', title, text:gsub(pattern, replacements)))
end
local function setText()
textbox:set_text(string.format('%s %s', title, text))
end
-- Since the title cannot contain markup, it must be escaped first so that
-- it is not interpreted by Pango later.
title = title:gsub(escape_pattern, escape_subs)
-- Try to set the text while only interpreting <br>.
-- (Setting a textbox' .text to an invalid pattern throws a lua error)
if not pcall(setMarkup, "<br.->", "\n") then
-- That failed, escape everything which might cause an error from pango
if not pcall(setMarkup, escape_pattern, escape_subs) then
-- Ok, just ignore all pango markup. If this fails, we got some invalid utf8
if not pcall(setText) then
textbox:set_markup("<i>&lt;Invalid markup or UTF8, cannot display message&gt;</i>")
end
end
end
end
--- Replace title and text of an existing notification.
-- @tparam notification notification Notification object, which contents are to be replaced.
-- @tparam string new_title New title of notification. If not specified, old title remains unchanged.
-- @tparam string new_text New text of notification. If not specified, old text remains unchanged.
-- @return None.
function naughty.replace_text(notification, new_title, new_text)
local title = new_title
if title then title = title .. "\n" else title = "" end
set_text(notification, title, new_text)
end
--- Create a notification. --- Create a notification.
-- --
-- @tab args The argument table containing any of the arguments below. -- @tab args The argument table containing any of the arguments below.
@ -351,8 +430,6 @@ function naughty.notify(args)
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 destroy_cb = args.destroy
local escape_pattern = "[<>&]"
local escape_subs = { ['<'] = "&lt;", ['>'] = "&gt;", ['&'] = "&amp;" }
-- beautiful -- beautiful
local beautiful = bt.get() local beautiful = bt.get()
@ -360,7 +437,7 @@ 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, destroy_cb = destroy_cb } local notification = { screen = screen, destroy_cb = destroy_cb, timeout = timeout }
-- replace notification if needed -- replace notification if needed
if args.replaces_id then if args.replaces_id then
@ -387,18 +464,8 @@ function naughty.notify(args)
if title then title = title .. "\n" else title = "" end if title then title = title .. "\n" else title = "" end
-- hook destroy -- hook destroy
local die = function (reason) set_timeout(notification, timeout)
naughty.destroy(notification, reason) local die = notification.die
end
if timeout > 0 then
local timer_die = timer { timeout = timeout }
timer_die:connect_signal("timeout", function() die(naughty.notificationClosedReason.expired) end)
if not suspended then
timer_die:start()
end
notification.timer = timer_die
end
notification.die = die
local run = function () local run = function ()
if args.run then if args.run then
@ -427,27 +494,9 @@ function naughty.notify(args)
textbox:set_valign("middle") textbox:set_valign("middle")
textbox:set_font(font) textbox:set_font(font)
local function setMarkup(pattern, replacements) notification.textbox = textbox
textbox:set_markup(string.format('<b>%s</b>%s', title, text:gsub(pattern, replacements)))
end
local function setText()
textbox:set_text(string.format('%s %s', title, text))
end
-- Since the title cannot contain markup, it must be escaped first so that set_text(notification, title, text)
-- it is not interpreted by Pango later.
title = title:gsub(escape_pattern, escape_subs)
-- Try to set the text while only interpreting <br>.
-- (Setting a textbox' .text to an invalid pattern throws a lua error)
if not pcall(setMarkup, "<br.->", "\n") then
-- That failed, escape everything which might cause an error from pango
if not pcall(setMarkup, escape_pattern, escape_subs) then
-- Ok, just ignore all pango markup. If this fails, we got some invalid utf8
if not pcall(setText) then
textbox:set_markup("<i>&lt;Invalid markup or UTF8, cannot display message&gt;</i>")
end
end
end
local actionslayout = wibox.layout.fixed.vertical() local actionslayout = wibox.layout.fixed.vertical()
local actions_max_width = 0 local actions_max_width = 0