awful: Set a miss handler for capi.root

There is no better place to put it and need to always be required
for backward compatibility. Given Awesome no longer works properly
without `awful`, I put the code there.
This commit is contained in:
Emmanuel Lepage Vallee 2018-12-26 15:52:34 -05:00
parent 95500ea71c
commit b1c81c4258
3 changed files with 51 additions and 4 deletions

View file

@ -26,7 +26,6 @@ read_globals = {
"key",
"keygrabber",
"mousegrabber",
"root",
"selection",
"tag",
"window",
@ -43,6 +42,7 @@ read_globals = {
globals = {
"screen",
"mouse",
"root",
"client"
}

View file

@ -5,6 +5,8 @@ local util = require("awful.util" )
local spawn = require("awful.spawn")
local gdebug = require("gears.debug")
local capi = {root = root}
function timer(...) -- luacheck: ignore
gdebug.deprecate("gears.timer", {deprecated_in=4})
return gtimer(...)
@ -25,3 +27,32 @@ util.pread = function()
.. "for an asynchronous alternative", {deprecated_in=4})
return ""
end
-- Allow properties to be set on the root object. This helps to migrate some
-- capi function to an higher level Lua implementation.
do
local root_props, root_object = {}, {}
capi.root.set_newindex_miss_handler(function(_,key,value)
if root_object["set_"..key] then
root_object["set_"..key](value)
elseif not root_object["get_"..key] then
root_props[key] = value
else
-- If there is a getter, but no setter, then the property is read-only
error("Cannot set '" .. tostring(key) .. " because it is read-only")
end
end)
capi.root.set_index_miss_handler(function(_,key)
if root_object["get_"..key] then
return root_object["get_"..key]()
else
return root_props[key]
end
end)
root._private = {}
root.object = root_object
assert(root.object == root_object)
end

View file

@ -176,13 +176,29 @@ end
function root.set_newindex_miss_handler(h)
rawset(mouse, "_ni_handler", h)
rawset(root, "_ni_handler", h)
end
function root.set_index_miss_handler(h)
rawset(mouse, "_i_handler", h)
rawset(root, "_i_handler", h)
end
return root
return setmetatable(root, {
__index = function(self, key)
if key == "screen" then
return screen[1]
end
local h = rawget(root,"_i_handler")
if h then
return h(self, key)
end
end,
__newindex = function(...)
local h = rawget(root,"_ni_handler")
if h then
h(...)
end
end,
})
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80