-
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #543 from Sharktheone/layout/taffy-inline
Support Inline elements
- Loading branch information
Showing
10 changed files
with
319 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod inline; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use taffy::{ | ||
AvailableSpace, CollapsibleMarginSet, Layout, LayoutInput, LayoutOutput, LayoutPartialTree, | ||
NodeId, Point, ResolveOrZero, RunMode, Size, | ||
}; | ||
|
||
use gosub_render_backend::layout::LayoutTree; | ||
|
||
use crate::{LayoutDocument, TaffyLayouter}; | ||
|
||
pub fn compute_inline_layout<LT: LayoutTree<TaffyLayouter>>( | ||
tree: &mut LayoutDocument<LT>, | ||
nod_id: LT::NodeId, | ||
mut layout_input: LayoutInput, | ||
) -> LayoutOutput { | ||
layout_input.known_dimensions = Size::NONE; | ||
layout_input.run_mode = RunMode::PerformLayout; //TODO: We should respect the run mode | ||
|
||
let Some(children) = tree.0.children(nod_id) else { | ||
return LayoutOutput::HIDDEN; | ||
}; | ||
|
||
let mut outputs = Vec::with_capacity(children.len()); | ||
|
||
let mut height = 0.0f32; | ||
for child in &children { | ||
let node_id = NodeId::from((*child).into()); | ||
|
||
let out = tree.compute_child_layout(node_id, layout_input); | ||
|
||
let style = tree.get_style(node_id); | ||
|
||
let margin = style.margin.resolve_or_zero(layout_input.parent_size); | ||
|
||
let child_height = out.size.height + margin.top + margin.bottom; | ||
|
||
height = height.max(child_height); | ||
|
||
outputs.push(out); | ||
} | ||
|
||
let mut width = 0.0f32; | ||
for (child, out) in children.into_iter().zip(outputs.into_iter()) { | ||
let node_id = NodeId::from(child.into()); | ||
|
||
let style = tree.get_style(node_id); | ||
|
||
let location = Point { | ||
x: width, | ||
y: height - out.size.height, | ||
}; | ||
|
||
let border = style.border.resolve_or_zero(layout_input.parent_size); | ||
let padding = style.padding.resolve_or_zero(layout_input.parent_size); | ||
|
||
width += out.size.width + border.left + border.right + padding.left + padding.right; | ||
|
||
tree.set_unrounded_layout( | ||
node_id, | ||
&Layout { | ||
size: out.size, | ||
content_size: out.content_size, | ||
order: 0, | ||
location, | ||
border, | ||
padding, | ||
scrollbar_size: Size::ZERO, //TODO | ||
}, | ||
); | ||
} | ||
|
||
let content_size = Size { width, height }; | ||
|
||
let mut size = content_size; | ||
|
||
if let AvailableSpace::Definite(width) = layout_input.available_space.width { | ||
size.width = size.width.min(width); | ||
} | ||
|
||
if let AvailableSpace::Definite(height) = layout_input.available_space.height { | ||
size.height = size.height.min(height); | ||
} | ||
|
||
dbg!(size); | ||
dbg!(content_size); | ||
|
||
LayoutOutput { | ||
size, | ||
content_size, | ||
first_baselines: Point::NONE, | ||
top_margin: CollapsibleMarginSet::ZERO, | ||
bottom_margin: CollapsibleMarginSet::ZERO, | ||
margins_can_collapse_through: false, | ||
} | ||
} |
Oops, something went wrong.