fix: support multiple bind_default_trigger calls
Previously trigger_action_name + trigger_widget_id were module-local scalars, so the second bind_default_trigger call overwrote the first (and reused the action name 'panel_toggle' so lib-core.input's binding was also clobbered). Switched to a triggers array; each binding gets a unique input action 'panel_toggle_<widget_id>' so multiple widgets can be toggled by their own keys independently.
This commit is contained in:
24
init.lua
24
init.lua
@@ -43,8 +43,10 @@ local widgets = {} -- widget_id (string) → widget_def table
|
||||
local active = nil -- active widget_id or nil
|
||||
local theme = {} -- merged DEFAULT_THEME + overrides
|
||||
local ctx_menu = nil -- context-menu state table or nil
|
||||
local trigger_action_name = nil -- input action name for default trigger
|
||||
local trigger_widget_id = nil -- which widget_id to toggle on trigger
|
||||
-- Default-trigger bindings: array of {action_name, widget_id}. Multiple
|
||||
-- bind_default_trigger calls append independent entries (each widget gets
|
||||
-- its own input-action so they don't clobber each other in lib-core.input).
|
||||
local triggers = {}
|
||||
local last_mouse_left = false -- for edge-detection (was down last frame)
|
||||
local last_mouse_right = false -- for edge-detection (was down last frame)
|
||||
|
||||
@@ -313,6 +315,8 @@ end
|
||||
--- Binds a keyboard key as the default toggle trigger for a widget.
|
||||
--- key defaults to "tab" when nil. Lazy-requires lib-core.input to avoid
|
||||
--- module-load-time circular dependency.
|
||||
--- Multiple calls register independent triggers (each widget gets its own
|
||||
--- input-action named "panel_toggle_<widget_id>" so bindings don't clobber).
|
||||
--- Loud-error if widget_id is not registered ("register before bind").
|
||||
function M.bind_default_trigger(key, widget_id)
|
||||
if key == nil then key = "tab" end
|
||||
@@ -321,9 +325,9 @@ function M.bind_default_trigger(key, widget_id)
|
||||
.. "' (register before bind)")
|
||||
end
|
||||
local input = require("lib-core.input") -- lazy require: avoid load-time cycle
|
||||
input.bind("panel_toggle", {key})
|
||||
trigger_action_name = "panel_toggle"
|
||||
trigger_widget_id = widget_id
|
||||
local action_name = "panel_toggle_" .. widget_id
|
||||
input.bind(action_name, {key})
|
||||
table.insert(triggers, { action_name = action_name, widget_id = widget_id })
|
||||
end
|
||||
|
||||
-- -----------------------------------------------------------------------
|
||||
@@ -384,11 +388,13 @@ end
|
||||
--- - Mouse edge-detection for click events (dispatches on press, not hold)
|
||||
--- - Mouse-wheel event dispatch
|
||||
function M.update(dt)
|
||||
-- Check default trigger (lazy requires lib-core.input internally if bound)
|
||||
if trigger_action_name then
|
||||
-- Check all default triggers (lazy requires lib-core.input if any are bound)
|
||||
if #triggers > 0 then
|
||||
local input = require("lib-core.input")
|
||||
if input.was_action_pressed(trigger_action_name) then
|
||||
M.toggle(trigger_widget_id)
|
||||
for _, t in ipairs(triggers) do
|
||||
if input.was_action_pressed(t.action_name) then
|
||||
M.toggle(t.widget_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user