Skip to content

Commit

Permalink
Use taffy for scroll container layouting (#731)
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex authored Jan 11, 2025
1 parent decb0a4 commit 5ab32a2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
19 changes: 17 additions & 2 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};

Expand Down Expand Up @@ -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 {}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down
18 changes: 15 additions & 3 deletions src/views/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -733,7 +740,12 @@ impl View for Scroll {
}

fn view_style(&self) -> Option<Style> {
Some(Style::new().items_start())
Some(
Style::new()
.items_start()
.set(OverflowX, taffy::Overflow::Scroll)
.set(OverflowY, taffy::Overflow::Scroll),
)
}

fn update(&mut self, cx: &mut crate::context::UpdateCx, state: Box<dyn std::any::Any>) {
Expand Down

0 comments on commit 5ab32a2

Please sign in to comment.