forked from mogenson/ActiveSpace.spoon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
80 lines (68 loc) · 2.3 KB
/
init.lua
File metadata and controls
80 lines (68 loc) · 2.3 KB
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
local ActiveSpace = {}
ActiveSpace.__index = ActiveSpace
-- Metadata
ActiveSpace.name = "ActiveSpace"
ActiveSpace.version = "0.3"
ActiveSpace.author = "Michael Mogenson"
ActiveSpace.homepage = "https://github.com/mogenson/ActiveSpace.spoon"
ActiveSpace.license = "MIT - https://opensource.org/licenses/MIT"
-- Variables
ActiveSpace.compact = false
local function build_title()
local title = {}
local num_spaces = 0
local spaces_layout = hs.spaces.allSpaces()
local active_spaces = hs.spaces.activeSpaces()
for _, screen in ipairs(hs.screen.allScreens()) do
if not ActiveSpace.compact then
table.insert(title, screen:name() .. ": ")
end
local screen_uuid = screen:getUUID()
local active_space = active_spaces[screen_uuid]
for i, space in ipairs(spaces_layout[screen_uuid]) do
local space_title = tostring(i + num_spaces)
if active_space and active_space == space then
table.insert(title, "[" .. space_title .. "]")
else
table.insert(title, " " .. space_title .. " ")
end
end
num_spaces = num_spaces + #spaces_layout[screen_uuid]
table.insert(title, (ActiveSpace.compact and " | " or " "))
end
table.remove(title)
return table.concat(title)
end
function ActiveSpace:start()
self.menu = hs.menubar.new()
-- Set an autosave name so macOS remembers the position after reloads :D
self.menu:autosaveName("ActiveSpace")
local title = build_title()
-- print(title)
self.menu:setTitle(title)
self.menu:setClickCallback(function()
ActiveSpace.compact = not ActiveSpace.compact
self.menu:setTitle(build_title())
end)
self.space_watcher = hs.spaces.watcher.new(function()
self.menu:setTitle(build_title())
end):start()
self.screen_watcher = hs.screen.watcher.new(function()
self.menu:setTitle(build_title())
end):start()
end
function ActiveSpace:stop()
if self.space_watcher then
self.space_watcher:stop()
self.space_watcher = nil
end
if self.screen_watcher then
self.screen_watcher:stop()
self.screen_watcher = nil
end
if self.menu then
self.menu:delete()
self.menu = nil
end
end
return ActiveSpace