Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
ui: Fix scrollbar settings not applying to open scrollbars (#66017)
Smit Barmase18512de483e4
diff --git a/crates/ui/src/components/scrollbar.rs b/crates/ui/src/components/scrollbar.rsindex 32010ce158a..3cc42efc7f3 100644--- a/crates/ui/src/components/scrollbar.rs+++ b/crates/ui/src/components/scrollbar.rs@@ -7,7 +7,7 @@ use gpui::{Entity, EntityId, GlobalElementId, Hitbox, HitboxBehavior, Hsla, InteractiveElement,IntoElement, IsZero, LayoutId, ListState, MouseButton, MouseDownEvent, MouseMoveEvent,MouseUpEvent, ParentElement, Pixels, Point, Position, Render, ScrollEvent, ScrollHandle, Size,- Stateful, StatefulInteractiveElement, Style, StyleRefinement, Styled, Task,+ Stateful, StatefulInteractiveElement, Style, StyleRefinement, Styled, Subscription, Task,UniformListDecoration, UniformListScrollHandle, Window, ease_in_out, fill,prelude::FluentBuilder as _, px, quad, relative, size,};@@ -324,12 +324,15 @@ enum ShowBehavior {}impl ShowBehavior {- fn from_setting(setting: ShowScrollbar, cx: &mut App) -> Self {+ fn from_setting(setting: ShowScrollbar, cx: &App) -> Self {match setting {ShowScrollbar::Never => Self::Never,ShowScrollbar::Auto => Self::Autohide,ShowScrollbar::System => {- if cx.default_global::<ScrollbarAutoHide>().should_hide() {+ if cx+ .try_global::<ScrollbarAutoHide>()+ .is_some_and(ScrollbarAutoHide::should_hide)+ {Self::Autohide} else {Self::Always@@ -671,14 +674,16 @@ struct TrackColors {has_border: bool,}-pub fn on_new_scrollbars<T: gpui::Global>(cx: &mut App) {- cx.observe_new::<ScrollbarState>(|_, window, cx| {- if let Some(window) = window {- cx.observe_global_in::<T>(window, ScrollbarState::settings_changed)- .detach();- }- })- .detach();+/// Lets scrollbars observe settings without `ui` depending on the settings crate.+struct ScrollbarSettingsChanged;++impl gpui::Global for ScrollbarSettingsChanged {}++pub fn observe_scrollbar_settings<T: gpui::Global>(cx: &mut App) {+ cx.observe_global::<T>(|cx| cx.set_global(ScrollbarSettingsChanged))+ .detach();+ cx.observe_global::<ScrollbarAutoHide>(|cx| cx.set_global(ScrollbarSettingsChanged))+ .detach();}/// This is used to ensure notifies within the state do not notify the parent@@ -702,6 +707,7 @@ struct ScrollbarState<T: ScrollableHandle = ScrollHandle> {mouse_in_parent: bool,last_prepaint_state: Option<ScrollbarPrepaintState>,_auto_hide_task: Option<Task<()>>,+ _settings_subscription: Subscription,}impl<T: ScrollableHandle> ScrollbarState<T> {@@ -736,23 +742,24 @@ impl<T: ScrollableHandle> ScrollbarState<T> {mouse_in_parent: true,last_prepaint_state: None,_auto_hide_task: None,+ _settings_subscription: cx+ .observe_global::<ScrollbarSettingsChanged>(Self::settings_changed),}}- fn settings_changed(&mut self, window: &mut Window, cx: &mut Context<Self>) {+ fn settings_changed(&mut self, cx: &mut Context<Self>) {self.set_show_behavior(ShowBehavior::from_setting((self.get_visibility)(cx), cx),- window,cx,);}/// Schedules a scrollbar auto hide if no auto hide is currently in progress yet.- fn schedule_auto_hide(&mut self, window: &mut Window, cx: &mut Context<Self>) {+ fn schedule_auto_hide(&mut self, cx: &mut Context<Self>) {if self._auto_hide_task.is_none() {self._auto_hide_task = (self.visible() && self.show_behavior == ShowBehavior::Autohide).then(|| {- cx.spawn_in(window, async move |scrollbar_state, cx| {+ cx.spawn(async move |scrollbar_state, cx| {cx.background_executor().timer(SCROLLBAR_HIDE_DELAY_INTERVAL).await;@@ -769,23 +776,27 @@ impl<T: ScrollableHandle> ScrollbarState<T> {}}- fn show_scrollbars(&mut self, window: &mut Window, cx: &mut Context<Self>) {+ fn restart_auto_hide(&mut self, cx: &mut Context<Self>) {+ self._auto_hide_task.take();+ self.schedule_auto_hide(cx);+ }++ fn show_scrollbars(&mut self, cx: &mut Context<Self>) {let visibility = self.show_state.toggle_visible(self.show_behavior);self.set_visibility(visibility, cx);- self._auto_hide_task.take();- self.schedule_auto_hide(window, cx);+ self.restart_auto_hide(cx);}- fn set_show_behavior(- &mut self,- behavior: ShowBehavior,- window: &mut Window,- cx: &mut Context<Self>,- ) {+ fn set_show_behavior(&mut self, behavior: ShowBehavior, cx: &mut Context<Self>) {if self.show_behavior != behavior {self.show_behavior = behavior;- self.set_visibility(VisibilityState::from_behavior(behavior), cx);- self.schedule_auto_hide(window, cx);+ let visibility = if behavior == ShowBehavior::Autohide && self.visible() {+ self.show_state.clone()+ } else {+ VisibilityState::from_behavior(behavior)+ };+ self.set_visibility(visibility, cx);+ self.restart_auto_hide(cx);cx.notify();}}@@ -840,14 +851,8 @@ impl<T: ScrollableHandle> ScrollbarState<T> {self.thumb_state.is_dragging()}- fn set_dragging(- &mut self,- axis: ScrollbarAxis,- drag_offset: Pixels,- window: &mut Window,- cx: &mut Context<Self>,- ) {- self.set_thumb_state(ThumbState::Dragging(axis, drag_offset), window, cx);+ fn set_dragging(&mut self, axis: ScrollbarAxis, drag_offset: Pixels, cx: &mut Context<Self>) {+ self.set_thumb_state(ThumbState::Dragging(axis, drag_offset), cx);self.scroll_handle().drag_started();}@@ -869,15 +874,14 @@ impl<T: ScrollableHandle> ScrollbarState<T> {} else {ThumbState::Inactive},- window,cx,);}- fn set_thumb_state(&mut self, state: ThumbState, window: &mut Window, cx: &mut Context<Self>) {+ fn set_thumb_state(&mut self, state: ThumbState, cx: &mut Context<Self>) {if self.thumb_state != state {if state == ThumbState::Inactive {- self.schedule_auto_hide(window, cx);+ self.schedule_auto_hide(cx);} else {self.set_visibility(self.show_state.toggle_visible(self.show_behavior), cx);self._auto_hide_task.take();@@ -1391,8 +1395,7 @@ impl<T: ScrollableHandle> Element for ScrollbarElement<T> {scrollbar_state.reveal_policy,)}) {- self.state- .update(cx, |state, cx| state.show_scrollbars(window, cx));+ self.state.update(cx, |state, cx| state.show_scrollbars(cx));}prepaint_state.map(|state| {@@ -1627,7 +1630,7 @@ impl<T: ScrollableHandle> Element for ScrollbarElement<T> {window.on_mouse_event({let state = self.state.clone();- move |event: &MouseDownEvent, phase, window, cx| {+ move |event: &MouseDownEvent, phase, _, cx| {state.update(cx, |state, cx| {let Some(scrollbar_layout) = (phase == capture_phase&& event.button == MouseButton::Left)@@ -1644,7 +1647,7 @@ impl<T: ScrollableHandle> Element for ScrollbarElement<T> {if thumb_bounds.contains(&event.position) {let offset =event.position.along(*axis) - thumb_bounds.origin.along(*axis);- state.set_dragging(*axis, offset, window, cx);+ state.set_dragging(*axis, offset, cx);} else {let scroll_handle = state.scroll_handle();let click_offset = scrollbar_layout.compute_click_offset(@@ -1706,7 +1709,7 @@ impl<T: ScrollableHandle> Element for ScrollbarElement<T> {if event.pressed_button.is_none() =>{if matches!(hover, ParentHoverEvent::Entered) {- state.show_scrollbars(window, cx);+ state.show_scrollbars(cx);}state.update_hovered_thumb(&event.position, window, cx);if state.thumb_state != ThumbState::Inactive {@@ -1714,7 +1717,7 @@ impl<T: ScrollableHandle> Element for ScrollbarElement<T> {}}ParentHoverEvent::Exited => {- state.set_thumb_state(ThumbState::Inactive, window, cx);+ state.set_thumb_state(ThumbState::Inactive, cx);}_ => {}}@@ -1736,7 +1739,7 @@ impl<T: ScrollableHandle> Element for ScrollbarElement<T> {}if !state.parent_hovered(window) {- state.schedule_auto_hide(window, cx);+ state.schedule_auto_hide(cx);return;}@@ -1763,7 +1766,80 @@ impl<T: ScrollableHandle> IntoElement for ScrollbarElement<T> {#[cfg(test)]mod tests {use super::*;- use gpui::point;+ use gpui::{TestAppContext, point};++ struct TestScrollbarSetting(ShowScrollbar);++ impl gpui::Global for TestScrollbarSetting {}++ fn new_scrollbar_state(+ setting: ShowScrollbar,+ cx: &mut TestAppContext,+ ) -> Entity<ScrollbarState<UniformListScrollHandle>> {+ cx.update(|cx| {+ cx.set_global(TestScrollbarSetting(setting));+ observe_scrollbar_settings::<TestScrollbarSetting>(cx);+ });+ cx.new(|cx| {+ ScrollbarState::new_from_config(+ Scrollbars::new_with_setting(ScrollAxes::Both, |cx| {+ cx.global::<TestScrollbarSetting>().0+ })+ .tracked_scroll_handle(&UniformListScrollHandle::new()),+ Corners::default(),+ cx.entity_id(),+ cx,+ )+ })+ }++ #[gpui::test]+ fn test_settings_change_updates_scrollbar_with_custom_handle(cx: &mut TestAppContext) {+ let state = new_scrollbar_state(ShowScrollbar::Always, cx);+ cx.update(|cx| cx.set_global(TestScrollbarSetting(ShowScrollbar::Never)));+ state.read_with(cx, |state, _| {+ assert_eq!(state.show_state, VisibilityState::Disabled);+ });+ }++ #[gpui::test]+ fn test_switching_to_auto_keeps_visible_scrollbar(cx: &mut TestAppContext) {+ let state = new_scrollbar_state(ShowScrollbar::Always, cx);+ cx.update(|cx| cx.set_global(TestScrollbarSetting(ShowScrollbar::Auto)));+ state.read_with(cx, |state, _| {+ assert_eq!(state.show_state, VisibilityState::Visible);+ });++ cx.executor().advance_clock(SCROLLBAR_HIDE_DELAY_INTERVAL);+ state.read_with(cx, |state, _| {+ assert_eq!(state.show_state, VisibilityState::for_autohide());+ });+ }++ #[gpui::test]+ fn test_settings_change_cancels_pending_auto_hide(cx: &mut TestAppContext) {+ let state = new_scrollbar_state(ShowScrollbar::Auto, cx);+ state.update(cx, |state, cx| state.schedule_auto_hide(cx));++ cx.update(|cx| cx.set_global(TestScrollbarSetting(ShowScrollbar::Always)));+ cx.executor().advance_clock(SCROLLBAR_HIDE_DELAY_INTERVAL);+ state.read_with(cx, |state, _| {+ assert_eq!(state.show_state, VisibilityState::Visible);+ });+ }++ #[gpui::test]+ fn test_auto_hide_change_updates_system_scrollbar(cx: &mut TestAppContext) {+ let state = new_scrollbar_state(ShowScrollbar::System, cx);+ state.read_with(cx, |state, _| {+ assert!(state.show_behavior == ShowBehavior::Always);+ });++ cx.update(|cx| cx.set_global(ScrollbarAutoHide(true)));+ state.read_with(cx, |state, _| {+ assert!(state.show_behavior == ShowBehavior::Autohide);+ });+ }#[test]fn default_reveal_policy_reveals_for_content_changes() {diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rsindex c3bde91b04d..78dad31e73e 100644--- a/crates/zed/src/main.rs+++ b/crates/zed/src/main.rs@@ -554,7 +554,7 @@ fn main() {tx.send(Some(options)).log_err();}).detach();- ui::on_new_scrollbars::<SettingsStore>(cx);+ ui::observe_scrollbar_settings::<SettingsStore>(cx);let node_runtime = NodeRuntime::new(client.http_client(), Some(shell_env_loaded_rx), rx);