diff options
| author | Vaxry <43317083+vaxerski@users.noreply.github.com> | 2026-05-06 13:56:12 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-05-06 13:56:12 +0100 |
| commit | 1681bea42dd2f11ba3fe6df05560d0b231de3c76 (patch) | |
| tree | 899103dbddf675a01758c7cdd379224c9c81a7fb /example/layouts/spiral.lua | |
| parent | 5f1350f522d3fe3af78cbe30a98311763b3509d5 (diff) | |
config/lua: add simple layout API (#14258)
Adds a simple layout lua api
Diffstat (limited to 'example/layouts/spiral.lua')
| -rw-r--r-- | example/layouts/spiral.lua | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/example/layouts/spiral.lua b/example/layouts/spiral.lua new file mode 100644 index 00000000..e97f4dbd --- /dev/null +++ b/example/layouts/spiral.lua @@ -0,0 +1,55 @@ +local state = { + ratio = 0.58, + offset = 0, +} + +local sides = { "left", "top", "right", "bottom" } +local opposite = { + left = "right", + right = "left", + top = "bottom", + bottom = "top", +} + +local function clamp(x, min, max) + return math.max(min, math.min(max, x)) +end + +hl.layout.register("spiral", { + recalculate = function(ctx) + local n = #ctx.targets + if n == 0 then + return + end + + local area = ctx.area + + for i, target in ipairs(ctx.targets) do + if i == n then + target:place(area) + else + local side = sides[((i - 1 + state.offset) % #sides) + 1] + target:place(ctx:split(area, side, state.ratio)) + area = ctx:split(area, opposite[side], 1.0 - state.ratio) + end + end + end, + + layout_msg = function(ctx, msg) + local command, arg = msg:match("^(%S+)%s*(.*)$") + + if command == "ratio" then + state.ratio = clamp(tonumber(arg) or state.ratio, 0.1, 0.9) + elseif command == "grow" then + state.ratio = clamp(state.ratio + 0.05, 0.1, 0.9) + elseif command == "shrink" then + state.ratio = clamp(state.ratio - 0.05, 0.1, 0.9) + elseif command == "rotate" then + state.offset = (state.offset + 1) % #sides + else + return "spiral: expected ratio <0.1..0.9>, grow, shrink, or rotate" + end + + return true + end, +}) |
