This commit is contained in:
MrSubidubi 2025-08-20 10:41:52 +02:00
parent fd8fb1ed16
commit da9084226d
4 changed files with 91 additions and 38 deletions

View file

@ -2504,7 +2504,7 @@ impl Window {
&mut self, &mut self,
key: impl Into<ElementId>, key: impl Into<ElementId>,
cx: &mut App, cx: &mut App,
init: impl FnOnce(&mut Self, &mut App) -> S, init: impl FnOnce(&mut Self, &mut Context<S>) -> S,
) -> Entity<S> { ) -> Entity<S> {
let current_view = self.current_view(); let current_view = self.current_view();
self.with_global_id(key.into(), |global_id, window| { self.with_global_id(key.into(), |global_id, window| {
@ -2537,7 +2537,7 @@ impl Window {
pub fn use_state<S: 'static>( pub fn use_state<S: 'static>(
&mut self, &mut self,
cx: &mut App, cx: &mut App,
init: impl FnOnce(&mut Self, &mut App) -> S, init: impl FnOnce(&mut Self, &mut Context<S>) -> S,
) -> Entity<S> { ) -> Entity<S> {
self.use_keyed_state( self.use_keyed_state(
ElementId::CodeLocation(*core::panic::Location::caller()), ElementId::CodeLocation(*core::panic::Location::caller()),

View file

@ -4700,7 +4700,7 @@ impl OutlinePanel {
Scrollbars::for_settings::<OutlinePanelSettings>() Scrollbars::for_settings::<OutlinePanelSettings>()
.tracked_scroll_handle(self.scroll_handle.clone()) .tracked_scroll_handle(self.scroll_handle.clone())
.with_track_along(ScrollAxes::Horizontal) .with_track_along(ScrollAxes::Horizontal)
.tracked_entity(cx.entity()), .notify_content(),
window, window,
cx, cx,
) )

View file

@ -5347,7 +5347,7 @@ impl Render for ProjectPanel {
Scrollbars::for_settings::<ProjectPanelSettings>() Scrollbars::for_settings::<ProjectPanelSettings>()
.tracked_scroll_handle(self.scroll_handle.clone()) .tracked_scroll_handle(self.scroll_handle.clone())
.with_track_along(ScrollAxes::Horizontal) .with_track_along(ScrollAxes::Horizontal)
.tracked_entity(cx.entity()), .notify_content(),
window, window,
cx, cx,
) )

View file

@ -18,7 +18,7 @@ use std::ops::Range;
use crate::scrollbars::{ScrollbarVisibilitySetting, ShowScrollbar}; use crate::scrollbars::{ScrollbarVisibilitySetting, ShowScrollbar};
const SCROLLBAR_SHOW_INTERVAL: Duration = Duration::from_secs(1); const SCROLLBAR_SHOW_INTERVAL: Duration = Duration::from_millis(1500);
const SCROLLBAR_PADDING: Pixels = px(4.); const SCROLLBAR_PADDING: Pixels = px(4.);
pub mod scrollbars { pub mod scrollbars {
@ -105,7 +105,10 @@ where
let element_id = config.id.take().unwrap_or_else(|| caller_location.into()); let element_id = config.id.take().unwrap_or_else(|| caller_location.into());
window.use_keyed_state(element_id, cx, |window, cx| { window.use_keyed_state(element_id, cx, |window, cx| {
ScrollbarStateWrapper(cx.new(|cx| ScrollbarState::new_from_config(config, window, cx))) let parent_id = cx.entity_id();
ScrollbarStateWrapper(
cx.new(|cx| ScrollbarState::new_from_config(config, parent_id, window, cx)),
)
}) })
} }
@ -352,7 +355,7 @@ pub struct Scrollbars<
> { > {
id: Option<ElementId>, id: Option<ElementId>,
tracked_setting: PhantomData<S>, tracked_setting: PhantomData<S>,
tracked_entity_id: Option<EntityId>, tracked_entity: Option<Option<EntityId>>,
scrollable_handle: Box<dyn FnOnce() -> T>, scrollable_handle: Box<dyn FnOnce() -> T>,
handle_was_added: bool, handle_was_added: bool,
visibility: Point<ReservedSpace>, visibility: Point<ReservedSpace>,
@ -376,7 +379,7 @@ impl<S: ScrollbarVisibilitySetting> Scrollbars<S> {
tracked_setting: PhantomData, tracked_setting: PhantomData,
handle_was_added: false, handle_was_added: false,
scrollable_handle: Box::new(ScrollHandle::new), scrollable_handle: Box::new(ScrollHandle::new),
tracked_entity_id: None, tracked_entity: None,
visibility: show_along.apply_to(Default::default(), ReservedSpace::Thumb), visibility: show_along.apply_to(Default::default(), ReservedSpace::Thumb),
scrollbar_width: ScrollbarWidth::Normal, scrollbar_width: ScrollbarWidth::Normal,
} }
@ -398,9 +401,15 @@ impl<Setting: ScrollbarVisibilitySetting, ScrollHandle: ScrollableHandle>
self self
} }
/// Set a parent model which should be notified whenever this Scrollbar gets a scroll event. /// Notify the current context whenever this scrollbar gets a scroll event
pub fn tracked_entity<E: 'static>(mut self, entity: Entity<E>) -> Self { pub fn notify_content(mut self) -> Self {
self.tracked_entity_id = Some(entity.entity_id()); self.tracked_entity = Some(None);
self
}
/// Set a parent model which should be notified whenever this scrollbar gets a scroll event.
pub fn tracked_entity<E: 'static>(mut self, entity: &Entity<E>) -> Self {
self.tracked_entity = Some(Some(entity.entity_id()));
self self
} }
@ -411,7 +420,7 @@ impl<Setting: ScrollbarVisibilitySetting, ScrollHandle: ScrollableHandle>
let Self { let Self {
id, id,
tracked_setting, tracked_setting,
tracked_entity_id, tracked_entity: tracked_entity_id,
scrollbar_width, scrollbar_width,
visibility, visibility,
.. ..
@ -422,7 +431,7 @@ impl<Setting: ScrollbarVisibilitySetting, ScrollHandle: ScrollableHandle>
scrollable_handle: Box::new(|| tracked_scroll_handle), scrollable_handle: Box::new(|| tracked_scroll_handle),
id, id,
tracked_setting, tracked_setting,
tracked_entity_id, tracked_entity: tracked_entity_id,
visibility, visibility,
scrollbar_width, scrollbar_width,
} }
@ -475,6 +484,11 @@ impl VisibilityState {
} }
} }
enum ParentHovered {
Yes(bool),
No(bool),
}
/// This is used to ensure notifies within the state do not notify the parent /// This is used to ensure notifies within the state do not notify the parent
/// unintentionally. /// unintentionally.
struct ScrollbarStateWrapper<S: ScrollbarVisibilitySetting, T: ScrollableHandle>( struct ScrollbarStateWrapper<S: ScrollbarVisibilitySetting, T: ScrollableHandle>(
@ -484,7 +498,7 @@ struct ScrollbarStateWrapper<S: ScrollbarVisibilitySetting, T: ScrollableHandle>
/// A scrollbar state that should be persisted across frames. /// A scrollbar state that should be persisted across frames.
struct ScrollbarState<S: ScrollbarVisibilitySetting, T: ScrollableHandle = ScrollHandle> { struct ScrollbarState<S: ScrollbarVisibilitySetting, T: ScrollableHandle = ScrollHandle> {
thumb_state: ThumbState, thumb_state: ThumbState,
parent_id: Option<EntityId>, notify_id: Option<EntityId>,
manually_added: bool, manually_added: bool,
scroll_handle: T, scroll_handle: T,
width: ScrollbarWidth, width: ScrollbarWidth,
@ -492,6 +506,7 @@ struct ScrollbarState<S: ScrollbarVisibilitySetting, T: ScrollableHandle = Scrol
show_setting: ShowScrollbar, show_setting: ShowScrollbar,
visibility: Point<ReservedSpace>, visibility: Point<ReservedSpace>,
show_state: VisibilityState, show_state: VisibilityState,
mouse_in_parent: bool,
last_prepaint_state: Option<ScrollbarPrepaintState>, last_prepaint_state: Option<ScrollbarPrepaintState>,
_auto_hide_task: Option<Task<()>>, _auto_hide_task: Option<Task<()>>,
} }
@ -499,6 +514,7 @@ struct ScrollbarState<S: ScrollbarVisibilitySetting, T: ScrollableHandle = Scrol
impl<S: ScrollbarVisibilitySetting, T: ScrollableHandle> ScrollbarState<S, T> { impl<S: ScrollbarVisibilitySetting, T: ScrollableHandle> ScrollbarState<S, T> {
fn new_from_config( fn new_from_config(
config: Scrollbars<S, T>, config: Scrollbars<S, T>,
parent_id: EntityId,
window: &mut Window, window: &mut Window,
cx: &mut Context<Self>, cx: &mut Context<Self>,
) -> Self { ) -> Self {
@ -507,7 +523,7 @@ impl<S: ScrollbarVisibilitySetting, T: ScrollableHandle> ScrollbarState<S, T> {
let mut state = ScrollbarState { let mut state = ScrollbarState {
thumb_state: Default::default(), thumb_state: Default::default(),
parent_id: config.tracked_entity_id, notify_id: config.tracked_entity.map(|id| id.unwrap_or(parent_id)),
manually_added: config.handle_was_added, manually_added: config.handle_was_added,
scroll_handle: (config.scrollable_handle)(), scroll_handle: (config.scrollable_handle)(),
width: config.scrollbar_width, width: config.scrollbar_width,
@ -515,6 +531,7 @@ impl<S: ScrollbarVisibilitySetting, T: ScrollableHandle> ScrollbarState<S, T> {
tracked_setting: PhantomData, tracked_setting: PhantomData,
show_setting: ShowScrollbar::Always, show_setting: ShowScrollbar::Always,
show_state: VisibilityState::Visible, show_state: VisibilityState::Visible,
mouse_in_parent: true,
last_prepaint_state: None, last_prepaint_state: None,
_auto_hide_task: None, _auto_hide_task: None,
}; };
@ -545,9 +562,10 @@ impl<S: ScrollbarVisibilitySetting, T: ScrollableHandle> ScrollbarState<S, T> {
} }
} }
fn show_scrollbars(&mut self, cx: &mut Context<Self>) { fn show_scrollbars(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self._auto_hide_task.take();
self.set_visibility(VisibilityState::Visible, cx); self.set_visibility(VisibilityState::Visible, cx);
self._auto_hide_task.take();
self.schedule_auto_hide(window, cx);
} }
fn set_show_scrollbar( fn set_show_scrollbar(
@ -600,7 +618,7 @@ impl<S: ScrollbarVisibilitySetting, T: ScrollableHandle> ScrollbarState<S, T> {
&self.scroll_handle &self.scroll_handle
} }
fn set_offset(&mut self, offset: Point<Pixels>, cx: &mut Context<Self>) { fn set_offset(&mut self, offset: Point<Pixels>, window: &mut Window, cx: &mut Context<Self>) {
if self.scroll_handle.offset() != offset { if self.scroll_handle.offset() != offset {
self.scroll_handle.set_offset(offset); self.scroll_handle.set_offset(offset);
self.notify_parent(cx); self.notify_parent(cx);
@ -608,19 +626,30 @@ impl<S: ScrollbarVisibilitySetting, T: ScrollableHandle> ScrollbarState<S, T> {
} }
// We always want to show scrollbars in cases where the offset is updated. // We always want to show scrollbars in cases where the offset is updated.
self.show_scrollbars(cx); self.show_scrollbars(window, cx);
} }
fn is_dragging(&self) -> bool { fn is_dragging(&self) -> bool {
self.thumb_state.is_dragging() self.thumb_state.is_dragging()
} }
fn set_dragging(&mut self, axis: ScrollbarAxis, drag_offset: Pixels, cx: &mut Context<Self>) { fn set_dragging(
self.set_thumb_state(ThumbState::Dragging(axis, drag_offset), cx); &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);
self.scroll_handle().drag_started(); self.scroll_handle().drag_started();
} }
fn update_hovered_thumb(&mut self, position: &Point<Pixels>, cx: &mut Context<Self>) { fn update_hovered_thumb(
&mut self,
position: &Point<Pixels>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.set_thumb_state( self.set_thumb_state(
if let Some(&ScrollbarLayout { axis, .. }) = self if let Some(&ScrollbarLayout { axis, .. }) = self
.last_prepaint_state .last_prepaint_state
@ -631,17 +660,33 @@ impl<S: ScrollbarVisibilitySetting, T: ScrollableHandle> ScrollbarState<S, T> {
} else { } else {
ThumbState::Inactive ThumbState::Inactive
}, },
window,
cx, cx,
); );
} }
fn set_thumb_state(&mut self, state: ThumbState, cx: &mut Context<Self>) { fn set_thumb_state(&mut self, state: ThumbState, window: &mut Window, cx: &mut Context<Self>) {
if self.thumb_state != state { if self.thumb_state != state {
if matches!(state, ThumbState::Inactive) {
self.schedule_auto_hide(window, cx);
} else {
self.show_scrollbars(window, cx);
}
self.thumb_state = state; self.thumb_state = state;
cx.notify(); cx.notify();
} }
} }
fn update_parent_hovered(&mut self, position: &Point<Pixels>) -> ParentHovered {
let last_parent_hovered = self.mouse_in_parent;
self.mouse_in_parent = self.parent_hovered(position);
let state_changed = self.mouse_in_parent != last_parent_hovered;
match self.mouse_in_parent {
true => ParentHovered::Yes(state_changed),
false => ParentHovered::No(state_changed),
}
}
fn parent_hovered(&self, position: &Point<Pixels>) -> bool { fn parent_hovered(&self, position: &Point<Pixels>) -> bool {
self.last_prepaint_state self.last_prepaint_state
.as_ref() .as_ref()
@ -714,7 +759,7 @@ impl<S: ScrollbarVisibilitySetting, T: ScrollableHandle> ScrollbarState<S, T> {
} }
fn notify_parent(&self, cx: &mut App) { fn notify_parent(&self, cx: &mut App) {
if let Some(entity_id) = self.parent_id { if let Some(entity_id) = self.notify_id {
cx.notify(entity_id); cx.notify(entity_id);
} }
} }
@ -1036,7 +1081,7 @@ impl<S: ScrollbarVisibilitySetting, T: ScrollableHandle> Element for ScrollbarEl
.. ..
} in &prepaint_state.thumbs } in &prepaint_state.thumbs
{ {
const MAXIMUM_OPACITY: f32 = 0.5; const MAXIMUM_OPACITY: f32 = 0.7;
let thumb_state = self.state.read(cx).thumb_state; let thumb_state = self.state.read(cx).thumb_state;
let thumb_base_color = match thumb_state { let thumb_base_color = match thumb_state {
ThumbState::Dragging(dragged_axis, _) if dragged_axis == *axis => { ThumbState::Dragging(dragged_axis, _) if dragged_axis == *axis => {
@ -1083,7 +1128,7 @@ impl<S: ScrollbarVisibilitySetting, T: ScrollableHandle> Element for ScrollbarEl
window.on_mouse_event({ window.on_mouse_event({
let state = self.state.clone(); let state = self.state.clone();
move |event: &MouseDownEvent, phase, _, cx| { move |event: &MouseDownEvent, phase, window, cx| {
state.update(cx, |state, cx| { state.update(cx, |state, cx| {
let Some(scrollbar_layout) = (phase.capture() let Some(scrollbar_layout) = (phase.capture()
&& event.button == MouseButton::Left) && event.button == MouseButton::Left)
@ -1100,7 +1145,7 @@ impl<S: ScrollbarVisibilitySetting, T: ScrollableHandle> Element for ScrollbarEl
if thumb_bounds.contains(&event.position) { if thumb_bounds.contains(&event.position) {
let offset = let offset =
event.position.along(*axis) - thumb_bounds.origin.along(*axis); event.position.along(*axis) - thumb_bounds.origin.along(*axis);
state.set_dragging(*axis, offset, cx); state.set_dragging(*axis, offset, window, cx);
} else { } else {
let scroll_handle = state.scroll_handle(); let scroll_handle = state.scroll_handle();
let click_offset = scrollbar_layout.compute_click_offset( let click_offset = scrollbar_layout.compute_click_offset(
@ -1110,6 +1155,7 @@ impl<S: ScrollbarVisibilitySetting, T: ScrollableHandle> Element for ScrollbarEl
); );
state.set_offset( state.set_offset(
scroll_handle.offset().apply_along(*axis, |_| click_offset), scroll_handle.offset().apply_along(*axis, |_| click_offset),
window,
cx, cx,
); );
}; };
@ -1131,9 +1177,10 @@ impl<S: ScrollbarVisibilitySetting, T: ScrollableHandle> Element for ScrollbarEl
state.update(cx, |state, cx| { state.update(cx, |state, cx| {
state.set_offset( state.set_offset(
current_offset + event.delta.pixel_delta(window.line_height()), current_offset + event.delta.pixel_delta(window.line_height()),
window,
cx, cx,
); );
state.show_scrollbars(cx); state.show_scrollbars(window, cx);
cx.stop_propagation(); cx.stop_propagation();
}); });
} }
@ -1160,21 +1207,27 @@ impl<S: ScrollbarVisibilitySetting, T: ScrollableHandle> Element for ScrollbarEl
let new_offset = let new_offset =
scroll_handle.offset().apply_along(axis, |_| drag_offset); scroll_handle.offset().apply_along(axis, |_| drag_offset);
state.update(cx, |state, cx| state.set_offset(new_offset, cx)); state.update(cx, |state, cx| {
state.set_offset(new_offset, window, cx)
});
cx.stop_propagation(); cx.stop_propagation();
} }
} }
_ => state.update(cx, |state, cx| { _ => state.update(cx, |state, cx| {
if state.parent_hovered(&event.position) { match state.update_parent_hovered(&event.position) {
// todo! this should only be fired on first re-enter, if at all ParentHovered::Yes(state_changed)
state.show_scrollbars(cx); if event.pressed_button.is_none() =>
{
if event.pressed_button.is_none() { if state_changed {
state.update_hovered_thumb(&event.position, cx); state.show_scrollbars(window, cx);
}
state.update_hovered_thumb(&event.position, window, cx);
cx.stop_propagation(); cx.stop_propagation();
} }
} else { ParentHovered::No(state_changed) if state_changed => {
state.schedule_auto_hide(window, cx); state.schedule_auto_hide(window, cx);
}
_ => {}
} }
}), }),
} }
@ -1198,7 +1251,7 @@ impl<S: ScrollbarVisibilitySetting, T: ScrollableHandle> Element for ScrollbarEl
return; return;
} }
state.update_hovered_thumb(&event.position, cx); state.update_hovered_thumb(&event.position, window, cx);
}); });
} }
}); });