Cleaned up layoutmanager code

master
Mark 2022-04-24 13:26:02 -07:00
parent 9a13f2406d
commit 1c5d1dc553
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
8 changed files with 129 additions and 107 deletions

View File

@ -61,7 +61,7 @@ return gears.table.join(
awful.key( {"Mod4"}, "l", awful.key( {"Mod4"}, "l",
function () function ()
layoutmanager:next() awful.screen.focused().layoutmanager:next()
end, end,
{ {
description = "select layouts", description = "select layouts",
@ -71,7 +71,7 @@ return gears.table.join(
awful.key( {"Mod4"}, "k", awful.key( {"Mod4"}, "k",
function () function ()
layoutmanager:next_alt() awful.screen.focused().layoutmanager:next_alt()
end, end,
{ {
description = "cycle alt layouts", description = "cycle alt layouts",

View File

@ -1,5 +1,6 @@
local desktop = { local desktop = {
Tagger = require("desktop.tagger"), Tagger = require("desktop.tagger"),
LayoutManager = require("desktop.layoutmanager"),
popup = { popup = {
language = require("desktop.popups.language") language = require("desktop.popups.language")
@ -8,7 +9,6 @@ local desktop = {
widgets = { widgets = {
tasklist = require("desktop.widgets.tasklist"), tasklist = require("desktop.widgets.tasklist"),
textclock = require("desktop.widgets.textclock"), textclock = require("desktop.widgets.textclock"),
layoutbox = require("desktop.widgets.layoutbox"),
keymap = require("desktop.widgets.keymap"), keymap = require("desktop.widgets.keymap"),
launcher = require("desktop.widgets.launcher"), launcher = require("desktop.widgets.launcher"),
shortcut = require("desktop.widgets.shortcut"), shortcut = require("desktop.widgets.shortcut"),
@ -87,7 +87,11 @@ awful.screen.connect_for_each_screen(
function(s) function(s)
-- s: the screen this function is being called for -- s: the screen this function is being called for
-- Create tag table -- Create tag table
s.tagger = desktop.Tagger{screen = s} s.layoutmanager = desktop.LayoutManager{screen = s}
s.tagger = desktop.Tagger{
screen = s,
layoutmanager = s.layoutmanager
}
-- Create a promptbox for each s -- Create a promptbox for each s
s.mypromptbox = awful.widget.prompt() s.mypromptbox = awful.widget.prompt()
@ -195,7 +199,7 @@ awful.screen.connect_for_each_screen(
desktop.widgets.space(18), desktop.widgets.space(18),
s.tagger.widget.widget, s.tagger.widget.widget,
desktop.widgets.layoutbox(s), s.layoutmanager.widget,
s.shortcuts, s.shortcuts,

View File

@ -1,86 +0,0 @@
-- Client layout manager
local LayoutManager = {
layouts = nil, -- Table of groups
-- current state is kept in each tag, as tag.layout.
}
-- screen.layouts, awful.layouts, and tag.layouts are all ignored.
-- This module replaces the standard layout functions.
-- Get group from layout
-- We assume that this layout is in LayoutManager.layouts
function LayoutManager:get_group(layout)
for k, v in pairs(self.layouts) do
for l, m in pairs(v) do
if (layout == m) then
return k
end
end
end
end
function LayoutManager:get_alt(layout)
for k, v in pairs(self.layouts) do
for l, m in pairs(v) do
if (layout == m) then
return l
end
end
end
end
function LayoutManager:default_layout()
return self.layouts[1][1]
end
-- Change layout group
function LayoutManager:group(step)
local s = awful.screen.focused()
local tag = s.selected_tag -- Multiple selected tags are NOT supported!
local layout = tag.layout
-- Subtract 1, 'cuz lua doesn't start at 0
local group = self:get_group(layout) - 1
group = ((group + step) % #self.layouts) + 1
awful.layout.set(self.layouts[group][1], tag)
end
-- Change layout alternate
function LayoutManager:alt(step)
local s = awful.screen.focused()
local tag = s.selected_tag -- Multiple selected tags are NOT supported!
local layout = tag.layout
-- Subtract 1, 'cuz lua doesn't start at 0
local alt = self:get_alt(layout) - 1
local group = self:get_group(layout)
alt = ((alt + step) % #self.layouts[group]) + 1
awful.layout.set(self.layouts[group][alt], tag)
end
function LayoutManager:next()
self:group(1)
end
function LayoutManager:prev()
self:group(-1)
end
function LayoutManager:next_alt()
self:alt(1)
end
function LayoutManager:prev_alt()
self:alt(-1)
end
return LayoutManager

View File

@ -0,0 +1,7 @@
local P = require("desktop/layoutmanager/layoutmanager")
local F = function(args)
return P:new(args)
end
return F

View File

@ -0,0 +1,99 @@
local P = {}
local Widget = require("desktop.layoutmanager.widget")
P.__index = P
-- screen.layouts, awful.layouts, and tag.layouts are all ignored.
-- This module replaces the standard layout functions.
function P:new(screen)
local l = {
layouts = conf.layouts,
screen = screen.screen
}
setmetatable(l, self)
l._widget = Widget:new(l)
l.widget = l._widget.widget
return l
--[[ Example
conf.layouts = {
{
awful.layout.suit.tile.left,
awful.layout.suit.tile,
},
{
awful.layout.suit.fair,
awful.layout.suit.fair.horizontal,
},
{
awful.layout.suit.floating,
}
}
--]]
end
-- Find layout in self.layouts
-- Returns group index, alt index
function P:find_layout(layout)
for k, v in pairs(self.layouts) do
for l, m in pairs(v) do
if (layout == m) then
return k, l
end
end
end
end
function P:get_default_layout()
return self.layouts[1][1]
end
-- Change layout group
function P:change_group(step)
local tag = self.screen.selected_tag -- Multiple selected tags are NOT supported!
local layout = tag.layout
-- Subtract 1, lua doesn't start at 0
local g, a
g, a = self:find_layout(layout)
g = g - 1
g = ((g + step) % #self.layouts) + 1
awful.layout.set(self.layouts[g][1], tag)
end
-- Change layout alternate
function P:change_alt(step)
local tag = self.screen.selected_tag -- Multiple selected tags are NOT supported!
local layout = tag.layout
-- Subtract 1, lua doesn't start at 0
local g, a
g, a = self:find_layout(layout)
a = a - 1
a = ((a + step) % #self.layouts[g]) + 1
awful.layout.set(self.layouts[g][a], tag)
end
function P:next()
self:change_group(1)
end
function P:prev()
self:change_group(-1)
end
function P:next_alt()
self:change_alt(1)
end
function P:prev_alt()
self:change_alt(-1)
end
return P

View File

@ -1,19 +1,20 @@
local layoutbox = {} local P = {}
P.__index = P
layoutbox.make = function(screen) function P:new(layoutmanager)
local widget local widget = {}
setmetatable(widget, self)
widget = wibox.widget { widget.widget = wibox.widget {
{ {
awful.widget.layoutbox(screen), awful.widget.layoutbox(layoutmanager.screen),
margins = beautiful.dpi(3), margins = beautiful.dpi(3),
layout = wibox.container.margin, layout = wibox.container.margin,
}, },
layout = wibox.container.background, layout = wibox.container.background,
} }
-- Setup buttons widget.widget:buttons(
widget:buttons(
gears.table.join( gears.table.join(
awful.button({ }, 1, function () layoutmanager:next_alt() end), awful.button({ }, 1, function () layoutmanager:next_alt() end),
awful.button({ }, 3, function () layoutmanager:prev_alt() end), awful.button({ }, 3, function () layoutmanager:prev_alt() end),
@ -22,17 +23,16 @@ layoutbox.make = function(screen)
) )
) )
widget.widget:connect_signal("mouse::enter", function(result)
-- Change background when mouse is over widget
widget:connect_signal("mouse::enter", function(result)
widget.bg = beautiful.color.bar.hover_bg widget.bg = beautiful.color.bar.hover_bg
end) end)
widget:connect_signal("mouse::leave", function(result) widget.widget:connect_signal("mouse::leave", function(result)
widget.bg = beautiful.color.transparent widget.bg = beautiful.color.transparent
end) end)
return widget return widget
end end
return layoutbox.make return P

View File

@ -9,6 +9,7 @@ Tagger.__index = Tagger
function Tagger:new(args) function Tagger:new(args)
local t = { local t = {
screen = args.screen, screen = args.screen,
layoutmanager = args.layoutmanager,
rows = args.rows or 2, rows = args.rows or 2,
cols = args.cols or 4, cols = args.cols or 4,
} }
@ -20,7 +21,7 @@ function Tagger:new(args)
awful.tag.add( awful.tag.add(
"(" .. tostring(r) .. ", " .. tostring(c) .. ")", "(" .. tostring(r) .. ", " .. tostring(c) .. ")",
{ {
layout = layoutmanager:default_layout(), layout = t.layoutmanager:get_default_layout(),
screen = t.screen, screen = t.screen,
gap_single_client = false, gap_single_client = false,

3
rc.lua
View File

@ -27,9 +27,6 @@ conf = require("conf")
conf.sound_dir = configuration_dir .. "theme/resources/sounds/" conf.sound_dir = configuration_dir .. "theme/resources/sounds/"
conf.icon_dir = configuration_dir .. "theme/resources/icons/" conf.icon_dir = configuration_dir .. "theme/resources/icons/"
layoutmanager = require("desktop.layoutmanager")
layoutmanager.layouts = conf.layouts
bin = require("bin") bin = require("bin")
wrapper = require("wrapper") wrapper = require("wrapper")
wrapper.ibus.set(1) wrapper.ibus.set(1)