Added on_scroll to mouse_event_handler and fixed the uniform list scroll implementation
This commit is contained in:
parent
0c4c5f9238
commit
ab7f7b3754
2 changed files with 39 additions and 19 deletions
|
@ -7,7 +7,8 @@ use crate::{
|
||||||
platform::CursorStyle,
|
platform::CursorStyle,
|
||||||
scene::{
|
scene::{
|
||||||
ClickRegionEvent, CursorRegion, DownOutRegionEvent, DownRegionEvent, DragRegionEvent,
|
ClickRegionEvent, CursorRegion, DownOutRegionEvent, DownRegionEvent, DragRegionEvent,
|
||||||
HandlerSet, HoverRegionEvent, MoveRegionEvent, UpOutRegionEvent, UpRegionEvent,
|
HandlerSet, HoverRegionEvent, MoveRegionEvent, ScrollWheelRegionEvent, UpOutRegionEvent,
|
||||||
|
UpRegionEvent,
|
||||||
},
|
},
|
||||||
DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, MeasurementContext,
|
DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, MeasurementContext,
|
||||||
MouseButton, MouseRegion, MouseState, PaintContext, RenderContext, SizeConstraint, View,
|
MouseButton, MouseRegion, MouseState, PaintContext, RenderContext, SizeConstraint, View,
|
||||||
|
@ -122,6 +123,14 @@ impl<Tag> MouseEventHandler<Tag> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn on_scroll(
|
||||||
|
mut self,
|
||||||
|
handler: impl Fn(ScrollWheelRegionEvent, &mut EventContext) + 'static,
|
||||||
|
) -> Self {
|
||||||
|
self.handlers = self.handlers.on_scroll(handler);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn with_hoverable(mut self, is_hoverable: bool) -> Self {
|
pub fn with_hoverable(mut self, is_hoverable: bool) -> Self {
|
||||||
self.hoverable = is_hoverable;
|
self.hoverable = is_hoverable;
|
||||||
self
|
self
|
||||||
|
|
|
@ -6,7 +6,8 @@ use crate::{
|
||||||
},
|
},
|
||||||
json::{self, json},
|
json::{self, json},
|
||||||
presenter::MeasurementContext,
|
presenter::MeasurementContext,
|
||||||
ElementBox, RenderContext, ScrollWheelEvent, View,
|
scene::ScrollWheelRegionEvent,
|
||||||
|
ElementBox, MouseRegion, RenderContext, ScrollWheelEvent, View,
|
||||||
};
|
};
|
||||||
use json::ToJson;
|
use json::ToJson;
|
||||||
use std::{cell::RefCell, cmp, ops::Range, rc::Rc};
|
use std::{cell::RefCell, cmp, ops::Range, rc::Rc};
|
||||||
|
@ -50,6 +51,7 @@ pub struct UniformList {
|
||||||
padding_top: f32,
|
padding_top: f32,
|
||||||
padding_bottom: f32,
|
padding_bottom: f32,
|
||||||
get_width_from_item: Option<usize>,
|
get_width_from_item: Option<usize>,
|
||||||
|
view_id: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UniformList {
|
impl UniformList {
|
||||||
|
@ -77,6 +79,7 @@ impl UniformList {
|
||||||
padding_top: 0.,
|
padding_top: 0.,
|
||||||
padding_bottom: 0.,
|
padding_bottom: 0.,
|
||||||
get_width_from_item: None,
|
get_width_from_item: None,
|
||||||
|
view_id: cx.handle().id(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,7 +99,7 @@ impl UniformList {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scroll(
|
fn scroll(
|
||||||
&self,
|
state: UniformListState,
|
||||||
_: Vector2F,
|
_: Vector2F,
|
||||||
mut delta: Vector2F,
|
mut delta: Vector2F,
|
||||||
precise: bool,
|
precise: bool,
|
||||||
|
@ -107,7 +110,7 @@ impl UniformList {
|
||||||
delta *= 20.;
|
delta *= 20.;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut state = self.state.0.borrow_mut();
|
let mut state = state.0.borrow_mut();
|
||||||
state.scroll_top = (state.scroll_top - delta.y()).max(0.0).min(scroll_max);
|
state.scroll_top = (state.scroll_top - delta.y()).max(0.0).min(scroll_max);
|
||||||
cx.notify();
|
cx.notify();
|
||||||
|
|
||||||
|
@ -283,6 +286,28 @@ impl Element for UniformList {
|
||||||
) -> Self::PaintState {
|
) -> Self::PaintState {
|
||||||
cx.scene.push_layer(Some(bounds));
|
cx.scene.push_layer(Some(bounds));
|
||||||
|
|
||||||
|
cx.scene.push_mouse_region(
|
||||||
|
MouseRegion::new::<Self>(self.view_id, 0, bounds).on_scroll({
|
||||||
|
let scroll_max = layout.scroll_max;
|
||||||
|
let state = self.state.clone();
|
||||||
|
move |ScrollWheelRegionEvent {
|
||||||
|
platform_event:
|
||||||
|
ScrollWheelEvent {
|
||||||
|
position,
|
||||||
|
delta,
|
||||||
|
precise,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
},
|
||||||
|
cx| {
|
||||||
|
if !Self::scroll(state.clone(), position, delta, precise, scroll_max, cx) {
|
||||||
|
cx.propogate_event();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
let mut item_origin = bounds.origin()
|
let mut item_origin = bounds.origin()
|
||||||
- vec2f(
|
- vec2f(
|
||||||
0.,
|
0.,
|
||||||
|
@ -300,7 +325,7 @@ impl Element for UniformList {
|
||||||
fn dispatch_event(
|
fn dispatch_event(
|
||||||
&mut self,
|
&mut self,
|
||||||
event: &Event,
|
event: &Event,
|
||||||
bounds: RectF,
|
_: RectF,
|
||||||
_: RectF,
|
_: RectF,
|
||||||
layout: &mut Self::LayoutState,
|
layout: &mut Self::LayoutState,
|
||||||
_: &mut Self::PaintState,
|
_: &mut Self::PaintState,
|
||||||
|
@ -311,20 +336,6 @@ impl Element for UniformList {
|
||||||
handled = item.dispatch_event(event, cx) || handled;
|
handled = item.dispatch_event(event, cx) || handled;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Event::ScrollWheel(ScrollWheelEvent {
|
|
||||||
position,
|
|
||||||
delta,
|
|
||||||
precise,
|
|
||||||
..
|
|
||||||
}) = event
|
|
||||||
{
|
|
||||||
if bounds.contains_point(*position)
|
|
||||||
&& self.scroll(*position, *delta, *precise, layout.scroll_max, cx)
|
|
||||||
{
|
|
||||||
handled = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
handled
|
handled
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue