flip return value of button handlers
This is compatible with Javascript, and it also seems like a better default; when people forget to think about return values in click handlers, they should be consumed.
This commit is contained in:
parent
8057f3e8fe
commit
468b791050
|
@ -1,7 +1,6 @@
|
|||
-- Simple immediate-mode buttons with (currently) just an onpress1 handler for
|
||||
-- the left button.
|
||||
-- If the handler returns true, it'll prevent any further processing of the
|
||||
-- event.
|
||||
-- If any applicable button handler returns true, it'll propagate the click to other handlers.
|
||||
|
||||
-- draw button and queue up event handlers
|
||||
function button(State, name, params)
|
||||
|
@ -15,14 +14,14 @@ function button(State, name, params)
|
|||
end
|
||||
|
||||
-- process button event handlers
|
||||
function propagate_to_button_handlers(State, x, y, mouse_button)
|
||||
function mouse_press_consumed_by_any_button_handler(State, x, y, mouse_button)
|
||||
if State.button_handlers == nil then
|
||||
return
|
||||
end
|
||||
for _,ev in ipairs(State.button_handlers) do
|
||||
if x>ev.x and x<ev.x+ev.w and y>ev.y and y<ev.y+ev.h then
|
||||
if ev.onpress1 and mouse_button == 1 then
|
||||
return ev.onpress1()
|
||||
return not ev.onpress1()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
3
edit.lua
3
edit.lua
|
@ -163,7 +163,6 @@ function edit.draw(State)
|
|||
end
|
||||
schedule_save(State)
|
||||
record_undo_event(State, {before=Drawing.before, after=snapshot(State, line_index-1, line_index+1)})
|
||||
return true -- don't handle any other events with this mouse button press
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
@ -209,7 +208,7 @@ end
|
|||
function edit.mouse_pressed(State, x,y, mouse_button)
|
||||
if State.search_term then return end
|
||||
--? print('press', State.selection1.line, State.selection1.pos)
|
||||
if propagate_to_button_handlers(State, x,y, mouse_button) then
|
||||
if mouse_press_consumed_by_any_button_handler(State, x,y, mouse_button) then
|
||||
-- press on a button and it returned 'true' to short-circuit
|
||||
return
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue