Keep the scroll position in tree mode when the list is rebuilt, GitHub

issue 5631 from Kim A Brandt.
This commit is contained in:
nicm
2026-09-24 18:59:00 +00:00
committed by tmux update bot
parent 7fc7f1bf69
commit 4ae837de68

View File

@@ -1,4 +1,4 @@
/* $OpenBSD: mode-tree.c,v 1.102 2026/09/21 12:14:32 nicm Exp $ */ /* $OpenBSD: mode-tree.c,v 1.103 2026/09/24 18:59:00 nicm Exp $ */
/* /*
* Copyright (c) 2017 Nicholas Marriott <nicholas.marriott@gmail.com> * Copyright (c) 2017 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -277,11 +277,22 @@ mode_tree_free_items(struct mode_tree_list *mtl)
static void static void
mode_tree_check_selected(struct mode_tree_data *mtd) mode_tree_check_selected(struct mode_tree_data *mtd)
{ {
if (mtd->height == 0)
return;
/* If the list has shrunk, do not leave empty lines at the bottom. */
if (mtd->line_size <= mtd->height)
mtd->offset = 0;
else if (mtd->offset > mtd->line_size - mtd->height)
mtd->offset = mtd->line_size - mtd->height;
/* /*
* If the current line would now be off screen reset the offset to the * If the current line would now be off screen move the offset so it is
* last visible line. * visible.
*/ */
if (mtd->current > mtd->height - 1) if (mtd->current < mtd->offset)
mtd->offset = mtd->current;
else if (mtd->current > mtd->offset + mtd->height - 1)
mtd->offset = mtd->current - mtd->height + 1; mtd->offset = mtd->current - mtd->height + 1;
} }
@@ -501,20 +512,14 @@ mode_tree_set_current(struct mode_tree_data *mtd, uint64_t tag)
if (mode_tree_get_tag(mtd, tag, &found)) { if (mode_tree_get_tag(mtd, tag, &found)) {
mtd->current = found; mtd->current = found;
if (mtd->current > mtd->height - 1) mode_tree_check_selected(mtd);
mtd->offset = mtd->current - mtd->height + 1;
else
mtd->offset = 0;
return (1); return (1);
} }
if (mtd->current >= mtd->line_size) { if (mtd->current >= mtd->line_size) {
if (mtd->line_size == 0) if (mtd->line_size == 0)
return (0); return (0);
mtd->current = mtd->line_size - 1; mtd->current = mtd->line_size - 1;
if (mtd->current > mtd->height - 1) mode_tree_check_selected(mtd);
mtd->offset = mtd->current - mtd->height + 1;
else
mtd->offset = 0;
} }
return (0); return (0);
} }
@@ -683,13 +688,13 @@ mode_tree_build(struct mode_tree_data *mtd)
if (mtd->line_list != NULL && tag == UINT64_MAX) if (mtd->line_list != NULL && tag == UINT64_MAX)
tag = mtd->line_list[mtd->current].item->tag; tag = mtd->line_list[mtd->current].item->tag;
mode_tree_set_current(mtd, tag);
mtd->width = screen_size_x(s); mtd->width = screen_size_x(s);
if (mtd->preview != MODE_TREE_PREVIEW_OFF) if (mtd->preview != MODE_TREE_PREVIEW_OFF)
mode_tree_set_height(mtd); mode_tree_set_height(mtd);
else else
mtd->height = screen_size_y(s); mtd->height = screen_size_y(s);
mode_tree_set_current(mtd, tag);
mode_tree_check_selected(mtd); mode_tree_check_selected(mtd);
} }