2018-05-29 02:53:06 +02:00
|
|
|
----------------------------------------------------------------------------
|
|
|
|
--- A notification popup widget.
|
|
|
|
--
|
|
|
|
-- By default, the box is composed of many other widgets:
|
|
|
|
--
|
|
|
|
--@DOC_wibox_nwidget_default_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- @author Emmanuel Lepage Vallee <elv1313@gmail.com>
|
|
|
|
-- @copyright 2017 Emmanuel Lepage Vallee
|
|
|
|
-- @popupmod naughty.layout.box
|
2021-03-29 09:39:46 +02:00
|
|
|
-- @supermodule awful.popup
|
2018-05-29 02:53:06 +02:00
|
|
|
----------------------------------------------------------------------------
|
|
|
|
|
2019-12-16 01:55:37 +01:00
|
|
|
local capi = {screen=screen}
|
2018-05-29 02:53:06 +02:00
|
|
|
local beautiful = require("beautiful")
|
2019-12-16 01:55:37 +01:00
|
|
|
local gtimer = require("gears.timer")
|
2018-05-29 02:53:06 +02:00
|
|
|
local gtable = require("gears.table")
|
|
|
|
local wibox = require("wibox")
|
|
|
|
local popup = require("awful.popup")
|
|
|
|
local awcommon = require("awful.widget.common")
|
|
|
|
local placement = require("awful.placement")
|
|
|
|
local abutton = require("awful.button")
|
2019-12-16 01:55:37 +01:00
|
|
|
local ascreen = require("awful.screen")
|
2019-07-22 06:33:34 +02:00
|
|
|
local gpcall = require("gears.protected_call")
|
2019-07-15 23:44:58 +02:00
|
|
|
local dpi = require("beautiful").xresources.apply_dpi
|
2018-05-29 02:53:06 +02:00
|
|
|
|
|
|
|
local default_widget = require("naughty.widget._default")
|
|
|
|
|
|
|
|
local box, by_position = {}, {}
|
|
|
|
|
|
|
|
-- Init the weak tables for each positions. It is done ahead of time rather
|
|
|
|
-- than when notifications are added to simplify the code.
|
2019-12-16 01:55:37 +01:00
|
|
|
|
|
|
|
local function init_screen(s)
|
|
|
|
if not s.valid then return end
|
|
|
|
|
|
|
|
if by_position[s] then return by_position[s] end
|
|
|
|
|
|
|
|
by_position[s] = setmetatable({},{__mode = "k"})
|
|
|
|
|
|
|
|
for _, pos in ipairs { "top_left" , "top_middle" , "top_right",
|
|
|
|
"bottom_left", "bottom_middle", "bottom_right" } do
|
|
|
|
by_position[s][pos] = setmetatable({},{__mode = "v"})
|
|
|
|
end
|
|
|
|
|
|
|
|
return by_position[s]
|
2018-05-29 02:53:06 +02:00
|
|
|
end
|
|
|
|
|
2021-09-20 06:54:42 +02:00
|
|
|
local function disconnect(self)
|
2021-11-01 03:30:17 +01:00
|
|
|
local n = self._private.notification[1]
|
|
|
|
|
|
|
|
if n then
|
|
|
|
n:disconnect_signal("destroyed",
|
2021-09-20 06:54:42 +02:00
|
|
|
self._private.destroy_callback)
|
|
|
|
|
2021-11-01 03:30:17 +01:00
|
|
|
n:disconnect_signal("property::margin",
|
2021-09-20 06:54:42 +02:00
|
|
|
self._private.update)
|
|
|
|
|
2021-11-01 03:30:17 +01:00
|
|
|
n:disconnect_signal("property::suspended",
|
2021-09-20 06:54:42 +02:00
|
|
|
self._private.hide)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-16 01:55:37 +01:00
|
|
|
ascreen.connect_for_each_screen(init_screen)
|
|
|
|
|
|
|
|
-- Manually cleanup to help the GC.
|
|
|
|
capi.screen.connect_signal("removed", function(scr)
|
|
|
|
-- By that time, all direct events should have been handled. Cleanup the
|
|
|
|
-- leftover. Being a weak table doesn't help Lua 5.1.
|
|
|
|
gtimer.delayed_call(function()
|
|
|
|
by_position[scr] = nil
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2018-05-29 02:53:06 +02:00
|
|
|
local function get_spacing()
|
2020-05-23 06:55:47 +02:00
|
|
|
local margin = beautiful.notification_spacing or dpi(2)
|
2018-05-29 02:53:06 +02:00
|
|
|
return {top = margin, bottom = margin}
|
|
|
|
end
|
|
|
|
|
2020-05-23 06:55:47 +02:00
|
|
|
local function get_offset(position, preset)
|
|
|
|
preset = preset or {}
|
|
|
|
local margin = preset.padding or beautiful.notification_spacing or dpi(4)
|
|
|
|
if position:match('_right') then
|
|
|
|
return {x = -margin}
|
|
|
|
elseif position:match('_left') then
|
|
|
|
return {x = margin}
|
|
|
|
end
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
|
2018-05-29 02:53:06 +02:00
|
|
|
-- Leverage `awful.placement` to create the stacks.
|
2020-05-23 06:55:47 +02:00
|
|
|
local function update_position(position, preset)
|
2018-05-29 02:53:06 +02:00
|
|
|
local pref = position:match("top_") and "bottom" or "top"
|
|
|
|
local align = position:match("_(.*)")
|
|
|
|
:gsub("left", "front"):gsub("right", "back")
|
|
|
|
|
2019-12-16 01:55:37 +01:00
|
|
|
for _, pos in pairs(by_position) do
|
|
|
|
for k, wdg in ipairs(pos[position]) do
|
|
|
|
local args = {
|
|
|
|
geometry = pos[position][k-1],
|
|
|
|
preferred_positions = {pref },
|
|
|
|
preferred_anchors = {align},
|
|
|
|
margins = get_spacing(),
|
|
|
|
honor_workarea = true,
|
|
|
|
}
|
2020-05-23 06:55:47 +02:00
|
|
|
if k == 1 then
|
2021-11-01 03:30:17 +01:00
|
|
|
args.offset = get_offset(position, preset)
|
2020-05-23 06:55:47 +02:00
|
|
|
end
|
2019-12-16 01:55:37 +01:00
|
|
|
|
|
|
|
-- The first entry is aligned to the workarea, then the following to the
|
|
|
|
-- previous widget.
|
|
|
|
placement[k==1 and position:gsub("_middle", "") or "next_to"](wdg, args)
|
|
|
|
end
|
2018-05-29 02:53:06 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function finish(self)
|
|
|
|
self.visible = false
|
2019-12-16 01:55:37 +01:00
|
|
|
assert(init_screen(self.screen)[self.position])
|
2018-05-29 02:53:06 +02:00
|
|
|
|
2019-12-16 01:55:37 +01:00
|
|
|
for k, v in ipairs(init_screen(self.screen)[self.position]) do
|
2018-05-29 02:53:06 +02:00
|
|
|
if v == self then
|
2019-12-16 01:55:37 +01:00
|
|
|
table.remove(init_screen(self.screen)[self.position], k)
|
2018-05-29 02:53:06 +02:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-01 03:30:17 +01:00
|
|
|
local preset = (self._private.notification[1] or {}).preset
|
2020-05-23 06:55:47 +02:00
|
|
|
|
|
|
|
update_position(self.position, preset)
|
2021-09-20 06:54:42 +02:00
|
|
|
|
|
|
|
disconnect(self)
|
2021-11-01 03:30:17 +01:00
|
|
|
|
|
|
|
self._private.notification = {}
|
2018-05-29 02:53:06 +02:00
|
|
|
end
|
|
|
|
|
2020-02-09 08:06:48 +01:00
|
|
|
-- It isn't a good idea to use the `attach` `awful.placement` property. If the
|
|
|
|
-- screen is resized or the notification is moved, it causes side effects.
|
|
|
|
-- Better listen to geometry changes and reflow.
|
|
|
|
capi.screen.connect_signal("property::geometry", function(s)
|
|
|
|
for pos, notifs in pairs(by_position[s]) do
|
|
|
|
if #notifs > 0 then
|
2020-05-23 06:55:47 +02:00
|
|
|
update_position(pos, notifs[1].preset)
|
2020-02-09 08:06:48 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2018-05-29 02:53:06 +02:00
|
|
|
--- The maximum notification width.
|
|
|
|
-- @beautiful beautiful.notification_max_width
|
|
|
|
-- @tparam[opt=500] number notification_max_width
|
|
|
|
|
|
|
|
--- The maximum notification position.
|
|
|
|
--
|
|
|
|
-- Valid values are:
|
|
|
|
--
|
|
|
|
-- * top_left
|
|
|
|
-- * top_middle
|
|
|
|
-- * top_right
|
|
|
|
-- * bottom_left
|
|
|
|
-- * bottom_middle
|
|
|
|
-- * bottom_right
|
|
|
|
--
|
|
|
|
-- @beautiful beautiful.notification_position
|
|
|
|
-- @tparam[opt="top_right"] string notification_position
|
|
|
|
|
|
|
|
--- The widget notification object.
|
2020-01-13 08:32:28 +01:00
|
|
|
--
|
2018-05-29 02:53:06 +02:00
|
|
|
-- @property notification
|
2020-01-13 08:32:28 +01:00
|
|
|
-- @tparam naughty.notification notification
|
|
|
|
-- @propemits true false
|
2018-05-29 02:53:06 +02:00
|
|
|
|
|
|
|
--- The widget template to construct the box content.
|
|
|
|
--
|
|
|
|
--@DOC_wibox_nwidget_default_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- The default template is (less or more):
|
|
|
|
--
|
|
|
|
-- {
|
|
|
|
-- {
|
|
|
|
-- {
|
|
|
|
-- {
|
|
|
|
-- {
|
|
|
|
-- naughty.widget.icon,
|
|
|
|
-- {
|
|
|
|
-- naughty.widget.title,
|
|
|
|
-- naughty.widget.message,
|
|
|
|
-- spacing = 4,
|
|
|
|
-- layout = wibox.layout.fixed.vertical,
|
|
|
|
-- },
|
|
|
|
-- fill_space = true,
|
|
|
|
-- spacing = 4,
|
|
|
|
-- layout = wibox.layout.fixed.horizontal,
|
|
|
|
-- },
|
|
|
|
-- naughty.list.actions,
|
|
|
|
-- spacing = 10,
|
|
|
|
-- layout = wibox.layout.fixed.vertical,
|
|
|
|
-- },
|
|
|
|
-- margins = beautiful.notification_margin,
|
2019-07-07 09:02:03 +02:00
|
|
|
-- widget = wibox.container.margin,
|
2018-05-29 02:53:06 +02:00
|
|
|
-- },
|
|
|
|
-- id = "background_role",
|
|
|
|
-- widget = naughty.container.background,
|
|
|
|
-- },
|
|
|
|
-- strategy = "max",
|
|
|
|
-- width = width(beautiful.notification_max_width
|
2019-07-07 09:02:03 +02:00
|
|
|
-- or beautiful.xresources.apply_dpi(500)),
|
2018-05-29 02:53:06 +02:00
|
|
|
-- widget = wibox.container.constraint,
|
|
|
|
-- }
|
|
|
|
--
|
|
|
|
-- @property widget_template
|
|
|
|
-- @param widget
|
2020-01-13 08:32:28 +01:00
|
|
|
-- @usebeautiful beautiful.notification_max_width The maximum width for the
|
|
|
|
-- resulting widget.
|
2018-05-29 02:53:06 +02:00
|
|
|
|
|
|
|
local function generate_widget(args, n)
|
2019-07-22 06:33:34 +02:00
|
|
|
local w = gpcall(wibox.widget.base.make_widget_from_value,
|
2019-07-15 23:44:58 +02:00
|
|
|
args.widget_template or (n and n.widget_template) or default_widget
|
2018-05-29 02:53:06 +02:00
|
|
|
)
|
|
|
|
|
2019-07-22 06:33:34 +02:00
|
|
|
-- This will happen if the user-provided widget_template is invalid and/or
|
|
|
|
-- got unexpected notifications.
|
|
|
|
if not w then
|
|
|
|
w = gpcall(wibox.widget.base.make_widget_from_value, default_widget)
|
|
|
|
|
|
|
|
-- In case this happens in an error message itself, make sure the
|
|
|
|
-- private error popup code knowns it and can revert to the fallback
|
|
|
|
-- popup.
|
|
|
|
if not w then
|
|
|
|
n._private.widget_template_failed = true
|
|
|
|
end
|
|
|
|
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2019-07-15 23:44:58 +02:00
|
|
|
if w.set_width then
|
|
|
|
w:set_width(n.max_width or beautiful.notification_max_width or dpi(500))
|
|
|
|
end
|
|
|
|
|
2018-05-29 02:53:06 +02:00
|
|
|
-- Call `:set_notification` on all children
|
2021-11-01 03:30:17 +01:00
|
|
|
awcommon._set_common_property(w, "notification", n)
|
2018-05-29 02:53:06 +02:00
|
|
|
|
|
|
|
return w
|
|
|
|
end
|
|
|
|
|
|
|
|
local function init(self, notification)
|
2019-07-15 06:55:34 +02:00
|
|
|
local preset = notification.preset or {}
|
2018-05-29 02:53:06 +02:00
|
|
|
|
2021-11-01 03:30:17 +01:00
|
|
|
local position = self._private.position or notification.position or
|
2020-05-23 06:55:47 +02:00
|
|
|
preset.position or beautiful.notification_position or "top_right"
|
2018-05-29 02:53:06 +02:00
|
|
|
|
|
|
|
if not self.widget then
|
2021-11-01 03:30:17 +01:00
|
|
|
self.widget = generate_widget(self._private, notification)
|
2018-05-29 02:53:06 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local bg = self._private.widget:get_children_by_id( "background_role" )[1]
|
|
|
|
|
|
|
|
-- Make sure the border isn't set twice, favor the widget one since it is
|
|
|
|
-- shared by the notification list and the notification box.
|
|
|
|
if bg then
|
|
|
|
if bg.set_notification then
|
|
|
|
bg:set_notification(notification)
|
|
|
|
self.border_width = 0
|
|
|
|
else
|
|
|
|
bg:set_bg(notification.bg)
|
|
|
|
self.border_width = notification.border_width
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-16 01:55:37 +01:00
|
|
|
local s = notification.screen
|
|
|
|
assert(s)
|
|
|
|
|
2018-05-29 02:53:06 +02:00
|
|
|
-- Add the notification to the active list
|
2020-02-10 01:38:47 +01:00
|
|
|
assert(init_screen(s)[position], "Invalid position "..position)
|
2018-05-29 02:53:06 +02:00
|
|
|
|
|
|
|
self:_apply_size_now()
|
|
|
|
|
2019-12-16 01:55:37 +01:00
|
|
|
table.insert(init_screen(s)[position], self)
|
2018-05-29 02:53:06 +02:00
|
|
|
|
2021-09-20 06:54:42 +02:00
|
|
|
self._private.update = function() update_position(position, preset) end
|
|
|
|
self._private.hide = function(_, value)
|
|
|
|
if value then
|
|
|
|
finish(self)
|
|
|
|
end
|
|
|
|
end
|
2018-05-29 02:53:06 +02:00
|
|
|
|
2021-09-20 06:54:42 +02:00
|
|
|
self:connect_signal("property::geometry", self._private.update)
|
|
|
|
notification:weak_connect_signal("property::margin", self._private.update)
|
|
|
|
notification:weak_connect_signal("property::suspended", self._private.hide)
|
|
|
|
notification:weak_connect_signal("destroyed", self._private.destroy_callback)
|
2018-05-29 02:53:06 +02:00
|
|
|
|
2020-05-23 06:55:47 +02:00
|
|
|
update_position(position, preset)
|
2018-05-29 02:53:06 +02:00
|
|
|
|
2021-09-20 06:54:42 +02:00
|
|
|
self.visible = true
|
2018-05-29 02:53:06 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function box:set_notification(notif)
|
2021-11-01 03:30:17 +01:00
|
|
|
if self._private.notification[1] == notif then return end
|
2018-05-29 02:53:06 +02:00
|
|
|
|
2021-09-20 06:54:42 +02:00
|
|
|
disconnect(self)
|
2018-05-29 02:53:06 +02:00
|
|
|
|
|
|
|
init(self, notif)
|
|
|
|
|
2021-11-01 03:30:17 +01:00
|
|
|
self._private.notification = setmetatable({notif}, {__mode="v"})
|
2020-01-13 08:32:28 +01:00
|
|
|
|
|
|
|
self:emit_signal("property::notification", notif)
|
2018-05-29 02:53:06 +02:00
|
|
|
end
|
|
|
|
|
2021-11-01 03:30:17 +01:00
|
|
|
function box:get_notification()
|
|
|
|
return self._private.notification[1]
|
|
|
|
end
|
|
|
|
|
2018-05-29 02:53:06 +02:00
|
|
|
function box:get_position()
|
2021-11-01 03:30:17 +01:00
|
|
|
local n = self._private.notification[1]
|
|
|
|
|
|
|
|
if n then
|
|
|
|
return n:get_position()
|
2018-05-29 02:53:06 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return "top_right"
|
|
|
|
end
|
|
|
|
|
2020-01-13 08:32:28 +01:00
|
|
|
--- Create a notification popup box.
|
|
|
|
--
|
|
|
|
-- @constructorfct naughty.layout.box
|
|
|
|
-- @tparam[opt=nil] table args
|
|
|
|
-- @tparam table args.widget_template A widget definition template which will
|
|
|
|
-- be instantiated for each box.
|
|
|
|
-- @tparam naughty.notification args.notification The notification object.
|
|
|
|
-- @tparam string args.position The position. See `naughty.notification.position`.
|
|
|
|
--@DOC_wibox_constructor_COMMON@
|
|
|
|
-- @usebeautiful beautiful.notification_position If `position` is not defined
|
|
|
|
-- in the notification object (or in this constructor).
|
|
|
|
|
2018-05-29 02:53:06 +02:00
|
|
|
local function new(args)
|
2019-12-16 01:55:37 +01:00
|
|
|
args = args or {}
|
|
|
|
|
2018-05-29 02:53:06 +02:00
|
|
|
-- Set the default wibox values
|
|
|
|
local new_args = {
|
|
|
|
ontop = true,
|
|
|
|
visible = false,
|
2019-12-16 01:55:37 +01:00
|
|
|
bg = args.bg or beautiful.notification_bg,
|
|
|
|
fg = args.fg or beautiful.notification_fg,
|
|
|
|
shape = args.shape or beautiful.notification_shape,
|
|
|
|
border_width = args.border_width or beautiful.notification_border_width or 1,
|
|
|
|
border_color = args.border_color or beautiful.notification_border_color,
|
2018-05-29 02:53:06 +02:00
|
|
|
}
|
|
|
|
|
2020-01-13 01:34:39 +01:00
|
|
|
-- The C code needs `pairs` to work, so a full copy is required.
|
|
|
|
gtable.crush(new_args, args, true)
|
|
|
|
|
2019-12-16 01:55:37 +01:00
|
|
|
-- Add a weak-table layer for the screen.
|
|
|
|
local weak_args = setmetatable({
|
|
|
|
screen = args.notification and args.notification.screen or nil
|
2020-01-13 01:34:39 +01:00
|
|
|
}, {__mode="v"})
|
2019-12-16 01:55:37 +01:00
|
|
|
|
2020-01-13 01:34:39 +01:00
|
|
|
setmetatable(new_args, {__index = weak_args})
|
2018-05-29 02:53:06 +02:00
|
|
|
|
|
|
|
-- Generate the box before the popup is created to avoid the size changing
|
2019-07-15 23:44:58 +02:00
|
|
|
new_args.widget = generate_widget(new_args, new_args.notification)
|
2018-05-29 02:53:06 +02:00
|
|
|
|
2019-07-22 06:33:34 +02:00
|
|
|
-- It failed, request::fallback will be used, there is nothing left to do.
|
|
|
|
if not new_args.widget then return nil end
|
|
|
|
|
2018-05-29 02:53:06 +02:00
|
|
|
local ret = popup(new_args)
|
2021-11-01 03:30:17 +01:00
|
|
|
ret._private.notification = {}
|
|
|
|
ret._private.widget_template = args.widget_template
|
|
|
|
ret._private.position = args.position
|
2018-05-29 02:53:06 +02:00
|
|
|
|
|
|
|
gtable.crush(ret, box, true)
|
|
|
|
|
|
|
|
function ret._private.destroy_callback()
|
|
|
|
finish(ret)
|
|
|
|
end
|
|
|
|
|
|
|
|
if new_args.notification then
|
|
|
|
ret:set_notification(new_args.notification)
|
|
|
|
end
|
|
|
|
|
|
|
|
--TODO remove
|
|
|
|
local function hide()
|
2021-11-01 03:30:17 +01:00
|
|
|
local n = ret._private.notification[1]
|
|
|
|
|
|
|
|
if n then
|
|
|
|
n:destroy()
|
2018-05-29 02:53:06 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--FIXME there's another pull request for this
|
|
|
|
ret:buttons(gtable.join(
|
|
|
|
abutton({ }, 1, hide),
|
|
|
|
abutton({ }, 3, hide)
|
|
|
|
))
|
|
|
|
|
2020-01-13 01:34:39 +01:00
|
|
|
gtable.crush(ret, box, false)
|
|
|
|
|
2018-05-29 02:53:06 +02:00
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
--@DOC_wibox_COMMON@
|
|
|
|
|
|
|
|
return setmetatable(box, {__call = function(_, args) return new(args) end})
|