From 4f7388dd17ebcd1dac24d637dd61e3462e5d54b7 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Sun, 13 Oct 2019 18:32:41 -0400 Subject: [PATCH] awful.button: Add a has_root_binding property and :trigger() method --- lib/awful/button.lua | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/awful/button.lua b/lib/awful/button.lua index c4c05f675..9f296012e 100644 --- a/lib/awful/button.lua +++ b/lib/awful/button.lua @@ -11,7 +11,7 @@ -- Grab environment we need local setmetatable = setmetatable local ipairs = ipairs -local capi = { button = button } +local capi = { button = button, root = root } local gmath = require("gears.math") local gtable = require("gears.table") @@ -133,15 +133,24 @@ end function button:trigger() local data = reverse_map[self] - if data.press then - data.press() + + local press = data.weak_content.press + + if press then + press() end - if data.release then - data.release() + local release = data.weak_content.release + + if release then + release() end end +function button:get_has_root_binding() + return capi.root.has_button(self) +end + local function index_handler(self, k) if button["get_"..k] then return button["get_"..k](self) @@ -154,7 +163,11 @@ local function index_handler(self, k) local data = reverse_map[self] assert(data) - return data[k] + if data[k] ~= nil then + return data[k] + else + return data.weak_content[k] + end end local function newindex_handler(self, key, value) @@ -165,7 +178,11 @@ local function newindex_handler(self, key, value) local data = reverse_map[self] assert(data) - data[key] = value + if data.weak_content[key] ~= nil then + data.weak_content[key] = value + else + data[key] = value + end end local obj_mt = { @@ -212,7 +229,15 @@ local function new_common(mod, _button, press, release) 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) end