From 5ab32a23cc610ff8167ffbaec8d01a7fa05ba412 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Mary=C5=84czak?= Date: Sat, 11 Jan 2025 23:34:22 +0100 Subject: [PATCH] Use taffy for scroll container layouting (#731) --- src/style.rs | 19 +++++++++++++++++-- src/views/scroll.rs | 18 +++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/style.rs b/src/style.rs index bec1f8dd..5eef9fc0 100644 --- a/src/style.rs +++ b/src/style.rs @@ -26,8 +26,8 @@ use taffy::{ geometry::{MinMax, Size}, prelude::{GridPlacement, Line, Rect}, style::{ - LengthPercentage, MaxTrackSizingFunction, MinTrackSizingFunction, Style as TaffyStyle, - TrackSizingFunction, + LengthPercentage, MaxTrackSizingFunction, MinTrackSizingFunction, Overflow, + Style as TaffyStyle, TrackSizingFunction, }, }; @@ -74,6 +74,7 @@ impl StylePropValue for f64 { Some(*self * (1.0 - value) + *other * value) } } +impl StylePropValue for Overflow {} impl StylePropValue for Display {} impl StylePropValue for Position {} impl StylePropValue for FlexDirection {} @@ -1674,6 +1675,16 @@ define_builtin_props!( Rotation rotate: Px {} = Px(0.), ); +prop!( + /// How children overflowing their container in Y axis should affect layout + pub(crate) OverflowX: Overflow {} = Overflow::default() +); + +prop!( + /// How children overflowing their container in X axis should affect layout + pub(crate) OverflowY: Overflow {} = Overflow::default() +); + prop_extractor! { pub FontProps { pub size: FontSize, @@ -2357,6 +2368,10 @@ impl Style { let style = self.builtin(); TaffyStyle { display: style.display(), + overflow: taffy::Point { + x: self.get(OverflowX), + y: self.get(OverflowY), + }, position: style.position(), size: taffy::prelude::Size { width: style.width().into(), diff --git a/src/views/scroll.rs b/src/views/scroll.rs index a5ce1a7e..fc79fa99 100644 --- a/src/views/scroll.rs +++ b/src/views/scroll.rs @@ -5,7 +5,7 @@ use floem_reactive::create_effect; use peniko::kurbo::{Point, Rect, Size, Stroke, Vec2}; use peniko::{Brush, Color}; -use crate::style::CustomStylable; +use crate::style::{CustomStylable, OverflowX, OverflowY}; use crate::unit::PxPct; use crate::{ app_state::AppState, @@ -425,7 +425,14 @@ impl Scroll { fn child_size(&self) -> Size { self.child .get_layout() - .map(|layout| Size::new(layout.size.width as f64, layout.size.height as f64)) + .map(|layout| { + // Whenever content overflows the container use content_size, + // otherwise just use size + Size::new( + layout.size.width.max(layout.content_size.width) as f64, + layout.size.height.max(layout.content_size.width) as f64, + ) + }) .unwrap() } @@ -733,7 +740,12 @@ impl View for Scroll { } fn view_style(&self) -> Option