Skip to content

Commit

Permalink
Use regular layout for scroll view
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Nov 14, 2023
1 parent 346de3c commit 8073216
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 64 deletions.
10 changes: 4 additions & 6 deletions examples/widget-gallery/src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use floem::{
peniko::Color,
reactive::create_signal,
style::{CursorStyle, JustifyContent},
unit::UnitExt,
view::View,
views::{
container, label, scroll, stack, virtual_list, Decorators, VirtualListDirection,
Expand All @@ -20,7 +19,7 @@ use crate::form::{form, form_item};
pub fn virt_list_view() -> impl View {
stack({
(
form((form_item("Simple List".to_string(), 120.0, simple_list),)),
form((form_item("Simple List".to_string(), 100.0, simple_list),)),
form((form_item("Enhanced List".to_string(), 120.0, enhanced_list),)),
)
})
Expand Down Expand Up @@ -69,7 +68,7 @@ fn enhanced_list() -> impl View {
set_is_checked.update(|checked: &mut bool| *checked = !*checked);
}),
label(move || item.to_string())
.style(|s| s.height(32.0).font_size(32.0)),
.style(|s| s.height(32.0).font_size(22.0)),
container({
label(move || " X ")
.on_click_stop(move |_| {
Expand All @@ -96,7 +95,7 @@ fn enhanced_list() -> impl View {
}),
)
})
.style(move |s| s.height(item_height).width(list_width).items_center())
.style(move |s| s.height(item_height).width_full().items_center())
})
.on_click_stop(move |_| {
set_selected.update(|v: &mut usize| {
Expand Down Expand Up @@ -128,7 +127,6 @@ fn enhanced_list() -> impl View {
.keyboard_navigatable()
.style(move |s| {
s.flex_row()
.width(list_width.pct())
.height(item_height)
.apply_if(index == selected.get(), |s| s.background(Color::GRAY))
.apply_if(index != 0, |s| {
Expand All @@ -139,7 +137,7 @@ fn enhanced_list() -> impl View {
})
},
)
.style(move |s| s.flex_col().width(list_width)),
.style(move |s| s.flex_col().flex_grow(1.0)),
)
.style(move |s| s.width(list_width).height(300.0).border(1.0))
}
22 changes: 17 additions & 5 deletions src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ fn captured_view_with_children(

let list = v_stack((line, list));

Box::new(v_stack((row, list)).style(|s| s.min_width_full()))
Box::new(v_stack((row, list)))
}

fn captured_view(
Expand Down Expand Up @@ -750,9 +750,15 @@ fn capture_view(capture: &Rc<Capture>) -> impl View {
header("Stats"),
stats(capture),
))
.style(|s| s.width_full()),
.style(|s| s.min_width_full()),
)
.style(|s| s.width_full().flex_basis(0).min_height(0).flex_grow(1.0));
.style(|s| {
s.width_full()
.flex_basis(0)
.min_height(0)
.flex_grow(1.0)
.flex_col()
});

let seperator = empty().style(move |s| {
s.width_full()
Expand All @@ -768,8 +774,14 @@ fn capture_view(capture: &Rc<Capture>) -> impl View {
))
.style(|s| s.max_width_pct(60.0));

let tree = scroll(captured_view(&capture.root, 0, &capture_view))
.style(|s| s.width_full().min_height(0).flex_basis(0).flex_grow(1.0))
let tree = scroll(captured_view(&capture.root, 0, &capture_view).style(|s| s.min_width_full()))
.style(|s| {
s.width_full()
.min_height(0)
.flex_basis(0)
.flex_grow(1.0)
.flex_col()
})
.on_event_cont(EventListener::PointerLeave, move |_| {
capture_view.highlighted.set(None)
})
Expand Down
62 changes: 9 additions & 53 deletions src/views/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ use floem_reactive::create_effect;
use floem_renderer::Renderer;
use kurbo::{Point, Rect, Size, Vec2};
use peniko::Color;
use taffy::{prelude::Node, style::Position};

use crate::{
context::{AppState, LayoutCx, PaintCx},
event::Event,
id::Id,
prop, prop_extracter,
style::{Background, BorderColor, BorderRadius, PositionProp, Style, StyleSelector},
style::{Background, BorderColor, BorderRadius, Style, StyleSelector},
style_class,
unit::Px,
view::View,
Expand Down Expand Up @@ -86,7 +85,6 @@ pub struct Scroll {
h_handle_hover: bool,
v_track_hover: bool,
h_track_hover: bool,
virtual_node: Option<Node>,
propagate_pointer_wheel: bool,
vertical_scroll_as_horizontal: bool,
handle_style: ScrollStyle,
Expand All @@ -111,7 +109,6 @@ pub fn scroll<V: View + 'static>(child: V) -> Scroll {
h_handle_hover: false,
v_track_hover: false,
h_track_hover: false,
virtual_node: None,
propagate_pointer_wheel: false,
vertical_scroll_as_horizontal: false,
hide: false,
Expand Down Expand Up @@ -262,16 +259,8 @@ impl Scroll {
}

fn update_size(&mut self, app_state: &mut AppState) {
let child_size = self.child_size;
let new_child_size = self.child_size(app_state).unwrap_or_default();
self.child_size = new_child_size;

self.child_size = self.child_size(app_state);
self.actual_rect = app_state.get_content_rect(self.id);

// Round to prevent loops due to floating point accuracy
if (child_size * 128.0).round() != (new_child_size * 128.0).round() {
app_state.request_layout(self.id);
}
}

fn clamp_child_viewport(
Expand Down Expand Up @@ -315,14 +304,11 @@ impl Scroll {
Some(())
}

fn child_size(&self, app_state: &mut AppState) -> Option<Size> {
fn child_size(&self, app_state: &mut AppState) -> Size {
app_state
.view_states
.get(&self.id)
.map(|view| &view.children_nodes)
.and_then(|nodes| nodes.get(1))
.and_then(|node| app_state.taffy.layout(*node).ok())
.get_layout(self.child.id())
.map(|layout| Size::new(layout.size.width as f64, layout.size.height as f64))
.unwrap()
}

fn v_handle_style(&self) -> &ScrollStyle {
Expand Down Expand Up @@ -608,6 +594,10 @@ impl View for Scroll {
"Scroll".into()
}

fn view_style(&self) -> Option<Style> {
Some(Style::new().items_start())
}

fn update(&mut self, cx: &mut crate::context::UpdateCx, state: Box<dyn std::any::Any>) {
if let Ok(state) = state.downcast::<ScrollState>() {
match *state {
Expand Down Expand Up @@ -676,40 +666,6 @@ impl View for Scroll {
cx.style_view(&mut self.child);
}

fn layout(&mut self, cx: &mut crate::context::LayoutCx) -> taffy::prelude::Node {
cx.layout_node(self.id, true, |cx| {
let child_id = self.child.id();
let child_view = cx.app_state_mut().view_state(child_id);
child_view.combined_style = child_view
.combined_style
.clone()
.set(PositionProp, Position::Absolute);
let child_node = cx.layout_view(&mut self.child);

let virtual_style = Style::new()
.width(self.child_size.width)
.height(self.child_size.height)
.min_width(0.0)
.min_height(0.0)
.to_taffy_style();
if self.virtual_node.is_none() {
self.virtual_node = Some(
cx.app_state_mut()
.taffy
.new_leaf(virtual_style.clone())
.unwrap(),
);
}
let virtual_node = self.virtual_node.unwrap();
let _ = cx
.app_state_mut()
.taffy
.set_style(virtual_node, virtual_style);

vec![virtual_node, child_node]
})
}

fn compute_layout(&mut self, cx: &mut LayoutCx) -> Option<Rect> {
self.update_size(cx.app_state_mut());
self.clamp_child_viewport(cx.app_state_mut(), self.child_viewport);
Expand Down
2 changes: 2 additions & 0 deletions src/views/virtual_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,15 @@ impl<V: View + 'static, T> View for VirtualList<V, T> {
let _ = cx.app_state_mut().taffy.set_style(
before_node,
taffy::style::Style {
min_size: before_size,
size: before_size,
..Default::default()
},
);
let _ = cx.app_state_mut().taffy.set_style(
after_node,
taffy::style::Style {
min_size: after_size,
size: after_size,
..Default::default()
},
Expand Down

0 comments on commit 8073216

Please sign in to comment.