bugfix: couple of margin-relative computations

This commit is contained in:
Kartik K. Agaram 2022-07-17 22:15:49 -07:00
parent 29dac6a6ec
commit 82cdd9ddd1
2 changed files with 63 additions and 3 deletions

View File

@ -728,7 +728,7 @@ function Text.to_pos_on_line(State, line_index, mx, my)
-- On all wrapped screen lines but the final one, clicks past end of
-- line position cursor on final character of screen line.
-- (The final screen line positions past end of screen line as always.)
if screen_line_index < #line_cache.screen_line_starting_pos and mx > Text.screen_line_width(State, line_index, screen_line_index) then
if screen_line_index < #line_cache.screen_line_starting_pos and mx > State.left + Text.screen_line_width(State, line_index, screen_line_index) then
--? print('past end of non-final line; return')
return line_cache.screen_line_starting_pos[screen_line_index+1]-1
end
@ -978,7 +978,7 @@ function Text.tweak_screen_top_and_cursor(State)
--? print('tweak')
State.cursor1 = {
line=State.screen_bottom1.line,
pos=Text.to_pos_on_line(State, State.screen_bottom1.line, App.screen.width-5, App.screen.height-5),
pos=Text.to_pos_on_line(State, State.screen_bottom1.line, State.right-5, App.screen.height-5),
}
end
end

View File

@ -265,10 +265,31 @@ function test_click_with_mouse()
edit.draw(Editor_state)
edit.run_after_mouse_click(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
-- cursor moves
check_eq(Editor_state.cursor1.line, 1, 'F - test_click_with_mouse/cursor')
check_eq(Editor_state.cursor1.line, 1, 'F - test_click_with_mouse/cursor:line')
check_eq(Editor_state.cursor1.pos, 2, 'F - test_click_with_mouse/cursor:pos')
check_nil(Editor_state.selection1.line, 'F - test_click_with_mouse/selection is empty to avoid perturbing future edits')
end
function test_click_with_mouse_takes_margins_into_account()
io.write('\ntest_click_with_mouse_takes_margins_into_account')
-- display two lines with cursor on one of them
App.screen.init{width=100, height=80}
Editor_state = edit.initialize_test_state()
Editor_state.left = 50 -- occupy only right side of screen
Editor_state.lines = load_array{'abc', 'def'}
Text.redraw_all(Editor_state)
Editor_state.cursor1 = {line=2, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
-- click on the other line
edit.draw(Editor_state)
edit.run_after_mouse_click(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
-- cursor moves
check_eq(Editor_state.cursor1.line, 1, 'F - test_click_with_mouse_takes_margins_into_account/cursor:line')
check_eq(Editor_state.cursor1.pos, 2, 'F - test_click_with_mouse_takes_margins_into_account/cursor:pos')
check_nil(Editor_state.selection1.line, 'F - test_click_with_mouse_takes_margins_into_account/selection is empty to avoid perturbing future edits')
end
function test_click_with_mouse_on_empty_line()
io.write('\ntest_click_with_mouse_on_empty_line')
-- display two lines with the first one empty
@ -340,6 +361,45 @@ function test_draw_word_wrapping_text()
App.screen.check(y, 'ghi', 'F - test_draw_word_wrapping_text/screen:3')
end
function test_click_with_mouse_on_wrapping_line()
io.write('\ntest_click_with_mouse_on_wrapping_line')
-- display two lines with cursor on one of them
App.screen.init{width=50, height=80}
Editor_state = edit.initialize_test_state()
Editor_state.lines = load_array{'abc def ghi jkl mno pqr stu'}
Text.redraw_all(Editor_state)
Editor_state.cursor1 = {line=1, pos=20}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
-- click on the other line
edit.draw(Editor_state)
edit.run_after_mouse_click(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
-- cursor moves
check_eq(Editor_state.cursor1.line, 1, 'F - test_click_with_mouse_on_wrapping_line/cursor:line')
check_eq(Editor_state.cursor1.pos, 2, 'F - test_click_with_mouse_on_wrapping_line/cursor:pos')
check_nil(Editor_state.selection1.line, 'F - test_click_with_mouse_on_wrapping_line/selection is empty to avoid perturbing future edits')
end
function test_click_with_mouse_on_wrapping_line_takes_margins_into_account()
io.write('\ntest_click_with_mouse_on_wrapping_line_takes_margins_into_account')
-- display two lines with cursor on one of them
App.screen.init{width=100, height=80}
Editor_state = edit.initialize_test_state()
Editor_state.left = 50 -- occupy only right side of screen
Editor_state.lines = load_array{'abc def ghi jkl mno pqr stu'}
Text.redraw_all(Editor_state)
Editor_state.cursor1 = {line=1, pos=20}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
-- click on the other line
edit.draw(Editor_state)
edit.run_after_mouse_click(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
-- cursor moves
check_eq(Editor_state.cursor1.line, 1, 'F - test_click_with_mouse_on_wrapping_line_takes_margins_into_account/cursor:line')
check_eq(Editor_state.cursor1.pos, 2, 'F - test_click_with_mouse_on_wrapping_line_takes_margins_into_account/cursor:pos')
check_nil(Editor_state.selection1.line, 'F - test_click_with_mouse_on_wrapping_line_takes_margins_into_account/selection is empty to avoid perturbing future edits')
end
function test_draw_text_wrapping_within_word()
-- arrange a screen line that needs to be split within a word
io.write('\ntest_draw_text_wrapping_within_word')