Add scrollbar offset logic in HourlyDetails widget #115
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request adds functionality to the HourlyDetails widget in order to properly handle scrollbar positioning based on the nearest current time index.
get_preferred_size()
returns a tuple containing the minimum and the natural size of the widget (width and height), so i accessed directly the natural size and retrieved the width:container_size = graphic_container.get_preferred_size()[1]
container_width = container_size.width
Then i calculate the offset based on the container width and the nearest current time index. It substracts 2 to leave the last 2 cards before the current time visible:
scrollbar_offset = (container_width / 24) * (nearest_current_time_idx - 2)
In the end i created Gtk adjustment for the horizontal scrollbar, setting its value to the calculated scrollbar offset and ensuring it remains within the bounds of 0 and the width of the container:
h_adjustment = Gtk.Adjustment(value=scrollbar_offset, lower=0, upper=container_width)
scrolled_window.set_hadjustment(h_adjustment)
Note: In case
nearest_current_time_idx
will be 0 or 1, the scrollbar_offset will have a negative value which means the scrollbar will be positioned at the extreme left, so we don't have to handle this case separately. Also the code is positioned at the end of the file because it relies on the initialization of UI elements which occur just before.