From 38aad579e12aead894ba1ad8400ed59e078c5bd6 Mon Sep 17 00:00:00 2001 From: ClanEver <562211524@qq.com> Date: Fri, 4 Dec 2026 13:50:55 +0800 Subject: [PATCH] Markdown: capture inherited text style before InlineFlow measures --- crates/ui/src/markdown/inline_flow.rs | 25 +++++++--- crates/ui/src/markdown/view.rs | 69 +++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/crates/ui/src/markdown/inline_flow.rs b/crates/ui/src/markdown/inline_flow.rs index 65abbb30d..f1ad75243 100644 --- a/crates/ui/src/markdown/inline_flow.rs +++ b/crates/ui/src/markdown/inline_flow.rs @@ -269,7 +269,12 @@ impl Element for InlineFlow { cx: &mut App, ) -> (LayoutId, Self::RequestLayoutState) { let measure_items = self.items.iter().map(MeasureItem::from).collect::>(); - let line_height = window.line_height(); + // Capture inherited text style and rem size before returning: Taffy + // runs the measure closure during `compute_layout`, after ancestor + // `with_text_style` and `with_rem_size` scopes have popped. + let text_style = window.text_style(); + let rem_size = window.rem_size(); + let line_height = text_style.line_height_in_pixels(rem_size); let element_sizes = self .items .iter() @@ -315,7 +320,6 @@ impl Element for InlineFlow { let fallback = self.view.read(cx).render_fallback_flag(); let layout_id = window.request_measured_layout(Default::default(), { move |known_dimensions, available_space, window, _| { - let text_style = window.text_style(); let wrap_width = if text_style.white_space == WhiteSpace::Normal { known_dimensions.width.or(match available_space.width { AvailableSpace::Definite(width) => Some(width), @@ -342,6 +346,7 @@ impl Element for InlineFlow { &measure_items, &element_sizes, &text_style, + rem_size, wrap_width, window, ) @@ -575,11 +580,12 @@ fn layout_flow( items: &[MeasureItem], element_sizes: &[Option>], text_style: &TextStyle, + rem_size: Pixels, wrap_width: Option, window: &mut Window, ) -> InlineFlowLayout { - let line_height = window.line_height(); - let font_size = text_style.font_size.to_pixels(window.rem_size()); + let line_height = text_style.line_height_in_pixels(rem_size); + let font_size = text_style.font_size.to_pixels(rem_size); let total_len = items.iter().map(MeasureItem::len).sum::(); if total_len == 0 { return InlineFlowLayout::default(); @@ -587,7 +593,14 @@ fn layout_flow( let mut fragments = Vec::new(); let mut max_width = Pixels::ZERO; let mut y = Pixels::ZERO; - for line_range in line_ranges(items, element_sizes, text_style, wrap_width, window) { + for line_range in line_ranges( + items, + element_sizes, + text_style, + font_size, + wrap_width, + window, + ) { let mut line_fragments = Vec::new(); let mut line_width = Pixels::ZERO; let mut actual_line_height = line_height; @@ -731,6 +744,7 @@ fn line_ranges( items: &[MeasureItem], element_sizes: &[Option>], text_style: &TextStyle, + font_size: Pixels, wrap_width: Option, window: &mut Window, ) -> Vec> { @@ -755,7 +769,6 @@ fn line_ranges( let Some(wrap_width) = wrap_width else { return segments; }; - let font_size = text_style.font_size.to_pixels(window.rem_size()); let mut ranges = Vec::new(); for segment in segments { let mut fragments = Vec::new(); diff --git a/crates/ui/src/markdown/view.rs b/crates/ui/src/markdown/view.rs index 7b90ae3b2..11e05d7fe 100644 --- a/crates/ui/src/markdown/view.rs +++ b/crates/ui/src/markdown/view.rs @@ -1197,4 +1197,73 @@ mod tests { let rendered = view.read_with(cx, |root, cx| root.markdown.read(cx).rendered_text()); assert!(rendered.contains("调 council 让多模型评审团看图打分挑刺")); } + + struct StyledOuterListRoot { + markdown: Option>, + list_state: ListState, + } + + impl Render for StyledOuterListRoot { + fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { + use crate::theme::ActiveTheme as _; + use crate::typography::TextRole as _; + + let markdown = self.markdown.clone(); + div() + .size_full() + .text_token(&cx.theme().tokens.typography.md) + .child( + gpui::list(self.list_state.clone(), move |_, _, cx| { + let Some(markdown) = markdown.clone() else { + return div().into_any_element(); + }; + div() + .w_full() + .text_token(&crate::typography::chat_prose(cx)) + .debug_selector(|| "markdown-prose-host".into()) + .child(MarkdownView::new(&markdown).selectable(true)) + .into_any_element() + }) + .size_full(), + ) + } + } + + #[gpui::test] + fn inline_flow_height_uses_inherited_text_style_inside_virtual_list(cx: &mut TestAppContext) { + cx.update(crate::test_client::init); + cx.update(crate::markdown::init); + let source = format!( + "See [preview.png](preview.png) and {}\n\nClosing line.", + "wrapped prose words ".repeat(40) + ); + let (root, cx) = cx.add_window_view(|_, _| StyledOuterListRoot { + markdown: None, + list_state: ListState::new(0, ListAlignment::Top, px(100.)), + }); + // Size the window before the message first lays out, as when opening a + // thread at phone width: `VirtualizedBlockList` takes its initial + // `ColumnLayout` measurement at this width without a later resize. + cx.simulate_resize(gpui::size(px(393.), px(852.))); + cx.update(|window, cx| _ = window.draw(cx)); + root.update(cx, |root, cx| { + root.markdown = Some(cx.new(|cx| MarkdownState::new(&source, cx))); + root.list_state.reset(1); + cx.notify(); + }); + for _ in 0..2 { + cx.run_until_parked(); + cx.update(|window, cx| _ = window.draw(cx)); + } + let host = cx + .debug_bounds("markdown-prose-host") + .expect("markdown host is laid out"); + let last = cx + .debug_bounds("markdown-block-1") + .expect("closing paragraph is painted"); + assert!( + last.bottom() <= host.bottom() + px(0.5), + "closing paragraph ({last:?}) overflowed container ({host:?})" + ); + } }