roll back mouse position in mouse_state struct in favor of using the dragged element position

This commit is contained in:
K Simmons 2022-10-22 21:58:14 -07:00
parent 8cde64d3f6
commit f5795ffc6f
6 changed files with 10 additions and 29 deletions

View File

@ -3908,7 +3908,6 @@ pub struct RenderContext<'a, T: View> {
pub(crate) window_id: usize,
pub(crate) view_id: usize,
pub(crate) view_type: PhantomData<T>,
pub(crate) mouse_position: Vector2F,
pub(crate) hovered_region_ids: HashSet<MouseRegionId>,
pub(crate) clicked_region_ids: Option<(HashSet<MouseRegionId>, MouseButton)>,
pub app: &'a mut MutableAppContext,
@ -3920,19 +3919,12 @@ pub struct RenderContext<'a, T: View> {
#[derive(Clone, Default)]
pub struct MouseState {
hovered: bool,
mouse_position: Vector2F,
clicked: Option<MouseButton>,
accessed_mouse_position: bool,
accessed_hovered: bool,
accessed_clicked: bool,
}
impl MouseState {
pub fn mouse_position(&mut self) -> Vector2F {
self.accessed_mouse_position = true;
self.mouse_position
}
pub fn hovered(&mut self) -> bool {
self.accessed_hovered = true;
self.hovered
@ -3943,10 +3935,6 @@ impl MouseState {
self.clicked
}
pub fn accessed_mouse_position(&self) -> bool {
self.accessed_mouse_position
}
pub fn accessed_hovered(&self) -> bool {
self.accessed_hovered
}
@ -3964,7 +3952,6 @@ impl<'a, V: View> RenderContext<'a, V> {
view_id: params.view_id,
view_type: PhantomData,
titlebar_height: params.titlebar_height,
mouse_position: params.mouse_position,
hovered_region_ids: params.hovered_region_ids.clone(),
clicked_region_ids: params.clicked_region_ids.clone(),
refreshing: params.refreshing,
@ -3987,7 +3974,6 @@ impl<'a, V: View> RenderContext<'a, V> {
pub fn mouse_state<Tag: 'static>(&self, region_id: usize) -> MouseState {
let region_id = MouseRegionId::new::<Tag>(self.view_id, region_id);
MouseState {
mouse_position: self.mouse_position.clone(),
hovered: self.hovered_region_ids.contains(&region_id),
clicked: self.clicked_region_ids.as_ref().and_then(|(ids, button)| {
if ids.contains(&region_id) {
@ -3996,7 +3982,6 @@ impl<'a, V: View> RenderContext<'a, V> {
None
}
}),
accessed_mouse_position: false,
accessed_hovered: false,
accessed_clicked: false,
}

View File

@ -186,7 +186,6 @@ impl TestAppContext {
view_id: handle.id(),
view_type: PhantomData,
titlebar_height: 0.,
mouse_position: Default::default(),
hovered_region_ids: Default::default(),
clicked_region_ids: None,
refreshing: false,

View File

@ -21,7 +21,6 @@ pub struct MouseEventHandler<Tag: 'static> {
cursor_style: Option<CursorStyle>,
handlers: HandlerSet,
hoverable: bool,
notify_on_move: bool,
notify_on_hover: bool,
notify_on_click: bool,
above: bool,
@ -39,7 +38,6 @@ impl<Tag> MouseEventHandler<Tag> {
{
let mut mouse_state = cx.mouse_state::<Tag>(region_id);
let child = render_child(&mut mouse_state, cx);
let notify_on_move = mouse_state.accessed_mouse_position();
let notify_on_hover = mouse_state.accessed_hovered();
let notify_on_click = mouse_state.accessed_clicked();
Self {
@ -47,7 +45,6 @@ impl<Tag> MouseEventHandler<Tag> {
region_id,
cursor_style: None,
handlers: Default::default(),
notify_on_move,
notify_on_hover,
notify_on_click,
hoverable: true,
@ -188,7 +185,6 @@ impl<Tag> MouseEventHandler<Tag> {
self.handlers.clone(),
)
.with_hoverable(self.hoverable)
.with_notify_on_move(self.notify_on_move)
.with_notify_on_hover(self.notify_on_hover)
.with_notify_on_click(self.notify_on_click),
);

View File

@ -185,7 +185,6 @@ impl Presenter {
asset_cache: &self.asset_cache,
view_stack: Vec::new(),
refreshing,
mouse_position: self.mouse_position.clone(),
hovered_region_ids: self.hovered_region_ids.clone(),
clicked_region_ids: self
.clicked_button
@ -568,7 +567,6 @@ pub struct LayoutContext<'a> {
pub window_size: Vector2F,
titlebar_height: f32,
appearance: Appearance,
mouse_position: Vector2F,
hovered_region_ids: HashSet<MouseRegionId>,
clicked_region_ids: Option<(HashSet<MouseRegionId>, MouseButton)>,
}
@ -640,7 +638,6 @@ impl<'a> LayoutContext<'a> {
view_id: handle.id(),
view_type: PhantomData,
titlebar_height: self.titlebar_height,
mouse_position: self.mouse_position.clone(),
hovered_region_ids: self.hovered_region_ids.clone(),
clicked_region_ids: self.clicked_region_ids.clone(),
refreshing: self.refreshing,

View File

@ -138,11 +138,6 @@ impl MouseRegion {
self
}
pub fn with_notify_on_move(mut self, notify: bool) -> Self {
self.notify_on_move = notify;
self
}
pub fn with_notify_on_hover(mut self, notify: bool) -> Self {
self.notify_on_hover = notify;
self

View File

@ -33,7 +33,7 @@ where
.global::<DragAndDrop<Workspace>>()
.currently_dragged::<DraggedItem>(cx.window_id())
.filter(|_| hovered)
.map(|_| state.mouse_position());
.map(|(drag_position, _)| drag_position);
Stack::new()
.with_child(render_child(state, cx))
@ -69,6 +69,15 @@ where
cx.notify();
}
})
.on_move(|_, cx| {
if cx
.global::<DragAndDrop<Workspace>>()
.currently_dragged::<DraggedItem>(cx.window_id())
.is_some()
{
cx.notify();
}
})
}
pub fn handle_dropped_item(