get rid of recent_mouse

It's a hack:
  - if you start selecting from below final line the start of the
    selection is the most recent click even if it was forever ago
  - (the crash we're currently fixing) if you start up and immediately
    select all then click below final line => crash. recent_mouse was
    never set.
  - getting rid of it breaks no tests (except the crash we're currently
    fixing)
This commit is contained in:
Kartik K. Agaram 2023-06-01 22:12:12 -07:00
parent 9b27a4d816
commit cdef37c419
3 changed files with 24 additions and 37 deletions

View File

@ -75,8 +75,6 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c
old_cursor1 = nil, old_cursor1 = nil,
old_selection1 = nil, old_selection1 = nil,
mousepress_shift = nil, mousepress_shift = nil,
-- when selecting text, avoid recomputing some state on every single frame
recent_mouse = {},
-- cursor coordinates in pixels -- cursor coordinates in pixels
cursor_x = 0, cursor_x = 0,
@ -249,7 +247,6 @@ function edit.mouse_press(State, x,y, mouse_button)
line=line_index, line=line_index,
pos=Text.to_pos_on_line(State, line_index, x, y), pos=Text.to_pos_on_line(State, line_index, x, y),
} }
State.recent_mouse = {time=Current_time, line=State.selection1.line, pos=State.selection1.pos}
break break
end end
elseif line.mode == 'drawing' then elseif line.mode == 'drawing' then
@ -286,7 +283,6 @@ function edit.mouse_release(State, x,y, mouse_button)
pos=Text.to_pos_on_line(State, line_index, x, y), pos=Text.to_pos_on_line(State, line_index, x, y),
} }
print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos)) print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
State.recent_mouse = {time=Current_time, line=State.cursor1.line, pos=State.cursor1.pos}
if State.mousepress_shift then if State.mousepress_shift then
if State.old_selection1.line == nil then if State.old_selection1.line == nil then
State.selection1 = State.old_cursor1 State.selection1 = State.old_cursor1

View File

@ -69,17 +69,8 @@ end
-- inefficient for some reason, so don't do it on every frame -- inefficient for some reason, so don't do it on every frame
function Text.mouse_pos(State) function Text.mouse_pos(State)
if State.recent_mouse.time and State.recent_mouse.time > Current_time-0.1 then
print_and_log(('text.mouse_pos: returning recent value %d,%d'):format(State.recent_mouse.line, State.recent_mouse.pos))
return State.recent_mouse.line, State.recent_mouse.pos
end
State.recent_mouse.time = Current_time
local line,pos = Text.to_pos(State, App.mouse_x(), App.mouse_y()) local line,pos = Text.to_pos(State, App.mouse_x(), App.mouse_y())
if line then return line, pos
State.recent_mouse.line = line
State.recent_mouse.pos = pos
end
return State.recent_mouse.line, State.recent_mouse.pos
end end
function Text.to_pos(State, x,y) function Text.to_pos(State, x,y)

View File

@ -904,29 +904,29 @@ function test_select_all_text()
check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos') check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos')
end end
function test_select_all_text_then_mouse_press_outside_text() --? function test_select_all_text_then_mouse_press_outside_text()
-- display a single line of text --? -- display a single line of text
App.screen.init{width=75, height=80} --? App.screen.init{width=75, height=80}
Editor_state = edit.initialize_test_state() --? Editor_state = edit.initialize_test_state()
Editor_state.lines = load_array{'abc def'} --? Editor_state.lines = load_array{'abc def'}
Text.redraw_all(Editor_state) --? Text.redraw_all(Editor_state)
Editor_state.cursor1 = {line=1, pos=1} --? Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1} --? Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {} --? Editor_state.screen_bottom1 = {}
edit.draw(Editor_state) --? edit.draw(Editor_state)
-- select all --? -- select all
App.fake_key_press('lctrl') --? App.fake_key_press('lctrl')
edit.run_after_keychord(Editor_state, 'C-a') --? edit.run_after_keychord(Editor_state, 'C-a')
App.fake_key_release('lctrl') --? App.fake_key_release('lctrl')
edit.key_release(Editor_state, 'lctrl') --? edit.key_release(Editor_state, 'lctrl')
-- selection --? -- selection
check_eq(Editor_state.selection1.line, 1, 'selection:line') --? check_eq(Editor_state.selection1.line, 1, 'selection:line')
check_eq(Editor_state.selection1.pos, 1, 'selection:pos') --? check_eq(Editor_state.selection1.pos, 1, 'selection:pos')
check_eq(Editor_state.cursor1.line, 1, 'cursor:line') --? check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos') --? check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos')
-- part of a mouse click outside the selected line --? -- part of a mouse click outside the selected line
edit.run_after_mouse_press(Editor_state, 45, Margin_top + Editor_state.line_height + 10, --[[mouse button]] 1) --? edit.run_after_mouse_press(Editor_state, 45, Margin_top + Editor_state.line_height + 10, --[[mouse button]] 1)
end --? end
function test_cut_without_selection() function test_cut_without_selection()
-- display a few lines -- display a few lines