adding keymapping to the layout_manager

This commit is contained in:
Squibid 2025-12-20 16:30:03 -05:00
parent 7f847ee468
commit 0417918562
Signed by: squibid
GPG key ID: BECE5684D3C4005D
3 changed files with 59 additions and 7 deletions

View file

@ -4,10 +4,18 @@
--- TODO: this may be overridden by the user by setting mez.layout prior to the --- TODO: this may be overridden by the user by setting mez.layout prior to the
--- runtime loading --- runtime loading
---@class layout a singular layout local mapper = require("mapper")
---@class layout.options options when creating a layout
---@field name string the name of the layout ---@field name string the name of the layout
---@field callback function this is the function which is run when changes to current state have been made ---@field callback function this is the function which is run when changes to current state have been made
---@field events string[]? this is a list of the events that callback will be run on ---@field events string[]? this is a list of the events that callback will be run on
---@field layout_maps mapper.map[]? a list of layout maps
---@field focus_maps mapper.map[]? a list of focus changing maps
---@field cursor_maps mapper.map[]? a list of focus changing maps
---@class layout : layout.options a singular layout
---@field callback_id number the id of the callback
---@class layout_manager manage all your layouts ---@class layout_manager manage all your layouts
---@field layouts layout[] all the layouts that are registered ---@field layouts layout[] all the layouts that are registered
@ -23,7 +31,7 @@ local layout_manager = {
} }
--- create a new layout --- create a new layout
---@param options layout ---@param options layout.options
---@return layout your_layout returns your new layout ---@return layout your_layout returns your new layout
function layout_manager.new_layout(options) function layout_manager.new_layout(options)
local layout = {} local layout = {}
@ -31,6 +39,9 @@ function layout_manager.new_layout(options)
layout.name = options.name layout.name = options.name
layout.callback = options.callback layout.callback = options.callback
layout.events = layout_manager.events layout.events = layout_manager.events
layout.layout_maps = options.layout_maps
layout.focus_maps = options.focus_maps
layout.cursor_maps = options.cursor_maps
-- store the new layout in the layout list -- store the new layout in the layout list
layout_manager.layouts[#layout_manager.layouts + 1] = layout layout_manager.layouts[#layout_manager.layouts + 1] = layout
@ -50,18 +61,30 @@ function layout_manager.set_layout(layout_name)
if layout_manager.current_layout then if layout_manager.current_layout then
local current_layout = layout_manager.current_layout local current_layout = layout_manager.current_layout
-- TODO: this actually doesn't exist in mez just yet mez.hook.del(layout_manager.current_layout.callback_id)
-- mez.hook.del(current_layout) for _, maps in ipairs({
current_layout.layout_maps,
current_layout.focus_maps,
current_layout.cursor_maps,
}) do
mapper.del(maps)
end end
mez.hook.add(layout.events, { end
layout_manager.current_layout = layout
layout.callback_id = mez.hook.add(layout.events, {
callback = function () callback = function ()
print("layouting")
for _, output in ipairs(mez.output.get_all_ids()) do for _, output in ipairs(mez.output.get_all_ids()) do
layout.callback(output) layout.callback(output)
end end
print("layouting done")
end, end,
}) })
for _, maps in ipairs({
layout.layout_maps,
layout.focus_maps,
layout.cursor_maps,
}) do
mapper.add(maps)
end
layout.callback() layout.callback()
end end

View file

@ -0,0 +1,28 @@
--- This is a wrapper around mez's keymapping system to allow for data driven
--- keymaps. The advantage of using this over the raw keybinds is the ability
--- to unmap and map a list of keymaps at runtime without too much hassle.
---@class mapper.map
---@field modifiers string formatted like in mez.input.add_keymap
---@field keys string formatted like in mez.input.add_keymap
---@field options table formatted like in mez.input.add_keymap
local mapper = {}
--- map a table of maps
---@param maps mapper.map[]
function mapper.add(maps)
for _, map in ipairs(maps) do
mez.input.add_keymap(map.modifiers, map.keys, map.options)
end
end
--- del a table of maps
---@param maps mapper.map[]
function mapper.del(maps)
for _, map in ipairs(maps) do
mez.input.del_keymap(map.modifiers, map.keys)
end
end
return mapper

View file

@ -1,6 +1,7 @@
mez.layout_manager.new_layout({ mez.layout_manager.new_layout({
name = "master", name = "master",
callback = function(output) callback = function(output)
if not output then return end
local mw, my, ty; local mw, my, ty;
local mon = mez.output.get_resolution(output) local mon = mez.output.get_resolution(output)