awful.button: Add a has_root_binding property and :trigger() method

This commit is contained in:
Emmanuel Lepage Vallee 2019-10-13 18:32:41 -04:00
parent 93b90026e9
commit 4f7388dd17
1 changed files with 33 additions and 8 deletions

View File

@ -11,7 +11,7 @@
-- Grab environment we need -- Grab environment we need
local setmetatable = setmetatable local setmetatable = setmetatable
local ipairs = ipairs local ipairs = ipairs
local capi = { button = button } local capi = { button = button, root = root }
local gmath = require("gears.math") local gmath = require("gears.math")
local gtable = require("gears.table") local gtable = require("gears.table")
@ -133,15 +133,24 @@ end
function button:trigger() function button:trigger()
local data = reverse_map[self] local data = reverse_map[self]
if data.press then
data.press() local press = data.weak_content.press
if press then
press()
end end
if data.release then local release = data.weak_content.release
data.release()
if release then
release()
end end
end end
function button:get_has_root_binding()
return capi.root.has_button(self)
end
local function index_handler(self, k) local function index_handler(self, k)
if button["get_"..k] then if button["get_"..k] then
return button["get_"..k](self) return button["get_"..k](self)
@ -154,7 +163,11 @@ local function index_handler(self, k)
local data = reverse_map[self] local data = reverse_map[self]
assert(data) assert(data)
return data[k] if data[k] ~= nil then
return data[k]
else
return data.weak_content[k]
end
end end
local function newindex_handler(self, key, value) local function newindex_handler(self, key, value)
@ -165,7 +178,11 @@ local function newindex_handler(self, key, value)
local data = reverse_map[self] local data = reverse_map[self]
assert(data) assert(data)
data[key] = value if data.weak_content[key] ~= nil then
data.weak_content[key] = value
else
data[key] = value
end
end end
local obj_mt = { local obj_mt = {
@ -212,7 +229,15 @@ local function new_common(mod, _button, press, release)
end end
end end
reverse_map[ret] = {_is_capi_button = false} reverse_map[ret] = {
-- Use weak tables to let Lua 5.1 and Luajit GC the `awful.buttons`,
-- Lua 5.3 is smart enough to figure this out.
weak_content = setmetatable({
press = press,
release = release,
}, {__mode = "v"}),
_is_capi_button = false
}
return setmetatable(ret, obj_mt) return setmetatable(ret, obj_mt)
end end