Skip to content

Commit 9c72b6a

Browse files
committed
feat(sketchybar): add clamshell toggle item
1 parent 46d6372 commit 9c72b6a

3 files changed

Lines changed: 133 additions & 1 deletion

File tree

modules/home/programs/graphical/bars/sketchybar/config/items/control_center/init.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
#!/usr/bin/env lua
22

33
local colors = require("colors")
4+
local power_config = require("power_config")
45

56
-- require order determines ui order on bar
7+
local keep_awake = nil
8+
if power_config.use_closed_lid_awake then
9+
keep_awake = require("items.control_center.keep_awake")
10+
end
611
local battery = require("items.control_center.battery")
712
local wifi = require("items.control_center.wifi")
813
local bluetooth = require("items.control_center.bluetooth")
@@ -11,6 +16,7 @@ local github = require("items.control_center.github")
1116
local volume = require("items.control_center.volume")
1217

1318
local items = {
19+
keep_awake and keep_awake.name or nil,
1420
battery.name,
1521
wifi.name,
1622
bluetooth.name,
@@ -19,7 +25,14 @@ local items = {
1925
volume.icon.name,
2026
}
2127

22-
Sbar.add("bracket", items, {
28+
local visible_items = {}
29+
for _, item in ipairs(items) do
30+
if item ~= nil then
31+
table.insert(visible_items, item)
32+
end
33+
end
34+
35+
Sbar.add("bracket", visible_items, {
2336
background = {
2437
color = colors.surface0,
2538
border_color = colors.surface1,
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env lua
2+
-- luacheck: globals DELAY
3+
4+
local colors = require("colors")
5+
local icons = require("icons")
6+
local power_config = require("power_config")
7+
local settings = require("settings")
8+
9+
local keep_awake = Sbar.add("item", "keep_awake", {
10+
position = "right",
11+
update_freq = 60,
12+
icon = {
13+
string = icons.sleep,
14+
font = {
15+
family = settings.nerd_font,
16+
style = "Regular",
17+
size = 19.0,
18+
},
19+
},
20+
background = {
21+
drawing = false,
22+
},
23+
label = {
24+
drawing = false,
25+
},
26+
popup = {
27+
align = "right",
28+
height = 20,
29+
},
30+
})
31+
32+
keep_awake.details = Sbar.add("item", "keep_awake.details", {
33+
position = "popup." .. keep_awake.name,
34+
click_script = "sketchybar --set keep_awake popup.drawing=off",
35+
background = {
36+
corner_radius = 12,
37+
padding_left = 5,
38+
padding_right = 10,
39+
},
40+
icon = {
41+
drawing = false,
42+
},
43+
label = {
44+
padding_right = 0,
45+
align = "center",
46+
},
47+
})
48+
49+
local function update_item()
50+
Sbar.exec(power_config.clamshell .. " status", function(state)
51+
local enabled = (state or ""):match("on") ~= nil
52+
53+
keep_awake:set({
54+
icon = {
55+
string = enabled and icons.power or icons.sleep,
56+
color = enabled and colors.green or colors.overlay1,
57+
},
58+
})
59+
60+
keep_awake.details:set({
61+
label = {
62+
string = enabled and "Closed-lid awake enabled" or "Closed-lid awake disabled",
63+
color = enabled and colors.green or colors.text,
64+
},
65+
})
66+
end)
67+
end
68+
69+
keep_awake:subscribe({
70+
"routine",
71+
"forced",
72+
"system_woke",
73+
}, function(_)
74+
update_item()
75+
end)
76+
77+
keep_awake:subscribe("mouse.clicked", function(info)
78+
if info.BUTTON == "right" then
79+
update_item()
80+
return
81+
end
82+
83+
Sbar.exec(power_config.clamshell .. " status", function(state)
84+
local enabled = (state or ""):match("on") ~= nil
85+
local command = enabled and " disable" or " enable"
86+
87+
Sbar.exec(power_config.clamshell .. command, function(_)
88+
update_item()
89+
DELAY(0.5, update_item)
90+
end)
91+
end)
92+
end)
93+
94+
keep_awake:subscribe({
95+
"mouse.entered",
96+
}, function(_)
97+
keep_awake:set({ popup = { drawing = true } })
98+
end)
99+
100+
keep_awake:subscribe({
101+
"mouse.exited",
102+
"mouse.exited.global",
103+
}, function(_)
104+
keep_awake:set({ popup = { drawing = false } })
105+
end)
106+
107+
update_item()
108+
109+
return keep_awake

modules/home/programs/graphical/bars/sketchybar/default.nix

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ in
8181
gnused
8282
jankyborders
8383
jq
84+
pkgs.khanelinix.clamshell
8485
pkgs.khanelinix.sketchyhelper
8586
wttrbar
8687
]
@@ -130,6 +131,15 @@ in
130131
}
131132
'';
132133

134+
"sketchybar/power_config.lua".text = ''
135+
return {
136+
use_closed_lid_awake = ${
137+
if (osConfig.khanelinix.system.power.enable or false) then "true" else "false"
138+
},
139+
clamshell = "${lib.getExe pkgs.khanelinix.clamshell}",
140+
}
141+
'';
142+
133143
"sketchybar/colors.lua".text = lib.mkDefault ''
134144
#!/usr/bin/env lua
135145

0 commit comments

Comments
 (0)