Skip to content

Commit feccc21

Browse files
authored
Merge pull request #24 from wechat-miniprogram/feat-inline-block-measure
feat: improve inline-block measure interface
2 parents ba55238 + 281eefd commit feccc21

File tree

4 files changed

+52
-55
lines changed

4 files changed

+52
-55
lines changed

float-pigment-forest/src/layout/layout_impl.rs

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use float_pigment_css::{
88
},
99
};
1010
use float_pigment_layout::{
11-
DefLength, Edge, EdgeOption, InlineMeasure, InlineUnit, LayoutNode, LayoutStyle,
11+
DefLength, EdgeOption, InlineMeasure, InlineUnit, InlineUnitMetadata, LayoutNode, LayoutStyle,
1212
LayoutTreeNode, LayoutTreeVisitor, MeasureResult, OptionNum, OptionSize, Point, Size, Vector,
1313
};
1414

@@ -240,22 +240,8 @@ impl LayoutTreeNode for Node {
240240
min: Size<Self::Length>,
241241
max: Size<Self::Length>,
242242
max_content: OptionSize<Self::Length>,
243-
) -> Self::InlineUnit {
244-
let measure_res = self.measure_block_size(env, req_size, min, max, max_content, false);
245-
let size = measure_res.size;
246-
LayoutInlineUnit {
247-
offset: Point::zero(),
248-
size,
249-
first_baseline_ascent: Vector::new(
250-
measure_res.first_baseline_ascent.x,
251-
measure_res.first_baseline_ascent.y,
252-
),
253-
last_baseline_ascent: Vector::new(
254-
measure_res.last_baseline_ascent.x,
255-
measure_res.last_baseline_ascent.y,
256-
),
257-
is_inline_block: false,
258-
}
243+
) -> MeasureResult<Self::Length> {
244+
self.measure_block_size(env, req_size, min, max, max_content, false)
259245
}
260246
}
261247

@@ -290,7 +276,6 @@ pub struct LayoutInlineUnit {
290276
size: Size<Len>,
291277
first_baseline_ascent: Vector<Len>,
292278
last_baseline_ascent: Vector<Len>,
293-
is_inline_block: bool,
294279
}
295280

296281
impl LayoutInlineUnit {
@@ -304,26 +289,16 @@ impl LayoutInlineUnit {
304289
},
305290
)
306291
}
307-
fn adjust_inline_size(&mut self, padding_border: Edge<Len>) {
308-
if self.is_inline_block {
309-
return;
310-
}
311-
self.size.height += padding_border.vertical();
312-
self.size.width += padding_border.horizontal();
313-
self.first_baseline_ascent.y += padding_border.top;
314-
self.last_baseline_ascent.y += padding_border.top;
315-
}
316292
}
317293

318294
impl InlineUnit<Node> for LayoutInlineUnit {
319295
type Env = Env;
320-
fn inline_block(_env: &mut Env, _node: &Node, size: Size<Len>, baseline_ascent: Len) -> Self {
296+
fn new(_env: &mut Env, _node: &Node, res: MeasureResult<Len>) -> Self {
321297
Self {
322298
offset: Point::zero(),
323-
size,
324-
first_baseline_ascent: Vector::new(Len::zero(), baseline_ascent),
325-
last_baseline_ascent: Vector::new(Len::zero(), baseline_ascent),
326-
is_inline_block: true,
299+
size: res.size,
300+
first_baseline_ascent: res.first_baseline_ascent,
301+
last_baseline_ascent: res.last_baseline_ascent,
327302
}
328303
}
329304
}
@@ -399,7 +374,7 @@ impl InlineMeasure<Node> for LayoutInlineMeasure {
399374
fn block_size(
400375
_env: &mut Env,
401376
block_node: &Node,
402-
inline_nodes: Vec<(LayoutInlineUnit, EdgeOption<Len>, Edge<Len>)>,
377+
inline_nodes: Vec<InlineUnitMetadata<Node>>,
403378
req_size: OptionSize<Len>,
404379
_max_content_with_max_size: OptionSize<Len>,
405380
_update_position: bool,
@@ -421,11 +396,8 @@ impl InlineMeasure<Node> for LayoutInlineMeasure {
421396
if let Some(suggested_width) = suggested_width.val() {
422397
inline_nodes
423398
.into_iter()
424-
.for_each(|(mut inline_unit, margin, padding_border)| {
425-
inline_unit.adjust_inline_size(padding_border);
426-
if (current_line.total_inline_size
427-
+ inline_unit.size.width
428-
+ margin.horizontal()
399+
.for_each(|InlineUnitMetadata { unit, margin }| {
400+
if (current_line.total_inline_size + unit.size.width + margin.horizontal()
429401
> suggested_width)
430402
&& !current_line.is_empty()
431403
{
@@ -434,14 +406,13 @@ impl InlineMeasure<Node> for LayoutInlineMeasure {
434406
current_line = Line::default();
435407
current_line.block_start = prev_line_block_start;
436408
}
437-
current_line.collect_inline_unit(inline_unit, margin);
409+
current_line.collect_inline_unit(unit, margin);
438410
});
439411
} else {
440412
inline_nodes
441413
.into_iter()
442-
.for_each(|(mut inline_unit, margin, padding_border)| {
443-
inline_unit.adjust_inline_size(padding_border);
444-
current_line.collect_inline_unit(inline_unit, margin);
414+
.for_each(|InlineUnitMetadata { unit, margin }| {
415+
current_line.collect_inline_unit(unit, margin);
445416
});
446417
}
447418
if !current_line.is_empty() {

float-pigment-layout/src/algo/flow.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -645,14 +645,20 @@ impl<T: LayoutTreeNode> Flow<T> for LayoutUnit<T> {
645645
parent_is_block: true,
646646
},
647647
);
648-
T::InlineUnit::inline_block(
648+
T::InlineUnit::new(
649649
env,
650650
child_node,
651-
*child_res.size,
652-
child_res.last_baseline_ascent.main_axis(axis_info.dir),
651+
MeasureResult {
652+
size: *child_res.size,
653+
first_baseline_ascent: child_res.first_baseline_ascent,
654+
last_baseline_ascent: child_res.last_baseline_ascent,
655+
},
653656
)
654657
};
655-
(unit, child_margin, child_padding_border)
658+
InlineUnitMetadata {
659+
unit,
660+
margin: child_margin,
661+
}
656662
})
657663
.collect();
658664
let (block_size, positions) = T::InlineMeasure::block_size(

float-pigment-layout/src/lib.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ pub trait LayoutTreeNode: Sized {
168168
min: Size<Self::Length>,
169169
max: Size<Self::Length>,
170170
max_content: OptionSize<Self::Length>,
171-
) -> Self::InlineUnit;
171+
) -> MeasureResult<Self::Length>;
172172

173173
/// A notifier that the layout size of itself (or any node in the subtree) has been re-evaluated.
174174
///
@@ -380,7 +380,7 @@ pub trait InlineMeasure<T: LayoutTreeNode> {
380380
fn block_size(
381381
env: &mut Self::Env,
382382
block_node: &T,
383-
inline_nodes: Vec<(Self::InlineUnit, EdgeOption<T::Length>, Edge<T::Length>)>,
383+
inline_nodes: Vec<InlineUnitMetadata<T>>,
384384
req_size: OptionSize<T::Length>,
385385
max_content_with_max_size: OptionSize<T::Length>,
386386
update_position: bool,
@@ -390,18 +390,21 @@ pub trait InlineMeasure<T: LayoutTreeNode> {
390390
);
391391
}
392392

393+
/// Inline unit with some metadata.
394+
#[allow(missing_docs)]
395+
#[derive(Debug)]
396+
pub struct InlineUnitMetadata<T: LayoutTreeNode> {
397+
pub unit: T::InlineUnit,
398+
pub margin: EdgeOption<T::Length>,
399+
}
400+
393401
/// A helper type as the inline form of a tree node.
394402
pub trait InlineUnit<T: LayoutTreeNode> {
395403
/// Some custom environment data.
396404
type Env;
397405

398406
/// Construct from a tree node with specified size and baseline information.
399-
fn inline_block(
400-
env: &mut Self::Env,
401-
node: &T,
402-
size: Size<T::Length>,
403-
baseline_ascent: T::Length,
404-
) -> Self;
407+
fn new(env: &mut Self::Env, node: &T, result: MeasureResult<T::Length>) -> Self;
405408
}
406409

407410
/// The result of the measure function.

float-pigment-layout/src/unit.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ impl<T: LayoutTreeNode> LayoutUnit<T> {
617617
self.css_border_box_size(node, parent_inner_size, border, padding_border);
618618
let min_max_limit =
619619
self.normalized_min_max_limit(node, parent_inner_size, border, padding_border);
620-
let ret = node.measure_inline_unit(
620+
let r = node.measure_inline_unit(
621621
env,
622622
OptionSize::new(
623623
req_size.width - padding_border.horizontal(),
@@ -638,6 +638,23 @@ impl<T: LayoutTreeNode> LayoutUnit<T> {
638638
max_content.height - padding_border.vertical(),
639639
),
640640
);
641+
let size = Size::new(
642+
r.size.width + padding_border.horizontal(),
643+
r.size.height + padding_border.vertical(),
644+
);
645+
let first_baseline_ascent =
646+
r.first_baseline_ascent + Vector::new(padding_border.left, padding_border.top);
647+
let last_baseline_ascent =
648+
r.last_baseline_ascent + Vector::new(padding_border.left, padding_border.top);
649+
let ret = T::InlineUnit::new(
650+
env,
651+
node,
652+
MeasureResult {
653+
size,
654+
first_baseline_ascent,
655+
last_baseline_ascent,
656+
},
657+
);
641658
Some(ret)
642659
} else {
643660
None

0 commit comments

Comments
 (0)