-
Notifications
You must be signed in to change notification settings - Fork 1
/
spaces.lua
157 lines (145 loc) · 5.18 KB
/
spaces.lua
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
local colors = require("colors")
local emojis = require("emojis")
local settings = require("settings")
local spaces = {}
local focused_display = nil
local session_restore = {}
local session_timestamp = {}
local max_display_count = 1
local function mouse_click(env)
if env.BUTTON == "right" then
sbar.exec("yabai -m space --destroy " .. env.SID ..
" && sketchybar --trigger space_change")
else
sbar.exec("yabai -m space --focus " .. env.SID)
end
end
function contains(display_mask, display)
return display_mask & (1 << display) ~= 0
end
local function space_change(env)
local is_selected = env.SELECTED == "true"
local displays = tonumber(sbar.query(env.NAME).geometry
.associated_display_mask)
local is_focused = not focused_display or
contains(displays, focused_display)
local bg_color, fg_color
if is_selected then
if is_focused then
bg_color = colors.selected.focused.background
fg_color = colors.selected.focused.foreground
else
bg_color = colors.selected.unfocused.background
fg_color = colors.selected.unfocused.foreground
end
else
bg_color = colors.unselected.background
fg_color = colors.unselected.foreground
end
local fg_props = {highlight = env.SELECTED, highlight_color = fg_color}
sbar.set(env.NAME, {
icon = fg_props,
label = fg_props,
background = {color = bg_color}
})
end
local function space_windows_change(env)
local space = env.INFO.space
local apps = env.INFO.apps
local app_emojis = {}
for name, _ in pairs(apps) do
table.insert(app_emojis, emojis[name] or ":default:")
end
icon_strip = "—"
if next(app_emojis) ~= nil then
icon_strip = table.concat(app_emojis, " ")
end
if spaces[space] then
sbar.animate("tanh", 10, function()
sbar.set(spaces[space], {label = icon_strip})
end)
end
sbar.exec("@display_count@", function(display_count_str)
local display_count = tonumber(display_count_str)
sbar.exec("@session_save@", function(restore)
session_restore[display_count] = restore
session_timestamp[display_count] = os.clock()
if display_count > max_display_count then
max_display_count = display_count
end
end)
end)
end
local function display_added(display_count) return display_count - 1, 1, -1 end
local function display_removed(display_count)
return display_count + 1, max_display_count, 1
end
local function display_count_change(range)
return function(env)
sbar.exec("@display_count@", function(display_count_str)
local display_count = tonumber(display_count_str)
local restore = nil
local max_timestamp = nil
local first, last, step = range(display_count)
for display_count = first, last, step do
if session_timestamp[display_count] and
(max_timestamp == null or session_timestamp[display_count] >
max_timestamp) then
max_timestamp = session_timestamp[display_count]
restore = session_restore[display_count]
end
end
if restore then sbar.exec(restore) end
sbar.trigger("space_windows_change")
end)
end
end
local function display_change(env)
focused_display = tonumber(env.INFO)
sbar.trigger("space_change")
end
for i = 1, 10, 1 do
local space = sbar.add("space", "spaces." .. tostring(i), {
associated_space = i,
background = {color = colors.unselected.background, corner_radius = 6},
icon = {
string = i,
padding_left = 10,
padding_right = 10,
color = colors.unselected.foreground,
highlight_color = colors.selected.foreground
},
padding_left = 2,
padding_right = 2,
label = {
string = "<>",
padding_right = 20,
color = colors.unselected.foreground,
highlight_color = colors.selected.foreground,
font = "sketchybar-app-font:Regular:16.0",
y_offset = -1,
drawing = true
}
})
spaces[i] = space.name
space:subscribe("space_change", space_change)
space:subscribe("mouse.clicked", mouse_click)
end
sbar.add("bracket", "spaces", spaces, {})
local space_creator = sbar.add("item", "space_creator", {
padding_left = 10,
padding_right = 8,
icon = {
string = "",
font = {style = "Heavy", size = settings.font.size - 2.0}
},
label = {drawing = false},
associated_display = "active"
})
space_creator:subscribe("mouse.clicked", function(_)
sbar.exec("yabai -m space --create && sketchybar --trigger space_change")
end)
space_creator:subscribe("space_windows_change", space_windows_change)
space_creator:subscribe("display_change", display_change)
space_creator:subscribe("display_added", display_count_change(display_added))
space_creator:subscribe("display_removed", display_count_change(display_removed))