From 0e9eea85af6bcc7d1c54435dc0b3c3f496827036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 24 Sep 2020 18:48:41 +0200 Subject: [PATCH] grid: reflow: handle viewport being too far down when enlarging the window MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the viewport is close to the bottom, but not *at* the bottom, and we’re enlarging the window, the translated viewport will be too far down. --- grid.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/grid.c b/grid.c index 5f2b4277..d8fc2842 100644 --- a/grid.c +++ b/grid.c @@ -317,8 +317,6 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, while (new_grid[grid->offset] == NULL) grid->offset = (grid->offset + 1) & (new_rows - 1); - grid->view = view_follows ? grid->offset : viewport.row; - /* Ensure all visible rows have been allocated */ for (int r = 0; r < new_screen_rows; r++) { int idx = (grid->offset + r) & (new_rows - 1); @@ -326,6 +324,23 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, new_grid[idx] = grid_row_alloc(new_cols, true); } + grid->view = view_follows ? grid->offset : viewport.row; + + /* If enlarging the window, the old viewport may be too far down, + * with unallocated rows. Make sure this cannot happen */ + while (true) { + int idx = (grid->view + new_screen_rows - 1) & (new_rows - 1); + if (new_grid[idx] != NULL) + break; + grid->view--; + if (grid->view < 0) + grid->view += new_rows; + } + for (size_t r = 0; r < new_screen_rows; r++) { + int idx = (grid->view + r) & (new_rows - 1); + assert(new_grid[idx] != NULL); + } + /* Free old grid */ for (int r = 0; r < grid->num_rows; r++) grid_row_free(old_grid[r]);