Cleaned up tagger code

This commit is contained in:
2022-04-24 12:57:41 -07:00
parent 58b2df6956
commit 9a13f2406d
5 changed files with 118 additions and 94 deletions

7
desktop/tagger/init.lua Normal file
View File

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

156
desktop/tagger/tagger.lua Executable file
View File

@ -0,0 +1,156 @@
-- Tag grid manager
local Tagger = {
screen = nil, rows = 2, cols = 4
}
local Widget = require("desktop.tagger.widget")
Tagger.__index = Tagger
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,
gap_single_client = false,
gap = beautiful.useless_gap,
volatile = false,
-- Only select the first tag
selected = ((r == 1) and (c == 1))
}
)
end
end
t.widget = Widget:new(t)
return t
end
function Tagger:get_tag()
return self.screen.selected_tag.index
end
-- Return current column
function Tagger:get_col()
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
end
-- Select a tag by position
function Tagger:setpos(row, col)
local target_absolute = (self.cols * (row - 1)) + col
local target_relative = target_absolute - self.screen.selected_tag.index
awful.tag.viewidx(target_relative)
self:update_widget()
end
-- Update this tagger's screen's tagindicator
function Tagger:update_widget()
local clients
local tgs = self.screen.tags
for i=1, #tgs do
clients = tgs[i]:clients()
-- Each tag indicator is a "checkbox" 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.widget.tagindicators[i].checked = true
else
self.widget.tagindicators[i].checked = false
end
-- Highlight tags that are not empty
if (#clients == 0) then
self.widget.tagindicators[i].border_color = beautiful.color.bar.inactive
else
self.widget.tagindicators[i].border_color = beautiful.color.bar.active
end
end
end
-- Navigation
function Tagger:up()
local row = self:get_row() - 1
local col = self:get_col()
if (row >= 1) then
self:setpos(row, col)
end
end
function Tagger:down()
local row = self:get_row() + 1
local col = self:get_col()
if (row <= self.rows) then
self:setpos(row, col)
end
end
function Tagger:right()
local row = self:get_row()
local col = self:get_col() + 1
if (col <= self.cols) then
self:setpos(row, col)
end
end
function Tagger:left()
local row = self:get_row()
local col = self:get_col() - 1
if (col >= 1) then
self:setpos(row, col)
end
end
-- Moving clients
function Tagger:move_client(client, direction)
if direction == "up" then
self:up()
elseif direction == "down" then
self:down()
elseif direction == "left" then
self:left()
elseif direction == "right" then
self:right()
end
if client.first_tag.layout == awful.layout.suit.floating then
client.floating = true
end
client:move_to_tag(self.screen.selected_tag)
self:update_widget()
end
return Tagger

74
desktop/tagger/widget.lua Normal file
View File

@ -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