capi.button: Enable the miss handlers.
This commit is contained in:
parent
fe603f7dc5
commit
42a906f300
|
@ -14,6 +14,7 @@ local ipairs = ipairs
|
||||||
local capi = { button = button, root = root }
|
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")
|
||||||
|
local gobject = require("gears.object")
|
||||||
|
|
||||||
local button = { mt = {} }
|
local button = { mt = {} }
|
||||||
|
|
||||||
|
@ -118,7 +119,7 @@ function button:set_modifiers(mod)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, prop in ipairs { "description", "group", "on_press", "on_release", "name" } do
|
for _, prop in ipairs { "description", "group", "name" } do
|
||||||
button["get_"..prop] = function(self)
|
button["get_"..prop] = function(self)
|
||||||
return reverse_map[self][prop]
|
return reverse_map[self][prop]
|
||||||
end
|
end
|
||||||
|
@ -127,6 +128,41 @@ for _, prop in ipairs { "description", "group", "on_press", "on_release", "name"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Store the callbacks as weakly as possible.
|
||||||
|
--
|
||||||
|
-- Lua 5.1 GC is not very smart with reference loops. When the titlebar has
|
||||||
|
-- buttons, they usually contain the client in their lambda. In turn, storing
|
||||||
|
-- the callback in something stored by the titlebar, which is stored by the
|
||||||
|
-- clients, will not be GC-able as-is.
|
||||||
|
--
|
||||||
|
-- To work around this, we use weak `v` references stored within a weak `k`
|
||||||
|
-- table. So both the `awful.button` and the callbacks are weak in their own
|
||||||
|
-- realm. To avoid the GC of the callback, it is important to use
|
||||||
|
-- `connect_signal` on the `capi.button` to keep a reference alive.
|
||||||
|
for _, prop in ipairs { "press", "release" } do
|
||||||
|
local long_name = "on_"..prop
|
||||||
|
|
||||||
|
button["get_"..long_name] = function(self)
|
||||||
|
return reverse_map[self].weak_content[long_name]
|
||||||
|
end
|
||||||
|
button["set_"..long_name] = function(self, value)
|
||||||
|
local old = reverse_map[self].weak_content[long_name]
|
||||||
|
local new = function(_, ...) value(...) end
|
||||||
|
|
||||||
|
if old then
|
||||||
|
for i=1, 4 do
|
||||||
|
self[i]:disconnect_signal(prop, old)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
reverse_map[self].weak_content[prop] = new
|
||||||
|
|
||||||
|
for i=1, 4 do
|
||||||
|
self[i]:connect_signal(prop, new)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function button:get_button()
|
function button:get_button()
|
||||||
return self[1].button
|
return self[1].button
|
||||||
end
|
end
|
||||||
|
@ -134,13 +170,13 @@ end
|
||||||
function button:trigger()
|
function button:trigger()
|
||||||
local data = reverse_map[self]
|
local data = reverse_map[self]
|
||||||
|
|
||||||
local press = data.weak_content.press
|
local press = self.on_press or data.weak_content.press
|
||||||
|
|
||||||
if press then
|
if press then
|
||||||
press()
|
press()
|
||||||
end
|
end
|
||||||
|
|
||||||
local release = data.weak_content.release
|
local release = self.on_release or data.weak_content.release
|
||||||
|
|
||||||
if release then
|
if release then
|
||||||
release()
|
release()
|
||||||
|
@ -151,6 +187,13 @@ function button:get_has_root_binding()
|
||||||
return capi.root.has_button(self)
|
return capi.root.has_button(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- This is used by the keygrabber and prompt to identify valid awful.button
|
||||||
|
-- objects. It *cannot* be put directly in the object since `capi` uses a lot
|
||||||
|
-- of `next` internally and fixing that would suck more.
|
||||||
|
function button:get__is_awful_button()
|
||||||
|
return true
|
||||||
|
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)
|
||||||
|
@ -219,14 +262,14 @@ local function new_common(mod, _button, press, release)
|
||||||
local ret = {}
|
local ret = {}
|
||||||
local subsets = gmath.subsets(ignore_modifiers)
|
local subsets = gmath.subsets(ignore_modifiers)
|
||||||
for _, set in ipairs(subsets) do
|
for _, set in ipairs(subsets) do
|
||||||
ret[#ret + 1] = capi.button({ modifiers = gtable.join(mod, set),
|
local sub_button = capi.button {
|
||||||
button = _button })
|
modifiers = gtable.join(mod, set),
|
||||||
if press then
|
button = _button
|
||||||
ret[#ret]:connect_signal("press", function(_, ...) press(...) end)
|
}
|
||||||
end
|
|
||||||
if release then
|
sub_button._private._legacy_convert_to = ret
|
||||||
ret[#ret]:connect_signal("release", function (_, ...) release(...) end)
|
|
||||||
end
|
ret[#ret + 1] = sub_button
|
||||||
end
|
end
|
||||||
|
|
||||||
reverse_map[ret] = {
|
reverse_map[ret] = {
|
||||||
|
@ -239,6 +282,14 @@ local function new_common(mod, _button, press, release)
|
||||||
_is_capi_button = false
|
_is_capi_button = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if press then
|
||||||
|
button.set_on_press(ret, press)
|
||||||
|
end
|
||||||
|
|
||||||
|
if release then
|
||||||
|
button.set_on_release(ret, release)
|
||||||
|
end
|
||||||
|
|
||||||
return setmetatable(ret, obj_mt)
|
return setmetatable(ret, obj_mt)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -261,6 +312,10 @@ function button.mt:__call(...)
|
||||||
return button.new(...)
|
return button.new(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
gobject.properties(capi.button, {
|
||||||
|
auto_emit = true,
|
||||||
|
})
|
||||||
|
|
||||||
return setmetatable(button, button.mt)
|
return setmetatable(button, button.mt)
|
||||||
|
|
||||||
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|
||||||
|
|
Loading…
Reference in New Issue