Skip to content

Commit c9ee263

Browse files
committed
refactor: special position layout
1 parent c3fb90a commit c9ee263

File tree

2 files changed

+54
-46
lines changed

2 files changed

+54
-46
lines changed

float-pigment-layout/src/special_positioned.rs

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -46,42 +46,21 @@ pub(crate) fn compute_special_position_children<T: LayoutTreeNode>(
4646
child.clear_display_none_result(child_node);
4747
return;
4848
}
49-
match child_style.position() {
50-
Position::Absolute | Position::Sticky => compute_special_position(
51-
env,
52-
child_node,
53-
ParentInfo {
54-
compute_result: node_result,
55-
style,
56-
inner_size: &node_inner_size,
57-
inner_option_size: &node_inner_option_size,
58-
border: &border,
59-
padding_border: &padding_border,
60-
},
61-
axis_info,
62-
accept_flex_props,
63-
false,
64-
),
65-
Position::Fixed => compute_special_position(
66-
env,
67-
child_node,
68-
ParentInfo {
69-
compute_result: node_result,
70-
style,
71-
inner_size: &node_inner_size,
72-
inner_option_size: &node_inner_option_size,
73-
border: &border,
74-
padding_border: &padding_border,
75-
},
76-
axis_info,
77-
accept_flex_props,
78-
true,
79-
),
80-
Position::Static => {}
81-
Position::Relative => {
82-
compute_position_relative(child_node, node_inner_size);
83-
}
84-
}
49+
compute_special_position(
50+
env,
51+
child_node,
52+
ParentInfo {
53+
compute_result: node_result,
54+
style,
55+
inner_size: &node_inner_size,
56+
inner_option_size: &node_inner_option_size,
57+
border: &border,
58+
padding_border: &padding_border,
59+
},
60+
axis_info,
61+
accept_flex_props,
62+
child_style.position(),
63+
);
8564
};
8665
node.tree_visitor()
8766
.for_each_child(|child_node, _| f(child_node));
@@ -96,23 +75,27 @@ pub(crate) struct ParentInfo<'a, T: LayoutTreeNode> {
9675
pub(crate) padding_border: &'a Edge<T::Length>,
9776
}
9877

78+
#[inline]
9979
pub(crate) fn compute_special_position<T: LayoutTreeNode>(
10080
env: &mut T::Env,
10181
node: &T,
10282
parent: ParentInfo<T>,
10383
axis_info: AxisInfo,
10484
accept_flex_props: bool,
105-
is_position_fixed: bool,
85+
position: Position,
10686
) {
87+
if position == Position::Static {
88+
return;
89+
}
10790
let mut layout_unit = node.layout_node().unit();
10891
let style = node.style();
10992

110-
let container_size = if is_position_fixed {
93+
let container_size = if position == Position::Fixed {
11194
Size::new(env.screen_width(), env.screen_height())
11295
} else {
11396
**parent.inner_size
11497
};
115-
let container_option_size = if is_position_fixed {
98+
let container_option_size = if position == Position::Fixed {
11699
Normalized(OptionSize::new(
117100
OptionNum::some(container_size.width),
118101
OptionNum::some(container_size.height),
@@ -125,6 +108,21 @@ pub(crate) fn compute_special_position<T: LayoutTreeNode>(
125108
let right = style.right().resolve_num(container_size.width, node);
126109
let top = style.top().resolve_num(container_size.height, node);
127110
let bottom = style.bottom().resolve_num(container_size.height, node);
111+
112+
if position == Position::Relative {
113+
if let Some(left) = left.val() {
114+
layout_unit.result.origin.x += left;
115+
} else if let Some(right) = right.val() {
116+
layout_unit.result.origin.x -= right;
117+
}
118+
if let Some(top) = top.val() {
119+
layout_unit.result.origin.y += top;
120+
} else if let Some(bottom) = bottom.val() {
121+
layout_unit.result.origin.y -= bottom;
122+
}
123+
return;
124+
}
125+
128126
let (margin, border, padding_border) =
129127
layout_unit.margin_border_padding(node, *container_option_size);
130128
let css_size =
@@ -233,7 +231,7 @@ pub(crate) fn compute_special_position<T: LayoutTreeNode>(
233231
let main_end = main_end.val().unwrap_or(T::Length::zero());
234232
let free_space_main_size = container_size.main_size(axis_info.dir);
235233

236-
if is_position_fixed {
234+
if position == Position::Fixed {
237235
main_start + (free_space_main_size - main_end - main_start).div_i32(2)
238236
- result.size.0.main_size(axis_info.dir).div_i32(2)
239237
} else {
@@ -245,7 +243,7 @@ pub(crate) fn compute_special_position<T: LayoutTreeNode>(
245243
}
246244
} else {
247245
let offset_main = if let Some(x) = main_start.val() {
248-
if is_position_fixed {
246+
if position == Position::Fixed {
249247
x
250248
} else {
251249
parent
@@ -254,7 +252,7 @@ pub(crate) fn compute_special_position<T: LayoutTreeNode>(
254252
+ x
255253
}
256254
} else if let Some(x) = main_end.val() {
257-
if is_position_fixed {
255+
if position == Position::Fixed {
258256
free_space.main_size(axis_info.dir) - x
259257
} else {
260258
parent
@@ -374,15 +372,15 @@ pub(crate) fn compute_special_position<T: LayoutTreeNode>(
374372
- (result.size.0.cross_size(axis_info.dir).div_i32(2))
375373
} else {
376374
let offset_cross = if let Some(x) = cross_start.val() {
377-
if is_position_fixed {
375+
if position == Position::Fixed {
378376
x
379377
} else {
380378
x + parent
381379
.border
382380
.cross_axis_start(axis_info.dir, axis_info.main_dir_rev)
383381
}
384382
} else if let Some(x) = cross_end.val() {
385-
if is_position_fixed {
383+
if position == Position::Fixed {
386384
free_space.cross_size(axis_info.dir) - x
387385
} else {
388386
parent

float-pigment-layout/src/unit.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,18 @@ impl<T: LayoutTreeNode> LayoutUnit<T> {
652652
offset_cross: T::Length,
653653
) -> Vector<T::Length> {
654654
let (width, height, width_rev, height_rev) = match axis_info.dir {
655-
AxisDirection::Horizontal => (offset_main, offset_cross, axis_info.main_dir_rev, axis_info.cross_dir_rev),
656-
AxisDirection::Vertical => (offset_cross, offset_main, axis_info.cross_dir_rev, axis_info.main_dir_rev),
655+
AxisDirection::Horizontal => (
656+
offset_main,
657+
offset_cross,
658+
axis_info.main_dir_rev,
659+
axis_info.cross_dir_rev,
660+
),
661+
AxisDirection::Vertical => (
662+
offset_cross,
663+
offset_main,
664+
axis_info.cross_dir_rev,
665+
axis_info.main_dir_rev,
666+
),
657667
};
658668
let width = match width_rev {
659669
AxisReverse::NotReversed => width,

0 commit comments

Comments
 (0)