From 9a13f2406dda6e64122c40fa2a12548acb045fe2 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 24 Apr 2022 12:57:41 -0700 Subject: [PATCH] Cleaned up tagger code --- desktop/init.lua | 8 +--- desktop/tagger/init.lua | 7 +++ desktop/{ => tagger}/tagger.lua | 55 +++++++++++++++--------- desktop/tagger/widget.lua | 74 ++++++++++++++++++++++++++++++++ desktop/widgets/tagindicator.lua | 68 ----------------------------- 5 files changed, 118 insertions(+), 94 deletions(-) create mode 100644 desktop/tagger/init.lua rename desktop/{ => tagger}/tagger.lua (70%) create mode 100644 desktop/tagger/widget.lua delete mode 100755 desktop/widgets/tagindicator.lua diff --git a/desktop/init.lua b/desktop/init.lua index 2dc0ca3..41f732a 100755 --- a/desktop/init.lua +++ b/desktop/init.lua @@ -10,7 +10,6 @@ local desktop = { textclock = require("desktop.widgets.textclock"), layoutbox = require("desktop.widgets.layoutbox"), keymap = require("desktop.widgets.keymap"), - tagindicator = require("desktop.widgets.tagindicator"), launcher = require("desktop.widgets.launcher"), shortcut = require("desktop.widgets.shortcut"), --mdadm = require("desktop.widgets.mdadm"), @@ -88,10 +87,7 @@ awful.screen.connect_for_each_screen( function(s) -- s: the screen this function is being called for -- Create tag table - - s.tagger = desktop.Tagger:new(s) - desktop.widgets.tagindicator(s) - + s.tagger = desktop.Tagger{screen = s} -- Create a promptbox for each s s.mypromptbox = awful.widget.prompt() @@ -198,7 +194,7 @@ awful.screen.connect_for_each_screen( desktop.widgets.launcher, desktop.widgets.space(18), - s.tagindicator, + s.tagger.widget.widget, desktop.widgets.layoutbox(s), s.shortcuts, diff --git a/desktop/tagger/init.lua b/desktop/tagger/init.lua new file mode 100644 index 0000000..9b2bd77 --- /dev/null +++ b/desktop/tagger/init.lua @@ -0,0 +1,7 @@ +local P = require("desktop/tagger/tagger") + +local F = function(args) + return P:new(args) +end + +return F diff --git a/desktop/tagger.lua b/desktop/tagger/tagger.lua similarity index 70% rename from desktop/tagger.lua rename to desktop/tagger/tagger.lua index f337321..d79c10d 100755 --- a/desktop/tagger.lua +++ b/desktop/tagger/tagger.lua @@ -3,29 +3,39 @@ local Tagger = { screen = nil, rows = 2, cols = 4 } +local Widget = require("desktop.tagger.widget") Tagger.__index = Tagger -function Tagger:new(screen) - local t = {} - setmetatable(t, Tagger) - t.screen = screen +function Tagger:new(args) + local t = { + screen = args.screen, + rows = args.rows or 2, + cols = args.cols or 4, + } + setmetatable(t, self) -- Create tags on this Tagger's screen for r=1, t.rows do for c=1, t.cols do - awful.tag.add("(" .. tostring(r) .. ", " .. tostring(c) .. ")", { - layout = layoutmanager:default_layout(), - screen = t.screen, + awful.tag.add( + "(" .. tostring(r) .. ", " .. tostring(c) .. ")", + { + layout = layoutmanager:default_layout(), + screen = t.screen, - gap_single_client = false, - gap = beautiful.useless_gap, - volatile = false, + gap_single_client = false, + gap = beautiful.useless_gap, + volatile = false, - -- Only select the first tag - selected = ((r == 1) and (c == 1)) - }) + -- Only select the first tag + selected = ((r == 1) and (c == 1)) + } + ) end end + + t.widget = Widget:new(t) + return t end @@ -36,12 +46,18 @@ end -- Return current column function Tagger:get_col() - return ( (self:get_tag() - 1) % self.cols ) + 1 + return ( + (self:get_tag() - 1) % self.cols + ) + 1 end -- Return current row function Tagger:get_row() - return math.floor( (self:get_tag() - self:get_col()) / self.cols ) + 1 + return math.floor( + ( + self:get_tag() - self:get_col() + ) / self.cols + ) + 1 end -- Select a tag by position @@ -65,16 +81,16 @@ function Tagger:update_widget() -- Make that the currently active tag is checked and that all -- others are not. if (tgs[i].index == self.screen.selected_tag.index) then - self.screen.tagindicators[i].checked = true + self.widget.tagindicators[i].checked = true else - self.screen.tagindicators[i].checked = false + self.widget.tagindicators[i].checked = false end -- Highlight tags that are not empty if (#clients == 0) then - self.screen.tagindicators[i].border_color = beautiful.color.bar.inactive + self.widget.tagindicators[i].border_color = beautiful.color.bar.inactive else - self.screen.tagindicators[i].border_color = beautiful.color.bar.active + self.widget.tagindicators[i].border_color = beautiful.color.bar.active end end end @@ -117,7 +133,6 @@ function Tagger:left() end - -- Moving clients function Tagger:move_client(client, direction) if direction == "up" then diff --git a/desktop/tagger/widget.lua b/desktop/tagger/widget.lua new file mode 100644 index 0000000..ee1047b --- /dev/null +++ b/desktop/tagger/widget.lua @@ -0,0 +1,74 @@ +local P = {} +P.__index = P + +function P:new(tagger) + local widget = {} + setmetatable(widget, self) + + + widget.tagindicator = wibox.widget { + homogeneous = true, + spacing = beautiful.dpi(2), + min_cols_size = 10, + min_rows_size = 10, + layout = wibox.layout.grid + } + + -- Calculate checkbox size limit + local cbox_maxsize = beautiful.dpi(conf.bar_height) + cbox_maxsize = cbox_maxsize - (2 * beautiful.dpi(conf.bar_margin)) + cbox_maxsize = cbox_maxsize - (tagger.rows - 1)*(beautiful.dpi(widget.tagindicator.spacing)) + + -- Make each tag's checkmark + widget.tagindicators = {} + for r=1, tagger.rows do + for c=1, tagger.cols do + local t = wibox.widget { + widget = wibox.widget.checkbox, + + bg = beautiful.color.transparent, + checked = false, + border_width = beautiful.dpi(2), + paddings = beautiful.dpi(3), + color = beautiful.color.bar.active, + border_color = beautiful.color.bar.inactive, + } + + widget.tagindicators[ + (c + (tagger.cols * (r - 1))) + ] = t + + if ((conf.bar_position == "bottom") or (conf.bar_position == "top")) then + cbox_maxsize = cbox_maxsize / tagger.rows + else + cbox_maxsize = cbox_maxsize / tagger.cols + end + + widget.tagindicator:add_widget_at( + -- The constraint container is necessary here, without it + -- the checkboxes will fill all the height that is available to them. + wibox.container.constraint( + widget.tagindicators[(c + (tagger.cols * (r - 1)))], + "exact", cbox_maxsize, cbox_maxsize + ), r, c) + end + end + + -- Wrap everything with a background container + widget.widget = wibox.widget { + widget.tagindicator, + layout = wibox.container.background + } + + widget.widget:connect_signal("mouse::enter", function(result) + widget.bg = beautiful.color.bar.hover_bg + end) + + widget.widget:connect_signal("mouse::leave", function(result) + widget.bg = beautiful.color.transparent + end) + + return widget +end + +return P diff --git a/desktop/widgets/tagindicator.lua b/desktop/widgets/tagindicator.lua deleted file mode 100755 index 698a873..0000000 --- a/desktop/widgets/tagindicator.lua +++ /dev/null @@ -1,68 +0,0 @@ -local tagindicator = {} - -tagindicator.make = function(screen) - local widget - - -- Create tag tagindicators - -- We're using flex.vertical and flex.horizontal layouts because the grid - -- layout doesn't expand things properly. - screen.tagindicators = {} - screen.tagindicator = wibox.widget { - homogeneous = true, - spacing = beautiful.dpi(2), - min_cols_size = 10, - min_rows_size = 10, - layout = wibox.layout.grid - } - - local tmp_row - for r=1, screen.tagger.rows do - for c=1, screen.tagger.cols do - screen.tagindicators[(c + (screen.tagger.cols * (r - 1)))] = wibox.widget { - checked = false, - border_width = beautiful.dpi(2), - paddings = beautiful.dpi(3), - color = beautiful.color.bar.active, - border_color = beautiful.color.bar.inactive, - widget = wibox.widget.checkbox - } - - -- Calculate checkbox size limit - local cbox_maxsize = beautiful.dpi(conf.bar_height) - cbox_maxsize = cbox_maxsize - (2 * beautiful.dpi(conf.bar_margin)) - cbox_maxsize = cbox_maxsize - (screen.tagger.rows - 1)*(beautiful.dpi(screen.tagindicator.spacing)) - - if ((conf.bar_position == "bottom") or (conf.bar_position == "top")) then - cbox_maxsize = cbox_maxsize / screen.tagger.rows - else - cbox_maxsize = cbox_maxsize / screen.tagger.cols - end - - screen.tagindicator:add_widget_at( - -- The constraint container is VERY necessary here! - -- Otherwise, the checkboxes will fill all the height that is available to them. - wibox.container.constraint( - screen.tagindicators[(c + (screen.tagger.cols * (r - 1)))], - "exact", cbox_maxsize, cbox_maxsize - ), r, c) - end - end - - widget = wibox.widget { - screen.tagindicator, - layout = wibox.container.background - } - - -- Change background when mouse is over widget - widget:connect_signal("mouse::enter", function(result) - widget.bg = beautiful.color.bar.hover_bg - end) - - widget:connect_signal("mouse::leave", function(result) - widget.bg = beautiful.color.transparent - end) - - return widget -end - -return tagindicator.make