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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
local COLORS = {
panel = "#11140FF5",
text = "#F4F1E8",
soft = "#D8DDD3",
muted = "#98A398",
accent = "#F2C66D",
line = "#2C3329",
}
local MESSAGES = {
"Stand up and reset your posture.",
"Look 20 feet away for 20 seconds.",
"Drop your shoulders and unclench your jaw.",
"Walk for a minute before the next block.",
"Take three slow breaths, then reset.",
}
local cfg = rb.ui.schema({
interval = rb.ui.slider(45, {
min = 5,
max = 120,
step = 5,
label = "Remind every (minutes)",
group = "Timing",
}),
break_len = rb.ui.slider(30, {
min = 10,
max = 180,
step = 5,
label = "Break length (seconds)",
group = "Timing",
}),
toggle_key = rb.ui.keybind("Ctrl+Alt+B", {
label = "Pause / resume",
group = "Hotkeys",
}),
snooze_key = rb.ui.keybind("Ctrl+Alt+Shift+B", {
label = "Snooze 5 minutes",
group = "Hotkeys",
}),
sound = rb.ui.toggle(true, {
label = "Play a chime",
group = "Behavior",
}),
show_hud = rb.ui.toggle(true, {
label = "Show countdown",
group = "HUD",
}),
hud_anchor = rb.ui.select("top-left", {
"top-left",
"top-right",
"bottom-left",
"bottom-right",
}, {
label = "Countdown position",
group = "HUD",
}),
})
local state = {
paused = false,
breaking = false,
next_at = 0,
break_until = 0,
remaining = 0,
break_index = 0,
break_message = MESSAGES[1],
hud_surface = nil,
break_surface = nil,
hud_signature = nil,
break_signature = nil,
timer = nil,
}
local function now()
return rb.system.time()
end
local function clamp(value, minimum, maximum)
return math.max(minimum, math.min(value, maximum))
end
local function add_rect(ops, x, y, width, height, fill, radius, border, border_width)
local op = {
kind = "rect",
x = x,
y = y,
w = width,
h = height,
fill = fill,
radius = radius or 0,
}
if border then
op.border = border
op.border_width = border_width or 1
end
ops[#ops + 1] = op
end
local function add_text(ops, x, y, size, value, color, bold)
ops[#ops + 1] = {
kind = "text",
x = x,
y = y,
size = size,
text = value,
color = color,
bold = bold == true,
}
end
local function format_time(milliseconds)
local seconds = math.max(0, math.ceil(milliseconds / 1000))
return string.format("%02d:%02d", seconds // 60, seconds % 60)
end
local function notify(message)
pcall(rb.overlay.toast, message, {
title = "Break Coach",
duration = 2600,
})
end
local function remove_surface(key)
local surface = state[key]
if surface then
pcall(function()
surface:remove()
end)
state[key] = nil
end
end
local function set_surface(key, width, height, anchor, ops)
local surface = state[key]
if surface then
local ok = pcall(function()
surface:set({
width = width,
height = height,
ops = ops,
})
end)
if ok then
return
end
remove_surface(key)
end
local ok, created = pcall(rb.overlay.surface, {
anchor = anchor,
width = width,
height = height,
ops = ops,
})
if ok then
state[key] = created
else
rb.log.warn("Break Coach: could not create surface: " .. tostring(created))
end
end
local function update_hud()
if cfg.show_hud == false then
remove_surface("hud_surface")
state.hud_signature = nil
return
end
local remaining
if state.paused then
remaining = state.remaining
elseif state.breaking then
remaining = math.max(0, state.break_until - now())
else
remaining = math.max(0, state.next_at - now())
end
local label
if state.paused then
label = "PAUSED"
elseif state.breaking then
label = "BREAK"
else
label = "NEXT RESET"
end
local value = format_time(remaining)
local signature = label .. "|" .. value .. "|" .. tostring(cfg.hud_anchor)
if signature == state.hud_signature then
return
end
state.hud_signature = signature
local ops = {}
add_rect(ops, 0, 0, 280, 78, COLORS.panel, 14, COLORS.line, 1)
add_rect(ops, 16, 16, 3, 46, COLORS.accent, 1)
add_text(ops, 28, 12, 10, label, COLORS.muted, true)
add_text(ops, 28, 28, 26, value, COLORS.text, true)
add_text(ops, 28, 60, 9, "Ctrl+Alt+B pause", COLORS.muted)
set_surface("hud_surface", 280, 78, cfg.hud_anchor, ops)
end
local function update_break()
if not state.breaking then
remove_surface("break_surface")
state.break_signature = nil
return
end
local remaining = math.max(0, state.break_until - now())
local countdown = tostring(math.max(0, math.ceil(remaining / 1000))) .. "s"
local signature = state.break_message .. "|" .. countdown
if signature == state.break_signature then
return
end
state.break_signature = signature
local ops = {}
add_rect(ops, 0, 0, 600, 230, "#11140FF7", 18, COLORS.line, 1)
add_rect(ops, 24, 24, 3, 32, COLORS.accent, 1)
add_text(ops, 40, 22, 24, "TIME TO RESET", COLORS.text, true)
add_text(ops, 40, 58, 13, state.break_message, COLORS.soft)
add_text(ops, 40, 92, 12, "Stand up. Look 20 feet away. Drop your shoulders.", COLORS.muted)
add_text(ops, 40, 126, 30, countdown, COLORS.accent, true)
add_text(ops, 40, 172, 10, "Ctrl+Alt+Shift+B snooze 5 min", COLORS.muted)
set_surface("break_surface", 600, 230, "center", ops)
end
local function chime()
if cfg.sound == false then
return
end
pcall(rb.audio.beep)
end
local function start_break()
state.break_index = state.break_index + 1
if state.break_index > #MESSAGES then
state.break_index = 1
end
state.break_message = MESSAGES[state.break_index]
state.breaking = true
state.break_until = now() + clamp(tonumber(cfg.break_len) or 30, 10, 180) * 1000
state.break_signature = nil
update_break()
update_hud()
chime()
end
local function finish_break()
state.breaking = false
state.next_at = now() + clamp(tonumber(cfg.interval) or 45, 5, 120) * 60000
state.break_signature = nil
remove_surface("break_surface")
update_hud()
notify("Break complete. Next reset is scheduled.")
end
local function toggle_pause()
if state.breaking then
finish_break()
return
end
if state.paused then
state.paused = false
state.next_at = now() + math.max(1000, state.remaining)
notify("Reminder resumed.")
else
state.paused = true
state.remaining = math.max(0, state.next_at - now())
notify("Reminder paused.")
end
update_hud()
end
local function snooze()
if state.breaking then
state.breaking = false
state.break_signature = nil
remove_surface("break_surface")
end
state.next_at = now() + 5 * 60 * 1000
notify("Snoozed for 5 minutes.")
update_hud()
end
local function tick()
if state.paused then
update_hud()
return
end
if state.breaking then
if now() >= state.break_until then
finish_break()
else
update_break()
update_hud()
end
return
end
if now() >= state.next_at then
start_break()
else
update_hud()
end
end
rb.bind(cfg.toggle_key, toggle_pause)
rb.bind(cfg.snooze_key, snooze)
function OnStart()
state.next_at = now() + clamp(tonumber(cfg.interval) or 45, 5, 120) * 60000
state.hud_signature = nil
update_hud()
state.timer = rb.timer.every(1000, tick)
end
function OnStop()
if state.timer then
pcall(function()
state.timer:stop()
end)
state.timer = nil
end
remove_surface("hud_surface")
remove_surface("break_surface")
end