diff --git a/lib/naughty/constants.lua b/lib/naughty/constants.lua index ce237b13..d1f5157d 100644 --- a/lib/naughty/constants.lua +++ b/lib/naughty/constants.lua @@ -9,9 +9,10 @@ -- @copyright 2017 Emmanuel Lepage Vallee ---------------------------------------------------------------------------- local beautiful = require("beautiful") +local gtable = require("gears.table") local dpi = beautiful.xresources.apply_dpi -local ret = {} +local ret, no_clear = {}, {} ret.config = { padding = dpi(4), @@ -21,7 +22,7 @@ ret.config = { notify_callback = nil, } -ret.config.presets = { +no_clear.presets = { low = { timeout = 5 }, @@ -55,15 +56,15 @@ ret.config._urgency = { } 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}, + {{urgency = ret.config._urgency.low }, no_clear.presets.low}, --compat + {{urgency = ret.config._urgency.normal }, no_clear.presets.normal}, --compat + {{urgency = ret.config._urgency.critical}, no_clear.presets.critical}, --compat + {{urgency = "low" }, no_clear.presets.low}, + {{urgency = "normal" }, no_clear.presets.normal}, + {{urgency = "critical"}, no_clear.presets.critical}, } -ret.config.defaults = { +no_clear.defaults = { timeout = 5, text = "", screen = nil, @@ -92,4 +93,21 @@ ret.notification_closed_reason = { -- Legacy --TODO v5 remove this alias ret.notificationClosedReason = ret.notification_closed_reason +-- `no_clear` is used to prevent users from setting the entire table. +-- If they did and we added a new default value, then it would not be +-- backward compatible. For safety, we just crush the tables rather than +-- replace them. +setmetatable(ret.config, { + __index = function(_, key) + return no_clear[key] + end, + __newindex = function(_, key, value) + if no_clear[key] then + gtable.crush(no_clear[key], value) + else + rawset(ret.config, key, value) + end + end +}) + return ret diff --git a/tests/test-naughty-legacy.lua b/tests/test-naughty-legacy.lua index 86c992ee..7d716e37 100644 --- a/tests/test-naughty-legacy.lua +++ b/tests/test-naughty-legacy.lua @@ -1256,4 +1256,15 @@ table.insert(steps, function() return true end) +-- Make sure it isn't possible to remove default variables (#3145). +table.insert(steps, function() + naughty.config.defaults = {fake_variable = 24} + naughty.config.text = 1337 + assert(naughty.config.defaults.fake_variable == 24) + assert(naughty.config.defaults.timeout == 5) + assert(naughty.config.text == 1337) + + return true +end) + require("_runner").run_steps(steps)