about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-05-23 20:57:56 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-05-23 20:57:56 -0700
commitb3251b23b5af380a1c5eb4bb3d2699acb934879b (patch)
tree39dda9948e42951c773fb1fb61ea8463052232ec
parent6b093fe3b43d0a4aa426eee4fc2556ea43fc00be (diff)
downloadtext.love-b3251b23b5af380a1c5eb4bb3d2699acb934879b.tar.gz
a few tests for pageup, and a bugfix
It wasn't screen-line aware. Now it is.
-rw-r--r--main.lua28
-rw-r--r--text.lua89
2 files changed, 107 insertions, 10 deletions
diff --git a/main.lua b/main.lua
index 6951158..504d394 100644
--- a/main.lua
+++ b/main.lua
@@ -68,6 +68,9 @@ App.screen.width, App.screen.height = love.window.getMode()
 App.screen.width = App.screen.width-100
 App.screen.height = App.screen.height-100
 love.window.setMode(App.screen.width, App.screen.height)
+--? App.screen.width = 120
+--? App.screen.height = 200
+--? love.window.setMode(App.screen.width, App.screen.height)
 
 Cursor_x, Cursor_y = 0, 0  -- in pixels
 
@@ -76,6 +79,7 @@ Previous_drawing_mode = nil
 
 -- maximum width available to either text or drawings, in pixels
 Line_width = math.floor(App.screen.width/2/40)*40
+--? Line_width = 100
 
 Zoom = 1.5
 
@@ -238,20 +242,24 @@ function App.keychord_pressed(chord)
 --?     print('top now', Screen_top1.line)
   elseif chord == 'pageup' then
     -- duplicate some logic from love.draw
-    local y = App.screen.height
-    while y >= 0 do
-      if Screen_top1.line == 1 then break end
-      y = y - math.floor(15*Zoom)
-      if Lines[Screen_top1.line].mode == 'drawing' then
-        y = y - Drawing.pixels(Lines[Screen_top1.line].h)
+    local top2 = Text.to2(Screen_top1)
+--?     print(App.screen.height)
+    local y = App.screen.height - math.floor(15*Zoom)
+    while y >= 15 do
+--?       print(y, top2.line)
+      if Screen_top1.line == 1 and Screen_top1.pos == 1 then break end
+      if Lines[Screen_top1.line].mode == 'text' then
+        y = y - math.floor(15*Zoom)
+      elseif Lines[Screen_top1.line].mode == 'drawing' then
+        y = y - 20 - Drawing.pixels(Lines[Screen_top1.line].h)
       end
-      Screen_top1.line = Screen_top1.line - 1
-    end
-    if Cursor1.line ~= Screen_top1.line then
-      Cursor1.pos = 1
+      top2 = Text.previous_screen_line(top2)
     end
+    Screen_top1 = Text.to1(top2)
     Cursor1.line = Screen_top1.line
+    Cursor1.pos = Screen_top1.pos
     Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary()
+--?     print(Cursor1.line, Cursor1.pos, Screen_top1.line, Screen_top1.pos)
   else
     Text.keychord_pressed(chord)
   end
diff --git a/text.lua b/text.lua
index 6ba454a..f765dd9 100644
--- a/text.lua
+++ b/text.lua
@@ -418,6 +418,95 @@ function test_up_arrow_scrolls_up_to_final_screen_line()
   check_eq(Cursor1.pos, 5, 'F - test_up_arrow_scrolls_up_to_final_screen_line/cursor')
 end
 
+function test_pageup()
+  io.write('\ntest_pageup')
+  App.screen.init{width=120, height=45}
+  Lines = load_array{'abc', 'def', 'ghi'}
+  Line_width = App.screen.width
+  Cursor1 = {line=2, pos=1}
+  Screen_top1 = {line=2, pos=1}
+  Screen_bottom1 = {}
+  Zoom = 1
+  local screen_top_margin = 15  -- pixels
+  local line_height = math.floor(15*Zoom)  -- pixels
+  -- initially the last two lines are displayed
+  App.draw()
+  local y = screen_top_margin
+  App.screen.check(y, 'def', 'F - test_pageup/baseline/screen:1')
+  y = y + line_height
+  App.screen.check(y, 'ghi', 'F - test_pageup/baseline/screen:2')
+  -- after pageup the cursor goes to first line
+  App.run_after_keychord('pageup')
+  check_eq(Screen_top1.line, 1, 'F - test_pageup/screen_top')
+  check_eq(Cursor1.line, 1, 'F - test_pageup/cursor')
+  y = screen_top_margin
+  App.screen.check(y, 'abc', 'F - test_pageup/screen:1')
+  y = y + line_height
+  App.screen.check(y, 'def', 'F - test_pageup/screen:2')
+end
+
+function test_pageup_scrolls_up_by_screen_line()
+  io.write('\ntest_pageup_scrolls_up_by_screen_line')
+  -- display the first three lines with the cursor on the bottom line
+  App.screen.init{width=25+30, height=60}
+  Lines = load_array{'abc def', 'ghi', 'jkl', 'mno'}
+  Line_width = App.screen.width
+  Cursor1 = {line=2, pos=1}
+  Screen_top1 = {line=2, 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, 'ghi', 'F - test_pageup_scrolls_up_by_screen_line/baseline/screen:1')
+  y = y + line_height
+  App.screen.check(y, 'jkl', 'F - test_pageup_scrolls_up_by_screen_line/baseline/screen:2')
+  y = y + line_height
+  App.screen.check(y, 'mno', 'F - test_pageup_scrolls_up_by_screen_line/baseline/screen:3')  -- line wrapping includes trailing whitespace
+  -- after hitting the page-up key the screen scrolls up to top
+  App.run_after_keychord('pageup')
+  check_eq(Screen_top1.line, 1, 'F - test_pageup_scrolls_up_by_screen_line/screen_top')
+  check_eq(Cursor1.line, 1, 'F - test_pageup_scrolls_up_by_screen_line/cursor:line')
+  check_eq(Cursor1.pos, 1, 'F - test_pageup_scrolls_up_by_screen_line/cursor:pos')
+  y = screen_top_margin
+  App.screen.check(y, 'abc ', 'F - test_pageup_scrolls_up_by_screen_line/screen:1')
+  y = y + line_height
+  App.screen.check(y, 'def', 'F - test_pageup_scrolls_up_by_screen_line/screen:2')
+  y = y + line_height
+  App.screen.check(y, 'ghi', 'F - test_pageup_scrolls_up_by_screen_line/screen:3')
+end
+
+function test_pageup_scrolls_up_from_middle_screen_line()
+  io.write('\ntest_pageup_scrolls_up_from_middle_screen_line')
+  -- display a few lines starting from the middle of a line (Cursor1.pos > 1)
+  App.screen.init{width=25+30, height=60}
+  Lines = load_array{'abc def', 'ghi jkl', 'mno'}
+  Line_width = App.screen.width
+  Cursor1 = {line=2, pos=5}
+  Screen_top1 = {line=2, pos=5}
+  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_pageup_scrolls_up_from_middle_screen_line/baseline/screen:2')
+  y = y + line_height
+  App.screen.check(y, 'mno', 'F - test_pageup_scrolls_up_from_middle_screen_line/baseline/screen:3')  -- line wrapping includes trailing whitespace
+  -- after hitting the page-up key the screen scrolls up to top
+  App.run_after_keychord('pageup')
+  check_eq(Screen_top1.line, 1, 'F - test_pageup_scrolls_up_from_middle_screen_line/screen_top')
+  check_eq(Cursor1.line, 1, 'F - test_pageup_scrolls_up_from_middle_screen_line/cursor:line')
+  check_eq(Cursor1.pos, 1, 'F - test_pageup_scrolls_up_from_middle_screen_line/cursor:pos')
+  y = screen_top_margin
+  App.screen.check(y, 'abc ', 'F - test_pageup_scrolls_up_from_middle_screen_line/screen:1')
+  y = y + line_height
+  App.screen.check(y, 'def', 'F - test_pageup_scrolls_up_from_middle_screen_line/screen:2')
+  y = y + line_height
+  App.screen.check(y, 'ghi ', 'F - test_pageup_scrolls_up_from_middle_screen_line/screen:3')
+end
+
 function Text.compute_fragments(line, line_width)
 --?   print('compute_fragments')
   line.fragments = {}