about summary refs log tree commit diff stats
path: root/text.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-05-30 17:24:23 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-05-30 17:24:23 -0700
commit819cb4b1cc2c4e3f202d3ef8626d674cf619a490 (patch)
tree628c7c149fdb43d87ff87bee8ad7040f5ed07f9f /text.lua
parentd0d22fffbc03199b7059b85d71b35741297902c7 (diff)
downloadtext.love-819cb4b1cc2c4e3f202d3ef8626d674cf619a490.tar.gz
avoid scrolling down if possible
Diffstat (limited to 'text.lua')
-rw-r--r--text.lua29
1 files changed, 28 insertions, 1 deletions
diff --git a/text.lua b/text.lua
index fe95772..d86e2bc 100644
--- a/text.lua
+++ b/text.lua
@@ -862,6 +862,32 @@ function test_enter_on_bottom_line_scrolls_down()
   App.screen.check(y, 'hi', 'F - test_enter_on_bottom_line_scrolls_down/screen:3')
 end
 
+function test_enter_on_final_line_avoids_scrolling_down_when_not_at_bottom()
+  io.write('\ntest_enter_on_final_line_avoids_scrolling_down_when_not_at_bottom')
+  -- display just the bottom line on screen
+  App.screen.init{width=25+30, height=60}
+  Lines = load_array{'abc', 'def', 'ghi', 'jkl'}
+  Line_width = App.screen.width
+  Cursor1 = {line=4, pos=2}
+  Screen_top1 = {line=4, pos=1}
+  Screen_bottom1 = {}
+  Zoom = 1
+  local screen_top_margin = 15  -- pixels
+  local line_height = math.floor(15*Zoom)  -- pixels
+  App.draw()
+  local y = screen_top_margin
+  App.screen.check(y, 'jkl', 'F - test_enter_on_final_line_avoids_scrolling_down_when_not_at_bottom/baseline/screen:1')
+  -- after hitting the enter key the screen does not scroll down
+  App.run_after_keychord('return')
+  check_eq(Screen_top1.line, 4, 'F - test_enter_on_final_line_avoids_scrolling_down_when_not_at_bottom/screen_top')
+  check_eq(Cursor1.line, 5, 'F - test_enter_on_final_line_avoids_scrolling_down_when_not_at_bottom/cursor:line')
+  check_eq(Cursor1.pos, 1, 'F - test_enter_on_final_line_avoids_scrolling_down_when_not_at_bottom/cursor:pos')
+  y = screen_top_margin
+  App.screen.check(y, 'j', 'F - test_enter_on_final_line_avoids_scrolling_down_when_not_at_bottom/screen:1')
+  y = y + line_height
+  App.screen.check(y, 'kl', 'F - test_enter_on_final_line_avoids_scrolling_down_when_not_at_bottom/screen:2')
+end
+
 function test_position_cursor_on_recently_edited_wrapping_line()
   -- draw a line wrapping over 2 screen lines
   io.write('\ntest_position_cursor_on_recently_edited_wrapping_line')
@@ -1135,12 +1161,13 @@ function Text.keychord_pressed(chord)
   if chord == 'return' then
     local byte_offset = utf8.offset(Lines[Cursor1.line].data, Cursor1.pos)
     table.insert(Lines, Cursor1.line+1, {mode='text', data=string.sub(Lines[Cursor1.line].data, byte_offset)})
+    local scroll_down = (Cursor_y + math.floor(15*Zoom)) > App.screen.height
     Lines[Cursor1.line].data = string.sub(Lines[Cursor1.line].data, 1, byte_offset-1)
     Lines[Cursor1.line].fragments = nil
     Cursor1.line = Cursor1.line+1
     Cursor1.pos = 1
     save_to_disk(Lines, Filename)
-    if Cursor1.line > Screen_bottom1.line then
+    if scroll_down then
       Screen_top1.line = Cursor1.line
       Text.scroll_up_while_cursor_on_screen()
     end