Skip to content

editor: Ensure scrollbar thumb is not layouted when content size is smaller than viewport #30189

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

Merged
merged 3 commits into from
May 8, 2025

Conversation

MrSubidubi
Copy link
Contributor

As discussed and explained in #26893 (comment)

This PR fixes an issue where we would have zero-divisions during scrollbar layouting for small files.

This happened due to the fact that for small files,

let text_unit_size =
(track_length - thumb_size) / (total_text_units - text_units_per_page).max(0.);

would be NaN, since (total_text_units - text_units_per_page).max(0.) would return 0., which we would divide by.

However, this was neccessary to be in place, as this prevented the scroll thumb from being rendered for small files: Due to this being NaN, the thumb origin would be Pixels(NaN), which prevented the rendering of the scrollbar thumb.

This PR fixes this behavior by accounting for this scenario and changing the thumb bounds to be an Option<Bounds<Pixels>> instead. This furthermore has the advantage that we have to compute the thumb only once and storing it in the layout, which was previously not possible.

Most notably, this enables scrollbar markers to show for smaller files:

scrollbar_small_files.mov

Currently, no markers are shown due to the fact that Pixels(NaN) is set as the origin point.

Also, I changed that the cursor style will only be changed on the scrollbar hitbox when we will actually show a thumb. This way, for small files (where viewport > content size) the cursor will not change when a user hovers with their mouse over the scrollbars hitbox.

Theoretically, I could also include the change mentioned in #26893 (comment) here. Given the introduction of the minimap as well as #29316 and the cursor style taken care of here, removing the guard would not change anything and creates the possibility to soon introduce scrollbars for auto height editors. Please let me know whether we want to have this in this PR or whether I shall create a seperate one.

Release Notes:

  • Enabled scrollbar marker rendering for small files.

MrSubidubi added 2 commits May 7, 2025 23:06
This ensures we do not change the cursor style if we insert a hitbox but do not end up showing a thumb at all
@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label May 7, 2025
Copy link
Contributor

@SomeoneToIgnore SomeoneToIgnore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let me know whether we want to have this in this PR or whether I shall create a seperate one.

That was +1/-1 change IIRC, so why not, this PR is not as huge as the original one.

@SomeoneToIgnore SomeoneToIgnore self-assigned this May 8, 2025
Copy link
Contributor

@SomeoneToIgnore SomeoneToIgnore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice follow-up, thank you.

@SomeoneToIgnore SomeoneToIgnore enabled auto-merge (squash) May 8, 2025 06:54
@SomeoneToIgnore SomeoneToIgnore merged commit 1ec466b into zed-industries:main May 8, 2025
19 checks passed
@MrSubidubi MrSubidubi deleted the scrollbar-thumb branch May 8, 2025 07:07
lj3954 pushed a commit to lj3954/zed that referenced this pull request May 9, 2025
…maller than viewport (zed-industries#30189)

As discussed and explained in
zed-industries#26893 (comment)

This PR fixes an issue where we would have zero-divisions during
scrollbar layouting for small files.

This happened due to the fact that for small files, 


https://github.com/zed-industries/zed/blob/9c1b2afa492755a1e8cb0a77f929845941c97cdc/crates/editor/src/element.rs#L8562-L8563

would be `NaN`, since `(total_text_units - text_units_per_page).max(0.)`
would return `0.`, which we would divide by.

However, this was neccessary to be in place, as this prevented the
scroll thumb from being rendered for small files: Due to this being
`NaN`, the thumb origin would be `Pixels(NaN)`, which prevented the
rendering of the scrollbar thumb.

This PR fixes this behavior by accounting for this scenario and changing
the thumb bounds to be an `Option<Bounds<Pixels>>` instead. This
furthermore has the advantage that we have to compute the thumb only
once and storing it in the layout, which was previously not possible.

Most notably, this enables scrollbar markers to show for smaller files:


https://github.com/user-attachments/assets/9fa5d240-8795-4fae-9933-aed144df4f5e

Currently, no markers are shown due to the fact that `Pixels(NaN)` is
set as the origin point.

Also, I changed that the cursor style will only be changed on the
scrollbar hitbox when we will actually show a thumb. This way, for small
files (where viewport > content size) the cursor will not change when a
user hovers with their mouse over the scrollbars hitbox.

Theoretically, I could also include the change mentioned in
zed-industries#26893 (comment)
here. Given the introduction of the minimap as well as zed-industries#29316 and the
cursor style taken care of here, removing the guard would not change
anything and creates the possibility to soon introduce scrollbars for
auto height editors. Please let me know whether we want to have this in
this PR or whether I shall create a seperate one.

Release Notes:

- Enabled scrollbar marker rendering for small files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed The user has signed the Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants