bounds when panning by keyboard

This makes the keyboard navigation feel more solid.

I'm not bothering to do the same for panning with the mouse. Mostly
because I'm lazy, but also as an escape hatch in case I find myself
wanting to move the top lower down the screen or something. But it'll
still snap back the first time I pan using the keyboard. Let's see how
much it matters.
This commit is contained in:
Kartik K. Agaram 2023-06-23 22:45:55 -07:00
parent 97171d0569
commit 4d7a8ac4d1
6 changed files with 55 additions and 17 deletions

View File

@ -5,4 +5,6 @@ on.initialize = function()
font:setLineHeight(1.3)
initialize_file_picker()
A()
Viewport.x, Viewport.y = Viewport_bounds.xmin, Viewport_bounds.ymin
B()
end

View File

@ -32,34 +32,46 @@ on.keychord_press = function(chord, key)
end
else
if chord == 'up' then
Viewport.y = Viewport.y - scale(20)
Viewport.y = math.max(
Viewport_bounds.ymin,
Viewport.y - scale(20))
B()
elseif chord == 'down' then
Viewport.y = Viewport.y + scale(20)
Viewport.y = math.min(
math.max(
Viewport_bounds.ymin,
Viewport_bounds.ymax - App.screen.width/2/Viewport.zoom), -- conservative; unclear why removing the '/2' makes the bottom inaccessible
Viewport.y + scale(20))
B()
elseif chord == 'left' then
Viewport.x = Viewport.x - scale(50)
Viewport.x = math.max(
Viewport_bounds.xmin,
Viewport.x - scale(50))
B()
elseif chord == 'right' then
Viewport.x = Viewport.x + scale(50)
Viewport.x = math.min(
math.max(Viewport_bounds.xmin, Viewport_bounds.xmax - App.screen.width/Viewport.zoom),
Viewport.x + scale(50))
B()
elseif chord == 'pageup' then
Viewport.y = Viewport.y - App.screen.height/Viewport.zoom
elseif chord == 'pageup' or chord == 'S-up' then
Viewport.y = math.max(Viewport_bounds.ymin, Viewport.y - App.screen.height/Viewport.zoom)
B()
elseif chord == 'S-up' then
Viewport.y = Viewport.y - App.screen.height/Viewport.zoom
B()
elseif chord == 'pagedown' then
Viewport.y = Viewport.y + App.screen.height/Viewport.zoom
B()
elseif chord == 'S-down' then
Viewport.y = Viewport.y + App.screen.height/Viewport.zoom
elseif chord == 'pagedown' or chord == 'S-down' then
Viewport.y = math.min(
math.max(
Viewport_bounds.ymin,
Viewport_bounds.ymax - App.screen.width/2/Viewport.zoom), -- conservative; unclear why removing the '/2' makes the bottom inaccessible
Viewport.y + App.screen.height/Viewport.zoom)
B()
elseif chord == 'S-left' then
Viewport.x = Viewport.x - App.screen.width/Viewport.zoom
Viewport.x = math.max(Viewport_bounds.xmin, Viewport.x - App.screen.width/Viewport.zoom)
B()
elseif chord == 'S-right' then
Viewport.x = Viewport.x + App.screen.width/Viewport.zoom
Viewport.x = math.min(
math.max(
Viewport_bounds.xmin,
Viewport_bounds.xmax - App.screen.width/Viewport.zoom),
Viewport.x + App.screen.width/Viewport.zoom)
B()
end
end

2
0028-A
View File

@ -8,7 +8,7 @@ A = function(preserve_screen_top_of_cursor_node)
else
compute_layout(Global_state.thread, 0,0, Surface, preserve_screen_top_of_cursor_node)
end
-- continue the pipeline
compute_viewport_bounds()
B(preserve_screen_top_of_cursor_node)
-- TODO: ugly that we're manipulating editor objects twice
end

View File

@ -3,4 +3,6 @@ open_thread = function(filename)
load_subtree(filename, Global_state.thread.data, 0)
love.window.setTitle('pothi.love - '..filename)
A()
Viewport.x, Viewport.y = Viewport_bounds.xmin, Viewport_bounds.ymin
B()
end

1
0147-Viewport_bounds Normal file
View File

@ -0,0 +1 @@
Viewport_bounds = {}

View File

@ -0,0 +1,21 @@
compute_viewport_bounds = function()
local xmin, ymin, xmax, ymax
for i,node in ipairs(Surface) do
if node.type == 'text' or node.type == 'rows' or node.type == 'cols' then
if xmin == nil or xmin > node.x then
xmin = node.x
end
if xmax == nil or xmax < node.x+node.w then
xmax = node.x+node.w
end
if ymin == nil or ymin > node.y then
ymin = node.y
end
if ymax == nil or ymax < node.y + node.h then
ymax = node.y+node.w
end
end
end
xmin, ymin = xmin-50, ymin-50
Viewport_bounds.xmin, Viewport_bounds.xmax, Viewport_bounds.ymin, Viewport_bounds.ymax = xmin,xmax, ymin, ymax
end