Skip to content

Commit

Permalink
Implement legacy text alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Jul 28, 2024
1 parent b46359c commit b95c199
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/compute/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::util::sys::f32_max;
use crate::util::sys::Vec;
use crate::util::MaybeMath;
use crate::util::{MaybeResolve, ResolveOrZero};
use crate::{BoxGenerationMode, BoxSizing, LayoutBlockContainer};
use crate::{BlockContainerStyle, BoxGenerationMode, BoxSizing, LayoutBlockContainer, TextAlign};

#[cfg(feature = "content_size")]
use super::common::content_size::compute_content_size_contribution;
Expand Down Expand Up @@ -190,6 +190,8 @@ fn compute_inner(tree: &mut impl LayoutBlockContainer, node_id: NodeId, inputs:
|| matches!(size.height, Some(h) if h > 0.0)
|| matches!(min_size.height, Some(h) if h > 0.0);

let text_align = style.text_align();

drop(style);

// 1. Generate items
Expand Down Expand Up @@ -219,6 +221,7 @@ fn compute_inner(tree: &mut impl LayoutBlockContainer, node_id: NodeId, inputs:
container_outer_width,
content_box_inset,
resolved_content_box_inset,
text_align,
own_margins_collapse_with_children,
);
let container_outer_height = known_dimensions
Expand Down Expand Up @@ -383,6 +386,7 @@ fn perform_final_layout_on_in_flow_children(
container_outer_width: f32,
content_box_inset: Rect<f32>,
resolved_content_box_inset: Rect<f32>,
text_align: TextAlign,
own_margins_collapse_with_children: Line<bool>,
) -> (Size<f32>, f32, CollapsibleMarginSet, CollapsibleMarginSet) {
// Resolve container_inner_width for sizing child nodes using initial content_box_inset
Expand Down Expand Up @@ -469,11 +473,26 @@ fn perform_final_layout_on_in_flow_children(
x: resolved_content_box_inset.left,
y: committed_y_offset + active_collapsible_margin_set.resolve(),
};
let location = Point {
let mut location = Point {
x: resolved_content_box_inset.left + inset_offset.x + resolved_margin.left,
y: committed_y_offset + inset_offset.y + y_margin_offset,
};

// Apply alignment
let item_outer_width = item_layout.size.width + resolved_margin.horizontal_axis_sum();
if item_outer_width < container_inner_width {
match text_align {
TextAlign::Auto => {
// Do nothing
}
TextAlign::LegacyLeft => {
// Do nothing. Left aligned by default.
}
TextAlign::LegacyRight => location.x += container_inner_width - item_outer_width,
TextAlign::LegacyCenter => location.x += (container_inner_width - item_outer_width) / 2.0,
}
}

let scrollbar_size = Size {
width: if item.overflow.y == Overflow::Scroll { item.scrollbar_width } else { 0.0 },
height: if item.overflow.x == Overflow::Scroll { item.scrollbar_width } else { 0.0 },
Expand Down
1 change: 1 addition & 0 deletions src/style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ pub struct Style {

// Block container properties
/// How items elements should aligned in the inline axis
#[cfg(feature = "block_layout")]
pub text_align: TextAlign,

// Flexbox container properties
Expand Down

0 comments on commit b95c199

Please sign in to comment.