def update_scroll_offset()

in src/pathpicker/screen_control.py [0:0]


    def update_scroll_offset(self) -> None:
        """
        yay scrolling logic! we will start simple here
        and basically just center the viewport to current
        matched line
        """
        window_height = self.get_viewport_height()
        half_height = int(round(window_height / 2.0))

        # important, we need to get the real SCREEN position
        # of the hover index, not its index within our matches
        hovered = self.line_matches[self.hover_index]
        desired_top_row = hovered.get_screen_index() - half_height

        old_offset = self.scroll_offset
        desired_top_row = max(desired_top_row, 0)
        new_offset = -desired_top_row
        # lets add in some leeway -- don't bother repositioning
        # if the old offset is within 1/2 of the window height
        # of our desired (unless we absolutely have to)
        if (
            abs(new_offset - old_offset) > half_height / 2
            or self.hover_index + old_offset < 0
        ):
            # need to reassign now we have gone too far
            self.scroll_offset = new_offset
        if old_offset is not self.scroll_offset:
            self.dirty_all()

        # also update our scroll bar
        self.scroll_bar.calc_box_fractions()