Saving state of scrollableframe after calling frame.rebuild

I have a content inside a scrollableframe that has two modes for sytle using (style = “style1” if self.selected else “style_2”. If object is selected I use style_1 else style_2. However to call this update I need to use ui.Window.frame.rebuild() and this causes the scroll to go to the initial state again, which causes a bad behavior. Is there a way to maintain the scroll’s bar at the same position after calling frame.rebuild() ?

Hi @icaroleles1! You can store the values for ScrollingFrame.scroll_x and ScrollingFrame.scroll_y and reuse them on the rebuild. Here’s an example:

import carb
import omni.ext
import omni.ui as ui

class MyWindow(ui.Window):
    def __init__(self, title: str = None, delegate=None, **kwargs):
        super().__init__(title, **kwargs)
        self.frame.set_build_fn(self._build_window)
        self._scroll_x = 0
        self._scroll_y = 0

    def _build_window(self):
        # Build the ScrollingFrame and give it scroll_x and scroll_y values.
        self._scroll_frame = ui.ScrollingFrame(height=200, width=200, scroll_x=self._scroll_x, scroll_y=self._scroll_y)
        with self._scroll_frame:
            with ui.VStack(height=300, width=300):
                ui.Label("My Label")
                def clicked():
                    # store the state of the ScrollingFrame before rebuilding.
                    self._scroll_x = self._scroll_frame.scroll_x
                    self._scroll_y = self._scroll_frame.scroll_y
                    self.frame.rebuild()

                ui.Button("Click Me", clicked_fn=clicked)
                ui.Button("Click Me2", clicked_fn=clicked)
                ui.Button("Click Me3", clicked_fn=clicked)

    def destroy(self) -> None:
        return super().destroy()


class MyExtension(omni.ext.IExt):
    def on_startup(self, ext_id):
        print("MyExtension startup")
        self._window = MyWindow("Window", width=300, height=300)

    def on_shutdown(self):
        print("MyExtension shutdown")
        if self._window:
            self._window.destroy()
            self._window = None

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.