Finished fixing flex scrolls

This commit is contained in:
Mikayla Maki 2022-09-22 09:35:52 -07:00
parent f4d4ea4123
commit dd7259c832

View file

@ -4,8 +4,7 @@ use crate::{
json::{self, ToJson, Value}, json::{self, ToJson, Value},
presenter::MeasurementContext, presenter::MeasurementContext,
Axis, DebugContext, Element, ElementBox, ElementStateHandle, Event, EventContext, Axis, DebugContext, Element, ElementBox, ElementStateHandle, Event, EventContext,
LayoutContext, MouseRegion, PaintContext, RenderContext, ScrollWheelEvent, SizeConstraint, LayoutContext, PaintContext, RenderContext, SizeConstraint, Vector2FExt, View,
Vector2FExt, View,
}; };
use pathfinder_geometry::{ use pathfinder_geometry::{
rect::RectF, rect::RectF,
@ -13,16 +12,16 @@ use pathfinder_geometry::{
}; };
use serde_json::json; use serde_json::json;
#[derive(Default, Clone, Copy)] #[derive(Default)]
struct ScrollState { struct ScrollState {
scroll_to: Option<usize>, scroll_to: Cell<Option<usize>>,
scroll_position: f32, scroll_position: Cell<f32>,
} }
pub struct Flex { pub struct Flex {
axis: Axis, axis: Axis,
children: Vec<ElementBox>, children: Vec<ElementBox>,
scroll_state: Option<(ElementStateHandle<Rc<Cell<ScrollState>>>, usize)>, scroll_state: Option<(ElementStateHandle<Rc<ScrollState>>, usize)>,
} }
impl Flex { impl Flex {
@ -52,15 +51,9 @@ impl Flex {
Tag: 'static, Tag: 'static,
V: View, V: View,
{ {
let scroll_state_handle = let scroll_state = cx.default_element_state::<Tag, Rc<ScrollState>>(element_id);
cx.default_element_state::<Tag, Rc<Cell<ScrollState>>>(element_id); scroll_state.read(cx).scroll_to.set(scroll_to);
let scroll_state_cell = scroll_state_handle.read(cx); self.scroll_state = Some((scroll_state, cx.handle().id()));
let mut scroll_state = scroll_state_cell.get();
scroll_state.scroll_to = scroll_to;
scroll_state_cell.set(scroll_state);
self.scroll_state = Some((scroll_state_handle, cx.handle().id()));
self self
} }
@ -106,38 +99,6 @@ impl Flex {
} }
} }
} }
fn handle_scroll(
e: ScrollWheelEvent,
axis: Axis,
scroll_state: Rc<Cell<ScrollState>>,
remaining_space: f32,
) -> bool {
let precise = e.precise;
let delta = e.delta;
if remaining_space < 0. {
let mut delta = match axis {
Axis::Horizontal => {
if delta.x() != 0. {
delta.x()
} else {
delta.y()
}
}
Axis::Vertical => delta.y(),
};
if !precise {
delta *= 20.;
}
let mut old_state = scroll_state.get();
old_state.scroll_position -= delta;
scroll_state.set(old_state);
return true;
}
return false;
}
} }
impl Extend<ElementBox> for Flex { impl Extend<ElementBox> for Flex {
@ -241,8 +202,8 @@ impl Element for Flex {
if let Some(scroll_state) = self.scroll_state.as_ref() { if let Some(scroll_state) = self.scroll_state.as_ref() {
scroll_state.0.update(cx, |scroll_state, _| { scroll_state.0.update(cx, |scroll_state, _| {
if let Some(scroll_to) = scroll_state.get().scroll_to.take() { if let Some(scroll_to) = scroll_state.scroll_to.take() {
let visible_start = scroll_state.get().scroll_position; let visible_start = scroll_state.scroll_position.get();
let visible_end = visible_start + size.along(self.axis); let visible_end = visible_start + size.along(self.axis);
if let Some(child) = self.children.get(scroll_to) { if let Some(child) = self.children.get(scroll_to) {
let child_start: f32 = self.children[..scroll_to] let child_start: f32 = self.children[..scroll_to]
@ -250,20 +211,23 @@ impl Element for Flex {
.map(|c| c.size().along(self.axis)) .map(|c| c.size().along(self.axis))
.sum(); .sum();
let child_end = child_start + child.size().along(self.axis); let child_end = child_start + child.size().along(self.axis);
let mut old_state = scroll_state.get();
if child_start < visible_start { if child_start < visible_start {
old_state.scroll_position = child_start; scroll_state.scroll_position.set(child_start);
} else if child_end > visible_end { } else if child_end > visible_end {
old_state.scroll_position = child_end - size.along(self.axis); scroll_state
.scroll_position
.set(child_end - size.along(self.axis));
} }
scroll_state.set(old_state);
} }
} }
let mut old_state = scroll_state.get(); scroll_state.scroll_position.set(
old_state.scroll_position = old_state.scroll_position.min(-remaining_space).max(0.); scroll_state
scroll_state.set(old_state); .scroll_position
.get()
.min(-remaining_space)
.max(0.),
);
}); });
} }
@ -286,28 +250,43 @@ impl Element for Flex {
if let Some(scroll_state) = &self.scroll_state { if let Some(scroll_state) = &self.scroll_state {
cx.scene.push_mouse_region( cx.scene.push_mouse_region(
MouseRegion::new::<Self>(scroll_state.1, 0, bounds) crate::MouseRegion::new::<Self>(scroll_state.1, 0, bounds)
.on_scroll({ .on_scroll({
let axis = self.axis;
let scroll_state = scroll_state.0.read(cx).clone(); let scroll_state = scroll_state.0.read(cx).clone();
let axis = self.axis;
move |e, cx| { move |e, cx| {
if Self::handle_scroll( if remaining_space < 0. {
e.platform_event, let mut delta = match axis {
axis, Axis::Horizontal => {
scroll_state.clone(), if e.delta.x() != 0. {
remaining_space, e.delta.x()
) { } else {
e.delta.y()
}
}
Axis::Vertical => e.delta.y(),
};
if !e.precise {
delta *= 20.;
}
scroll_state
.scroll_position
.set(scroll_state.scroll_position.get() - delta);
cx.notify();
} else {
cx.propogate_event(); cx.propogate_event();
} }
} }
}) })
.on_move(|_, _| { /* Eat move events so they don't propogate */ }), .on_move(|_, _| { /* Capture move events */ }),
); )
} }
let mut child_origin = bounds.origin(); let mut child_origin = bounds.origin();
if let Some(scroll_state) = self.scroll_state.as_ref() { if let Some(scroll_state) = self.scroll_state.as_ref() {
let scroll_position = scroll_state.0.read(cx).get().scroll_position; let scroll_position = scroll_state.0.read(cx).scroll_position.get();
match self.axis { match self.axis {
Axis::Horizontal => child_origin.set_x(child_origin.x() - scroll_position), Axis::Horizontal => child_origin.set_x(child_origin.x() - scroll_position),
Axis::Vertical => child_origin.set_y(child_origin.y() - scroll_position), Axis::Vertical => child_origin.set_y(child_origin.y() - scroll_position),