summaryrefslogtreecommitdiff
path: root/.config/sketchybar/items/spaces.lua
blob: 28b1ebb947194462c2b22a565a90affa8b958ec6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
-- TODO: Highlighting is kinda fixed, multiple windows in one space label is broken, last two things, but annoying
local constants = require("constants")
local settings = require("config.settings")

local spaces = {}

local swapWatcher = sbar.add("item", {
	drawing = false,
	updates = true,
})

local currentWorkspaceWatcher = sbar.add("item", {
	drawing = false,
	updates = true,
})

local function selectCurrentWorkspace(focusedWorkspaceName)
	for sid, item in pairs(spaces) do
		if item ~= nil then
			local isSelected = sid == constants.items.SPACES .. "." .. focusedWorkspaceName
			item:set({
				icon = { color = isSelected and settings.colors.bg1 or settings.colors.white },
				label = { color = isSelected and settings.colors.bg1 or settings.colors.white },
				background = { color = isSelected and settings.colors.white or settings.colors.bg1 },
			})
		end
	end

	-- sbar.trigger(constants.events.UPDATE_WINDOWS)
end

local function findAndSelectCurrentWorkspace()
	sbar.exec(constants.aerospace.GET_CURRENT_WORKSPACE, function(focusedWorkspaceOutput)
		local focusedWorkspaceName = focusedWorkspaceOutput:match("[^\r\n]+")
		selectCurrentWorkspace(focusedWorkspaceName)
	end)
end

local function addWorkspaceItem(workspaceName)
	local spaceName = constants.items.SPACES .. "." .. workspaceName

	spaces[spaceName] = sbar.add("item", spaceName, {
		label = {
			width = 0,
			padding_left = 0,
			string = "Empty",
		},
		icon = {
			string = workspaceName,
			font = settings.fonts.icons(),
			color = settings.colors.white,
		},
		background = {
			color = settings.colors.bg1,
		},
		click_script = "aerospace workspace " .. workspaceName,
	})

	spaces[spaceName]:subscribe("mouse.entered", function(env)
		sbar.animate("tanh", 30, function()
			spaces[spaceName]:set({ label = { width = "dynamic" } })
		end)
	end)

	spaces[spaceName]:subscribe("mouse.exited", function(env)
		sbar.animate("tanh", 30, function()
			spaces[spaceName]:set({ label = { width = 0 } })
		end)
	end)

	sbar.add("item", spaceName .. ".padding", {
		width = settings.dimensions.paddings.label,
	})
end

local function updateWorkspaceItem(workspaceWindows, spacePos)
	local space = constants.items.SPACES .. "." .. spacePos
	-- spaces[space] = nil
	-- sbar.remove("/" .. constants.items.SPACES .. "\\." .. spacePos .. "/")
	local foundWindows = {}
	if workspaceWindows then
		foundWindows = workspaceWindows:gmatch("[^\n]+")
	end

	local windowsInSpace = {}
	local firstIcon = nil
	local firstWinID = nil
	local cnt = 0
	if foundWindows ~= nil and foundWindows ~= "" then
		for window in foundWindows do
			cnt = cnt + 1
			local parsedWindow = {}
			for key, value in string.gmatch(window, "(%w+)=([%w%s]+)") do
				parsedWindow[key] = value
			end

			local windowName = parsedWindow["name"]
			local icon = settings.icons.apps[windowName] or settings.icons.apps["default"]
			table.insert(windowsInSpace, windowName)

			if cnt == 1 then
				firstIcon = icon
				firstWinID = parsedWindow["id"]
			end
		end
	elseif foundWindows == "" then
		print("!!! no windows in " .. space .. "moving along !!!")
	else
		print("nothing works anymore... and your gonna carry that weight")
	end

	spaces[space]:set("item", space, {
		label = {
			width = 0,
			padding_left = 0,
			string = table.concat(windowsInSpace, " ") or "Empty",
		},
		icon = {
			string = firstIcon or settings.icons.apps["default"],
			font = settings.fonts.icons(),
			color = settings.colors.white,
		},
		background = {
			color = settings.colors.bg1,
		},
		click_script = "aerospace focus --window-id " .. tostring(firstWinID),
	})

	spaces[space]:subscribe("mouse.entered", function(env)
		sbar.animate("tanh", 30, function()
			spaces[space]:set({ label = { width = "dynamic" } })
		end)
	end)

	spaces[space]:subscribe("mouse.exited", function(env)
		sbar.animate("tanh", 30, function()
			spaces[space]:set({ label = { width = 0 } })
		end)
	end)

	sbar.add("item", space .. ".padding", {
		width = settings.dimensions.paddings.label,
	})
end

local function triggerWorkspaceUpdates(focusedWorkSpace)
	sbar.exec(constants.aerospace.LIST_ALL_WORKSPACES, function(workspacesOutput)
		-- sbar.remove("/" .. constants.items.FRONT_APPS .. "\\.*/")
		-- sbar.remove("/" .. constants.items.SPACES .. "\\.*/")
		workspacesOutput = workspacesOutput:gmatch("[^\r\n]+")

		for wpName in workspacesOutput do
			addWorkspaceItem(wpName)
		end

		-- so sbar.exec() doesn't trigger the function if output is nothing from cmd; lazy workaround
		for i = 1, 5, 1 do
			sbar.exec(
				"aerospace list-windows --workspace " .. i .. ' --format "id=%{window-id}, name=%{app-name}"',
				function(workspaceWindows)
					updateWorkspaceItem(workspaceWindows, i)
				end
			)
		end

		if focusedWorkSpace then
			selectCurrentWorkspace(focusedWorkSpace)
		end
	end)
end

local function createWorkspaces()
	sbar.exec(constants.aerospace.LIST_ALL_WORKSPACES, function(workspacesOutput)
		sbar.exec('aerospace list-windows --all --format "id=%{window-id}, name=%{app-name}"', function(anyWindows)
			triggerWorkspaceUpdates()
		end)
		-- if ~tableContains(sbar.query("items"), "workspaces.1") then
		-- 	print()
		-- 	for workspaceName in workspacesOutput:gmatch("[^\r\n]+") do
		-- 		print("addWorkspaceItem(workspaceName)")
		-- 		addWorkspaceItem(workspaceName)
		-- 	end
		-- end
	end)
	findAndSelectCurrentWorkspace()
end

-- that button on the left side
swapWatcher:subscribe(constants.events.SWAP_MENU_AND_SPACES, function(env)
	local isShowingSpaces = env.isShowingMenu == "off" and true or false
	sbar.set("/" .. constants.items.SPACES .. "\\..*/", { drawing = isShowingSpaces })
end)

-- highlighting
currentWorkspaceWatcher:subscribe(constants.events.AEROSPACE_WORKSPACE_CHANGED, function(env)
	triggerWorkspaceUpdates(env.FOCUSED_WORKSPACE)
end)

-- entry point
createWorkspaces()