This commit is contained in:
Aire-One 2021-05-24 11:40:33 +02:00
commit 4d0f0b9be3
6 changed files with 263 additions and 0 deletions

189
init.lua Normal file
View File

@ -0,0 +1,189 @@
-----
-- AwesomeWM - Slot
--
-- An OO/Declarative API to connect signals for the AwesomeWM.
-- It completes the native `gears.signal` module to make signal connection
-- easier to manage.
-----
local gtable = require 'gears.table'
-- Load global awesome components from the C API
local capi = {
client = _G.client,
screen = _G.screen,
tag = _G.tag
}
local awesome_slot = {
mt = {},
_instance = nil,
slots = require 'awesome-slot.slots',
static_connect = {
client = capi.client,
screen = capi.screen,
tag = capi.tag,
ruled_client = require 'ruled.client',
ruled_notification = require 'ruled.notification'
}
}
-- Singleton
function awesome_slot.instance()
if not awesome_slot._instance then
awesome_slot._instance = {
slots = {}, -- List of created slot
-- Methods
do_action = awesome_slot.do_action,
create_slot = awesome_slot.create_slot,
delete_slot = awesome_slot.delete_slot,
connect_slot = awesome_slot.connect_slot,
disconnect_slot = awesome_slot.disconnect_slot,
get_slot = awesome_slot.get_slot
}
end
return awesome_slot._instance
end
function awesome_slot:get_slot(id)
assert(id)
return self.slots[id]
end
awesome_slot.actions = {
NONE = 0,
CREATE = 1,
DELETE = 1 << 1,
CONNECT = 1 << 2,
DISCONNECT = 1 << 3
}
awesome_slot.slot_status = {
CREATED = 1,
CONNECTED = 2
}
function awesome_slot:create_slot(params)
local id = params.id
-- Generate an id if none whas provided by the user
if not id then
id = #self.slots + 1
while self.slots[id] ~= nil do
id = id + 1
end
end
local slot = {}
slot.target = params.target
slot.signal = params.signal
slot.params = params.slot_params
if slot.params then
slot.callback = function ()
params.slot(slot.params)
end
else
slot.callback = params.slot
end
slot.status = awesome_slot.slot_status.CREATED
-- Insert the ne slot into the slots list
self.slots[id] = slot
return slot
end
function awesome_slot:delete_slot(params)
local slot = self:get_slot(params.id)
if not slot then
return false
end
if slot.status == awesome_slot.slot_status.CONNECTED then
print 'please disconnnect the slot before deleting it'
return false
end
self.slots[params.id] = nil
return true
end
function awesome_slot:connect_slot(params)
local slot = self:get_slot(params.id)
if not slot then
return false
end
-- 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
slot.status = awesome_slot.slot_status.CONNECTED
return slot
end
function awesome_slot:disconnect_slot(params)
local slot = self:get_slot(params.id)
if not slot then
return false
end
-- 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
slot.status = awesome_slot.slot_status.CREATED
return slot
end
function awesome_slot:do_action (params)
local params = params or {} -- luacheck: ignore shadowing params
local out = self
if not params.action then
return out
end
if awesome_slot.actions.DISCONNECT & params.action ~= 0 then
out = self:disconnect_slot(params)
end
if awesome_slot.actions.CREATE & params.action ~= 0 then
out = self:create_slot(params)
end
if awesome_slot.actions.DELETE & params.action ~= 0 then
out = self:delete_slot(params)
end
if awesome_slot.actions.CONNECT & params.action ~= 0 then
out = self:connect_slot(params)
end
return out
end
function awesome_slot.mt:__call(params)
return awesome_slot.instance():do_action(params)
end
return setmetatable(awesome_slot, awesome_slot.mt)

15
slots/client.lua Normal file
View File

@ -0,0 +1,15 @@
local client_slots = {}
function client_slots.append_mousebindings (params)
local amouse = require 'awful.mouse'
amouse.append_client_mousebindings(params.mousebindings)
end
function client_slots.append_keybindings (params)
local akeyboard = require 'awful.keyboard'
akeyboard.append_client_keybindings(params.keybindings)
end
return client_slots

8
slots/init.lua Normal file
View File

@ -0,0 +1,8 @@
local aslot_slots = {}
aslot_slots.client = require 'awesome-slot.slots.client'
aslot_slots.ruled = require 'awesome-slot.slots.ruled'
aslot_slots.screen = require 'awesome-slot.slots.screen'
aslot_slots.tag = require 'awesome-slot.slots.tag'
return aslot_slots

19
slots/ruled.lua Normal file
View File

@ -0,0 +1,19 @@
local ruled_slots = {}
function ruled_slots.append_client_rules (params)
local rclient = require 'ruled.client'
for _,rule in pairs(params.rules) do
rclient.append_rule(rule)
end
end
function ruled_slots.append_notification_rules (params)
local rnotification = require 'ruled.notification'
for _,rule in pairs(params.rules) do
rnotification.append_rule(rule)
end
end
return ruled_slots

23
slots/screen.lua Normal file
View File

@ -0,0 +1,23 @@
local screen_slots = {}
function screen_slots.wallpaper (screen, params)
local beautiful = require 'beautiful'
local gwallpaper = require 'gears.wallpaper'
params = params or {
wallpaper = beautiful.wallpaper
}
local wallpaper = params.wallpaper
if wallpaper then
-- If wallpaper is a function, call it with the screen
if type(wallpaper) == 'function' then
wallpaper = wallpaper(screen)
end
gwallpaper.maximized(wallpaper, screen, true)
end
end
return screen_slots

9
slots/tag.lua Normal file
View File

@ -0,0 +1,9 @@
local tag_slots = {}
function tag_slots.default_layouts (params)
local alayout = require 'awful.layout'
alayout.append_default_layouts(params.layouts)
end
return tag_slots