From 690028da4597cef0ae052281664b711126890c65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Tue, 14 Nov 2023 23:16:37 +0100 Subject: [PATCH] Use regular layout for `scroll` view --- examples/widget-gallery/src/lists.rs | 10 ++--- src/inspector.rs | 22 +++++++--- src/views/scroll.rs | 62 ++++------------------------ src/views/virtual_list.rs | 2 + 4 files changed, 32 insertions(+), 64 deletions(-) diff --git a/examples/widget-gallery/src/lists.rs b/examples/widget-gallery/src/lists.rs index 94621322..8dc6958e 100644 --- a/examples/widget-gallery/src/lists.rs +++ b/examples/widget-gallery/src/lists.rs @@ -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, @@ -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),)), ) }) @@ -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 |_| { @@ -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| { @@ -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| { @@ -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)) } diff --git a/src/inspector.rs b/src/inspector.rs index 3db7875b..d0ad3ae8 100644 --- a/src/inspector.rs +++ b/src/inspector.rs @@ -334,7 +334,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( @@ -751,9 +751,15 @@ fn capture_view(capture: &Rc) -> 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() @@ -769,8 +775,14 @@ fn capture_view(capture: &Rc) -> 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) }) diff --git a/src/views/scroll.rs b/src/views/scroll.rs index fee0c6dc..d3fc0dc2 100644 --- a/src/views/scroll.rs +++ b/src/views/scroll.rs @@ -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, ComputeLayoutCx, 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, ViewData}, @@ -86,7 +85,6 @@ pub struct Scroll { h_handle_hover: bool, v_track_hover: bool, h_track_hover: bool, - virtual_node: Option, propagate_pointer_wheel: bool, vertical_scroll_as_horizontal: bool, handle_style: ScrollStyle, @@ -111,7 +109,6 @@ pub fn scroll(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, @@ -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( @@ -315,14 +304,11 @@ impl Scroll { Some(()) } - fn child_size(&self, app_state: &mut AppState) -> Option { + 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 { @@ -612,6 +598,10 @@ impl View for Scroll { "Scroll".into() } + fn view_style(&self) -> Option