Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use the real text height for layout #932

Merged
merged 6 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion crates/components/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ pub fn Button(
text_align: "center",
main_align: "center",
cross_align: "center",
line_height: "1.1",
{&children}
}
)
Expand Down
11 changes: 5 additions & 6 deletions crates/components/src/tooltip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ pub fn TooltipContainer(
onmouseenter,
onmouseleave,
{children},
if *is_hovering.read() {
rect {
height: "0",
width: "0",
layer: "-1500",
rect {
height: "0",
width: "0",
layer: "-1500",
if *is_hovering.read() {
match position {
TooltipPosition::Below => rsx!(
rect {
Expand All @@ -107,7 +107,6 @@ pub fn TooltipContainer(
}
),
}

}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/elements/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl ElementUtils for ParagraphElement {
};

if node_cursor_state.position.is_some() {
let (paragraph, _) = create_paragraph(
let paragraph = create_paragraph(
node_ref,
&area.size,
font_collection,
Expand Down
3 changes: 2 additions & 1 deletion crates/core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl NodeState {
),
(
"line_height",
AttributeType::Measure(self.font_style.line_height),
AttributeType::OptionalMeasure(self.font_style.line_height),
),
(
"text_align",
Expand Down Expand Up @@ -160,6 +160,7 @@ pub enum AttributeType<'a> {
Gradient(Fill),
Size(&'a Size),
Measure(f32),
OptionalMeasure(Option<f32>),
Measures(Gaps),
CornerRadius(CornerRadius),
Direction(&'a DirectionMode),
Expand Down
52 changes: 16 additions & 36 deletions crates/core/src/render/skia_measurer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,32 @@ impl<'a> LayoutMeasurer<NodeId> for SkiaMeasurer<'a> {

match &*node_type {
NodeType::Element(ElementNode { tag, .. }) if tag == &TagName::Label => {
let (label, paragraph_font_height) = create_label(
let label = create_label(
&node,
area_size,
self.font_collection,
self.default_fonts,
self.scale_factor,
);

let res = Size2D::new(label.longest_line(), label.height());
let height = label.height();
let res = Size2D::new(label.longest_line(), height);
let mut map = SendAnyMap::new();
map.insert(CachedParagraph(label, paragraph_font_height));
map.insert(CachedParagraph(label, height));
Some((res, Arc::new(map)))
}
NodeType::Element(ElementNode { tag, .. }) if tag == &TagName::Paragraph => {
let (paragraph, paragraph_font_height) = create_paragraph(
let paragraph = create_paragraph(
&node,
area_size,
self.font_collection,
false,
self.default_fonts,
self.scale_factor,
);
let res = Size2D::new(paragraph.longest_line(), paragraph.height());
let height = paragraph.height();
let res = Size2D::new(paragraph.longest_line(), height);
let mut map = SendAnyMap::new();
map.insert(CachedParagraph(paragraph, paragraph_font_height));
map.insert(CachedParagraph(paragraph, height));
Some((res, Arc::new(map)))
}
_ => None,
Expand Down Expand Up @@ -131,7 +132,7 @@ pub fn create_label(
font_collection: &FontCollection,
default_font_family: &[String],
scale_factor: f32,
) -> (Paragraph, f32) {
) -> Paragraph {
let font_style = &*node.get::<FontStyleState>().unwrap();

let mut paragraph_style = ParagraphStyle::default();
Expand All @@ -143,7 +144,7 @@ pub fn create_label(
paragraph_style.set_ellipsis(ellipsis);
}

let text_style = font_style.text_style(default_font_family, scale_factor, true);
let text_style = font_style.text_style(default_font_family, scale_factor);
paragraph_style.set_text_style(&text_style);

let mut paragraph_builder = ParagraphBuilder::new(&paragraph_style, font_collection);
Expand All @@ -161,16 +162,7 @@ pub fn create_label(
area_size.width + 1.0
});

// Measure the actual text height, ignoring the line height
let mut height = paragraph.height();
for line in paragraph.get_line_metrics() {
for (_, text) in line.get_style_metrics(0..1) {
let text_height = -(text.font_metrics.ascent - text.font_metrics.descent);
height = height.max(text_height);
}
}

(paragraph, height)
paragraph
}

/// Align the Y axis of the highlights and cursor of a paragraph
Expand Down Expand Up @@ -228,7 +220,7 @@ pub fn create_paragraph(
is_rendering: bool,
default_font_family: &[String],
scale_factor: f32,
) -> (Paragraph, f32) {
) -> Paragraph {
let font_style = &*node.get::<FontStyleState>().unwrap();

let mut paragraph_style = ParagraphStyle::default();
Expand All @@ -242,13 +234,10 @@ pub fn create_paragraph(

let mut paragraph_builder = ParagraphBuilder::new(&paragraph_style, font_collection);

let text_style = font_style.text_style(default_font_family, scale_factor, true);
let text_style = font_style.text_style(default_font_family, scale_factor);
paragraph_builder.push_style(&text_style);

let node_children = node.children();
let node_children_len = node_children.len();

for text_span in node_children {
for text_span in node.children() {
if let NodeType::Element(ElementNode {
tag: TagName::Text, ..
}) = &*text_span.node_type()
Expand All @@ -257,7 +246,7 @@ pub fn create_paragraph(
let text_node = *text_nodes.first().unwrap();
let text_node_type = &*text_node.node_type();
let font_style = text_span.get::<FontStyleState>().unwrap();
let text_style = font_style.text_style(default_font_family, scale_factor, true);
let text_style = font_style.text_style(default_font_family, scale_factor);
paragraph_builder.push_style(&text_style);

if let NodeType::Text(text) = text_node_type {
Expand All @@ -278,14 +267,5 @@ pub fn create_paragraph(
area_size.width + 1.0
});

// Measure the actual text height, ignoring the line height
let mut height = paragraph.height();
for line in paragraph.get_line_metrics() {
for (_, text) in line.get_style_metrics(0..node_children_len) {
let text_height = -(text.font_metrics.ascent - text.font_metrics.descent);
height = height.max(text_height);
}
}

(paragraph, height)
paragraph
}
9 changes: 9 additions & 0 deletions crates/devtools/src/tabs/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ pub fn NodeInspectorStyle(node_id: String) -> Element {
}
}
}
AttributeType::OptionalMeasure(measure) => {
rsx!{
Property {
key: "{i}",
name: "{name}",
value: measure.map(|measure| measure.to_string()).unwrap_or_else(|| "inherit".to_string())
}
}
}
AttributeType::Measures(measures) => {
rsx!{
Property {
Expand Down
4 changes: 2 additions & 2 deletions crates/hooks/tests/use_editable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ pub async fn highlight_multiple_lines_single_editor() {
utils.wait_for_update().await;

// Move cursor
utils.move_cursor((80., 20.)).await;
utils.move_cursor((80., 25.)).await;

utils.wait_for_update().await;

Expand Down Expand Up @@ -849,7 +849,7 @@ pub async fn highlight_shift_click_multiple_lines_single_editor() {
utils.wait_for_update().await;

// Move and click cursor
utils.click_cursor((80., 20.)).await;
utils.click_cursor((80., 25.)).await;

utils.wait_for_update().await;

Expand Down
21 changes: 9 additions & 12 deletions crates/state/src/font_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct FontStyleState {
pub font_slant: Slant,
pub font_weight: Weight,
pub font_width: Width,
pub line_height: f32, // https://developer.mozilla.org/en-US/docs/Web/CSS/line-height,
pub line_height: Option<f32>,
pub decoration: Decoration,
pub word_spacing: f32,
pub letter_spacing: f32,
Expand All @@ -48,12 +48,7 @@ pub struct FontStyleState {
}

impl FontStyleState {
pub fn text_style(
&self,
default_font_family: &[String],
scale_factor: f32,
height_override: bool,
) -> TextStyle {
pub fn text_style(&self, default_font_family: &[String], scale_factor: f32) -> TextStyle {
let mut text_style = TextStyle::new();
let mut font_family = self.font_family.clone();

Expand All @@ -69,9 +64,11 @@ impl FontStyleState {
.set_font_size(self.font_size * scale_factor)
.set_font_families(&font_family)
.set_word_spacing(self.word_spacing)
.set_letter_spacing(self.letter_spacing)
.set_height_override(height_override)
.set_height(self.line_height);
.set_letter_spacing(self.letter_spacing);

if let Some(line_height) = self.line_height {
text_style.set_height_override(true).set_height(line_height);
}

for text_shadow in self.text_shadows.iter() {
text_style.add_shadow(*text_shadow);
Expand All @@ -95,7 +92,7 @@ impl Default for FontStyleState {
font_weight: Weight::NORMAL,
font_slant: Slant::Upright,
font_width: Width::NORMAL,
line_height: 1.2,
line_height: None,
word_spacing: 0.0,
letter_spacing: 0.0,
decoration: Decoration {
Expand Down Expand Up @@ -151,7 +148,7 @@ impl ParseAttribute for FontStyleState {
AttributeName::LineHeight => {
if let Some(value) = attr.value.as_text() {
if let Ok(line_height) = value.parse::<f32>() {
self.line_height = line_height.max(1.0);
self.line_height = Some(line_height);
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions examples/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn app() -> Element {
padding: "7",
width: "100%",
height: "100%",
font_size: "10",
label {
color: "black",
"Your name:"
Expand All @@ -38,9 +39,12 @@ fn app() -> Element {
values.write().1 = txt;
}
},
label {
color: "black",
"You are {values.read().0} and you are {values.read().1} years old."
rect {
background: "red",
label {
color: "black",
"You are {values.read().0} and you are {values.read().1} years old."
}
}
}
)
Expand Down
Loading