From 1c5d1dc55356b18b947d6a62954ca7ad43cf28ae Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 24 Apr 2022 13:26:02 -0700 Subject: [PATCH] Cleaned up layoutmanager code --- binds/window/layout.lua | 4 +- desktop/init.lua | 10 +- desktop/layoutmanager.lua | 86 ---------------- desktop/layoutmanager/init.lua | 7 ++ desktop/layoutmanager/layoutmanager.lua | 99 +++++++++++++++++++ .../widget.lua} | 24 ++--- desktop/tagger/tagger.lua | 3 +- rc.lua | 3 - 8 files changed, 129 insertions(+), 107 deletions(-) delete mode 100644 desktop/layoutmanager.lua create mode 100644 desktop/layoutmanager/init.lua create mode 100644 desktop/layoutmanager/layoutmanager.lua rename desktop/{widgets/layoutbox.lua => layoutmanager/widget.lua} (60%) mode change 100755 => 100644 diff --git a/binds/window/layout.lua b/binds/window/layout.lua index bd30d34..c88e06e 100755 --- a/binds/window/layout.lua +++ b/binds/window/layout.lua @@ -61,7 +61,7 @@ return gears.table.join( awful.key( {"Mod4"}, "l", function () - layoutmanager:next() + awful.screen.focused().layoutmanager:next() end, { description = "select layouts", @@ -71,7 +71,7 @@ return gears.table.join( awful.key( {"Mod4"}, "k", function () - layoutmanager:next_alt() + awful.screen.focused().layoutmanager:next_alt() end, { description = "cycle alt layouts", diff --git a/desktop/init.lua b/desktop/init.lua index 41f732a..089192d 100755 --- a/desktop/init.lua +++ b/desktop/init.lua @@ -1,5 +1,6 @@ local desktop = { Tagger = require("desktop.tagger"), + LayoutManager = require("desktop.layoutmanager"), popup = { language = require("desktop.popups.language") @@ -8,7 +9,6 @@ local desktop = { widgets = { tasklist = require("desktop.widgets.tasklist"), textclock = require("desktop.widgets.textclock"), - layoutbox = require("desktop.widgets.layoutbox"), keymap = require("desktop.widgets.keymap"), launcher = require("desktop.widgets.launcher"), shortcut = require("desktop.widgets.shortcut"), @@ -87,7 +87,11 @@ awful.screen.connect_for_each_screen( function(s) -- s: the screen this function is being called for -- 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 s.mypromptbox = awful.widget.prompt() @@ -195,7 +199,7 @@ awful.screen.connect_for_each_screen( desktop.widgets.space(18), s.tagger.widget.widget, - desktop.widgets.layoutbox(s), + s.layoutmanager.widget, s.shortcuts, diff --git a/desktop/layoutmanager.lua b/desktop/layoutmanager.lua deleted file mode 100644 index d1acc72..0000000 --- a/desktop/layoutmanager.lua +++ /dev/null @@ -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 diff --git a/desktop/layoutmanager/init.lua b/desktop/layoutmanager/init.lua new file mode 100644 index 0000000..bac736b --- /dev/null +++ b/desktop/layoutmanager/init.lua @@ -0,0 +1,7 @@ +local P = require("desktop/layoutmanager/layoutmanager") + +local F = function(args) + return P:new(args) +end + +return F diff --git a/desktop/layoutmanager/layoutmanager.lua b/desktop/layoutmanager/layoutmanager.lua new file mode 100644 index 0000000..12106c5 --- /dev/null +++ b/desktop/layoutmanager/layoutmanager.lua @@ -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 diff --git a/desktop/widgets/layoutbox.lua b/desktop/layoutmanager/widget.lua old mode 100755 new mode 100644 similarity index 60% rename from desktop/widgets/layoutbox.lua rename to desktop/layoutmanager/widget.lua index 21f5d74..11890d7 --- a/desktop/widgets/layoutbox.lua +++ b/desktop/layoutmanager/widget.lua @@ -1,19 +1,20 @@ -local layoutbox = {} +local P = {} +P.__index = P -layoutbox.make = function(screen) - local widget +function P:new(layoutmanager) + 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), layout = wibox.container.margin, }, layout = wibox.container.background, } - -- Setup buttons - widget:buttons( + widget.widget:buttons( gears.table.join( awful.button({ }, 1, function () layoutmanager:next_alt() end), awful.button({ }, 3, function () layoutmanager:prev_alt() end), @@ -22,17 +23,16 @@ layoutbox.make = function(screen) ) ) - - -- Change background when mouse is over widget - widget:connect_signal("mouse::enter", function(result) + widget.widget:connect_signal("mouse::enter", function(result) widget.bg = beautiful.color.bar.hover_bg end) - widget:connect_signal("mouse::leave", function(result) + widget.widget:connect_signal("mouse::leave", function(result) widget.bg = beautiful.color.transparent end) + return widget end -return layoutbox.make +return P diff --git a/desktop/tagger/tagger.lua b/desktop/tagger/tagger.lua index d79c10d..8e22839 100755 --- a/desktop/tagger/tagger.lua +++ b/desktop/tagger/tagger.lua @@ -9,6 +9,7 @@ Tagger.__index = Tagger function Tagger:new(args) local t = { screen = args.screen, + layoutmanager = args.layoutmanager, rows = args.rows or 2, cols = args.cols or 4, } @@ -20,7 +21,7 @@ function Tagger:new(args) awful.tag.add( "(" .. tostring(r) .. ", " .. tostring(c) .. ")", { - layout = layoutmanager:default_layout(), + layout = t.layoutmanager:get_default_layout(), screen = t.screen, gap_single_client = false, diff --git a/rc.lua b/rc.lua index 94db145..a4108d4 100755 --- a/rc.lua +++ b/rc.lua @@ -27,9 +27,6 @@ conf = require("conf") conf.sound_dir = configuration_dir .. "theme/resources/sounds/" conf.icon_dir = configuration_dir .. "theme/resources/icons/" -layoutmanager = require("desktop.layoutmanager") -layoutmanager.layouts = conf.layouts - bin = require("bin") wrapper = require("wrapper") wrapper.ibus.set(1)