Improve cursor style behavior for some draggable elements (#31965)

Follow-up to #24797

This PR ensures some cursor styles do not change for draggable elements
during dragging. The linked PR covered this on the higher level for
draggable divs. However, e.g. the pane divider inbetween two editors is
not a draggable div and thus still has the issue that the cursor style
changes during dragging. This PR fixes this issue by setting the hitbox
to `None` in cases where the element is currently being dragged, which
ensures the cursor style is applied to the cursor no matter what during
dragging.

Namely, this change fixes this for
- non-div pane dividers
- minimap slider and the
- editor scrollbars

and implements it for the UI scrollbars (Notably, UI scrollbars do
already have `cursor_default` on their parent container but would not
keep this during dragging. I opted out on removing this from the parent
containers until #30194 or a similar PR is merged).


https://github.com/user-attachments/assets/f97859dd-5f1d-4449-ab92-c27f2d933c4a

Release Notes:

- N/A
This commit is contained in:
Finn Evers 2025-06-06 22:56:27 +02:00 committed by GitHub
parent e0057ccd0f
commit 2fe1293fba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 88 additions and 43 deletions

View file

@ -61,7 +61,7 @@ impl Render for WindowShadow {
CursorStyle::ResizeUpRightDownLeft
}
},
Some(&hitbox),
&hitbox,
);
},
)

View file

@ -1744,11 +1744,11 @@ impl Interactivity {
if let Some(drag) = cx.active_drag.as_ref() {
if let Some(mouse_cursor) = drag.cursor_style {
window.set_cursor_style(mouse_cursor, None);
window.set_window_cursor_style(mouse_cursor);
}
} else {
if let Some(mouse_cursor) = style.mouse_cursor {
window.set_cursor_style(mouse_cursor, Some(hitbox));
window.set_cursor_style(mouse_cursor, hitbox);
}
}

View file

@ -769,7 +769,7 @@ impl Element for InteractiveText {
.iter()
.any(|range| range.contains(&ix))
{
window.set_cursor_style(crate::CursorStyle::PointingHand, Some(hitbox))
window.set_cursor_style(crate::CursorStyle::PointingHand, hitbox)
}
}

View file

@ -408,7 +408,7 @@ pub(crate) type AnyMouseListener =
#[derive(Clone)]
pub(crate) struct CursorStyleRequest {
pub(crate) hitbox_id: Option<HitboxId>, // None represents whole window
pub(crate) hitbox_id: HitboxId,
pub(crate) style: CursorStyle,
}
@ -622,6 +622,7 @@ pub(crate) struct Frame {
pub(crate) input_handlers: Vec<Option<PlatformInputHandler>>,
pub(crate) tooltip_requests: Vec<Option<TooltipRequest>>,
pub(crate) cursor_styles: Vec<CursorStyleRequest>,
window_cursor_style: Option<CursorStyle>,
#[cfg(any(test, feature = "test-support"))]
pub(crate) debug_bounds: FxHashMap<String, Bounds<Pixels>>,
#[cfg(any(feature = "inspector", debug_assertions))]
@ -666,6 +667,7 @@ impl Frame {
input_handlers: Vec::new(),
tooltip_requests: Vec::new(),
cursor_styles: Vec::new(),
window_cursor_style: None,
#[cfg(any(test, feature = "test-support"))]
debug_bounds: FxHashMap::default(),
@ -691,6 +693,7 @@ impl Frame {
self.window_control_hitboxes.clear();
self.deferred_draws.clear();
self.focus = None;
self.window_cursor_style = None;
#[cfg(any(feature = "inspector", debug_assertions))]
{
@ -699,6 +702,17 @@ impl Frame {
}
}
pub(crate) fn cursor_style(&self, window: &Window) -> Option<CursorStyle> {
self.window_cursor_style.or_else(|| {
self.cursor_styles.iter().rev().find_map(|request| {
request
.hitbox_id
.is_hovered(window)
.then_some(request.style)
})
})
}
pub(crate) fn hit_test(&self, position: Point<Pixels>) -> HitTest {
let mut set_hover_hitbox_count = false;
let mut hit_test = HitTest::default();
@ -2157,14 +2171,23 @@ impl Window {
/// Updates the cursor style at the platform level. This method should only be called
/// during the prepaint phase of element drawing.
pub fn set_cursor_style(&mut self, style: CursorStyle, hitbox: Option<&Hitbox>) {
pub fn set_cursor_style(&mut self, style: CursorStyle, hitbox: &Hitbox) {
self.invalidator.debug_assert_paint();
self.next_frame.cursor_styles.push(CursorStyleRequest {
hitbox_id: hitbox.map(|hitbox| hitbox.id),
hitbox_id: hitbox.id,
style,
});
}
/// Updates the cursor style for the entire window at the platform level. A cursor
/// style using this method will have precedence over any cursor style set using
/// `set_cursor_style`. This method should only be called during the prepaint
/// phase of element drawing.
pub fn set_window_cursor_style(&mut self, style: CursorStyle) {
self.invalidator.debug_assert_paint();
self.next_frame.window_cursor_style = Some(style);
}
/// Sets a tooltip to be rendered for the upcoming frame. This method should only be called
/// during the paint phase of element drawing.
pub fn set_tooltip(&mut self, tooltip: AnyTooltip) -> TooltipId {
@ -3245,15 +3268,7 @@ impl Window {
if self.is_window_hovered() {
let style = self
.rendered_frame
.cursor_styles
.iter()
.rev()
.find(|request| {
request
.hitbox_id
.map_or(true, |hitbox_id| hitbox_id.is_hovered(self))
})
.map(|request| request.style)
.cursor_style(self)
.unwrap_or(CursorStyle::Arrow);
cx.platform.set_cursor_style(style);
}