Fix cursor and hover styles changing when dragging the mouse
This commit is contained in:
parent
1320fadc30
commit
4d2f5a8e04
3 changed files with 26 additions and 15 deletions
|
@ -3299,15 +3299,15 @@ impl<'a, 'b, V: 'static> ViewContext<'a, 'b, V> {
|
||||||
let region_id = MouseRegionId::new(tag, self.view_id, region_id);
|
let region_id = MouseRegionId::new(tag, self.view_id, region_id);
|
||||||
MouseState {
|
MouseState {
|
||||||
hovered: self.window.hovered_region_ids.contains(®ion_id),
|
hovered: self.window.hovered_region_ids.contains(®ion_id),
|
||||||
clicked: if let Some((clicked_region_id, button)) = self.window.clicked_region {
|
mouse_down: !self.window.clicked_region_ids.is_empty(),
|
||||||
if region_id == clicked_region_id {
|
clicked: self
|
||||||
Some(button)
|
.window
|
||||||
} else {
|
.clicked_region_ids
|
||||||
None
|
.iter()
|
||||||
}
|
.find(|click_region_id| **click_region_id == region_id)
|
||||||
} else {
|
// If we've gotten here, there should always be a clicked region.
|
||||||
None
|
// But let's be defensive and return None if there isn't.
|
||||||
},
|
.and_then(|_| self.window.clicked_region.map(|(_, button)| button)),
|
||||||
accessed_hovered: false,
|
accessed_hovered: false,
|
||||||
accessed_clicked: false,
|
accessed_clicked: false,
|
||||||
}
|
}
|
||||||
|
@ -3823,14 +3823,20 @@ impl<'a, T> DerefMut for Reference<'a, T> {
|
||||||
pub struct MouseState {
|
pub struct MouseState {
|
||||||
pub(crate) hovered: bool,
|
pub(crate) hovered: bool,
|
||||||
pub(crate) clicked: Option<MouseButton>,
|
pub(crate) clicked: Option<MouseButton>,
|
||||||
|
pub(crate) mouse_down: bool,
|
||||||
pub(crate) accessed_hovered: bool,
|
pub(crate) accessed_hovered: bool,
|
||||||
pub(crate) accessed_clicked: bool,
|
pub(crate) accessed_clicked: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MouseState {
|
impl MouseState {
|
||||||
|
pub fn dragging(&mut self) -> bool {
|
||||||
|
self.accessed_hovered = true;
|
||||||
|
self.hovered && self.mouse_down
|
||||||
|
}
|
||||||
|
|
||||||
pub fn hovered(&mut self) -> bool {
|
pub fn hovered(&mut self) -> bool {
|
||||||
self.accessed_hovered = true;
|
self.accessed_hovered = true;
|
||||||
self.hovered
|
self.hovered && (!self.mouse_down || self.clicked.is_some())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clicked(&mut self) -> Option<MouseButton> {
|
pub fn clicked(&mut self) -> Option<MouseButton> {
|
||||||
|
|
|
@ -617,10 +617,11 @@ impl<'a> WindowContext<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if self
|
if pressed_button.is_none()
|
||||||
.window
|
&& self
|
||||||
.platform_window
|
.window
|
||||||
.is_topmost_for_position(*position)
|
.platform_window
|
||||||
|
.is_topmost_for_position(*position)
|
||||||
{
|
{
|
||||||
self.platform().set_cursor_style(style_to_assign);
|
self.platform().set_cursor_style(style_to_assign);
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,11 @@ where
|
||||||
let mut handler = MouseEventHandler::above::<Tag, _>(region_id, cx, |state, cx| {
|
let mut handler = MouseEventHandler::above::<Tag, _>(region_id, cx, |state, cx| {
|
||||||
// Observing hovered will cause a render when the mouse enters regardless
|
// Observing hovered will cause a render when the mouse enters regardless
|
||||||
// of if mouse position was accessed before
|
// of if mouse position was accessed before
|
||||||
let drag_position = if state.hovered() { drag_position } else { None };
|
let drag_position = if state.dragging() {
|
||||||
|
drag_position
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
Stack::new()
|
Stack::new()
|
||||||
.with_child(render_child(state, cx))
|
.with_child(render_child(state, cx))
|
||||||
.with_children(drag_position.map(|drag_position| {
|
.with_children(drag_position.map(|drag_position| {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue