Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting OptionList.highlighted after repopulating with new options doesn't update the vertical scrollbar #5632

Open
davep opened this issue Mar 10, 2025 · 0 comments

Comments

@davep
Copy link
Contributor

davep commented Mar 10, 2025

This might be a variation of #5631, but the workaround I used there doesn't seem to work here so I thought it worth writing up as a separate MRE of a possibly-different effect.

Given this code:

from textual.app import App, ComposeResult
from textual.widgets import OptionList

class OptionListScrollIssueApp(App[None]):

    CSS = """
    OptionList {
        height: 1fr;
    }
    """

    BINDINGS = [("space", "toggle_size")]

    def compose(self) -> ComposeResult:
        yield OptionList(*[f"This is option {n}" for n in range(1000)])

    def on_mount(self) -> None:
        # Workaround for https://github.com/Textualize/textual/issues/5631
        self.call_after_refresh(self.query_one(OptionList).action_last)

    def action_toggle_size(self) -> None:
        option_list = self.query_one(OptionList)
        new_conent = [f"This is option {n}" for n in range(5 if option_list.option_count == 1000 else 1000)]
        option_list.clear_options().add_options(new_conent)
        option_list.highlighted = 0

if __name__ == "__main__":
    OptionListScrollIssueApp().run()

when you first run it you see this:

Image

That is: the list is populated, the highlight is on the last item, the vertical scrollbar is in the correct place.

If you then press space you see this:

Image

Again: all good; the list is repopulated and there is no vertical scrollbar visible.

If you press space again you see this:

Image

Here the list is repopulated with lots of items, the highlight is on the first item (as intended), but the vertical scrollbar is positioned as if we were on the last item.

Even doing some form of call_after_refresh to reset the highlight doesn't seem to help. I can get the effect I would expect by tearing down and recreating the OptionList, of course:

from textual.app import App, ComposeResult
from textual.widgets import OptionList

class OptionListScrollIssueApp(App[None]):

    CSS = """
    OptionList {
        height: 1fr;
    }
    """

    BINDINGS = [("space", "toggle_size")]

    def compose(self) -> ComposeResult:
        yield OptionList(*[f"This is option {n}" for n in range(1000)])

    def on_mount(self) -> None:
        # Workaround for https://github.com/Textualize/textual/issues/5631
        self.call_after_refresh(self.query_one(OptionList).action_last)

    async def action_toggle_size(self) -> None:
        next_size = 5 if self.query_one(OptionList).option_count == 1000 else 1000
        with self.batch_update():
            await self.query_one(OptionList).remove()
            await self.mount(OptionList(*[f"This is option {n}" for n in range(next_size)]))
            self.query_one(OptionList).focus()

if __name__ == "__main__":
    OptionListScrollIssueApp().run()

Textual Diagnostics

Versions

Name Value
Textual 2.1.2
Rich 13.9.4

Python

Name Value
Version 3.13.1
Implementation CPython
Compiler Clang 18.1.8
Executable /Users/davep/temp/z/.venv/bin/python3

Operating System

Name Value
System Darwin
Release 24.3.0
Version Darwin Kernel Version 24.3.0: Thu Jan 2 20:23:36 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T8112

Terminal

Name Value
Terminal Application ghostty (1.1.2)
TERM xterm-256color
COLORTERM truecolor
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=114, height=56
legacy_windows False
min_width 1
max_width 114
is_terminal False
encoding utf-8
max_height 56
justify None
overflow None
no_wrap False
highlight None
markup None
height None
davep added a commit to davep/aging that referenced this issue Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant