The scroll_visible method behaves erratically for uncomposed widgets #1354
Replies: 4 comments 1 reply
-
I'm not 100% certain, but I suspect the issue here is that you're attempting to scroll to a widget that, at the time of the request, may not be fully available in the DOM yet. I'd be tempted to write your problem method like this: async def action_add_problem_widget(self):
new_widget = ProblemWidget()
await self.query_one("#widgets").mount(new_widget)
new_widget.scroll_visible() Trying that here seems to make all the difference for me. As a slight aside (nothing to do with the issue here), you could write your reset action more like this: def action_reset(self):
self.good_timer.pause()
self.problem_timer.pause()
self.query("#widgets > *").remove() Saves looping over all the children. :-) |
Beta Was this translation helpful? Give feedback.
-
Thanks, Dave! I was beginning to wonder about this but had no idea I could simply await the |
Beta Was this translation helpful? Give feedback.
-
And thanks for the oneliner to remove all widgets. I forgot about the child combinator and universal selector. I knew there had to be a better way, 😉. |
Beta Was this translation helpful? Give feedback.
-
I'll have to defer to @willmcgugan for those questions I'm afraid (or at least catch up with him tomorrow and get back to you). I'll bump this over into the Questions in Discussions to keep it in mind and keep going. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
When you add a widget to a container and call
new_widget.scroll_visible()
the container should scroll down to make that widget visible. That works for composed widgets (widgets having acompose()
method) but behaves erratically for simple static widgets (widgets having only anon_mount()
method). When you add new widgets through a key binding, it seems to work as expected. However, if you add widgets through an automatic process like an interval timer the scroll behaviour is very erratic. In my experience, it mostly scrolls to the top of the container instead of the bottom. However, if you scroll back and forth you can see that it sometimes stays fixed at the bottom for a bit and then jumps to the middle or top again.I've included example code. Press 'g' or 'p' to see that manually adding widgets does not seem to be a problem. Start a good timer or a problem timer to see the difference between composed and uncomposed widgets being added automatically to the container.
Beta Was this translation helpful? Give feedback.
All reactions