From 9f429592ce7b4465a460c5042c33a08a692c1c70 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Tue, 25 Feb 2025 14:27:59 +0800 Subject: [PATCH] text_view: Remove last paragraph bottom margin. (#660) Now, we can be easy to use TextView for 1 line text. ## Break Changes - Removed `inline` method by previous #649 added, this is not needed. --- crates/story/src/switch_story.rs | 1 - crates/ui/src/text/element.rs | 18 ++++++++++++------ crates/ui/src/text/html.rs | 2 +- crates/ui/src/text/markdown.rs | 2 +- crates/ui/src/text/text_view.rs | 14 -------------- 5 files changed, 14 insertions(+), 23 deletions(-) diff --git a/crates/story/src/switch_story.rs b/crates/story/src/switch_story.rs index b14ade1a..812b8f3c 100644 --- a/crates/story/src/switch_story.rs +++ b/crates/story/src/switch_story.rs @@ -218,7 +218,6 @@ impl Render for SwitchStory { "The [long long label](https://github.com) text used markdown, \ it should wrap when the text is too long.", ) - .inline(), ), ), ), diff --git a/crates/ui/src/text/element.rs b/crates/ui/src/text/element.rs index 3249405d..a8c36c49 100644 --- a/crates/ui/src/text/element.rs +++ b/crates/ui/src/text/element.rs @@ -386,6 +386,7 @@ impl Node { ordered: state.ordered, todo: checked.is_some(), }), + true, text_view_style, window, cx, @@ -399,6 +400,7 @@ impl Node { ordered: state.ordered, todo: checked.is_some(), }), + true, text_view_style, window, cx, @@ -489,12 +491,13 @@ impl Node { pub(crate) fn render( self, list_state: Option, + is_last_child: bool, text_view_style: &TextViewStyle, window: &mut Window, cx: &mut App, ) -> impl IntoElement { let in_list = list_state.is_some(); - let mb = if in_list { + let mb = if in_list || is_last_child { rems(0.) } else { text_view_style.paragraph_gap @@ -502,11 +505,14 @@ impl Node { match self { Node::Root { children } => div() - .children( - children - .into_iter() - .map(|c| c.render(None, text_view_style, window, cx)), - ) + .children({ + let children_len = children.len(); + + children.into_iter().enumerate().map(move |(ix, c)| { + let is_last_child = ix == children_len - 1; + c.render(None, is_last_child, text_view_style, window, cx) + }) + }) .into_any_element(), Node::Paragraph(paragraph) => div().mb(mb).child(paragraph).into_any_element(), Node::Heading { level, children } => { diff --git a/crates/ui/src/text/html.rs b/crates/ui/src/text/html.rs index 807a8370..461422d3 100644 --- a/crates/ui/src/text/html.rs +++ b/crates/ui/src/text/html.rs @@ -160,7 +160,7 @@ impl Element for HtmlElement { let mut el = div() .map(|this| match root { - Ok(node) => this.child(node.render(None, &self.style, window, cx)), + Ok(node) => this.child(node.render(None, true, &self.style, window, cx)), Err(err) => this.child( v_flex() .gap_1() diff --git a/crates/ui/src/text/markdown.rs b/crates/ui/src/text/markdown.rs index 8ad52b98..8b5ad540 100644 --- a/crates/ui/src/text/markdown.rs +++ b/crates/ui/src/text/markdown.rs @@ -116,7 +116,7 @@ impl Element for MarkdownElement { let mut el = div() .map(|this| match root { - Ok(node) => this.child(node.render(None, &self.style, window, cx)), + Ok(node) => this.child(node.render(None, true, &self.style, window, cx)), Err(err) => this.child( v_flex() .gap_1() diff --git a/crates/ui/src/text/text_view.rs b/crates/ui/src/text/text_view.rs index 7dcdf438..d841f6e4 100644 --- a/crates/ui/src/text/text_view.rs +++ b/crates/ui/src/text/text_view.rs @@ -64,15 +64,6 @@ impl Default for TextViewStyle { } impl TextViewStyle { - /// Default style for inline text. - /// - /// This style has no paragraph gap. - pub fn inline() -> Self { - Self { - paragraph_gap: rems(0.), - } - } - /// Set paragraph gap, default is 1 rem. pub fn paragraph_gap(mut self, gap: Rems) -> Self { self.paragraph_gap = gap; @@ -106,11 +97,6 @@ impl TextView { Self::Html(el) => Self::Html(el.style(style)), } } - - /// Set to use [`TextViewStyle::inline`]. - pub fn inline(self) -> Self { - self.style(TextViewStyle::inline()) - } } impl RenderOnce for TextView {