Merge branch 'mouse-events' into project-panel-context-menu
This commit is contained in:
commit
9099c40364
26 changed files with 816 additions and 523 deletions
|
@ -7,7 +7,7 @@ use crate::{
|
|||
},
|
||||
json::ToJson,
|
||||
platform::CursorStyle,
|
||||
scene::{self, Border, Quad},
|
||||
scene::{self, Border, CursorRegion, Quad},
|
||||
Element, ElementBox, Event, EventContext, LayoutContext, PaintContext, SizeConstraint,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
|
@ -213,7 +213,10 @@ impl Element for Container {
|
|||
}
|
||||
|
||||
if let Some(style) = self.style.cursor {
|
||||
cx.scene.push_cursor_style(quad_bounds, style);
|
||||
cx.scene.push_cursor_region(CursorRegion {
|
||||
bounds: quad_bounds,
|
||||
style,
|
||||
});
|
||||
}
|
||||
|
||||
let child_origin =
|
||||
|
|
|
@ -2,8 +2,8 @@ use std::{any::Any, f32::INFINITY};
|
|||
|
||||
use crate::{
|
||||
json::{self, ToJson, Value},
|
||||
Axis, DebugContext, Element, ElementBox, ElementStateContext, ElementStateHandle, Event,
|
||||
EventContext, LayoutContext, PaintContext, SizeConstraint, Vector2FExt,
|
||||
Axis, DebugContext, Element, ElementBox, ElementStateHandle, Event, EventContext,
|
||||
LayoutContext, PaintContext, RenderContext, SizeConstraint, Vector2FExt, View,
|
||||
};
|
||||
use pathfinder_geometry::{
|
||||
rect::RectF,
|
||||
|
@ -40,15 +40,15 @@ impl Flex {
|
|||
Self::new(Axis::Vertical)
|
||||
}
|
||||
|
||||
pub fn scrollable<Tag, C>(
|
||||
pub fn scrollable<Tag, V>(
|
||||
mut self,
|
||||
element_id: usize,
|
||||
scroll_to: Option<usize>,
|
||||
cx: &mut C,
|
||||
cx: &mut RenderContext<V>,
|
||||
) -> Self
|
||||
where
|
||||
Tag: 'static,
|
||||
C: ElementStateContext,
|
||||
V: View,
|
||||
{
|
||||
let scroll_state = cx.element_state::<Tag, ScrollState>(element_id);
|
||||
scroll_state.update(cx, |scroll_state, _| scroll_state.scroll_to = scroll_to);
|
||||
|
|
|
@ -5,7 +5,7 @@ use crate::{
|
|||
},
|
||||
json::json,
|
||||
DebugContext, Element, ElementBox, ElementRc, Event, EventContext, LayoutContext, PaintContext,
|
||||
SizeConstraint,
|
||||
RenderContext, SizeConstraint, View, ViewContext,
|
||||
};
|
||||
use std::{cell::RefCell, collections::VecDeque, ops::Range, rc::Rc};
|
||||
use sum_tree::{Bias, SumTree};
|
||||
|
@ -26,7 +26,7 @@ pub enum Orientation {
|
|||
|
||||
struct StateInner {
|
||||
last_layout_width: Option<f32>,
|
||||
render_item: Box<dyn FnMut(usize, &mut LayoutContext) -> ElementBox>,
|
||||
render_item: Box<dyn FnMut(usize, &mut LayoutContext) -> Option<ElementBox>>,
|
||||
rendered_range: Range<usize>,
|
||||
items: SumTree<ListItem>,
|
||||
logical_scroll_top: Option<ListOffset>,
|
||||
|
@ -135,9 +135,12 @@ impl Element for List {
|
|||
break;
|
||||
}
|
||||
|
||||
let element = state.render_item(scroll_top.item_ix + ix, item, item_constraint, cx);
|
||||
rendered_height += element.size().y();
|
||||
rendered_items.push_back(ListItem::Rendered(element));
|
||||
if let Some(element) =
|
||||
state.render_item(scroll_top.item_ix + ix, item, item_constraint, cx)
|
||||
{
|
||||
rendered_height += element.size().y();
|
||||
rendered_items.push_back(ListItem::Rendered(element));
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare to start walking upward from the item at the scroll top.
|
||||
|
@ -149,9 +152,12 @@ impl Element for List {
|
|||
while rendered_height < size.y() {
|
||||
cursor.prev(&());
|
||||
if let Some(item) = cursor.item() {
|
||||
let element = state.render_item(cursor.start().0, item, item_constraint, cx);
|
||||
rendered_height += element.size().y();
|
||||
rendered_items.push_front(ListItem::Rendered(element));
|
||||
if let Some(element) =
|
||||
state.render_item(cursor.start().0, item, item_constraint, cx)
|
||||
{
|
||||
rendered_height += element.size().y();
|
||||
rendered_items.push_front(ListItem::Rendered(element));
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
@ -182,9 +188,12 @@ impl Element for List {
|
|||
while leading_overdraw < state.overdraw {
|
||||
cursor.prev(&());
|
||||
if let Some(item) = cursor.item() {
|
||||
let element = state.render_item(cursor.start().0, item, item_constraint, cx);
|
||||
leading_overdraw += element.size().y();
|
||||
rendered_items.push_front(ListItem::Rendered(element));
|
||||
if let Some(element) =
|
||||
state.render_item(cursor.start().0, item, item_constraint, cx)
|
||||
{
|
||||
leading_overdraw += element.size().y();
|
||||
rendered_items.push_front(ListItem::Rendered(element));
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
@ -330,20 +339,25 @@ impl Element for List {
|
|||
}
|
||||
|
||||
impl ListState {
|
||||
pub fn new<F>(
|
||||
pub fn new<F, V>(
|
||||
element_count: usize,
|
||||
orientation: Orientation,
|
||||
overdraw: f32,
|
||||
render_item: F,
|
||||
cx: &mut ViewContext<V>,
|
||||
mut render_item: F,
|
||||
) -> Self
|
||||
where
|
||||
F: 'static + FnMut(usize, &mut LayoutContext) -> ElementBox,
|
||||
V: View,
|
||||
F: 'static + FnMut(&mut V, usize, &mut RenderContext<V>) -> ElementBox,
|
||||
{
|
||||
let mut items = SumTree::new();
|
||||
items.extend((0..element_count).map(|_| ListItem::Unrendered), &());
|
||||
let handle = cx.handle();
|
||||
Self(Rc::new(RefCell::new(StateInner {
|
||||
last_layout_width: None,
|
||||
render_item: Box::new(render_item),
|
||||
render_item: Box::new(move |ix, cx| {
|
||||
Some(cx.render(&handle, |view, cx| render_item(view, ix, cx)))
|
||||
}),
|
||||
rendered_range: 0..0,
|
||||
items,
|
||||
logical_scroll_top: None,
|
||||
|
@ -414,13 +428,13 @@ impl StateInner {
|
|||
existing_item: &ListItem,
|
||||
constraint: SizeConstraint,
|
||||
cx: &mut LayoutContext,
|
||||
) -> ElementRc {
|
||||
) -> Option<ElementRc> {
|
||||
if let ListItem::Rendered(element) = existing_item {
|
||||
element.clone()
|
||||
Some(element.clone())
|
||||
} else {
|
||||
let mut element = (self.render_item)(ix, cx);
|
||||
let mut element = (self.render_item)(ix, cx)?;
|
||||
element.layout(constraint, cx);
|
||||
element.into()
|
||||
Some(element.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -593,22 +607,26 @@ impl<'a> sum_tree::SeekTarget<'a, ListItemSummary, ListItemSummary> for Height {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::geometry::vector::vec2f;
|
||||
use crate::{elements::Empty, geometry::vector::vec2f, Entity};
|
||||
use rand::prelude::*;
|
||||
use std::env;
|
||||
|
||||
#[crate::test(self)]
|
||||
fn test_layout(cx: &mut crate::MutableAppContext) {
|
||||
let mut presenter = cx.build_presenter(0, 0.);
|
||||
let (_, view) = cx.add_window(Default::default(), |_| TestView);
|
||||
let constraint = SizeConstraint::new(vec2f(0., 0.), vec2f(100., 40.));
|
||||
|
||||
let elements = Rc::new(RefCell::new(vec![(0, 20.), (1, 30.), (2, 100.)]));
|
||||
let state = ListState::new(elements.borrow().len(), Orientation::Top, 1000.0, {
|
||||
let elements = elements.clone();
|
||||
move |ix, _| {
|
||||
let (id, height) = elements.borrow()[ix];
|
||||
TestElement::new(id, height).boxed()
|
||||
}
|
||||
|
||||
let state = view.update(cx, |_, cx| {
|
||||
ListState::new(elements.borrow().len(), Orientation::Top, 1000.0, cx, {
|
||||
let elements = elements.clone();
|
||||
move |_, ix, _| {
|
||||
let (id, height) = elements.borrow()[ix];
|
||||
TestElement::new(id, height).boxed()
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
let mut list = List::new(state.clone());
|
||||
|
@ -694,6 +712,7 @@ mod tests {
|
|||
.map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
|
||||
.unwrap_or(10);
|
||||
|
||||
let (_, view) = cx.add_window(Default::default(), |_| TestView);
|
||||
let mut presenter = cx.build_presenter(0, 0.);
|
||||
let mut next_id = 0;
|
||||
let elements = Rc::new(RefCell::new(
|
||||
|
@ -709,12 +728,15 @@ mod tests {
|
|||
.choose(&mut rng)
|
||||
.unwrap();
|
||||
let overdraw = rng.gen_range(1..=100) as f32;
|
||||
let state = ListState::new(elements.borrow().len(), orientation, overdraw, {
|
||||
let elements = elements.clone();
|
||||
move |ix, _| {
|
||||
let (id, height) = elements.borrow()[ix];
|
||||
TestElement::new(id, height).boxed()
|
||||
}
|
||||
|
||||
let state = view.update(cx, |_, cx| {
|
||||
ListState::new(elements.borrow().len(), orientation, overdraw, cx, {
|
||||
let elements = elements.clone();
|
||||
move |_, ix, _| {
|
||||
let (id, height) = elements.borrow()[ix];
|
||||
TestElement::new(id, height).boxed()
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
let mut width = rng.gen_range(0..=2000) as f32 / 2.;
|
||||
|
@ -851,6 +873,22 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
struct TestView;
|
||||
|
||||
impl Entity for TestView {
|
||||
type Event = ();
|
||||
}
|
||||
|
||||
impl View for TestView {
|
||||
fn ui_name() -> &'static str {
|
||||
"TestView"
|
||||
}
|
||||
|
||||
fn render(&mut self, _: &mut RenderContext<'_, Self>) -> ElementBox {
|
||||
Empty::new().boxed()
|
||||
}
|
||||
}
|
||||
|
||||
struct TestElement {
|
||||
id: usize,
|
||||
size: Vector2F,
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
use std::{any::TypeId, rc::Rc};
|
||||
|
||||
use super::Padding;
|
||||
use crate::{
|
||||
geometry::{
|
||||
|
@ -5,49 +7,42 @@ use crate::{
|
|||
vector::{vec2f, Vector2F},
|
||||
},
|
||||
platform::CursorStyle,
|
||||
DebugContext, Element, ElementBox, ElementStateContext, ElementStateHandle, Event,
|
||||
EventContext, LayoutContext, PaintContext, SizeConstraint,
|
||||
scene::CursorRegion,
|
||||
DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, MouseRegion, MouseState,
|
||||
PaintContext, RenderContext, SizeConstraint, View,
|
||||
};
|
||||
use serde_json::json;
|
||||
|
||||
pub struct MouseEventHandler {
|
||||
state: ElementStateHandle<MouseState>,
|
||||
child: ElementBox,
|
||||
tag: TypeId,
|
||||
id: usize,
|
||||
cursor_style: Option<CursorStyle>,
|
||||
mouse_down_handler: Option<Box<dyn FnMut(Vector2F, &mut EventContext)>>,
|
||||
click_handler: Option<Box<dyn FnMut(Vector2F, usize, &mut EventContext)>>,
|
||||
drag_handler: Option<Box<dyn FnMut(Vector2F, &mut EventContext)>>,
|
||||
right_mouse_down_handler: Option<Box<dyn FnMut(Vector2F, &mut EventContext)>>,
|
||||
right_click_handler: Option<Box<dyn FnMut(Vector2F, usize, &mut EventContext)>>,
|
||||
mouse_down_handler: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
|
||||
click_handler: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>,
|
||||
right_mouse_down_handler: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
|
||||
right_click_handler: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>,
|
||||
drag_handler: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
|
||||
padding: Padding,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct MouseState {
|
||||
pub hovered: bool,
|
||||
pub clicked: bool,
|
||||
pub right_clicked: bool,
|
||||
prev_drag_position: Option<Vector2F>,
|
||||
}
|
||||
|
||||
impl MouseEventHandler {
|
||||
pub fn new<Tag, C, F>(id: usize, cx: &mut C, render_child: F) -> Self
|
||||
pub fn new<Tag, V, F>(id: usize, cx: &mut RenderContext<V>, render_child: F) -> Self
|
||||
where
|
||||
Tag: 'static,
|
||||
C: ElementStateContext,
|
||||
F: FnOnce(&MouseState, &mut C) -> ElementBox,
|
||||
V: View,
|
||||
F: FnOnce(MouseState, &mut RenderContext<V>) -> ElementBox,
|
||||
{
|
||||
let state_handle = cx.element_state::<Tag, _>(id);
|
||||
let child = state_handle.update(cx, |state, cx| render_child(state, cx));
|
||||
Self {
|
||||
state: state_handle,
|
||||
child,
|
||||
id,
|
||||
tag: TypeId::of::<Tag>(),
|
||||
child: render_child(cx.mouse_state::<Tag>(id), cx),
|
||||
cursor_style: None,
|
||||
mouse_down_handler: None,
|
||||
click_handler: None,
|
||||
drag_handler: None,
|
||||
right_mouse_down_handler: None,
|
||||
right_click_handler: None,
|
||||
drag_handler: None,
|
||||
padding: Default::default(),
|
||||
}
|
||||
}
|
||||
|
@ -59,38 +54,38 @@ impl MouseEventHandler {
|
|||
|
||||
pub fn on_mouse_down(
|
||||
mut self,
|
||||
handler: impl FnMut(Vector2F, &mut EventContext) + 'static,
|
||||
handler: impl Fn(Vector2F, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.mouse_down_handler = Some(Box::new(handler));
|
||||
self.mouse_down_handler = Some(Rc::new(handler));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_click(
|
||||
mut self,
|
||||
handler: impl FnMut(Vector2F, usize, &mut EventContext) + 'static,
|
||||
handler: impl Fn(Vector2F, usize, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.click_handler = Some(Box::new(handler));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_drag(mut self, handler: impl FnMut(Vector2F, &mut EventContext) + 'static) -> Self {
|
||||
self.drag_handler = Some(Box::new(handler));
|
||||
self.click_handler = Some(Rc::new(handler));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_right_mouse_down(
|
||||
mut self,
|
||||
handler: impl FnMut(Vector2F, &mut EventContext) + 'static,
|
||||
handler: impl Fn(Vector2F, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.right_mouse_down_handler = Some(Box::new(handler));
|
||||
self.right_mouse_down_handler = Some(Rc::new(handler));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_right_click(
|
||||
mut self,
|
||||
handler: impl FnMut(Vector2F, usize, &mut EventContext) + 'static,
|
||||
handler: impl Fn(Vector2F, usize, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.right_click_handler = Some(Box::new(handler));
|
||||
self.right_click_handler = Some(Rc::new(handler));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_drag(mut self, handler: impl Fn(Vector2F, &mut EventContext) + 'static) -> Self {
|
||||
self.drag_handler = Some(Rc::new(handler));
|
||||
self
|
||||
}
|
||||
|
||||
|
@ -127,10 +122,26 @@ impl Element for MouseEventHandler {
|
|||
_: &mut Self::LayoutState,
|
||||
cx: &mut PaintContext,
|
||||
) -> Self::PaintState {
|
||||
if let Some(cursor_style) = self.cursor_style {
|
||||
cx.scene
|
||||
.push_cursor_style(self.hit_bounds(bounds), cursor_style);
|
||||
if let Some(style) = self.cursor_style {
|
||||
cx.scene.push_cursor_region(CursorRegion {
|
||||
bounds: self.hit_bounds(bounds),
|
||||
style,
|
||||
});
|
||||
}
|
||||
|
||||
cx.scene.push_mouse_region(MouseRegion {
|
||||
view_id: cx.current_view_id(),
|
||||
tag: self.tag,
|
||||
region_id: self.id,
|
||||
bounds: self.hit_bounds(bounds),
|
||||
hover: None,
|
||||
click: self.click_handler.clone(),
|
||||
mouse_down: self.mouse_down_handler.clone(),
|
||||
right_click: self.right_click_handler.clone(),
|
||||
right_mouse_down: self.right_mouse_down_handler.clone(),
|
||||
drag: self.drag_handler.clone(),
|
||||
});
|
||||
|
||||
self.child.paint(bounds.origin(), visible_bounds, cx);
|
||||
}
|
||||
|
||||
|
@ -138,113 +149,12 @@ impl Element for MouseEventHandler {
|
|||
&mut self,
|
||||
event: &Event,
|
||||
_: RectF,
|
||||
visible_bounds: RectF,
|
||||
_: RectF,
|
||||
_: &mut Self::LayoutState,
|
||||
_: &mut Self::PaintState,
|
||||
cx: &mut EventContext,
|
||||
) -> bool {
|
||||
let hit_bounds = self.hit_bounds(visible_bounds);
|
||||
let mouse_down_handler = self.mouse_down_handler.as_mut();
|
||||
let click_handler = self.click_handler.as_mut();
|
||||
let drag_handler = self.drag_handler.as_mut();
|
||||
let right_mouse_down_handler = self.right_mouse_down_handler.as_mut();
|
||||
let right_click_handler = self.right_click_handler.as_mut();
|
||||
|
||||
let handled_in_child = self.child.dispatch_event(event, cx);
|
||||
|
||||
self.state.update(cx, |state, cx| match event {
|
||||
Event::MouseMoved {
|
||||
position,
|
||||
left_mouse_down,
|
||||
} => {
|
||||
if !left_mouse_down {
|
||||
let mouse_in = hit_bounds.contains_point(*position);
|
||||
if state.hovered != mouse_in {
|
||||
state.hovered = mouse_in;
|
||||
cx.notify();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
handled_in_child
|
||||
}
|
||||
Event::LeftMouseDown { position, .. } => {
|
||||
if !handled_in_child && hit_bounds.contains_point(*position) {
|
||||
state.clicked = true;
|
||||
state.prev_drag_position = Some(*position);
|
||||
cx.notify();
|
||||
if let Some(handler) = mouse_down_handler {
|
||||
handler(*position, cx);
|
||||
}
|
||||
true
|
||||
} else {
|
||||
handled_in_child
|
||||
}
|
||||
}
|
||||
Event::LeftMouseUp {
|
||||
position,
|
||||
click_count,
|
||||
..
|
||||
} => {
|
||||
state.prev_drag_position = None;
|
||||
if !handled_in_child && state.clicked {
|
||||
state.clicked = false;
|
||||
cx.notify();
|
||||
if let Some(handler) = click_handler {
|
||||
if hit_bounds.contains_point(*position) {
|
||||
handler(*position, *click_count, cx);
|
||||
}
|
||||
}
|
||||
true
|
||||
} else {
|
||||
handled_in_child
|
||||
}
|
||||
}
|
||||
Event::LeftMouseDragged { position, .. } => {
|
||||
if !handled_in_child && state.clicked {
|
||||
let prev_drag_position = state.prev_drag_position.replace(*position);
|
||||
if let Some((handler, prev_position)) = drag_handler.zip(prev_drag_position) {
|
||||
let delta = *position - prev_position;
|
||||
if !delta.is_zero() {
|
||||
(handler)(delta, cx);
|
||||
}
|
||||
}
|
||||
true
|
||||
} else {
|
||||
handled_in_child
|
||||
}
|
||||
}
|
||||
Event::RightMouseDown { position, .. } => {
|
||||
if !handled_in_child && hit_bounds.contains_point(*position) {
|
||||
state.right_clicked = true;
|
||||
cx.notify();
|
||||
if let Some(handler) = right_mouse_down_handler {
|
||||
handler(*position, cx);
|
||||
}
|
||||
true
|
||||
} else {
|
||||
handled_in_child
|
||||
}
|
||||
}
|
||||
Event::RightMouseUp {
|
||||
position,
|
||||
click_count,
|
||||
..
|
||||
} => {
|
||||
if !handled_in_child && state.right_clicked {
|
||||
state.right_clicked = false;
|
||||
cx.notify();
|
||||
if let Some(handler) = right_click_handler {
|
||||
if hit_bounds.contains_point(*position) {
|
||||
handler(*position, *click_count, cx);
|
||||
}
|
||||
}
|
||||
true
|
||||
} else {
|
||||
handled_in_child
|
||||
}
|
||||
}
|
||||
_ => handled_in_child,
|
||||
})
|
||||
self.child.dispatch_event(event, cx)
|
||||
}
|
||||
|
||||
fn debug(
|
||||
|
|
|
@ -5,7 +5,7 @@ use crate::{
|
|||
vector::{vec2f, Vector2F},
|
||||
},
|
||||
json::{self, json},
|
||||
ElementBox,
|
||||
ElementBox, RenderContext, View,
|
||||
};
|
||||
use json::ToJson;
|
||||
use std::{cell::RefCell, cmp, ops::Range, rc::Rc};
|
||||
|
@ -41,27 +41,37 @@ pub struct LayoutState {
|
|||
items: Vec<ElementBox>,
|
||||
}
|
||||
|
||||
pub struct UniformList<F>
|
||||
where
|
||||
F: Fn(Range<usize>, &mut Vec<ElementBox>, &mut LayoutContext),
|
||||
{
|
||||
pub struct UniformList {
|
||||
state: UniformListState,
|
||||
item_count: usize,
|
||||
append_items: F,
|
||||
append_items: Box<dyn Fn(Range<usize>, &mut Vec<ElementBox>, &mut LayoutContext)>,
|
||||
padding_top: f32,
|
||||
padding_bottom: f32,
|
||||
get_width_from_item: Option<usize>,
|
||||
}
|
||||
|
||||
impl<F> UniformList<F>
|
||||
where
|
||||
F: Fn(Range<usize>, &mut Vec<ElementBox>, &mut LayoutContext),
|
||||
{
|
||||
pub fn new(state: UniformListState, item_count: usize, append_items: F) -> Self {
|
||||
impl UniformList {
|
||||
pub fn new<F, V>(
|
||||
state: UniformListState,
|
||||
item_count: usize,
|
||||
cx: &mut RenderContext<V>,
|
||||
append_items: F,
|
||||
) -> Self
|
||||
where
|
||||
V: View,
|
||||
F: 'static + Fn(&mut V, Range<usize>, &mut Vec<ElementBox>, &mut RenderContext<V>),
|
||||
{
|
||||
let handle = cx.handle();
|
||||
Self {
|
||||
state,
|
||||
item_count,
|
||||
append_items,
|
||||
append_items: Box::new(move |range, items, cx| {
|
||||
if let Some(handle) = handle.upgrade(cx) {
|
||||
cx.render(&handle, |view, cx| {
|
||||
append_items(view, range, items, cx);
|
||||
});
|
||||
}
|
||||
}),
|
||||
padding_top: 0.,
|
||||
padding_bottom: 0.,
|
||||
get_width_from_item: None,
|
||||
|
@ -144,10 +154,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<F> Element for UniformList<F>
|
||||
where
|
||||
F: Fn(Range<usize>, &mut Vec<ElementBox>, &mut LayoutContext),
|
||||
{
|
||||
impl Element for UniformList {
|
||||
type LayoutState = LayoutState;
|
||||
type PaintState = ();
|
||||
|
||||
|
@ -162,40 +169,51 @@ where
|
|||
);
|
||||
}
|
||||
|
||||
let no_items = (
|
||||
constraint.min,
|
||||
LayoutState {
|
||||
item_height: 0.,
|
||||
scroll_max: 0.,
|
||||
items: Default::default(),
|
||||
},
|
||||
);
|
||||
|
||||
if self.item_count == 0 {
|
||||
return (
|
||||
constraint.min,
|
||||
LayoutState {
|
||||
item_height: 0.,
|
||||
scroll_max: 0.,
|
||||
items: Default::default(),
|
||||
},
|
||||
);
|
||||
return no_items;
|
||||
}
|
||||
|
||||
let mut items = Vec::new();
|
||||
let mut size = constraint.max;
|
||||
let mut item_size;
|
||||
let sample_item_ix;
|
||||
let mut sample_item;
|
||||
let sample_item;
|
||||
if let Some(sample_ix) = self.get_width_from_item {
|
||||
(self.append_items)(sample_ix..sample_ix + 1, &mut items, cx);
|
||||
sample_item_ix = sample_ix;
|
||||
sample_item = items.pop().unwrap();
|
||||
item_size = sample_item.layout(constraint, cx);
|
||||
size.set_x(item_size.x());
|
||||
|
||||
if let Some(mut item) = items.pop() {
|
||||
item_size = item.layout(constraint, cx);
|
||||
size.set_x(item_size.x());
|
||||
sample_item = item;
|
||||
} else {
|
||||
return no_items;
|
||||
}
|
||||
} else {
|
||||
(self.append_items)(0..1, &mut items, cx);
|
||||
sample_item_ix = 0;
|
||||
sample_item = items.pop().unwrap();
|
||||
item_size = sample_item.layout(
|
||||
SizeConstraint::new(
|
||||
vec2f(constraint.max.x(), 0.0),
|
||||
vec2f(constraint.max.x(), f32::INFINITY),
|
||||
),
|
||||
cx,
|
||||
);
|
||||
item_size.set_x(size.x());
|
||||
if let Some(mut item) = items.pop() {
|
||||
item_size = item.layout(
|
||||
SizeConstraint::new(
|
||||
vec2f(constraint.max.x(), 0.0),
|
||||
vec2f(constraint.max.x(), f32::INFINITY),
|
||||
),
|
||||
cx,
|
||||
);
|
||||
item_size.set_x(size.x());
|
||||
sample_item = item
|
||||
} else {
|
||||
return no_items;
|
||||
}
|
||||
}
|
||||
|
||||
let item_constraint = SizeConstraint {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue