naughty: Begin to deprecate the presets.

The old preset code had a primitive implementation of the rule API
used in `naughty.dbus`. Now that `gears.matcher` is extracted from
`awful.rules`, it is possible to share the code.

The first step is to only enable the old API when the new
`request::preset` isn't connected. This is the same way the legacy
popup is only enabled when nothing is connected to `request::display`.
This commit is contained in:
Emmanuel Lepage Vallee 2019-07-15 00:55:34 -04:00
parent 41149ed335
commit c36d35756f
6 changed files with 72 additions and 37 deletions

View File

@ -48,6 +48,21 @@ ret.config.presets = {
}, },
} }
ret.config._urgency = {
low = "\0",
normal = "\1",
critical = "\2"
}
ret.config.mapping = {
{{urgency = ret.config._urgency.low }, ret.config.presets.low}, --compat
{{urgency = ret.config._urgency.normal }, ret.config.presets.normal}, --compat
{{urgency = ret.config._urgency.critical}, ret.config.presets.critical}, --compat
{{urgency = "low" }, ret.config.presets.low},
{{urgency = "normal" }, ret.config.presets.normal},
{{urgency = "critical"}, ret.config.presets.critical},
}
ret.config.defaults = { ret.config.defaults = {
timeout = 5, timeout = 5,
text = "", text = "",

View File

@ -224,7 +224,9 @@ local function update_index(n)
remove_from_index(n) remove_from_index(n)
-- Add to the index again -- Add to the index again
local s = get_screen(n.screen or n.preset.screen or screen.focused()) local s = get_screen(n.screen
or (n.preset and n.preset.screen)
or screen.focused())
naughty.notifications[s] = naughty.notifications[s] or {} naughty.notifications[s] = naughty.notifications[s] or {}
table.insert(naughty.notifications[s][n.position], n) table.insert(naughty.notifications[s][n.position], n)
end end
@ -399,6 +401,11 @@ function naughty.get_has_display_handler()
return conns["request::display"] and #conns["request::display"] > 0 or false return conns["request::display"] and #conns["request::display"] > 0 or false
end end
-- Presets are "deprecated" when notification rules are used.
function naughty.get__has_preset_handler()
return conns["request::preset"] and #conns["request::preset"] > 0 or false
end
--- Set new notification timeout. --- Set new notification timeout.
-- --
-- This function is deprecated, use `notification:reset_timeout(new_timeout)`. -- This function is deprecated, use `notification:reset_timeout(new_timeout)`.
@ -545,7 +552,9 @@ local function register(notification, args)
rawset(notification, "get_suspended", get_suspended) rawset(notification, "get_suspended", get_suspended)
--TODO v5 uncouple the notifications and the screen --TODO v5 uncouple the notifications and the screen
local s = get_screen(args.screen or notification.preset.screen or screen.focused()) local s = get_screen(args.screen
or (notification.preset and notification.preset.screen)
or screen.focused())
-- insert the notification to the table -- insert the notification to the table
table.insert(naughty._active, notification) table.insert(naughty._active, notification)
@ -560,7 +569,7 @@ local function register(notification, args)
naughty.emit_signal("added", notification, args) naughty.emit_signal("added", notification, args)
end end
assert(rawget(notification, "preset")) assert(rawget(notification, "preset") or naughty._has_preset_handler)
naughty.emit_signal("property::active") naughty.emit_signal("property::active")

View File

@ -12,7 +12,6 @@ local pairs = pairs
local type = type local type = type
local string = string local string = string
local capi = { awesome = awesome } local capi = { awesome = awesome }
local gtable = require("gears.table")
local gsurface = require("gears.surface") local gsurface = require("gears.surface")
local gdebug = require("gears.debug") local gdebug = require("gears.debug")
local protected_call = require("gears.protected_call") local protected_call = require("gears.protected_call")
@ -56,11 +55,7 @@ local urgency = {
-- @tfield table 2 normal urgency -- @tfield table 2 normal urgency
-- @tfield table 3 critical urgency -- @tfield table 3 critical urgency
-- @table config.mapping -- @table config.mapping
dbus.config.mapping = { dbus.config.mapping = cst.mapping
{{urgency = urgency.low}, cst.config.presets.low},
{{urgency = urgency.normal}, cst.config.presets.normal},
{{urgency = urgency.critical}, cst.config.presets.critical}
}
local function sendActionInvoked(notificationId, action) local function sendActionInvoked(notificationId, action)
if bus_connection then if bus_connection then
@ -149,14 +144,6 @@ function notif_methods.Notify(sender, object_path, interface, method, parameters
if appname ~= "" then if appname ~= "" then
args.appname = appname args.appname = appname
end end
for _, obj in pairs(dbus.config.mapping) do
local filter, preset = obj[1], obj[2]
if (not filter.urgency or filter.urgency == hints.urgency) and
(not filter.category or filter.category == hints.category) and
(not filter.appname or filter.appname == appname) then
args.preset = gtable.join(args.preset, preset)
end
end
local preset = args.preset or cst.config.defaults local preset = args.preset or cst.config.defaults
local notification local notification
if actions then if actions then

View File

@ -148,8 +148,7 @@ end
local function init(self, notification) local function init(self, notification)
local args = self._private.args local args = self._private.args
local preset = notification.preset local preset = notification.preset or {}
assert(preset)
local position = args.position or notification.position or local position = args.position or notification.position or
beautiful.notification_position or preset.position or "top_right" beautiful.notification_position or preset.position or "top_right"

View File

@ -309,7 +309,7 @@ function naughty.default_notification_handler(notification, args)
return return
end end
local preset = notification.preset local preset = notification.preset or {}
local text = args.message or args.text or preset.message or preset.text local text = args.message or args.text or preset.message or preset.text
local title = args.title or preset.title local title = args.title or preset.title
local s = get_screen(args.screen or preset.screen or screen.focused()) local s = get_screen(args.screen or preset.screen or screen.focused())

View File

@ -653,6 +653,33 @@ local function convert_actions(actions)
end end
end end
-- The old API used monkey-patched variable presets.
--
-- Monkey-patched anything is always an issue and prevent module from safely
-- doing anything without stepping on each other foot. In the background,
-- presets were selected with a rule-like API anyway.
local function select_legacy_preset(n, args)
for _, obj in pairs(cst.config.mapping) do
local filter, preset = obj[1], obj[2]
if (not filter.urgency or filter.urgency == args.urgency) and
(not filter.category or filter.category == args.category) and
(not filter.appname or filter.appname == args.appname) then
args.preset = gtable.join(args.preset or {}, preset)
end
end
-- gather variables together
rawset(n, "preset", gtable.join(
cst.config.defaults or {},
args.preset or cst.config.presets.normal or {},
rawget(n, "preset") or {}
))
for k, v in pairs(n.preset) do
n._private[k] = v
end
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.
@ -734,22 +761,17 @@ local function create(args)
-- Avoid modifying the original table -- Avoid modifying the original table
local private = {} local private = {}
rawset(n, "_private", private)
-- gather variables together -- Allow extensions to create override the preset with custom data
rawset(n, "preset", gtable.join( if not naughty._has_preset_handler then
cst.config.defaults or {}, select_legacy_preset(n, args)
args.preset or cst.config.presets.normal or {}, end
rawget(n, "preset") or {}
))
if is_old_action then if is_old_action then
convert_actions(args.actions) convert_actions(args.actions)
end end
for k, v in pairs(n.preset) do
private[k] = v
end
for k, v in pairs(args) do for k, v in pairs(args) do
private[k] = v private[k] = v
end end
@ -767,27 +789,30 @@ local function create(args)
-- It's an automatic property -- It's an automatic property
n.is_expired = false n.is_expired = false
rawset(n, "_private", private)
gtable.crush(n, notification, true) gtable.crush(n, notification, true)
n.id = n.id or notification._gen_next_id() n.id = n.id or notification._gen_next_id()
-- Allow extensions to create override the preset with custom data
naughty.emit_signal("request::preset", n, args)
-- Register the notification before requesting a widget -- Register the notification before requesting a widget
n:emit_signal("new", args) n:emit_signal("new", args)
-- The rules are attached to this.
if naughty._has_preset_handler then
naughty.emit_signal("request::preset", n, args)
end
-- Let all listeners handle the actual visual aspects -- Let all listeners handle the actual visual aspects
if (not n.ignore) and (not n.preset.ignore) then if (not n.ignore) and ((not n.preset) or n.preset.ignore ~= true) then
naughty.emit_signal("request::display" , n, args) naughty.emit_signal("request::display" , n, args)
naughty.emit_signal("request::fallback", n, args) naughty.emit_signal("request::fallback", n, args)
end end
-- Because otherwise the setter logic would not be executed -- Because otherwise the setter logic would not be executed
if n._private.timeout then if n._private.timeout then
n:set_timeout(n._private.timeout or n.preset.timeout) n:set_timeout(n._private.timeout
or (n.preset and n.preset.timeout)
or cst.config.timeout
)
end end
return n return n