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

Move cursor after adding rows #3544

Closed
willmcgugan opened this issue Oct 16, 2023 · 4 comments
Closed

Move cursor after adding rows #3544

willmcgugan opened this issue Oct 16, 2023 · 4 comments
Assignees

Comments

@willmcgugan
Copy link
Collaborator

Reported on Discord. If you add rows then call move cursor, the view won't follow the cursor.

A workaround is to use call_after_refresh, which suggests something on idle needs to be done before move_cursor works?

from textual.app import App
from textual.widgets import DataTable


class TestMove(App):
    def compose(self):
        yield DataTable()

    def on_mount(self):
        dt = self.query_one(DataTable)
        dt.add_column("thecolumn")

        for row in range(1000):
            dt.add_row(f"row number {row}")

        dt.move_cursor(row=500, column=1, animate=True)
        # self.call_after_refresh(dt.move_cursor, row=500, column=1, animate=False)


TestMove().run()
@TomJGooding
Copy link
Contributor

I think similar to #3533, the problem is that DataTable dimensions are only calculated on idle. The window in scroll_to_region has a height/width of zero, so the scrolling with move_cursor won't work.

But I'm sure you figured that out already, I'm just not sure of the proper fix.

@darrenburns darrenburns self-assigned this Oct 17, 2023
@darrenburns
Copy link
Member

Yeah, exactly right. The virtual_size needs to be updated before move_cursor works. Since the virtual_size is recomputed inside on_idle, if you try to move the cursor immediately, it won't work.

I think the fix is literally just to use call_after_refresh(self._scroll_cursor_into_view) inside move_cursor, since this can only work after on_idle runs.

I've implemented this fix in #3552

@TomJGooding
Copy link
Contributor

Both fixes a lot more straightforward then I expected!

@github-actions
Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

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

3 participants