-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchrome.lua
More file actions
321 lines (289 loc) · 12.4 KB
/
chrome.lua
File metadata and controls
321 lines (289 loc) · 12.4 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
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
-- See docs/chrome.md for the model.
--shape: chrome = { colour(name)->u32, pushChromeStyles(), popChromeStyles(), pushChromeWindow(), popChromeWindow(), verticalSeparator(), disabledIf(cond,fn), checkbox(label,v), radio(label,active), makeToolbar()->fn(segments), drawPicker(d), libPicker(key, current, excludeOthers?)->items, pickerIsActive()->bool, resetPickerActive(), requestPickerOpen(kind) }
--shape: pickerSpec = { kind: string, heading: string, buttonLabel: string, items: [{label, key, group?=int, current?=bool}], onPick: fn(key), width?, minWidth?, maxWidth? }
--contract: one chrome instance per coordinator; threaded into every page
--invariant: colour cache lives on the chrome instance and is invalidated on cm:configChanged
local ImGui = require 'imgui' '0.10'
local cm, ctx = (...).cm, (...).ctx
local cache = {}
cm:subscribe('configChanged', function() cache = {} end)
--contract: walks colour aliases (see docs/configManager.md) to a terminal atom; outermost alpha override wins; cycles raise with the resolved chain
local function resolve(key)
local seen, override = {}, nil
while true do
if seen[key] then
seen[#seen+1] = key
error('colour cycle: ' .. table.concat(seen, ' → '))
end
seen[#seen+1] = key; seen[key] = true
local v = cm:get(key)
if v == nil then error('unknown colour: ' .. key) end
if type(v) == 'string' then
key = v
elseif type(v[1]) == 'string' then
key = v[1]
override = override or v[2]
else
return v[1], v[2], v[3], override or v[4]
end
end
end
local function colour(name)
name = name or 'text'
if not cache[name] then
local r, g, b, a = resolve('colour.' .. name)
cache[name] = ImGui.ColorConvertDouble4ToU32(r, g, b, a)
end
return cache[name]
end
local function pushChromeStyles()
ImGui.PushStyleVar(ctx, ImGui.StyleVar_FrameBorderSize, 1)
ImGui.PushStyleColor(ctx, ImGui.Col_Text, colour('toolbar.text'))
ImGui.PushStyleColor(ctx, ImGui.Col_Button, colour('toolbar.button'))
ImGui.PushStyleColor(ctx, ImGui.Col_ButtonHovered, colour('toolbar.buttonHover'))
ImGui.PushStyleColor(ctx, ImGui.Col_ButtonActive, colour('toolbar.buttonActive'))
ImGui.PushStyleColor(ctx, ImGui.Col_FrameBg, colour('toolbar.button'))
ImGui.PushStyleColor(ctx, ImGui.Col_FrameBgHovered, colour('toolbar.buttonHover'))
ImGui.PushStyleColor(ctx, ImGui.Col_FrameBgActive, colour('toolbar.buttonActive'))
ImGui.PushStyleColor(ctx, ImGui.Col_CheckMark, colour('toolbar.checkMark'))
ImGui.PushStyleColor(ctx, ImGui.Col_PopupBg, colour('toolbar.popupBg'))
ImGui.PushStyleColor(ctx, ImGui.Col_Border, colour('toolbar.buttonBorder'))
end
local function popChromeStyles()
ImGui.PopStyleColor(ctx, 10)
ImGui.PopStyleVar(ctx, 1)
end
-- Floating surfaces fill with editor.bg (opaque); toolbar.bg is 0.5 alpha and would bleed the grid through.
local function pushChromeWindow()
pushChromeStyles()
ImGui.PushStyleVar(ctx, ImGui.StyleVar_WindowBorderSize, 1)
ImGui.PushStyleColor(ctx, ImGui.Col_WindowBg, colour('editor.bg'))
ImGui.PushStyleColor(ctx, ImGui.Col_PopupBg, colour('editor.bg'))
ImGui.PushStyleColor(ctx, ImGui.Col_TitleBg, colour('editor.bg'))
ImGui.PushStyleColor(ctx, ImGui.Col_TitleBgActive, colour('editor.bg'))
ImGui.PushStyleColor(ctx, ImGui.Col_TitleBgCollapsed, colour('editor.bg'))
ImGui.PushStyleColor(ctx, ImGui.Col_Separator, colour('toolbar.buttonBorder'))
end
local function popChromeWindow()
ImGui.PopStyleColor(ctx, 6)
ImGui.PopStyleVar(ctx, 1)
popChromeStyles()
end
-- reaper-imgui has no Separator(Vertical); draw a 1px vertical line
-- via the window draw list and reserve a Dummy slot so SameLine works.
local function verticalSeparator()
local x, y = ImGui.GetCursorScreenPos(ctx)
local h = ImGui.GetFrameHeight(ctx)
ImGui.DrawList_AddLine(ImGui.GetWindowDrawList(ctx),
x, y, x, y + h, colour('separator'), 1)
ImGui.Dummy(ctx, 1, h)
end
-- RAII wrapper for ImGui.BeginDisabled / EndDisabled: dropping the
-- bracket-match removes a class of mismatched-pop bugs on early return.
local function disabledIf(cond, fn)
if cond then ImGui.BeginDisabled(ctx) end
fn()
if cond then ImGui.EndDisabled(ctx) end
end
-- Compact checkbox / radio for toolbar contexts: zero FramePadding
-- shrinks the box to its glyph; the +3 cursorY nudge re-aligns the
-- small box with framed siblings on the same row.
local function checkbox(label, value)
ImGui.PushStyleVar(ctx, ImGui.StyleVar_FramePadding, 0, 0)
ImGui.SetCursorPosY(ctx, ImGui.GetCursorPosY(ctx) + 3)
local changed, v = ImGui.Checkbox(ctx, label, value)
ImGui.PopStyleVar(ctx, 1)
return changed, v
end
local function radio(label, active)
ImGui.PushStyleVar(ctx, ImGui.StyleVar_FramePadding, 0, 0)
ImGui.SetCursorPosY(ctx, ImGui.GetCursorPosY(ctx) + 3)
local pressed = ImGui.RadioButton(ctx, label, active)
ImGui.PopStyleVar(ctx, 1)
return pressed
end
--shape: toolbarSegment = { id: string, render: fn, visible?: fn() -> bool }
-- Wraps each segment in BeginGroup/EndGroup so GetItemRectMin/Max measures the whole
-- segment. Caches last-frame width per id; if (lastEnd + sep + cached) overflows the
-- row, the leading SameLine is skipped and ImGui wraps. One-frame slop on size change.
local function makeToolbar()
local widths = {}
return function(segments)
local startX = ImGui.GetCursorScreenPos(ctx)
local availW = ImGui.GetContentRegionAvail(ctx)
local rightX = startX + availW
local lastEndX, first = startX, true
for _, seg in ipairs(segments) do
if not seg.visible or seg.visible() then
local cachedW = widths[seg.id] or 0
if not first then
local sepW = 12 + 1 + 12
if lastEndX + sepW + cachedW <= rightX then
ImGui.SameLine(ctx, 0, 12)
verticalSeparator()
ImGui.SameLine(ctx, 0, 12)
end
end
ImGui.BeginGroup(ctx)
seg.render()
ImGui.EndGroup(ctx)
local minX = ImGui.GetItemRectMin(ctx)
local maxX = ImGui.GetItemRectMax(ctx)
widths[seg.id] = maxX - minX
lastEndX, first = maxX, false
end
end
end
end
----- Picker (typeahead popup, shared across pages)
-- Per-kind state; popups close on focus loss so a missing entry just
-- means "default empty filter / cursor at top".
local pickerFilter, pickerCursor = {}, {}
local pickerOpenReq = nil -- kind name; consumed by next drawPicker(kind)
local pickerActive = false -- frame-scoped: any picker popup live this frame
local function requestPickerOpen(kind) pickerOpenReq = kind end
local function pickerIsActive() return pickerActive end
local function resetPickerActive() pickerActive = false end
-- Build the picker-item list for a library-shaped cm key (e.g. 'swings',
-- 'tempers'). Three groups, in order:
-- 1. Off — nil key.
-- 2. Project entries (cm.project[key]) — plain label.
-- 3. Other entries (anything in the merged view but not in project) —
-- `+` prefix, marking "available but not yet localized to project".
-- excludeOthers is a set of names to filter out of group 3 only — used
-- to hide `id` from the swing picker (already covered by Off).
local function libPicker(key, current, excludeOthers)
excludeOthers = excludeOthers or {}
local proj = cm:getAt('project', key) or {}
local merged = cm:get(key, { mergeTiers = true }) or {}
local items = { { label = 'Off', key = nil, group = 1, current = current == nil } }
local projNames = {}
for k in pairs(proj) do projNames[#projNames+1] = k end
table.sort(projNames)
for _, name in ipairs(projNames) do
items[#items+1] = { label = name, key = name, group = 2, current = current == name }
end
local otherNames = {}
for k in pairs(merged) do
if not proj[k] and not excludeOthers[k] then
otherNames[#otherNames+1] = k
end
end
table.sort(otherNames)
for _, name in ipairs(otherNames) do
items[#items+1] = { label = '+ ' .. name, key = name, group = 3, current = false }
end
return items
end
-- Generic typeahead picker. Enter picks the highlighted match; group
-- separators show only when filter is empty.
local function drawPicker(d)
local popupId = '##picker_' .. d.kind
-- Heading inherits the toolbar's outer Col_Text push; no inner push.
ImGui.AlignTextToFramePadding(ctx)
ImGui.Text(ctx, d.heading .. ': ')
ImGui.SameLine(ctx)
-- ##d.kind disambiguates the ImGui ID — different pickers may all
-- show the same buttonLabel once the heading is no longer in the ID.
local btnTxt = d.buttonLabel .. ' \xe2\x96\xbe##' .. d.kind
local minW, maxW = d.minWidth, d.maxWidth
if d.width then minW, maxW = d.width, d.width end
local btnW
if minW or maxW then
local tw = ImGui.CalcTextSize(ctx, btnTxt)
local fpx = ImGui.GetStyleVar(ctx, ImGui.StyleVar_FramePadding)
btnW = tw + fpx * 2
if minW and btnW < minW then btnW = minW end
if maxW and btnW > maxW then btnW = maxW end
end
ImGui.PushStyleVar(ctx, ImGui.StyleVar_ButtonTextAlign, 0, 0.5)
local opening
if btnW then opening = ImGui.Button(ctx, btnTxt, btnW, 0)
else opening = ImGui.Button(ctx, btnTxt) end
ImGui.PopStyleVar(ctx, 1)
-- Anchor popup to the button rect; OpenPopup otherwise uses mouse
-- position, putting a keyboard-triggered popup at the text cursor.
local btnX = ImGui.GetItemRectMin(ctx)
local _, btnY = ImGui.GetItemRectMax(ctx)
if pickerOpenReq == d.kind then
pickerOpenReq = nil
opening = true
end
if opening then
pickerFilter[d.kind] = ''
ImGui.OpenPopup(ctx, popupId)
end
ImGui.SetNextWindowPos(ctx, btnX, btnY, ImGui.Cond_Appearing)
-- NoNav: kill ImGui's built-in keyboard nav highlight on the popup —
-- otherwise it draws a second cursor that fights ours and steals
-- arrow keys / character input from the filter InputText.
if not ImGui.BeginPopup(ctx, popupId, ImGui.WindowFlags_NoNav) then return end
pickerActive = true -- block page key dispatch this frame so Enter doesn't leak
if ImGui.IsWindowAppearing(ctx) then ImGui.SetKeyboardFocusHere(ctx) end
ImGui.SetNextItemWidth(ctx, 180)
local prevFilter = pickerFilter[d.kind] or ''
-- Plain InputText (no EnterReturnsTrue): with that flag, ReaImGui
-- only commits the buffer on Enter, so the live filter would never
-- update during typing. We watch Enter ourselves below.
local _, filter = ImGui.InputText(ctx, '##filter_' .. d.kind, prevFilter)
pickerFilter[d.kind] = filter
local entered = ImGui.IsKeyPressed(ctx, ImGui.Key_Enter)
or ImGui.IsKeyPressed(ctx, ImGui.Key_KeypadEnter)
ImGui.Separator(ctx)
local lf = filter:lower()
local matches, currentMatch = {}, nil
for _, it in ipairs(d.items) do
if filter == '' or it.label:lower():find(lf, 1, true) then
matches[#matches + 1] = it
if it.current then currentMatch = #matches end
end
end
-- On open or filter-change, highlight the current pick if it survived; else top.
if ImGui.IsWindowAppearing(ctx) or filter ~= prevFilter then
pickerCursor[d.kind] = currentMatch or 1
end
local cursor = pickerCursor[d.kind] or 1
local n = #matches
if n > 0 then
if ImGui.IsKeyPressed(ctx, ImGui.Key_DownArrow) then
cursor = cursor % n + 1
elseif ImGui.IsKeyPressed(ctx, ImGui.Key_UpArrow) then
cursor = (cursor - 2) % n + 1
end
end
cursor = math.min(math.max(cursor, 1), math.max(n, 1))
pickerCursor[d.kind] = cursor
if ImGui.IsKeyPressed(ctx, ImGui.Key_Escape) then
ImGui.CloseCurrentPopup(ctx)
elseif entered then
if matches[cursor] then d.onPick(matches[cursor].key) end
ImGui.CloseCurrentPopup(ctx)
else
local lastGroup
for i, it in ipairs(matches) do
if filter == '' and lastGroup and lastGroup ~= (it.group or 1) then
ImGui.Separator(ctx)
end
if ImGui.Selectable(ctx, it.label, i == cursor) then d.onPick(it.key) end
lastGroup = it.group or 1
end
end
ImGui.EndPopup(ctx)
end
return {
colour = colour,
pushChromeStyles = pushChromeStyles,
popChromeStyles = popChromeStyles,
pushChromeWindow = pushChromeWindow,
popChromeWindow = popChromeWindow,
verticalSeparator = verticalSeparator,
disabledIf = disabledIf,
checkbox = checkbox,
radio = radio,
makeToolbar = makeToolbar,
drawPicker = drawPicker,
libPicker = libPicker,
pickerIsActive = pickerIsActive,
resetPickerActive = resetPickerActive,
requestPickerOpen = requestPickerOpen,
}