Skip to content
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
25 changes: 19 additions & 6 deletions crates/ui/src/markdown/inline_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,12 @@ impl Element for InlineFlow {
cx: &mut App,
) -> (LayoutId, Self::RequestLayoutState) {
let measure_items = self.items.iter().map(MeasureItem::from).collect::<Vec<_>>();
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()
Expand Down Expand Up @@ -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),
Expand All @@ -342,6 +346,7 @@ impl Element for InlineFlow {
&measure_items,
&element_sizes,
&text_style,
rem_size,
wrap_width,
window,
)
Expand Down Expand Up @@ -575,19 +580,27 @@ fn layout_flow(
items: &[MeasureItem],
element_sizes: &[Option<Size<Pixels>>],
text_style: &TextStyle,
rem_size: Pixels,
wrap_width: Option<Pixels>,
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::<usize>();
if total_len == 0 {
return InlineFlowLayout::default();
}
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;
Expand Down Expand Up @@ -731,6 +744,7 @@ fn line_ranges(
items: &[MeasureItem],
element_sizes: &[Option<Size<Pixels>>],
text_style: &TextStyle,
font_size: Pixels,
wrap_width: Option<Pixels>,
window: &mut Window,
) -> Vec<Range<usize>> {
Expand All @@ -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();
Expand Down
69 changes: 69 additions & 0 deletions crates/ui/src/markdown/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity<MarkdownState>>,
list_state: ListState,
}

impl Render for StyledOuterListRoot {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> 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:?})"
);
}
}
Loading