2008-10-17 22:38:01 +02:00
|
|
|
----------------------------------------------------------------------------
|
2014-05-19 13:28:57 +02:00
|
|
|
--- Notification library
|
|
|
|
--
|
2008-10-17 22:38:01 +02:00
|
|
|
-- @author koniu <gkusnierz@gmail.com>
|
|
|
|
-- @copyright 2008 koniu
|
|
|
|
-- @release @AWESOME_VERSION@
|
2014-05-20 23:39:31 +02:00
|
|
|
-- @module naughty
|
2008-10-17 22:38:01 +02:00
|
|
|
----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Package environment
|
|
|
|
local pairs = pairs
|
|
|
|
local table = table
|
2008-12-17 17:47:23 +01:00
|
|
|
local type = type
|
2008-10-17 22:38:01 +02:00
|
|
|
local string = string
|
2010-07-16 19:35:51 +02:00
|
|
|
local pcall = pcall
|
2009-05-08 09:01:14 +02:00
|
|
|
local capi = { screen = screen,
|
2012-05-27 19:20:34 +02:00
|
|
|
awesome = awesome }
|
2014-12-06 16:44:04 +01:00
|
|
|
local timer = require("gears.timer")
|
2009-04-17 18:08:52 +02:00
|
|
|
local button = require("awful.button")
|
2015-07-27 14:35:46 +02:00
|
|
|
local screen = require("awful.screen")
|
2009-04-17 18:08:52 +02:00
|
|
|
local util = require("awful.util")
|
2008-11-13 11:53:41 +01:00
|
|
|
local bt = require("beautiful")
|
2010-10-06 14:19:20 +02:00
|
|
|
local wibox = require("wibox")
|
2012-05-27 19:20:34 +02:00
|
|
|
local surface = require("gears.surface")
|
|
|
|
local cairo = require("lgi").cairo
|
2015-07-15 18:35:52 +02:00
|
|
|
local dpi = require("beautiful").xresources.apply_dpi
|
2008-10-17 22:38:01 +02:00
|
|
|
|
2016-02-26 18:30:42 +01:00
|
|
|
local function get_screen(s)
|
|
|
|
return s and capi.screen[s]
|
|
|
|
end
|
|
|
|
|
2012-06-12 03:48:41 +02:00
|
|
|
local naughty = {}
|
2008-10-17 22:38:01 +02:00
|
|
|
|
2014-05-26 21:43:39 +02:00
|
|
|
--[[--
|
|
|
|
Naughty configuration - a table containing common popup settings.
|
|
|
|
|
|
|
|
@table naughty.config
|
2015-07-15 18:35:52 +02:00
|
|
|
@tfield[opt=apply_dpi(4)] int padding Space between popups and edge of the
|
|
|
|
workarea.
|
|
|
|
@tfield[opt=apply_dpi(1)] int spacing Spacing between popups.
|
2015-07-27 15:54:47 +02:00
|
|
|
@tfield[opt={"/usr/share/pixmaps/"}] table icon_dirs List of directories
|
|
|
|
that will be checked by `getIcon()`.
|
|
|
|
@tfield[opt={ "png", "gif" }] table icon_formats List of formats that will be
|
|
|
|
checked by `getIcon()`.
|
|
|
|
@tfield[opt] function notify_callback Callback used to modify or reject
|
|
|
|
notifications, e.g.
|
2014-05-26 21:43:39 +02:00
|
|
|
naughty.config.notify_callback = function(args)
|
|
|
|
args.text = 'prefix: ' .. args.text
|
|
|
|
return args
|
|
|
|
end
|
|
|
|
|
2015-07-27 15:54:47 +02:00
|
|
|
@tfield table presets Notification presets. See `config.presets`.
|
|
|
|
|
|
|
|
@tfield table defaults Default values for the params to `notify()`. These can
|
|
|
|
optionally be overridden by specifying a preset. See `config.defaults`.
|
|
|
|
|
2014-05-26 21:43:39 +02:00
|
|
|
--]]
|
2014-05-20 23:39:31 +02:00
|
|
|
--
|
2014-05-25 14:25:01 +02:00
|
|
|
naughty.config = {
|
2015-07-15 18:35:52 +02:00
|
|
|
padding = dpi(4),
|
|
|
|
spacing = dpi(1),
|
2014-05-25 14:25:01 +02:00
|
|
|
icon_dirs = { "/usr/share/pixmaps/", },
|
|
|
|
icon_formats = { "png", "gif" },
|
|
|
|
notify_callback = nil,
|
|
|
|
}
|
|
|
|
|
2015-07-27 15:54:47 +02:00
|
|
|
--- Notification presets for `naughty.notify`.
|
|
|
|
-- This holds presets for different purposes. A preset is a table of any
|
|
|
|
-- parameters for `notify()`, overriding the default values
|
|
|
|
-- (`naughty.config.defaults`).
|
|
|
|
--
|
|
|
|
-- You have to pass a reference of a preset in your `notify()` as the `preset`
|
|
|
|
-- argument.
|
|
|
|
--
|
|
|
|
-- The presets `"low"`, `"normal"` and `"critical"` are used for notifications
|
|
|
|
-- over DBUS.
|
|
|
|
--
|
|
|
|
-- @table config.presets
|
|
|
|
-- @tfield table low The preset for notifications with low urgency level.
|
|
|
|
-- @tfield[opt=5] int low.timeout
|
|
|
|
-- @tfield[opt=empty] table normal The default preset for every notification without a
|
|
|
|
-- preset that will also be used for normal urgency level.
|
|
|
|
-- @tfield table critical The preset for notifications with a critical urgency
|
|
|
|
-- level.
|
|
|
|
-- @tfield[opt="#ff0000"] string critical.bg
|
|
|
|
-- @tfield[opt="#ffffff"] string critical.fg
|
|
|
|
-- @tfield[opt=0] string critical.timeout
|
2012-06-12 03:48:41 +02:00
|
|
|
naughty.config.presets = {
|
2008-11-27 10:07:07 +01:00
|
|
|
low = {
|
|
|
|
timeout = 5
|
|
|
|
},
|
2014-04-13 18:06:49 +02:00
|
|
|
normal = {},
|
2008-11-27 10:07:07 +01:00
|
|
|
critical = {
|
|
|
|
bg = "#ff0000",
|
|
|
|
fg = "#ffffff",
|
|
|
|
timeout = 0,
|
|
|
|
}
|
|
|
|
}
|
2008-10-17 22:38:01 +02:00
|
|
|
|
2015-07-27 15:54:47 +02:00
|
|
|
--- Defaults for `naughty.notify`.
|
|
|
|
--
|
|
|
|
-- @table config.defaults
|
|
|
|
-- @tfield[opt=5] int timeout
|
|
|
|
-- @tfield[opt=""] string text
|
|
|
|
-- @tfield[opt] int screen Defaults to `awful.screen.focused`.
|
|
|
|
-- @tfield[opt=true] boolean ontop
|
2015-07-15 18:35:52 +02:00
|
|
|
-- @tfield[opt=apply_dpi(5)] int margin
|
|
|
|
-- @tfield[opt=apply_dpi(1)] int border_width
|
2015-07-27 15:54:47 +02:00
|
|
|
-- @tfield[opt="top_right"] string position
|
2012-06-12 03:48:41 +02:00
|
|
|
naughty.config.defaults = {
|
2012-05-28 19:16:10 +02:00
|
|
|
timeout = 5,
|
|
|
|
text = "",
|
2015-07-27 15:54:47 +02:00
|
|
|
screen = nil,
|
2012-05-28 19:16:10 +02:00
|
|
|
ontop = true,
|
2015-07-15 18:35:52 +02:00
|
|
|
margin = dpi(5),
|
|
|
|
border_width = dpi(1),
|
2012-05-28 19:16:10 +02:00
|
|
|
position = "top_right"
|
|
|
|
}
|
2009-05-06 18:39:56 +02:00
|
|
|
|
2015-01-10 17:32:27 +01:00
|
|
|
naughty.notificationClosedReason = {
|
|
|
|
silent = -1,
|
2014-12-31 14:28:46 +01:00
|
|
|
expired = 1,
|
|
|
|
dismissedByUser = 2,
|
|
|
|
dismissedByCommand = 3,
|
|
|
|
undefined = 4
|
|
|
|
}
|
|
|
|
|
2008-12-11 13:53:24 +01:00
|
|
|
-- Counter for the notifications
|
|
|
|
-- Required for later access via DBUS
|
|
|
|
local counter = 1
|
|
|
|
|
2010-02-16 17:54:45 +01:00
|
|
|
-- True if notifying is suspended
|
|
|
|
local suspended = false
|
|
|
|
|
2014-05-20 23:39:31 +02:00
|
|
|
--- Index of notifications per screen and position.
|
2014-05-26 21:43:39 +02:00
|
|
|
-- See config table for valid 'position' values.
|
|
|
|
-- Each element is a table consisting of:
|
|
|
|
--
|
2008-10-17 22:38:01 +02:00
|
|
|
-- @field box Wibox object containing the popup
|
2008-11-15 17:55:38 +01:00
|
|
|
-- @field height Popup height
|
2008-11-19 03:16:44 +01:00
|
|
|
-- @field width Popup width
|
2008-11-15 17:55:38 +01:00
|
|
|
-- @field die Function to be executed on timeout
|
2008-11-22 15:46:51 +01:00
|
|
|
-- @field id Unique notification id based on a counter
|
2014-05-20 23:39:31 +02:00
|
|
|
-- @table notifications
|
2012-12-15 20:58:18 +01:00
|
|
|
naughty.notifications = { suspended = { } }
|
2016-04-05 06:01:31 +02:00
|
|
|
screen.connect_for_each_screen(function(s)
|
2016-03-26 18:36:04 +01:00
|
|
|
naughty.notifications[s] = {
|
2008-11-20 17:13:23 +01:00
|
|
|
top_left = {},
|
2015-02-21 23:37:30 +01:00
|
|
|
top_middle = {},
|
2008-11-20 17:13:23 +01:00
|
|
|
top_right = {},
|
|
|
|
bottom_left = {},
|
2015-02-21 23:37:30 +01:00
|
|
|
bottom_middle = {},
|
2008-11-20 17:13:23 +01:00
|
|
|
bottom_right = {},
|
|
|
|
}
|
2016-03-26 18:36:04 +01:00
|
|
|
end)
|
2008-10-17 22:38:01 +02:00
|
|
|
|
2016-05-05 16:46:40 +02:00
|
|
|
capi.screen.connect_signal("removed", function(scr)
|
|
|
|
-- Destroy all notifications on this screen
|
|
|
|
for _, list in pairs(naughty.notifications[scr]) do
|
|
|
|
while #list > 0 do
|
|
|
|
naughty.destroy(list[1])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
naughty.notifications[scr] = nil
|
|
|
|
end)
|
|
|
|
|
2015-05-13 05:36:05 +02:00
|
|
|
--- Notification state
|
|
|
|
function naughty.is_suspended()
|
|
|
|
return suspended
|
|
|
|
end
|
|
|
|
|
2010-02-16 17:54:45 +01:00
|
|
|
--- Suspend notifications
|
2012-06-12 03:48:41 +02:00
|
|
|
function naughty.suspend()
|
2010-02-16 17:54:45 +01:00
|
|
|
suspended = true
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Resume notifications
|
2012-06-12 03:48:41 +02:00
|
|
|
function naughty.resume()
|
2010-02-16 17:54:45 +01:00
|
|
|
suspended = false
|
2016-02-07 14:19:42 +01:00
|
|
|
for _, v in pairs(naughty.notifications.suspended) do
|
2010-02-16 17:54:45 +01:00
|
|
|
v.box.visible = true
|
|
|
|
if v.timer then v.timer:start() end
|
|
|
|
end
|
2012-12-15 20:58:18 +01:00
|
|
|
naughty.notifications.suspended = { }
|
2010-02-16 17:54:45 +01:00
|
|
|
end
|
|
|
|
|
2011-07-23 09:17:34 +02:00
|
|
|
--- Toggle notification state
|
2012-06-12 03:48:41 +02:00
|
|
|
function naughty.toggle()
|
2011-07-23 09:17:34 +02:00
|
|
|
if suspended then
|
2012-06-12 03:48:41 +02:00
|
|
|
naughty.resume()
|
2011-07-23 09:17:34 +02:00
|
|
|
else
|
2012-06-12 03:48:41 +02:00
|
|
|
naughty.suspend()
|
2011-07-23 09:17:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Evaluate desired position of the notification by index - internal
|
2014-05-26 21:43:39 +02:00
|
|
|
--
|
2016-02-07 14:19:42 +01:00
|
|
|
-- @param s Screen to use
|
2008-10-17 22:38:01 +02:00
|
|
|
-- @param position top_right | top_left | bottom_right | bottom_left
|
2015-10-14 00:21:29 +02:00
|
|
|
-- | top_middle | bottom_middle
|
2015-02-20 15:45:53 +01:00
|
|
|
-- @param idx Index of the notification
|
2015-02-26 22:06:34 +01:00
|
|
|
-- @param[opt] width Popup width.
|
2015-02-20 15:45:53 +01:00
|
|
|
-- @param height Popup height
|
2009-05-08 13:08:09 +02:00
|
|
|
-- @return Absolute position and index in { x = X, y = Y, idx = I } table
|
2016-02-07 14:19:42 +01:00
|
|
|
local function get_offset(s, position, idx, width, height)
|
2016-02-26 18:30:42 +01:00
|
|
|
s = get_screen(s)
|
|
|
|
local ws = s.workarea
|
2008-10-17 22:38:01 +02:00
|
|
|
local v = {}
|
2016-02-07 14:19:42 +01:00
|
|
|
idx = idx or #naughty.notifications[s][position] + 1
|
|
|
|
width = width or naughty.notifications[s][position][idx].width
|
2008-10-17 22:38:01 +02:00
|
|
|
|
|
|
|
-- calculate x
|
|
|
|
if position:match("left") then
|
2012-06-12 03:48:41 +02:00
|
|
|
v.x = ws.x + naughty.config.padding
|
2015-02-21 23:37:30 +01:00
|
|
|
elseif position:match("middle") then
|
|
|
|
v.x = (ws.width / 2) - (width / 2)
|
2008-10-17 22:38:01 +02:00
|
|
|
else
|
2012-06-12 03:48:41 +02:00
|
|
|
v.x = ws.x + ws.width - (width + naughty.config.padding)
|
2008-10-17 22:38:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- calculate existing popups' height
|
|
|
|
local existing = 0
|
|
|
|
for i = 1, idx-1, 1 do
|
2016-02-07 14:19:42 +01:00
|
|
|
existing = existing + naughty.notifications[s][position][i].height + naughty.config.spacing
|
2008-10-17 22:38:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- calculate y
|
|
|
|
if position:match("top") then
|
2012-06-12 03:48:41 +02:00
|
|
|
v.y = ws.y + naughty.config.padding + existing
|
2008-10-17 22:38:01 +02:00
|
|
|
else
|
2012-06-12 03:48:41 +02:00
|
|
|
v.y = ws.y + ws.height - (naughty.config.padding + height + existing)
|
2008-10-17 22:38:01 +02:00
|
|
|
end
|
|
|
|
|
2015-07-12 17:20:05 +02:00
|
|
|
-- Find old notification to replace in case there is not enough room.
|
|
|
|
-- This tries to skip permanent notifications (without a timeout),
|
|
|
|
-- e.g. critical ones.
|
|
|
|
local find_old_to_replace = function()
|
|
|
|
for i = 1, idx-1 do
|
2016-02-07 14:19:42 +01:00
|
|
|
local n = naughty.notifications[s][position][i]
|
2015-07-12 17:20:05 +02:00
|
|
|
if n.timeout > 0 then
|
|
|
|
return n
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- Fallback to first one.
|
2016-02-07 14:19:42 +01:00
|
|
|
return naughty.notifications[s][position][1]
|
2015-07-12 17:20:05 +02:00
|
|
|
end
|
|
|
|
|
2008-10-23 18:47:43 +02:00
|
|
|
-- if positioned outside workarea, destroy oldest popup and recalculate
|
2008-11-15 17:55:38 +01:00
|
|
|
if v.y + height > ws.y + ws.height or v.y < ws.y then
|
2015-07-12 17:20:05 +02:00
|
|
|
naughty.destroy(find_old_to_replace())
|
2008-10-23 18:47:43 +02:00
|
|
|
idx = idx - 1
|
2016-02-07 14:19:42 +01:00
|
|
|
v = get_offset(s, position, idx, width, height)
|
2008-10-23 18:47:43 +02:00
|
|
|
end
|
2009-05-08 13:08:09 +02:00
|
|
|
if not v.idx then v.idx = idx end
|
2008-10-23 18:47:43 +02:00
|
|
|
|
2008-10-17 22:38:01 +02:00
|
|
|
return v
|
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Re-arrange notifications according to their position and index - internal
|
2014-05-26 21:43:39 +02:00
|
|
|
--
|
2008-10-17 22:38:01 +02:00
|
|
|
-- @return None
|
2016-02-07 14:19:42 +01:00
|
|
|
local function arrange(s)
|
|
|
|
for p in pairs(naughty.notifications[s]) do
|
|
|
|
for i,notification in pairs(naughty.notifications[s][p]) do
|
|
|
|
local offset = get_offset(s, p, i, notification.width, notification.height)
|
2009-08-07 23:13:49 +02:00
|
|
|
notification.box:geometry({ x = offset.x, y = offset.y })
|
2009-05-08 13:08:09 +02:00
|
|
|
notification.idx = offset.idx
|
2008-10-17 22:38:01 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-16 20:16:17 +01:00
|
|
|
--- Destroy notification by notification object
|
2014-05-26 21:43:39 +02:00
|
|
|
--
|
2008-11-25 09:45:28 +01:00
|
|
|
-- @param notification Notification object to be destroyed
|
2015-01-10 17:32:27 +01:00
|
|
|
-- @param reason One of the reasons from notificationClosedReason
|
2008-10-17 22:38:01 +02:00
|
|
|
-- @return True if the popup was successfully destroyed, nil otherwise
|
2015-01-10 17:32:27 +01:00
|
|
|
function naughty.destroy(notification, reason)
|
2011-03-27 16:36:01 +02:00
|
|
|
if notification and notification.box.visible then
|
2010-02-16 17:54:45 +01:00
|
|
|
if suspended then
|
2012-12-15 20:58:18 +01:00
|
|
|
for k, v in pairs(naughty.notifications.suspended) do
|
2010-02-16 17:54:45 +01:00
|
|
|
if v.box == notification.box then
|
2012-12-15 20:58:18 +01:00
|
|
|
table.remove(naughty.notifications.suspended, k)
|
2010-02-16 17:54:45 +01:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-03-27 16:36:01 +02:00
|
|
|
local scr = notification.screen
|
2012-12-15 20:58:18 +01:00
|
|
|
table.remove(naughty.notifications[scr][notification.position], notification.idx)
|
2009-08-20 18:07:30 +02:00
|
|
|
if notification.timer then
|
|
|
|
notification.timer:stop()
|
|
|
|
end
|
2011-03-27 16:36:01 +02:00
|
|
|
notification.box.visible = false
|
2008-11-20 17:13:23 +01:00
|
|
|
arrange(scr)
|
2015-01-10 17:32:27 +01:00
|
|
|
if notification.destroy_cb and reason ~= naughty.notificationClosedReason.silent then
|
|
|
|
notification.destroy_cb(reason or naughty.notificationClosedReason.undefined)
|
|
|
|
end
|
2008-10-17 22:38:01 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Get notification by ID
|
2014-05-26 21:43:39 +02:00
|
|
|
--
|
2008-11-20 17:51:29 +01:00
|
|
|
-- @param id ID of the notification
|
|
|
|
-- @return notification object if it was found, nil otherwise
|
2015-01-10 21:06:30 +01:00
|
|
|
function naughty.getById(id)
|
2008-11-20 17:51:29 +01:00
|
|
|
-- iterate the notifications to get the notfications with the correct ID
|
2016-03-06 13:31:30 +01:00
|
|
|
for s in pairs(naughty.notifications) do
|
2016-02-07 14:19:42 +01:00
|
|
|
for p in pairs(naughty.notifications[s]) do
|
|
|
|
for _, notification in pairs(naughty.notifications[s][p]) do
|
2008-11-20 17:51:29 +01:00
|
|
|
if notification.id == id then
|
|
|
|
return notification
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-27 21:45:03 +01:00
|
|
|
--- 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
|
|
|
|
|
2016-02-07 14:19:12 +01:00
|
|
|
local timeout = new_timeout or notification.timeout
|
2015-02-27 21:45:03 +01:00
|
|
|
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 = { ['<'] = "<", ['>'] = ">", ['&'] = "&" }
|
|
|
|
|
|
|
|
local textbox = notification.textbox
|
|
|
|
|
|
|
|
local function setMarkup(pattern, replacements)
|
2016-01-17 16:51:07 +01:00
|
|
|
return textbox:set_markup_silently(string.format('<b>%s</b>%s', title, text:gsub(pattern, replacements)))
|
2015-02-27 21:45:03 +01:00
|
|
|
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>.
|
2016-01-17 16:51:07 +01:00
|
|
|
if not setMarkup("<br.->", "\n") then
|
2015-02-27 21:45:03 +01:00
|
|
|
-- That failed, escape everything which might cause an error from pango
|
2016-01-17 16:51:07 +01:00
|
|
|
if not setMarkup(escape_pattern, escape_subs) then
|
2015-02-27 21:45:03 +01:00
|
|
|
-- Ok, just ignore all pango markup. If this fails, we got some invalid utf8
|
|
|
|
if not pcall(setText) then
|
|
|
|
textbox:set_markup("<i><Invalid markup or UTF8, cannot display message></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
|
|
|
|
|
2014-05-26 21:43:39 +02:00
|
|
|
--- Create a notification.
|
2014-05-25 14:25:01 +02:00
|
|
|
--
|
2014-05-26 21:43:39 +02:00
|
|
|
-- @tab args The argument table containing any of the arguments below.
|
2015-07-27 15:54:47 +02:00
|
|
|
-- @string[opt=""] args.text Text of the notification.
|
|
|
|
-- @string[opt] args.title Title of the notification.
|
|
|
|
-- @int[opt=5] args.timeout Time in seconds after which popup expires.
|
2014-05-25 14:25:01 +02:00
|
|
|
-- Set 0 for no timeout.
|
2015-07-27 15:54:47 +02:00
|
|
|
-- @int[opt] args.hover_timeout Delay in seconds after which hovered popup disappears.
|
2016-02-26 18:30:42 +01:00
|
|
|
-- @tparam[opt=focused] integer|screen args.screen Target screen for the notification.
|
2015-07-27 15:54:47 +02:00
|
|
|
-- @string[opt="top_right"] args.position Corner of the workarea displaying the popups.
|
|
|
|
-- Values: `"top_right"`, `"top_left"`, `"bottom_left"`,
|
|
|
|
-- `"bottom_right"`, `"top_middle"`, `"bottom_middle"`.
|
|
|
|
-- @bool[opt=true] args.ontop Boolean forcing popups to display on top.
|
|
|
|
-- @int[opt=auto] args.height Popup height.
|
|
|
|
-- @int[opt=auto] args.width Popup width.
|
|
|
|
-- @string[opt=beautiful.font or awesome.font] args.font Notification font.
|
|
|
|
-- @string[opt] args.icon Path to icon.
|
|
|
|
-- @int[opt] args.icon_size Desired icon size in px.
|
|
|
|
-- @string[opt=`beautiful.fg_focus` or `'#ffffff'`] args.fg Foreground color.
|
|
|
|
-- @string[opt=`beautiful.bg_focus` or `'#535d6c'`] args.bg Background color.
|
|
|
|
-- @int[opt=1] args.border_width Border width.
|
|
|
|
-- @string[opt=`beautiful.border_focus` or `'#535d6c'`] args.border_color Border color.
|
2015-09-23 10:37:02 +02:00
|
|
|
-- @tparam[opt] func args.run Function to run on left click. The notification
|
|
|
|
-- object will be passed to it as an argument.
|
|
|
|
-- You need to call e.g.
|
|
|
|
-- `notification.die(naughty.notificationClosedReason.dismissedByUser)` from
|
|
|
|
-- there to dismiss the notification yourself.
|
2015-07-27 15:54:47 +02:00
|
|
|
-- @tparam[opt] func args.destroy Function to run when notification is destroyed.
|
|
|
|
-- @tparam[opt] table args.preset Table with any of the above parameters.
|
|
|
|
-- Note: Any parameters specified directly in args will override ones defined
|
|
|
|
-- in the preset.
|
|
|
|
-- @tparam[opt] int args.replaces_id Replace the notification with the given ID.
|
|
|
|
-- @tparam[opt] func args.callback Function that will be called with all arguments.
|
|
|
|
-- The notification will only be displayed if the function returns true.
|
|
|
|
-- Note: this function is only relevant to notifications sent via dbus.
|
|
|
|
-- @tparam[opt] table args.actions Mapping that maps a string to a callback when this
|
|
|
|
-- action is selected.
|
2009-05-06 18:39:56 +02:00
|
|
|
-- @usage naughty.notify({ title = "Achtung!", text = "You're idling", timeout = 0 })
|
2016-04-05 00:31:45 +02:00
|
|
|
-- @treturn ?table The notification object, or nil in case a notification was
|
|
|
|
-- not displayed.
|
2012-06-12 03:48:41 +02:00
|
|
|
function naughty.notify(args)
|
|
|
|
if naughty.config.notify_callback then
|
|
|
|
args = naughty.config.notify_callback(args)
|
2011-01-28 01:37:47 +01:00
|
|
|
if not args then return end
|
|
|
|
end
|
|
|
|
|
2008-11-15 17:55:38 +01:00
|
|
|
-- gather variables together
|
2012-06-12 03:48:41 +02:00
|
|
|
local preset = util.table.join(naughty.config.defaults or {},
|
|
|
|
args.preset or naughty.config.presets.normal or {})
|
2012-05-28 19:16:10 +02:00
|
|
|
local timeout = args.timeout or preset.timeout
|
2009-05-06 18:39:56 +02:00
|
|
|
local icon = args.icon or preset.icon
|
|
|
|
local icon_size = args.icon_size or preset.icon_size
|
2012-05-28 19:16:10 +02:00
|
|
|
local text = args.text or preset.text
|
2009-05-06 18:39:56 +02:00
|
|
|
local title = args.title or preset.title
|
2016-02-26 18:30:42 +01:00
|
|
|
local s = get_screen(args.screen or preset.screen or screen.focused())
|
2016-04-05 00:31:45 +02:00
|
|
|
if not s then
|
2016-04-17 15:21:02 +02:00
|
|
|
local err = "naughty.notify: there is no screen available to display the following notification:"
|
|
|
|
err = string.format("%s title='%s' text='%s'", err, tostring(title or ""), tostring(text or ""))
|
|
|
|
require("gears.debug").print_warning(err)
|
2016-04-05 00:31:45 +02:00
|
|
|
return
|
|
|
|
end
|
2012-05-28 19:16:10 +02:00
|
|
|
local ontop = args.ontop or preset.ontop
|
2009-05-06 18:39:56 +02:00
|
|
|
local width = args.width or preset.width
|
|
|
|
local height = args.height or preset.height
|
|
|
|
local hover_timeout = args.hover_timeout or preset.hover_timeout
|
|
|
|
local opacity = args.opacity or preset.opacity
|
2012-05-28 19:16:10 +02:00
|
|
|
local margin = args.margin or preset.margin
|
|
|
|
local border_width = args.border_width or preset.border_width
|
|
|
|
local position = args.position or preset.position
|
2014-12-31 14:28:46 +01:00
|
|
|
local actions = args.actions
|
2015-01-10 17:32:27 +01:00
|
|
|
local destroy_cb = args.destroy
|
2008-10-17 22:38:01 +02:00
|
|
|
|
2008-11-21 01:43:18 +01:00
|
|
|
-- beautiful
|
|
|
|
local beautiful = bt.get()
|
2009-06-06 12:28:25 +02:00
|
|
|
local font = args.font or preset.font or beautiful.font or capi.awesome.font
|
2009-05-06 18:39:56 +02:00
|
|
|
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 border_color = args.border_color or preset.border_color or beautiful.bg_focus or '#535d6c'
|
2016-02-07 14:19:42 +01:00
|
|
|
local notification = { screen = s, destroy_cb = destroy_cb, timeout = timeout }
|
2008-11-20 17:51:29 +01:00
|
|
|
|
|
|
|
-- replace notification if needed
|
|
|
|
if args.replaces_id then
|
2015-01-10 21:06:30 +01:00
|
|
|
local obj = naughty.getById(args.replaces_id)
|
2008-11-20 17:51:29 +01:00
|
|
|
if obj then
|
|
|
|
-- destroy this and ...
|
2015-01-10 17:32:27 +01:00
|
|
|
naughty.destroy(obj, naughty.notificationClosedReason.silent)
|
2008-11-20 17:51:29 +01:00
|
|
|
end
|
|
|
|
-- ... may use its ID
|
2011-09-07 21:41:47 +02:00
|
|
|
if args.replaces_id <= counter then
|
2008-11-20 17:51:29 +01:00
|
|
|
notification.id = args.replaces_id
|
|
|
|
else
|
|
|
|
counter = counter + 1
|
|
|
|
notification.id = counter
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- get a brand new ID
|
|
|
|
counter = counter + 1
|
|
|
|
notification.id = counter
|
|
|
|
end
|
|
|
|
|
2009-05-06 18:39:56 +02:00
|
|
|
notification.position = position
|
2008-10-17 22:38:01 +02:00
|
|
|
|
2009-05-06 18:39:56 +02:00
|
|
|
if title then title = title .. "\n" else title = "" end
|
2008-10-19 20:58:50 +02:00
|
|
|
|
2008-11-15 17:55:38 +01:00
|
|
|
-- hook destroy
|
2015-02-27 21:45:03 +01:00
|
|
|
set_timeout(notification, timeout)
|
|
|
|
local die = notification.die
|
2008-11-04 20:35:18 +01:00
|
|
|
|
2008-11-25 06:45:40 +01:00
|
|
|
local run = function ()
|
2008-12-03 23:52:59 +01:00
|
|
|
if args.run then
|
|
|
|
args.run(notification)
|
|
|
|
else
|
2015-01-10 17:32:27 +01:00
|
|
|
die(naughty.notificationClosedReason.dismissedByUser)
|
2008-12-03 23:52:59 +01:00
|
|
|
end
|
2008-11-25 06:45:40 +01:00
|
|
|
end
|
2008-11-05 03:03:28 +01:00
|
|
|
|
2008-11-04 20:35:18 +01:00
|
|
|
local hover_destroy = function ()
|
2009-08-20 18:07:30 +02:00
|
|
|
if hover_timeout == 0 then
|
2015-01-10 17:32:27 +01:00
|
|
|
die(naughty.notificationClosedReason.expired)
|
2009-08-25 01:19:46 +02:00
|
|
|
else
|
|
|
|
if notification.timer then notification.timer:stop() end
|
2014-12-06 16:44:04 +01:00
|
|
|
notification.timer = timer { timeout = hover_timeout }
|
2015-01-10 17:32:27 +01:00
|
|
|
notification.timer:connect_signal("timeout", function() die(naughty.notificationClosedReason.expired) end)
|
2009-08-25 01:19:46 +02:00
|
|
|
notification.timer:start()
|
2009-08-20 18:07:30 +02:00
|
|
|
end
|
2008-11-04 20:35:18 +01:00
|
|
|
end
|
|
|
|
|
2008-11-15 17:55:38 +01:00
|
|
|
-- create textbox
|
2010-10-06 14:19:20 +02:00
|
|
|
local textbox = wibox.widget.textbox()
|
|
|
|
local marginbox = wibox.layout.margin()
|
|
|
|
marginbox:set_margins(margin)
|
|
|
|
marginbox:set_widget(textbox)
|
2011-04-14 07:04:48 +02:00
|
|
|
textbox:set_valign("middle")
|
|
|
|
textbox:set_font(font)
|
2008-10-17 22:38:01 +02:00
|
|
|
|
2015-02-27 21:45:03 +01:00
|
|
|
notification.textbox = textbox
|
2010-07-16 19:35:51 +02:00
|
|
|
|
2015-02-27 21:45:03 +01:00
|
|
|
set_text(notification, title, text)
|
2010-07-16 19:35:51 +02:00
|
|
|
|
2014-12-31 14:28:46 +01:00
|
|
|
local actionslayout = wibox.layout.fixed.vertical()
|
|
|
|
local actions_max_width = 0
|
|
|
|
local actions_total_height = 0
|
|
|
|
if actions then
|
2015-01-10 17:48:32 +01:00
|
|
|
for action, callback in pairs(actions) do
|
|
|
|
local actiontextbox = wibox.widget.textbox()
|
|
|
|
local actionmarginbox = wibox.layout.margin()
|
|
|
|
actionmarginbox:set_margins(margin)
|
|
|
|
actionmarginbox:set_widget(actiontextbox)
|
|
|
|
actiontextbox:set_valign("middle")
|
|
|
|
actiontextbox:set_font(font)
|
|
|
|
actiontextbox:set_markup(string.format('<b>%s</b>', action))
|
|
|
|
-- calculate the height and width
|
2015-10-17 18:44:07 +02:00
|
|
|
local w, h = actiontextbox:get_preferred_size(s)
|
2016-02-07 14:19:42 +01:00
|
|
|
local action_height = h + 2 * margin
|
|
|
|
local action_width = w + 2 * margin
|
2015-01-10 17:48:32 +01:00
|
|
|
|
|
|
|
actionmarginbox:buttons(util.table.join(
|
|
|
|
button({ }, 1, callback),
|
|
|
|
button({ }, 3, callback)
|
|
|
|
))
|
|
|
|
actionslayout:add(actionmarginbox)
|
|
|
|
|
2016-02-07 14:19:42 +01:00
|
|
|
actions_total_height = actions_total_height + action_height
|
|
|
|
if actions_max_width < action_width then
|
|
|
|
actions_max_width = action_width
|
2014-12-31 14:28:46 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-15 17:55:38 +01:00
|
|
|
-- create iconbox
|
2008-10-17 22:38:01 +02:00
|
|
|
local iconbox = nil
|
2010-10-06 14:19:20 +02:00
|
|
|
local iconmargin = nil
|
|
|
|
local icon_w, icon_h = 0, 0
|
2008-10-17 22:38:01 +02:00
|
|
|
if icon then
|
2014-04-13 12:22:24 +02:00
|
|
|
-- Is this really an URI instead of a path?
|
|
|
|
if type(icon) == "string" and string.sub(icon, 1, 7) == "file://" then
|
|
|
|
icon = string.sub(icon, 8)
|
|
|
|
end
|
2008-11-22 18:24:33 +01:00
|
|
|
-- try to guess icon if the provided one is non-existent/readable
|
2009-05-08 09:01:14 +02:00
|
|
|
if type(icon) == "string" and not util.file_readable(icon) then
|
2013-03-10 10:57:49 +01:00
|
|
|
icon = util.geticonpath(icon, naughty.config.icon_formats, naughty.config.icon_dirs, icon_size) or icon
|
|
|
|
end
|
|
|
|
|
|
|
|
-- is the icon file readable?
|
2016-02-07 14:19:42 +01:00
|
|
|
icon = surface.load_uncached(icon)
|
2008-11-22 18:24:33 +01:00
|
|
|
|
|
|
|
-- if we have an icon, use it
|
|
|
|
if icon then
|
2010-10-06 14:19:20 +02:00
|
|
|
iconbox = wibox.widget.imagebox()
|
2010-10-08 17:22:00 +02:00
|
|
|
iconmargin = wibox.layout.margin(iconbox, margin, margin, margin, margin)
|
2008-11-22 18:24:33 +01:00
|
|
|
if icon_size then
|
2012-05-27 19:20:34 +02:00
|
|
|
local scaled = cairo.ImageSurface(cairo.Format.ARGB32, icon_size, icon_size)
|
|
|
|
local cr = cairo.Context(scaled)
|
2013-03-10 10:57:49 +01:00
|
|
|
cr:scale(icon_size / icon:get_height(), icon_size / icon:get_width())
|
|
|
|
cr:set_source_surface(icon, 0, 0)
|
2010-10-06 14:19:20 +02:00
|
|
|
cr:paint()
|
2013-03-10 10:57:49 +01:00
|
|
|
icon = scaled
|
2008-11-22 18:24:33 +01:00
|
|
|
end
|
2010-10-06 14:19:20 +02:00
|
|
|
iconbox:set_resize(false)
|
2013-03-10 10:57:49 +01:00
|
|
|
iconbox:set_image(icon)
|
|
|
|
icon_w = icon:get_width()
|
|
|
|
icon_h = icon:get_height()
|
2008-11-04 21:09:28 +01:00
|
|
|
end
|
2008-10-17 22:38:01 +02:00
|
|
|
end
|
|
|
|
|
2008-11-15 17:55:38 +01:00
|
|
|
-- create container wibox
|
2010-10-06 14:19:20 +02:00
|
|
|
notification.box = wibox({ fg = fg,
|
|
|
|
bg = bg,
|
|
|
|
border_color = border_color,
|
|
|
|
border_width = border_width,
|
|
|
|
type = "notification" })
|
2008-11-15 17:55:38 +01:00
|
|
|
|
2009-10-09 20:39:55 +02:00
|
|
|
if hover_timeout then notification.box:connect_signal("mouse::enter", hover_destroy) end
|
2009-08-17 15:43:32 +02:00
|
|
|
|
2015-10-17 18:52:41 +02:00
|
|
|
-- calculate the width
|
|
|
|
if not width then
|
2016-02-27 09:24:19 +01:00
|
|
|
local w, _ = textbox:get_preferred_size(s)
|
2015-10-17 18:52:41 +02:00
|
|
|
width = w + (iconbox and icon_w + 2 * margin or 0) + 2 * margin
|
|
|
|
end
|
|
|
|
|
|
|
|
if width < actions_max_width then
|
|
|
|
width = actions_max_width
|
|
|
|
end
|
|
|
|
|
2009-05-06 20:43:28 +02:00
|
|
|
-- calculate the height
|
|
|
|
if not height then
|
2015-10-17 18:52:41 +02:00
|
|
|
local w = width - (iconbox and icon_w + 2 * margin or 0) - 2 * margin
|
2016-02-27 09:24:19 +01:00
|
|
|
local h = textbox:get_height_for_width(w, s)
|
2010-10-06 14:19:20 +02:00
|
|
|
if iconbox and icon_h + 2 * margin > h + 2 * margin then
|
|
|
|
height = icon_h + 2 * margin
|
2009-05-05 20:38:15 +02:00
|
|
|
else
|
2010-10-06 14:19:20 +02:00
|
|
|
height = h + 2 * margin
|
2009-05-05 20:38:15 +02:00
|
|
|
end
|
|
|
|
end
|
2009-05-06 20:43:28 +02:00
|
|
|
|
2014-12-31 14:28:46 +01:00
|
|
|
height = height + actions_total_height
|
|
|
|
|
2009-05-06 20:43:28 +02:00
|
|
|
-- crop to workarea size if too big
|
2016-02-26 18:30:42 +01:00
|
|
|
local workarea = s.workarea
|
2012-06-12 03:48:41 +02:00
|
|
|
if width > workarea.width - 2 * (border_width or 0) - 2 * (naughty.config.padding or 0) then
|
|
|
|
width = workarea.width - 2 * (border_width or 0) - 2 * (naughty.config.padding or 0)
|
2009-05-05 20:38:15 +02:00
|
|
|
end
|
2012-06-12 03:48:41 +02:00
|
|
|
if height > workarea.height - 2 * (border_width or 0) - 2 * (naughty.config.padding or 0) then
|
|
|
|
height = workarea.height - 2 * (border_width or 0) - 2 * (naughty.config.padding or 0)
|
2009-05-05 20:38:15 +02:00
|
|
|
end
|
|
|
|
|
2009-05-06 20:43:28 +02:00
|
|
|
-- set size in notification object
|
|
|
|
notification.height = height + 2 * (border_width or 0)
|
|
|
|
notification.width = width + 2 * (border_width or 0)
|
|
|
|
|
|
|
|
-- position the wibox
|
2016-02-07 14:19:42 +01:00
|
|
|
local offset = get_offset(s, notification.position, nil, notification.width, notification.height)
|
2009-03-10 20:25:56 +01:00
|
|
|
notification.box.ontop = ontop
|
2009-07-29 14:43:00 +02:00
|
|
|
notification.box:geometry({ width = width,
|
|
|
|
height = height,
|
2008-11-15 17:55:38 +01:00
|
|
|
x = offset.x,
|
|
|
|
y = offset.y })
|
2009-04-10 23:18:51 +02:00
|
|
|
notification.box.opacity = opacity
|
2011-03-27 16:36:01 +02:00
|
|
|
notification.box.visible = true
|
2009-05-08 13:08:09 +02:00
|
|
|
notification.idx = offset.idx
|
2008-11-15 17:55:38 +01:00
|
|
|
|
|
|
|
-- populate widgets
|
2010-10-06 14:19:20 +02:00
|
|
|
local layout = wibox.layout.fixed.horizontal()
|
|
|
|
if iconmargin then
|
|
|
|
layout:add(iconmargin)
|
2009-10-26 23:31:20 +01:00
|
|
|
end
|
2010-10-06 14:19:20 +02:00
|
|
|
layout:add(marginbox)
|
2014-12-31 14:28:46 +01:00
|
|
|
|
|
|
|
local completelayout = wibox.layout.fixed.vertical()
|
|
|
|
completelayout:add(layout)
|
|
|
|
completelayout:add(actionslayout)
|
|
|
|
notification.box:set_widget(completelayout)
|
2010-10-06 14:19:20 +02:00
|
|
|
|
|
|
|
-- Setup the mouse events
|
2014-12-31 14:28:46 +01:00
|
|
|
layout:buttons(util.table.join(button({ }, 1, run),
|
|
|
|
button({ }, 3, function()
|
2015-01-10 17:32:27 +01:00
|
|
|
die(naughty.notificationClosedReason.dismissedByUser)
|
2014-12-31 14:28:46 +01:00
|
|
|
end)))
|
2008-10-17 22:38:01 +02:00
|
|
|
|
|
|
|
-- insert the notification to the table
|
2016-02-07 14:19:42 +01:00
|
|
|
table.insert(naughty.notifications[s][notification.position], notification)
|
2008-10-17 22:38:01 +02:00
|
|
|
|
2010-02-16 17:54:45 +01:00
|
|
|
if suspended then
|
|
|
|
notification.box.visible = false
|
2012-12-15 20:58:18 +01:00
|
|
|
table.insert(naughty.notifications.suspended, notification)
|
2010-02-16 17:54:45 +01:00
|
|
|
end
|
|
|
|
|
2008-11-25 09:45:28 +01:00
|
|
|
-- return the notification
|
|
|
|
return notification
|
|
|
|
end
|
2008-11-20 17:51:29 +01:00
|
|
|
|
2012-06-12 03:48:41 +02:00
|
|
|
return naughty
|
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|