2021-05-24 11:40:33 +02:00
|
|
|
-----
|
|
|
|
-- AwesomeWM - Slot
|
|
|
|
--
|
2021-10-16 16:10:23 +02:00
|
|
|
-- A declarative API to connect signals for the AwesomeWM.
|
2021-05-24 11:40:33 +02:00
|
|
|
-- It completes the native `gears.signal` module to make signal connection
|
|
|
|
-- easier to manage.
|
|
|
|
-----
|
|
|
|
|
2021-10-16 15:53:00 +02:00
|
|
|
local gtable = require "gears.table"
|
2021-05-24 11:40:33 +02:00
|
|
|
|
|
|
|
-- Load global awesome components from the C API
|
|
|
|
local capi = {
|
|
|
|
client = _G.client,
|
|
|
|
screen = _G.screen,
|
2021-10-16 15:53:00 +02:00
|
|
|
tag = _G.tag,
|
2021-05-24 11:40:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
local awesome_slot = {
|
|
|
|
mt = {},
|
2021-10-16 15:53:00 +02:00
|
|
|
slots = require "awesome-slot.slots",
|
2021-05-24 11:40:33 +02:00
|
|
|
static_connect = {
|
|
|
|
client = capi.client,
|
|
|
|
screen = capi.screen,
|
|
|
|
tag = capi.tag,
|
2021-10-16 15:53:00 +02:00
|
|
|
ruled_client = require "ruled.client",
|
|
|
|
ruled_notification = require "ruled.notification",
|
|
|
|
},
|
2021-10-16 16:10:23 +02:00
|
|
|
_private = {
|
|
|
|
registered_slots = {},
|
2021-10-16 15:53:00 +02:00
|
|
|
},
|
2021-05-24 11:40:33 +02:00
|
|
|
}
|
|
|
|
|
2021-10-16 14:42:16 +02:00
|
|
|
local function generate_id(base_id)
|
|
|
|
local id = base_id
|
|
|
|
local n = 0
|
|
|
|
|
|
|
|
while awesome_slot.slots[id] ~= nil do
|
|
|
|
n = n + 1
|
|
|
|
id = base_id .. "_#" .. n
|
|
|
|
end
|
|
|
|
|
|
|
|
return id
|
|
|
|
end
|
|
|
|
|
2021-10-16 16:10:23 +02:00
|
|
|
function awesome_slot.get_slot(slot)
|
2021-10-16 15:53:00 +02:00
|
|
|
assert(slot)
|
|
|
|
local id = type(slot) == "string" and slot or slot.id
|
|
|
|
assert(id, "Slot not found!")
|
2021-05-24 11:40:33 +02:00
|
|
|
|
2021-10-16 16:10:23 +02:00
|
|
|
return awesome_slot._private.registered_slots[id]
|
2021-05-24 11:40:33 +02:00
|
|
|
end
|
|
|
|
|
2021-10-16 16:10:23 +02:00
|
|
|
function awesome_slot.create(params)
|
2021-05-24 11:40:33 +02:00
|
|
|
local slot = {}
|
|
|
|
|
2021-10-16 15:53:00 +02:00
|
|
|
slot.id = generate_id(params.id or "UNNAMED_SLOT")
|
2021-05-24 11:40:33 +02:00
|
|
|
slot.target = params.target
|
|
|
|
slot.signal = params.signal
|
2021-10-16 16:10:23 +02:00
|
|
|
slot.connected = false
|
2021-05-24 11:40:33 +02:00
|
|
|
|
2021-10-16 15:53:00 +02:00
|
|
|
if params.slot_params then
|
|
|
|
slot.params = params.slot_params
|
|
|
|
slot.callback = function()
|
2021-05-24 11:40:33 +02:00
|
|
|
params.slot(slot.params)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
slot.callback = params.slot
|
|
|
|
end
|
|
|
|
|
2021-10-16 15:53:00 +02:00
|
|
|
-- Insert the new slot into the slots list
|
2021-10-16 16:10:23 +02:00
|
|
|
awesome_slot._private.registered_slots[slot.id] = slot
|
|
|
|
|
|
|
|
if params.connect then
|
|
|
|
awesome_slot.connect(slot)
|
|
|
|
end
|
2021-05-24 11:40:33 +02:00
|
|
|
|
|
|
|
return slot
|
|
|
|
end
|
|
|
|
|
2021-10-16 16:10:23 +02:00
|
|
|
function awesome_slot.remove(params)
|
|
|
|
local slot = awesome_slot.get_slot(params)
|
2021-05-24 11:40:33 +02:00
|
|
|
|
2021-10-16 16:10:23 +02:00
|
|
|
if slot.connected then
|
|
|
|
awesome_slot.disconnect_slot(slot)
|
2021-05-24 11:40:33 +02:00
|
|
|
end
|
|
|
|
|
2021-10-16 16:10:23 +02:00
|
|
|
awesome_slot._private.registered_slots[slot.id] = nil
|
2021-05-24 11:40:33 +02:00
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2021-10-16 16:10:23 +02:00
|
|
|
function awesome_slot.connect(params)
|
|
|
|
local slot = awesome_slot.get_slot(params)
|
2021-05-24 11:40:33 +02:00
|
|
|
|
|
|
|
-- Some modules expose a static connect_signals function
|
|
|
|
-- at the module level, while other tables/objects inheriting from
|
|
|
|
-- gears.object implement the signal connection API at the instance level.
|
|
|
|
if gtable.hasitem(awesome_slot.static_connect, slot.target) then
|
|
|
|
slot.target.connect_signal(slot.signal, slot.callback)
|
|
|
|
else
|
|
|
|
slot.target:connect_signal(slot.signal, slot.callback)
|
|
|
|
end
|
|
|
|
|
2021-10-16 16:10:23 +02:00
|
|
|
slot.connected = true
|
2021-05-24 11:40:33 +02:00
|
|
|
|
|
|
|
return slot
|
|
|
|
end
|
|
|
|
|
2021-10-16 16:10:23 +02:00
|
|
|
function awesome_slot.disconnect(params)
|
|
|
|
local slot = awesome_slot.get_slot(params)
|
2021-05-24 11:40:33 +02:00
|
|
|
|
|
|
|
-- Please check the `:connect_slot` method to understand why we do this.
|
|
|
|
if gtable.hasitem(awesome_slot.static_connect, slot.target) then
|
|
|
|
slot.target.disconnect_slot(slot.signal, slot.callback)
|
|
|
|
else
|
|
|
|
slot.target:disconnect_slot(slot.signal, slot.callback)
|
|
|
|
end
|
|
|
|
|
2021-10-16 16:10:23 +02:00
|
|
|
slot.connected = false
|
2021-05-24 11:40:33 +02:00
|
|
|
|
|
|
|
return slot
|
|
|
|
end
|
|
|
|
|
2021-10-16 15:53:00 +02:00
|
|
|
function awesome_slot.mt:__call(...) -- luacheck: ignore unused argument self
|
2021-10-16 16:10:23 +02:00
|
|
|
return awesome_slot.create(...)
|
2021-05-24 11:40:33 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(awesome_slot, awesome_slot.mt)
|
2021-10-16 15:33:51 +02:00
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|