-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRayMA.lua
More file actions
380 lines (331 loc) · 13.6 KB
/
Copy pathRayMA.lua
File metadata and controls
380 lines (331 loc) · 13.6 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
-- ============================================================
-- RayMA v8.0
-- Modular MA3 object explorer, search, and action tool
-- Firmware 2.3.x | Lua 5.4
-- ============================================================
-- Architecture:
-- core/state.lua - global state, module registry
-- core/helpers.lua - shared utilities
-- core/ui.lua - UI builder and rendering
-- core/keys.lua - key dispatch and signal wiring
-- modules/browser.lua - object tree browser (default mode)
-- modules/search.lua - DataPool search engine
-- modules/qa.lua - Quick Actions (extensible)
-- modules/properties.lua - property inspector
-- modules/cmd.lua - command line
-- modules/math.lua - calculator with history
-- modules/help.lua - auto-populated help
-- ============================================================
local pluginName = select(1, ...)
local componentName = select(2, ...)
local signalTable = select(3, ...)
local my_handle = select(4, ...)
-- Bootstrap: ensure global exists before any other component
RayMA = RayMA or {}
-- ── Mode switching ──────────────────────────────────────────
function RayMA.saveSession()
pcall(function()
SetVar(GlobalVars(), "RayMA_Mode", RayMA.T.MODE or "search")
SetVar(GlobalVars(), "RayMA_Query", RayMA.T.QUERY or "")
end)
end
function RayMA.switchMode(newMode)
local T = RayMA.T
-- In elevated mode, check if the target module is elevatedOnly
-- If so, open it in a new elevated tab instead of switching compact mode
if T.UI_MODE == "compact" then
local targetMod = RayMA.getModule(newMode)
if targetMod and targetMod.elevatedOnly then
RayMA.elevate(newMode, targetMod.description or newMode)
return
end
end
local old = RayMA.getActiveModule()
RayMA.dbg("switchMode: " .. (T.MODE or "nil") .. " → " .. newMode .. " (stack depth " .. #T.MODE_STACK .. ")")
if old and old.onExit then old.onExit() end
-- Push current state onto back stack
T.MODE_STACK[#T.MODE_STACK + 1] = { mode = T.MODE, query = T.QUERY }
T.MODE = newMode
T.QUERY = ""
T.CARET = 0
T.CURSOR = 0
T.OFFSET = 0
T.RESULTS = {}
local mod = RayMA.getActiveModule()
if mod and mod.onEnter then
RayMA.dbg("switchMode: calling onEnter for " .. newMode)
mod.onEnter()
end
RayMA.dbg("switchMode: done, results=" .. #T.RESULTS .. " cursor=" .. T.CURSOR)
RayMA.saveSession()
RayMA.UI.refresh()
end
-- ── Elevated mode transitions ─────────────────────────────────
function RayMA.elevate(startMode, startLabel)
local T = RayMA.T
if T.UI_MODE == "elevated" then
-- Already elevated — just add a tab
RayMA.Elevated.addTab(startMode, startLabel)
return
end
-- Tear down compact UI
if T.OVERLAY then
pcall(function()
local existing = T.OVERLAY:Find("RayMA", "BaseInput")
if existing then existing:Parent():Delete(existing:Index()) end
end)
end
if T.PROP_FRAME then
pcall(function() T.PROP_FRAME:Parent():Delete(T.PROP_FRAME:Index()) end)
T.PROP_FRAME = nil
end
-- Switch to elevated mode
T.UI_MODE = "elevated"
RayMA.Elevated.tabs = {}
RayMA.Elevated.activeIdx = 0
RayMA.Elevated.nextId = 1
RayMA.Elevated.build(RayMA._display)
coroutine.yield({ui = 1})
-- Add initial tab
RayMA.Elevated.addTab(startMode, startLabel or startMode)
Echo(RayMA.LOG .. " elevated mode: " .. (startLabel or startMode))
end
function RayMA.compact()
local T = RayMA.T
if T.UI_MODE ~= "elevated" then return end
-- Cleanup elevated
RayMA.Elevated.destroy()
T.UI_MODE = "compact"
-- Rebuild compact UI
RayMA.UI.build(RayMA._display)
coroutine.yield({ui = 1})
-- Restore search mode
RayMA.switchMode("search")
Echo(RayMA.LOG .. " compact mode restored")
end
function RayMA.goBack()
local T = RayMA.T
local stack = T.MODE_STACK
if #stack == 0 then
RayMA.dbg("goBack: stack empty, closing")
T.RUNNING = false
return
end
local old = RayMA.getActiveModule()
local oldMode = T.MODE
if old and old.onExit then old.onExit() end
local prev = stack[#stack]
stack[#stack] = nil
RayMA.dbg("goBack: " .. (oldMode or "nil") .. " → " .. (prev.mode or "nil") .. " (stack depth " .. #stack .. ")")
T.MODE = prev.mode
T.CARET = 0
T.CURSOR = 0
T.OFFSET = 0
T.RESULTS = {}
local mod = RayMA.getActiveModule()
if mod and mod.onEnter then
RayMA.dbg("goBack: calling onEnter for " .. prev.mode)
mod.onEnter()
end
RayMA.dbg("goBack: after onEnter, results=" .. #T.RESULTS .. " cursor=" .. T.CURSOR)
-- Restore query AFTER onEnter (which may reset it)
T.QUERY = prev.query or ""
T.CARET = #T.QUERY
if T.QUERY ~= "" and mod and mod.onSearch then
mod.onSearch()
end
RayMA.saveSession()
RayMA.dbg("goBack: refresh, results=" .. #T.RESULTS .. " cursor=" .. T.CURSOR .. " mode=" .. T.MODE)
RayMA.UI.refresh()
end
-- ── Main ────────────────────────────────────────────────────
-- Prisms (prisms/), photons (photons/), and user modules (user_modules/) are
-- shipped as <ComponentLua> entries in RayMA.xml and load at plugin-load time,
-- before Main runs. They register themselves onto the RayMA global (registerPrism
-- / registerRayHints / registerModule, all defined in core/state.lua, which is
-- declared first). There is no runtime directory scan — adding a file means adding
-- a <ComponentLua> line and redeploying.
local function Main(display_handle, argument)
Echo(RayMA.LOG .. " v" .. RayMA.VERSION .. " starting")
RayMA.initState()
local T = RayMA.T
-- ── Headless exec: "Ray <command>" runs without UI ──────
if argument and argument:lower():sub(1, 4) == "ray " then
local rayCmd = argument:sub(5):match("^%s*(.-)%s*$")
if rayCmd and rayCmd ~= "" then
Echo(RayMA.LOG .. " exec: " .. rayCmd)
local rayMod = RayMA.getModule("ray")
if rayMod and rayMod.executeRay then
rayMod.executeRay(rayCmd)
local rayState = RayMA.getModuleState("ray")
Echo(string.format("%s exec: %s → %s",
RayMA.LOG, rayCmd, rayState.lastResult or "done"))
else
Echo(RayMA.LOG .. " Ray module not found")
end
return
end
end
-- Keyboard shortcuts are suspended per-window via root.OverrideKeybSC="Yes"
-- (set in UI.build / Elevated.build) — scoped to our overlay, ShCuts icon turns
-- red, and it auto-restores on close. No global KeyboardShortcutsActive toggle.
-- Build UI
local display = RayMA.H.getTargetDisplay(display_handle)
RayMA._display = display -- store for elevated mode transitions
RayMA.cacheColors()
RayMA.UI.build(display)
coroutine.yield({ui = 1})
-- Build search index in background
T.STATUS.Text = "Scanning show..."
coroutine.yield({ui = 1})
local search = RayMA.getModule("search")
if search then
search.searchState.INDEX = search.buildIndex()
search.searchState.INDEX_SOURCE = "live"
Echo(string.format("%s index: %d objects", RayMA.LOG, #search.searchState.INDEX))
end
-- Check for debug flag in argument
if argument and argument:lower():find("debug") then
RayMA.DEBUG = true
Echo(RayMA.LOG .. " DEBUG mode enabled")
-- Strip "debug" from argument to allow combining: "debug root"
argument = argument:gsub("[Dd]ebug", ""):match("^%s*(.-)%s*$")
if argument == "" then argument = nil end
end
-- Elevated shortcut: "#ModuleName" opens directly in elevated mode
local elevatedStarted = false
if argument and argument:sub(1, 1) == "#" then
local elevArg = argument:sub(2):match("^%s*(.-)%s*$")
if elevArg and elevArg ~= "" then
local modName = elevArg:lower()
local targetMod = RayMA.getModule(modName)
if not targetMod then
for _, cmd in ipairs(RayMA.commands) do
if cmd.command:lower() == modName and cmd._module then
targetMod = RayMA.getModule(cmd._module)
modName = cmd._module
break
end
end
end
local label = targetMod and (targetMod.description or modName) or elevArg
RayMA.elevate(modName, label)
elevatedStarted = true
end
end
-- Determine startup mode: always start with empty search (home screen)
-- unless a specific argument is given
if elevatedStarted then
-- skip compact mode setup
elseif argument and argument ~= "" then
if argument:lower() == "root" then
RayMA.switchMode("browser")
local browser = RayMA.getModule("browser")
if browser then pcall(function() browser.browseObj(Root()) end) end
elseif argument:lower() == "search" then
RayMA.switchMode("search")
else
-- Route-based argument: "ModuleName" or "ModuleName/action/params..."
-- First segment is module name, rest is passed as route segments
local segments = {}
for seg in argument:gmatch("[^/]+") do
segments[#segments + 1] = seg:match("^%s*(.-)%s*$")
end
local modName = segments[1] and segments[1]:lower() or ""
local targetMod = RayMA.getModule(modName)
-- Also check registered commands (e.g. "bpm" → module "bpm")
if not targetMod then
for _, cmd in ipairs(RayMA.commands) do
if cmd.command:lower() == modName and cmd._module then
targetMod = RayMA.getModule(cmd._module)
modName = cmd._module
break
end
end
end
if targetMod then
-- Build route from remaining segments
local route = {}
for i = 2, #segments do route[#route + 1] = segments[i] end
RayMA.switchMode(modName)
-- Pass route to module if it supports it
if #route > 0 and targetMod.onRoute then
targetMod.onRoute(route)
end
else
-- Fallback: use as search query
RayMA.switchMode("search")
T.QUERY = argument
T.CARET = #argument
if search and search.onSearch then search.onSearch() end
end
end
else
-- Default: fresh home screen with empty search + module list
RayMA.switchMode("search")
end
RayMA.UI.refresh()
-- The capture field grabs the keyboard natively via Focus="InitialFocus";
-- OverrideKeybSC keeps MA's shortcuts suspended. No focus grabbing needed.
Echo(string.format("%s v%s ready — %d modules, %d commands, %d QA actions",
RayMA.LOG, RayMA.VERSION,
#RayMA.modules, #RayMA.commands, #RayMA.qaActions))
-- Main loop
T.RUNNING = true
while T.RUNNING do
-- Deferred property panel build
if T.SHOW_PROPS_FOR then
local obj = T.SHOW_PROPS_FOR
T.SHOW_PROPS_FOR = nil
local props = RayMA.getModule("properties")
if props then props.buildPropertyPanel(obj) end
end
-- Module tick (for async operations like terminal socket polling)
local activeMod = RayMA.getActiveModule()
if activeMod and activeMod.onTick then
pcall(activeMod.onTick)
end
-- Check if our window is still alive on the overlay
local alive = pcall(function()
local _ = T.ROOT.Name
if T.OVERLAY then
local uiName = (T.UI_MODE == "elevated") and "RayMAE" or "RayMA"
local found = T.OVERLAY:Find(uiName, "BaseInput")
if not found then error("gone") end
end
end)
if not alive then T.RUNNING = false; break end
coroutine.yield(0.2)
end
-- Save session before cleanup
RayMA.saveSession()
-- Module cleanup (e.g. terminal disconnect)
for _, m in ipairs(RayMA.modules) do
if m.onPluginClose then pcall(m.onPluginClose) end
end
-- Keyboard shortcuts restore themselves when the OverrideKeybSC window closes
-- below — nothing to undo here.
if T.PROP_FRAME then
pcall(function() T.PROP_FRAME:Parent():Delete(T.PROP_FRAME:Index()) end)
end
-- Cleanup: remove UI (compact or elevated)
if T.OVERLAY then
pcall(function()
local existing = T.OVERLAY:Find("RayMA", "BaseInput")
if existing then existing:Parent():Delete(existing:Index()) end
end)
pcall(function()
local existing = T.OVERLAY:Find("RayMAE", "BaseInput")
if existing then existing:Parent():Delete(existing:Index()) end
end)
end
-- Execute pending action
local pending = T.PENDING_ACTION
Echo(RayMA.LOG .. " closed")
if pending then
pcall(function()
CmdIndirectWait(pending.verb .. " " .. pending.entry.cmd)
end)
end
end
return Main