Skip to content

Commit 1f1a30c

Browse files
MrSubidubitomatitito
authored andcommitted
ui: Properly update scrollbar track color (zed-industries#41354)
Closes zed-industries#41334 This comes down to a caching issue.. Release Notes: - Fixed an issue where the scrollbar track color would not update in case the theme was changed.
1 parent 713ce0c commit 1f1a30c

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

crates/ui/src/components/scrollbar.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,21 @@ where
101101
T: ScrollableHandle,
102102
{
103103
let element_id = config.id.take().unwrap_or_else(|| caller_location.into());
104+
let track_color = config.track_color;
104105

105-
window.use_keyed_state(element_id, cx, |window, cx| {
106+
let state = window.use_keyed_state(element_id, cx, |window, cx| {
106107
let parent_id = cx.entity_id();
107108
ScrollbarStateWrapper(
108109
cx.new(|cx| ScrollbarState::new_from_config(config, parent_id, window, cx)),
109110
)
110-
})
111+
});
112+
113+
state.update(cx, |state, cx| {
114+
state
115+
.0
116+
.update(cx, |state, _cx| state.update_track_color(track_color))
117+
});
118+
state
111119
}
112120

113121
pub trait WithScrollbar: Sized {
@@ -334,7 +342,7 @@ enum ReservedSpace {
334342
#[default]
335343
None,
336344
Thumb,
337-
Track(Hsla),
345+
Track,
338346
}
339347

340348
impl ReservedSpace {
@@ -343,14 +351,7 @@ impl ReservedSpace {
343351
}
344352

345353
fn needs_scroll_track(&self) -> bool {
346-
matches!(self, ReservedSpace::Track(_))
347-
}
348-
349-
fn track_color(&self) -> Option<Hsla> {
350-
match self {
351-
ReservedSpace::Track(color) => Some(*color),
352-
_ => None,
353-
}
354+
*self == ReservedSpace::Track
354355
}
355356
}
356357

@@ -385,6 +386,7 @@ pub struct Scrollbars<T: ScrollableHandle = ScrollHandle> {
385386
tracked_entity: Option<Option<EntityId>>,
386387
scrollable_handle: Handle<T>,
387388
visibility: Point<ReservedSpace>,
389+
track_color: Option<Hsla>,
388390
scrollbar_width: ScrollbarWidth,
389391
}
390392

@@ -406,6 +408,7 @@ impl Scrollbars {
406408
scrollable_handle: Handle::Untracked(ScrollHandle::new),
407409
tracked_entity: None,
408410
visibility: show_along.apply_to(Default::default(), ReservedSpace::Thumb),
411+
track_color: None,
409412
scrollbar_width: ScrollbarWidth::Normal,
410413
}
411414
}
@@ -446,6 +449,7 @@ impl<ScrollHandle: ScrollableHandle> Scrollbars<ScrollHandle> {
446449
scrollbar_width,
447450
visibility,
448451
get_visibility,
452+
track_color,
449453
..
450454
} = self;
451455

@@ -455,6 +459,7 @@ impl<ScrollHandle: ScrollableHandle> Scrollbars<ScrollHandle> {
455459
tracked_entity: tracked_entity_id,
456460
visibility,
457461
scrollbar_width,
462+
track_color,
458463
get_visibility,
459464
}
460465
}
@@ -465,7 +470,8 @@ impl<ScrollHandle: ScrollableHandle> Scrollbars<ScrollHandle> {
465470
}
466471

467472
pub fn with_track_along(mut self, along: ScrollAxes, background_color: Hsla) -> Self {
468-
self.visibility = along.apply_to(self.visibility, ReservedSpace::Track(background_color));
473+
self.visibility = along.apply_to(self.visibility, ReservedSpace::Track);
474+
self.track_color = Some(background_color);
469475
self
470476
}
471477

@@ -593,6 +599,7 @@ struct ScrollbarState<T: ScrollableHandle = ScrollHandle> {
593599
show_behavior: ShowBehavior,
594600
get_visibility: fn(&App) -> ShowScrollbar,
595601
visibility: Point<ReservedSpace>,
602+
track_color: Option<Hsla>,
596603
show_state: VisibilityState,
597604
mouse_in_parent: bool,
598605
last_prepaint_state: Option<ScrollbarPrepaintState>,
@@ -622,6 +629,7 @@ impl<T: ScrollableHandle> ScrollbarState<T> {
622629
scroll_handle,
623630
width: config.scrollbar_width,
624631
visibility: config.visibility,
632+
track_color: config.track_color,
625633
show_behavior,
626634
get_visibility: config.get_visibility,
627635
show_state: VisibilityState::from_behavior(show_behavior),
@@ -794,6 +802,10 @@ impl<T: ScrollableHandle> ScrollbarState<T> {
794802
}
795803
}
796804

805+
fn update_track_color(&mut self, track_color: Option<Hsla>) {
806+
self.track_color = track_color;
807+
}
808+
797809
fn parent_hovered(&self, window: &Window) -> bool {
798810
self.last_prepaint_state
799811
.as_ref()
@@ -1103,8 +1115,10 @@ impl<T: ScrollableHandle> Element for ScrollbarElement<T> {
11031115
.not()
11041116
.then(|| ScrollbarPrepaintState {
11051117
thumbs: {
1106-
let thumb_ranges = self.state.read(cx).thumb_ranges().collect::<Vec<_>>();
1107-
let width = self.state.read(cx).width.to_pixels();
1118+
let state = self.state.read(cx);
1119+
let thumb_ranges = state.thumb_ranges().collect::<Vec<_>>();
1120+
let width = state.width.to_pixels();
1121+
let track_color = state.track_color;
11081122

11091123
let additional_padding = if thumb_ranges.len() == 2 {
11101124
width
@@ -1169,8 +1183,7 @@ impl<T: ScrollableHandle> Element for ScrollbarElement<T> {
11691183
},
11701184
HitboxBehavior::BlockMouseExceptScroll,
11711185
),
1172-
track_background: reserved_space
1173-
.track_color()
1186+
track_background: track_color
11741187
.map(|color| (padded_bounds.dilate(SCROLLBAR_PADDING), color)),
11751188
reserved_space,
11761189
}

0 commit comments

Comments
 (0)