2021-12-14 07:13:48 +01:00
|
|
|
local awful = require("awful")
|
2021-12-14 08:58:43 +01:00
|
|
|
local naughty = require("naughty")
|
|
|
|
|
2021-12-14 09:35:50 +01:00
|
|
|
function script_path()
|
|
|
|
local str = debug.getinfo(2, "S").source:sub(2)
|
|
|
|
return str:match("(.*/)")
|
|
|
|
end
|
|
|
|
|
2021-12-14 07:13:48 +01:00
|
|
|
local workspace_grid = {}
|
|
|
|
|
|
|
|
function workspace_grid:new(args)
|
2021-12-14 09:11:20 +01:00
|
|
|
return setmetatable({}, {__index = self}):init(args)
|
2021-12-14 07:13:48 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function workspace_grid:init(args)
|
2021-12-14 09:18:08 +01:00
|
|
|
self.rows = args.rows or 2
|
|
|
|
self.columns = args.columns or 3
|
2021-12-25 10:34:55 +01:00
|
|
|
self.position = args.position or "top_middle"
|
2022-01-23 19:04:14 +01:00
|
|
|
if args.visual == nil then
|
|
|
|
self.visual = true
|
|
|
|
else
|
|
|
|
self.visual = args.visual
|
|
|
|
end
|
2021-12-25 10:46:02 +01:00
|
|
|
self.cycle = args.cycle or false
|
2021-12-25 10:49:41 +01:00
|
|
|
self.icon_size = args.icon_size or 100
|
2022-01-23 19:04:14 +01:00
|
|
|
if args.switch_all_screens == nil then
|
|
|
|
self.switch_all_screens = true
|
|
|
|
else
|
|
|
|
self.switch_all_screens = args.switch_all_screens
|
|
|
|
end
|
2021-12-14 08:58:43 +01:00
|
|
|
|
2021-12-25 10:34:55 +01:00
|
|
|
if self.visual then
|
2021-12-14 08:58:43 +01:00
|
|
|
awful.screen.connect_for_each_screen(function(s)
|
|
|
|
s.workspace_notification_id = nil
|
|
|
|
end)
|
2021-12-25 08:36:03 +01:00
|
|
|
tag.connect_signal("property::selected", function(t)
|
|
|
|
self:on_tag_selected(t)
|
|
|
|
end)
|
2021-12-14 08:58:43 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return self
|
2021-12-14 07:13:48 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function workspace_grid:navigate(direction)
|
2021-12-14 09:11:20 +01:00
|
|
|
local t = awful.screen.focused().selected_tag
|
|
|
|
local i = t.index - 1
|
|
|
|
local c = self.columns
|
|
|
|
local r = self.rows
|
2021-12-14 07:13:48 +01:00
|
|
|
|
2021-12-25 10:46:02 +01:00
|
|
|
if not self.cycle then
|
|
|
|
-- Top row
|
|
|
|
if (i < c) and (direction == "up") then return true end
|
|
|
|
-- Left column
|
|
|
|
if (i % c == 0) and (direction == "left") then return true end
|
|
|
|
-- Right column
|
|
|
|
if ((i + 1) % c == 0) and (direction == "right") then return true end
|
|
|
|
-- Bottom row
|
|
|
|
if (i >= (r - 1) * c) and (direction == "down") then return true end
|
|
|
|
end
|
2021-12-14 07:13:48 +01:00
|
|
|
|
2021-12-14 09:11:20 +01:00
|
|
|
action = {
|
|
|
|
["down"] = (i + c) % (r * c) + 1,
|
|
|
|
["up"] = (i - c) % (r * c) + 1,
|
|
|
|
["left"] = (math.ceil((i + 1) / c) - 1) * c + ((i - 1) % c) + 1,
|
|
|
|
["right"] = (math.ceil((i + 1) / c) - 1) * c + ((i + 1) % c) + 1,
|
|
|
|
}
|
|
|
|
local j = action[direction]
|
2021-12-14 07:13:48 +01:00
|
|
|
|
2022-01-22 07:49:57 +01:00
|
|
|
if self.switch_all_screens then
|
|
|
|
-- Switch tags on all screens at the same time.
|
|
|
|
for s in screen do
|
2021-12-14 09:11:20 +01:00
|
|
|
t = s.tags[j]
|
|
|
|
if t then t:view_only() end
|
2022-01-22 07:49:57 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
-- Switch tags on the focused screen only.
|
2022-01-23 19:04:14 +01:00
|
|
|
t = awful.screen.focused().tags[j]
|
2022-01-22 07:49:57 +01:00
|
|
|
if t then t:view_only() end
|
|
|
|
end
|
2021-12-14 07:13:48 +01:00
|
|
|
end
|
|
|
|
|
2021-12-25 08:36:03 +01:00
|
|
|
function workspace_grid:on_tag_selected(t)
|
|
|
|
if t.screen == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- Only need to do anything on the focused screen.
|
|
|
|
if t.screen.index ~= awful.screen.focused().index then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if t.selected == false then
|
|
|
|
-- This is the tag we are leaving
|
|
|
|
return
|
|
|
|
end
|
2022-01-23 19:12:13 +01:00
|
|
|
icon_path = "icons/workspace_" .. self.rows .. "x" .. self.columns .. "_" .. t.index .. ".svg"
|
|
|
|
notification_config = {
|
|
|
|
icon = script_path() .. icon_path,
|
|
|
|
icon_size = self.icon_size,
|
|
|
|
margin = 0,
|
|
|
|
position = self.position,
|
|
|
|
preset = naughty.config.presets.normal,
|
|
|
|
text = nil,
|
|
|
|
timeout = 1,
|
|
|
|
}
|
|
|
|
if self.switch_all_screens then
|
|
|
|
for i = 1, screen.count() do
|
|
|
|
s = screen[i]
|
|
|
|
notification_config.screen = i
|
|
|
|
notification_config.replaces_id = s.workspace_notification_id
|
|
|
|
notification = naughty.notify(notification_config)
|
|
|
|
s.workspace_notification_id = notification.id
|
|
|
|
end
|
|
|
|
else
|
|
|
|
focused_screen = awful.screen.focused()
|
|
|
|
notification_config.screen = focused_screen.index
|
|
|
|
notification_config.replaces_id = focused_screen.workspace_notification_id
|
|
|
|
notification = naughty.notify(notification_config)
|
|
|
|
focused_screen.workspace_notification_id = notification.id
|
2021-12-25 08:36:03 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-14 09:11:20 +01:00
|
|
|
return setmetatable(workspace_grid, { __call = workspace_grid.new,})
|