manually maintain mouse button press state

Just checking mouse.isDown works if the editor is the entirety of the
app, as is true in this fork. However, we often want to introduce other
widgets. We'd like tapping on them to not cause the selection to flash:
  https://news.ycombinator.com/context?id=38404923&submission=38397715

The right architecture to enforce this is: have each layer of the UI
maintain its own state machine between mouse_press and mouse_release
events. And only check the state machine in the next level down rather
than lower layers or the bottommost layer of raw LÖVE.
This commit is contained in:
Kartik K. Agaram 2023-12-01 21:52:10 -08:00
parent 9ed7c576e6
commit 8a588880b7
2 changed files with 3 additions and 1 deletions

View File

@ -233,6 +233,7 @@ end
function edit.mouse_press(State, x,y, mouse_button) function edit.mouse_press(State, x,y, mouse_button)
if State.search_term then return end if State.search_term then return end
State.mouse_down = mouse_button
--? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos)) --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
if mouse_press_consumed_by_any_button_handler(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 -- press on a button and it returned 'true' to short-circuit
@ -297,6 +298,7 @@ end
function edit.mouse_release(State, x,y, mouse_button) function edit.mouse_release(State, x,y, mouse_button)
if State.search_term then return end if State.search_term then return end
--? print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos)) --? print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
State.mouse_down = nil
if State.lines.current_drawing then if State.lines.current_drawing then
Drawing.mouse_release(State, x,y, mouse_button) Drawing.mouse_release(State, x,y, mouse_button)
schedule_save(State) schedule_save(State)

View File

@ -11,7 +11,7 @@ function Text.clip_selection(State, line_index, apos, bpos)
-- min,max = sorted(State.selection1,State.cursor1) -- min,max = sorted(State.selection1,State.cursor1)
local minl,minp = State.selection1.line,State.selection1.pos local minl,minp = State.selection1.line,State.selection1.pos
local maxl,maxp local maxl,maxp
if App.mouse_down(1) then if State.mouse_down then
maxl,maxp = Text.mouse_pos(State) maxl,maxp = Text.mouse_pos(State)
else else
maxl,maxp = State.cursor1.line,State.cursor1.pos maxl,maxp = State.cursor1.line,State.cursor1.pos