BlueGit

ict rusty-teams

fix(messages): images inside a table cell stay in the cell

- table cells keep their image urls instead of an "[image]" placeholder
- the message view draws them in the cell and counts them as placed, so they no longer repeat below the message

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-11-08 · 3 weeks ago

Hash
9f4968e3d876734ab6f3b4a331725966015a5d37
Parent
94095612
4 files changed +159 −39
crates/app-ui-kit/src/render/blocks.rs +86 −7
@@ -235,7 +235,7 @@ pub enum Block {
235 235 },
236 236 Table {
237 237 header: bool,
238 rows: Vec<Vec<Inline>>,
238 rows: Vec<Vec<TableCell>>,
239 239 },
240 240 Rule,
241 241 Image {
@@ -243,6 +243,60 @@ pub enum Block {
243 243 },
244 244 }
245 245
246 #[derive(Debug, Clone, PartialEq, Eq)]
247 pub struct TableCell {
248 pub inline: Inline,
249 /// Images inside the cell, drawn in the cell by a renderer that knows them.
250 pub images: Vec<String>,
251 }
252
253 impl TableCell {
254 fn of(spans: &[Span]) -> TableCell {
255 let mut images = Vec::new();
256 collect_images(spans, &mut images);
257 let inline = inline_of(spans);
258 let inline = if images.is_empty() {
259 inline
260 } else {
261 inline.without(IMAGE_PLACEHOLDER)
262 };
263 TableCell { inline, images }
264 }
265 }
266
267 fn collect_images(spans: &[Span], images: &mut Vec<String>) {
268 for span in spans {
269 match span {
270 Span::Image { hosted_content_url } => images.push(hosted_content_url.clone()),
271 Span::Bold(children)
272 | Span::Italic(children)
273 | Span::Strike(children)
274 | Span::Superscript(children)
275 | Span::Subscript(children)
276 | Span::Sized(_, children)
277 | Span::Underline(children)
278 | Span::BlockQuote(children)
279 | Span::Link { children, .. }
280 | Span::Colored { children, .. }
281 | Span::Heading { children, .. }
282 | Span::Indent { children, .. } => collect_images(children, images),
283 _ => {}
284 }
285 }
286 }
287
288 /// Image urls that a table draws inside its cells.
289 pub fn table_images(block: &Block) -> Vec<&str> {
290 match block {
291 Block::Table { rows, .. } => rows
292 .iter()
293 .flatten()
294 .flat_map(|cell| cell.images.iter().map(String::as_str))
295 .collect(),
296 _ => Vec::new(),
297 }
298 }
299
246 300 impl Block {
247 301 pub fn markdown(&self) -> Option<String> {
248 302 let join = |blocks: &[Block]| {
@@ -275,7 +329,7 @@ impl Block {
275 329 rows.iter()
276 330 .map(|row| {
277 331 row.iter()
278 .map(Inline::plain_text)
332 .map(|cell| cell.inline.plain_text())
279 333 .collect::<Vec<_>>()
280 334 .join(" | ")
281 335 })
@@ -292,7 +346,7 @@ impl Block {
292 346 Block::Paragraph(inline) | Block::Heading { inline, .. } => Some(inline.plain_text()),
293 347 Block::Code { code, .. } | Block::CodeLine { code } => Some(code.clone()),
294 348 Block::List { items, .. } => items.first()?.iter().find_map(Block::first_line),
295 Block::Table { rows, .. } => Some(rows.first()?.first()?.plain_text()),
349 Block::Table { rows, .. } => Some(rows.first()?.first()?.inline.plain_text()),
296 350 Block::Quote(children) | Block::Indent { children, .. } => {
297 351 children.iter().find_map(Block::first_line)
298 352 }
@@ -387,7 +441,7 @@ pub fn emoji_urls(blocks: &[Block]) -> Vec<String> {
387 441 while let Some(block) = pending.pop() {
388 442 match block {
389 443 Block::Paragraph(inline) | Block::Heading { inline, .. } => add(inline),
390 Block::Table { rows, .. } => rows.iter().flatten().for_each(&mut add),
444 Block::Table { rows, .. } => rows.iter().flatten().for_each(|cell| add(&cell.inline)),
391 445 Block::Reply { children, .. } | Block::Quote(children) | Block::Indent { children, .. } => {
392 446 pending.extend(children)
393 447 }
@@ -473,9 +527,9 @@ fn layout_blocks_with(spans: &[Span], split_images: bool, frame_code: bool) -> V
473 527 }
474 528 Span::Table { header, rows } => {
475 529 flush(&mut current, &mut blocks, frame_code);
476 let rows: Vec<Vec<Inline>> = rows
530 let rows: Vec<Vec<TableCell>> = rows
477 531 .iter()
478 .map(|row| row.iter().map(|cell| inline_of(cell)).collect())
532 .map(|row| row.iter().map(|cell| TableCell::of(cell)).collect())
479 533 .collect();
480 534 if !rows.is_empty() {
481 535 blocks.push(Block::Table {
@@ -1066,6 +1120,31 @@ mod tests {
1066 1120 );
1067 1121 }
1068 1122
1123 fn cell(text: &str) -> TableCell {
1124 TableCell {
1125 inline: Inline::plain(text),
1126 images: Vec::new(),
1127 }
1128 }
1129
1130 #[test]
1131 fn an_image_in_a_cell_stays_in_the_cell() {
1132 let image = Span::Image {
1133 hosted_content_url: "https://img/1".into(),
1134 };
1135 let blocks = layout_blocks(&[Span::Table {
1136 header: false,
1137 rows: vec![vec![vec![image], vec![text("words")]]],
1138 }]);
1139 let [Block::Table { rows, .. }] = blocks.as_slice() else {
1140 panic!("one table: {blocks:?}");
1141 };
1142 assert_eq!(rows[0][0].images, ["https://img/1"]);
1143 assert!(rows[0][0].inline.is_blank());
1144 assert_eq!(rows[0][1], cell("words"));
1145 assert_eq!(table_images(&blocks[0]), ["https://img/1"]);
1146 }
1147
1069 1148 #[test]
1070 1149 fn heading_rule_and_table_are_blocks() {
1071 1150 let blocks = layout_blocks(&[
@@ -1089,7 +1168,7 @@ mod tests {
1089 1168 Block::Rule,
1090 1169 Block::Table {
1091 1170 header: true,
1092 rows: vec![vec![Inline::plain("a"), Inline::plain("b")]],
1171 rows: vec![vec![cell("a"), cell("b")]],
1093 1172 },
1094 1173 ]
1095 1174 );
@@ -235,7 +235,7 @@ pub enum Block {
235 },
235 },
236 Table {
236 Table {
237 header: bool,
237 header: bool,
238 rows: Vec<Vec<Inline>>,
238 rows: Vec<Vec<TableCell>>,
239 },
239 },
240 Rule,
240 Rule,
241 Image {
241 Image {
@@ -243,6 +243,60 @@ pub enum Block {
243 },
243 },
244}
244}
245
245
246#[derive(Debug, Clone, PartialEq, Eq)]
247pub struct TableCell {
248 pub inline: Inline,
249 /// Images inside the cell, drawn in the cell by a renderer that knows them.
250 pub images: Vec<String>,
251}
252
253impl TableCell {
254 fn of(spans: &[Span]) -> TableCell {
255 let mut images = Vec::new();
256 collect_images(spans, &mut images);
257 let inline = inline_of(spans);
258 let inline = if images.is_empty() {
259 inline
260 } else {
261 inline.without(IMAGE_PLACEHOLDER)
262 };
263 TableCell { inline, images }
264 }
265}
266
267fn collect_images(spans: &[Span], images: &mut Vec<String>) {
268 for span in spans {
269 match span {
270 Span::Image { hosted_content_url } => images.push(hosted_content_url.clone()),
271 Span::Bold(children)
272 | Span::Italic(children)
273 | Span::Strike(children)
274 | Span::Superscript(children)
275 | Span::Subscript(children)
276 | Span::Sized(_, children)
277 | Span::Underline(children)
278 | Span::BlockQuote(children)
279 | Span::Link { children, .. }
280 | Span::Colored { children, .. }
281 | Span::Heading { children, .. }
282 | Span::Indent { children, .. } => collect_images(children, images),
283 _ => {}
284 }
285 }
286}
287
288/// Image urls that a table draws inside its cells.
289pub fn table_images(block: &Block) -> Vec<&str> {
290 match block {
291 Block::Table { rows, .. } => rows
292 .iter()
293 .flatten()
294 .flat_map(|cell| cell.images.iter().map(String::as_str))
295 .collect(),
296 _ => Vec::new(),
297 }
298}
299
246impl Block {
300impl Block {
247 pub fn markdown(&self) -> Option<String> {
301 pub fn markdown(&self) -> Option<String> {
248 let join = |blocks: &[Block]| {
302 let join = |blocks: &[Block]| {
@@ -275,7 +329,7 @@ impl Block {
275 rows.iter()
329 rows.iter()
276 .map(|row| {
330 .map(|row| {
277 row.iter()
331 row.iter()
278 .map(Inline::plain_text)
332 .map(|cell| cell.inline.plain_text())
279 .collect::<Vec<_>>()
333 .collect::<Vec<_>>()
280 .join(" | ")
334 .join(" | ")
281 })
335 })
@@ -292,7 +346,7 @@ impl Block {
292 Block::Paragraph(inline) | Block::Heading { inline, .. } => Some(inline.plain_text()),
346 Block::Paragraph(inline) | Block::Heading { inline, .. } => Some(inline.plain_text()),
293 Block::Code { code, .. } | Block::CodeLine { code } => Some(code.clone()),
347 Block::Code { code, .. } | Block::CodeLine { code } => Some(code.clone()),
294 Block::List { items, .. } => items.first()?.iter().find_map(Block::first_line),
348 Block::List { items, .. } => items.first()?.iter().find_map(Block::first_line),
295 Block::Table { rows, .. } => Some(rows.first()?.first()?.plain_text()),
349 Block::Table { rows, .. } => Some(rows.first()?.first()?.inline.plain_text()),
296 Block::Quote(children) | Block::Indent { children, .. } => {
350 Block::Quote(children) | Block::Indent { children, .. } => {
297 children.iter().find_map(Block::first_line)
351 children.iter().find_map(Block::first_line)
298 }
352 }
@@ -387,7 +441,7 @@ pub fn emoji_urls(blocks: &[Block]) -> Vec<String> {
387 while let Some(block) = pending.pop() {
441 while let Some(block) = pending.pop() {
388 match block {
442 match block {
389 Block::Paragraph(inline) | Block::Heading { inline, .. } => add(inline),
443 Block::Paragraph(inline) | Block::Heading { inline, .. } => add(inline),
390 Block::Table { rows, .. } => rows.iter().flatten().for_each(&mut add),
444 Block::Table { rows, .. } => rows.iter().flatten().for_each(|cell| add(&cell.inline)),
391 Block::Reply { children, .. } | Block::Quote(children) | Block::Indent { children, .. } => {
445 Block::Reply { children, .. } | Block::Quote(children) | Block::Indent { children, .. } => {
392 pending.extend(children)
446 pending.extend(children)
393 }
447 }
@@ -473,9 +527,9 @@ fn layout_blocks_with(spans: &[Span], split_images: bool, frame_code: bool) -> V
473 }
527 }
474 Span::Table { header, rows } => {
528 Span::Table { header, rows } => {
475 flush(&mut current, &mut blocks, frame_code);
529 flush(&mut current, &mut blocks, frame_code);
476 let rows: Vec<Vec<Inline>> = rows
530 let rows: Vec<Vec<TableCell>> = rows
477 .iter()
531 .iter()
478 .map(|row| row.iter().map(|cell| inline_of(cell)).collect())
532 .map(|row| row.iter().map(|cell| TableCell::of(cell)).collect())
479 .collect();
533 .collect();
480 if !rows.is_empty() {
534 if !rows.is_empty() {
481 blocks.push(Block::Table {
535 blocks.push(Block::Table {
@@ -1066,6 +1120,31 @@ mod tests {
1066 );
1120 );
1067 }
1121 }
1068
1122
1123 fn cell(text: &str) -> TableCell {
1124 TableCell {
1125 inline: Inline::plain(text),
1126 images: Vec::new(),
1127 }
1128 }
1129
1130 #[test]
1131 fn an_image_in_a_cell_stays_in_the_cell() {
1132 let image = Span::Image {
1133 hosted_content_url: "https://img/1".into(),
1134 };
1135 let blocks = layout_blocks(&[Span::Table {
1136 header: false,
1137 rows: vec![vec![vec![image], vec![text("words")]]],
1138 }]);
1139 let [Block::Table { rows, .. }] = blocks.as_slice() else {
1140 panic!("one table: {blocks:?}");
1141 };
1142 assert_eq!(rows[0][0].images, ["https://img/1"]);
1143 assert!(rows[0][0].inline.is_blank());
1144 assert_eq!(rows[0][1], cell("words"));
1145 assert_eq!(table_images(&blocks[0]), ["https://img/1"]);
1146 }
1147
1069 #[test]
1148 #[test]
1070 fn heading_rule_and_table_are_blocks() {
1149 fn heading_rule_and_table_are_blocks() {
1071 let blocks = layout_blocks(&[
1150 let blocks = layout_blocks(&[
@@ -1089,7 +1168,7 @@ mod tests {
1089 Block::Rule,
1168 Block::Rule,
1090 Block::Table {
1169 Block::Table {
1091 header: true,
1170 header: true,
1092 rows: vec![vec![Inline::plain("a"), Inline::plain("b")]],
1171 rows: vec![vec![cell("a"), cell("b")]],
1093 },
1172 },
1094 ]
1173 ]
1095 );
1174 );
crates/app-ui-kit/src/render/elements.rs +24 −9
@@ -2,7 +2,7 @@ use gpui_kit::component::{h_flex, v_flex};
2 2 use gpui_kit::prelude::FluentBuilder as _;
3 3 use gpui_kit::*;
4 4
5 use super::blocks::{Block, Inline, StyleFlags};
5 use super::blocks::{Block, Inline, StyleFlags, TableCell};
6 6 use super::flow_text::{FlowSegment, FlowText};
7 7 use super::hooks::jump_to_quoted;
8 8 use super::selectable::{Pill, SelectableRichText};
@@ -101,7 +101,7 @@ fn render_block(block: &Block, id: String, own: bool, depth: usize, cx: &App) ->
101 101 items,
102 102 } => render_list(*ordered, *start, items, &id, own, depth, cx),
103 103 Block::Image { .. } => div().into_any_element(),
104 Block::Table { header, rows } => render_table(*header, rows, &id, own, cx),
104 Block::Table { header, rows } => render_table(*header, rows, &id, own, cx, &|_| None),
105 105 Block::Rule => div()
106 106 .w_full()
107 107 .h(px(1.))
@@ -312,7 +312,15 @@ fn render_list_item(blocks: &[Block], id: &str, own: bool, depth: usize, cx: &Ap
312 312 .into_any_element()
313 313 }
314 314
315 fn render_table(header: bool, rows: &[Vec<Inline>], id: &str, own: bool, cx: &App) -> AnyElement {
315 /// `image` draws an image of a cell; cells without a drawn image show only their text.
316 pub fn render_table(
317 header: bool,
318 rows: &[Vec<TableCell>],
319 id: &str,
320 own: bool,
321 cx: &App,
322 image: &dyn Fn(&str) -> Option<AnyElement>,
323 ) -> AnyElement {
316 324 let columns = rows.iter().map(Vec::len).max().unwrap_or(0);
317 325 v_flex()
318 326 .w_full()
@@ -340,12 +348,19 @@ fn render_table(header: bool, rows: &[Vec<Inline>], id: &str, own: bool, cx: &Ap
340 348 .border_b_1()
341 349 .border_color(theme::border_strong());
342 350 match row.get(column) {
343 Some(inline) => cell.child(render_inline(
344 inline,
345 format!("{id}-{row_index}-{column}"),
346 own,
347 cx,
348 )),
351 Some(content) => cell
352 .flex()
353 .flex_col()
354 .gap(px(6.))
355 .children(content.images.iter().filter_map(|url| image(url)))
356 .when(!content.inline.is_blank(), |cell| {
357 cell.child(render_inline(
358 &content.inline,
359 format!("{id}-{row_index}-{column}"),
360 own,
361 cx,
362 ))
363 }),
349 364 None => cell,
350 365 }
351 366 }))
@@ -2,7 +2,7 @@ use gpui_kit::component::{h_flex, v_flex};
2use gpui_kit::prelude::FluentBuilder as _;
2use gpui_kit::prelude::FluentBuilder as _;
3use gpui_kit::*;
3use gpui_kit::*;
4
4
5use super::blocks::{Block, Inline, StyleFlags};
5use super::blocks::{Block, Inline, StyleFlags, TableCell};
6use super::flow_text::{FlowSegment, FlowText};
6use super::flow_text::{FlowSegment, FlowText};
7use super::hooks::jump_to_quoted;
7use super::hooks::jump_to_quoted;
8use super::selectable::{Pill, SelectableRichText};
8use super::selectable::{Pill, SelectableRichText};
@@ -101,7 +101,7 @@ fn render_block(block: &Block, id: String, own: bool, depth: usize, cx: &App) ->
101 items,
101 items,
102 } => render_list(*ordered, *start, items, &id, own, depth, cx),
102 } => render_list(*ordered, *start, items, &id, own, depth, cx),
103 Block::Image { .. } => div().into_any_element(),
103 Block::Image { .. } => div().into_any_element(),
104 Block::Table { header, rows } => render_table(*header, rows, &id, own, cx),
104 Block::Table { header, rows } => render_table(*header, rows, &id, own, cx, &|_| None),
105 Block::Rule => div()
105 Block::Rule => div()
106 .w_full()
106 .w_full()
107 .h(px(1.))
107 .h(px(1.))
@@ -312,7 +312,15 @@ fn render_list_item(blocks: &[Block], id: &str, own: bool, depth: usize, cx: &Ap
312 .into_any_element()
312 .into_any_element()
313}
313}
314
314
315fn render_table(header: bool, rows: &[Vec<Inline>], id: &str, own: bool, cx: &App) -> AnyElement {
315/// `image` draws an image of a cell; cells without a drawn image show only their text.
316pub fn render_table(
317 header: bool,
318 rows: &[Vec<TableCell>],
319 id: &str,
320 own: bool,
321 cx: &App,
322 image: &dyn Fn(&str) -> Option<AnyElement>,
323) -> AnyElement {
316 let columns = rows.iter().map(Vec::len).max().unwrap_or(0);
324 let columns = rows.iter().map(Vec::len).max().unwrap_or(0);
317 v_flex()
325 v_flex()
318 .w_full()
326 .w_full()
@@ -340,12 +348,19 @@ fn render_table(header: bool, rows: &[Vec<Inline>], id: &str, own: bool, cx: &Ap
340 .border_b_1()
348 .border_b_1()
341 .border_color(theme::border_strong());
349 .border_color(theme::border_strong());
342 match row.get(column) {
350 match row.get(column) {
343 Some(inline) => cell.child(render_inline(
351 Some(content) => cell
344 inline,
352 .flex()
345 format!("{id}-{row_index}-{column}"),
353 .flex_col()
346 own,
354 .gap(px(6.))
347 cx,
355 .children(content.images.iter().filter_map(|url| image(url)))
348 )),
356 .when(!content.inline.is_blank(), |cell| {
357 cell.child(render_inline(
358 &content.inline,
359 format!("{id}-{row_index}-{column}"),
360 own,
361 cx,
362 ))
363 }),
349 None => cell,
364 None => cell,
350 }
365 }
351 }))
366 }))
crates/app-ui-kit/src/render/mod.rs +2 −2
@@ -5,5 +5,5 @@ pub mod hooks;
5 5 mod selectable;
6 6 mod syntax;
7 7
8 pub use blocks::{Block, layout_blocks, layout_card_blocks};
9 pub use elements::{CODE_BLOCK_TAG, CODE_LINE_TAG, render_blocks};
8 pub use blocks::{Block, TableCell, layout_blocks, layout_card_blocks, table_images};
9 pub use elements::{CODE_BLOCK_TAG, CODE_LINE_TAG, render_blocks, render_table};
@@ -5,5 +5,5 @@ pub mod hooks;
5mod selectable;
5mod selectable;
6mod syntax;
6mod syntax;
7
7
8pub use blocks::{Block, layout_blocks, layout_card_blocks};
8pub use blocks::{Block, TableCell, layout_blocks, layout_card_blocks, table_images};
9pub use elements::{CODE_BLOCK_TAG, CODE_LINE_TAG, render_blocks};
9pub use elements::{CODE_BLOCK_TAG, CODE_LINE_TAG, render_blocks, render_table};
crates/app/src/views/attachments.rs +47 −21
@@ -13,7 +13,7 @@ use crate::downloads::{self, DownloadState};
13 13 use crate::format;
14 14 use crate::inline_video;
15 15 use crate::remote_image;
16 use crate::render::{Block, render_blocks};
16 use crate::render::{Block, render_blocks, render_table, table_images};
17 17 use crate::rows::LocalImage;
18 18 use crate::theme;
19 19
@@ -393,13 +393,18 @@ fn placement(url: &str, images: &[ImageRef], local_count: usize) -> Option<Place
393 393 fn placements(blocks: &[Block], images: &[ImageRef], local_count: usize) -> Vec<Placement> {
394 394 blocks
395 395 .iter()
396 .filter_map(|block| match block {
397 Block::Image { url } => placement(url, images, local_count),
398 _ => None,
396 .flat_map(|block| match block {
397 Block::Image { url } => vec![url.as_str()],
398 table => table_images(table),
399 399 })
400 .filter_map(|url| placement(url, images, local_count))
400 401 .collect()
401 402 }
402 403
404 fn draws_images(block: &Block) -> bool {
405 matches!(block, Block::Image { .. }) || !table_images(block).is_empty()
406 }
407
403 408 pub fn message_body(
404 409 blocks: &[Block],
405 410 images: &[ImageRef],
@@ -410,10 +415,7 @@ pub fn message_body(
410 415 open_image: Option<&ImageActions>,
411 416 cx: &App,
412 417 ) -> Vec<AnyElement> {
413 if !blocks
414 .iter()
415 .any(|block| matches!(block, Block::Image { .. }))
416 {
418 if !blocks.iter().any(draws_images) {
417 419 return vec![render_blocks(blocks, id, own, cx)];
418 420 }
419 421 let mut elements = Vec::new();
@@ -424,24 +426,35 @@ pub fn message_body(
424 426 elements.push(render_blocks(&blocks[start..end], &part_id, own, cx));
425 427 }
426 428 };
427 for (index, block) in blocks.iter().enumerate() {
428 let Block::Image { url } = block else {
429 continue;
430 };
431 render_run(run_start, index, &mut elements);
432 run_start = index + 1;
433 match placement(url, images, local_images.len()) {
434 Some(Placement::Remote(position)) => elements.push(image_view(
429 let placed_view = |url: &str, key: &str| -> Option<AnyElement> {
430 match placement(url, images, local_images.len())? {
431 Placement::Remote(position) => Some(image_view(
435 432 &images[position],
436 format!("{id}-image-{position}"),
433 format!("{key}-image-{position}"),
437 434 directory,
438 435 open_image,
439 436 )),
440 Some(Placement::Local(position)) => elements.push(local_image_view(
437 Placement::Local(position) => Some(local_image_view(
441 438 &local_images[position],
442 format!("{id}-local-image-{position}"),
439 format!("{key}-local-image-{position}"),
443 440 )),
444 None => {}
441 }
442 };
443 for (index, block) in blocks.iter().enumerate() {
444 if !draws_images(block) {
445 continue;
446 }
447 render_run(run_start, index, &mut elements);
448 run_start = index + 1;
449 match block {
450 Block::Image { url } => elements.extend(placed_view(url, id)),
451 Block::Table { header, rows } => {
452 let table_id = format!("{id}-table-{index}");
453 elements.push(render_table(*header, rows, &table_id, own, cx, &|url| {
454 placed_view(url, &table_id)
455 }));
456 }
457 _ => {}
445 458 }
446 459 }
447 460 render_run(run_start, blocks.len(), &mut elements);
@@ -502,7 +515,7 @@ mod tests {
502 515 IMAGE_FALLBACK, Placement, file_subtitle, fit_image, fit_image_within, local_image_index,
503 516 placement, placements,
504 517 };
505 use crate::render::Block;
518 use crate::render::{Block, TableCell};
506 519 use teams_core::{FileCard, FileKind, ImageRef};
507 520
508 521 fn image_ref(url: &str) -> ImageRef {
@@ -542,6 +555,19 @@ mod tests {
542 555 assert_eq!(placement("a", &images, 0), Some(Placement::Remote(0)));
543 556 }
544 557
558 #[test]
559 fn an_image_in_a_table_cell_counts_as_placed() {
560 let images = [image_ref("a"), image_ref("b")];
561 let table = Block::Table {
562 header: false,
563 rows: vec![vec![TableCell {
564 inline: Default::default(),
565 images: vec!["b".into()],
566 }]],
567 };
568 assert_eq!(placements(&[table], &images, 0), vec![Placement::Remote(1)]);
569 }
570
545 571 #[test]
546 572 fn wide_images_shrink_to_the_max_width_keeping_aspect() {
547 573 assert_eq!(fit_image(Some((720, 360))), (360., 180.));
@@ -13,7 +13,7 @@ use crate::downloads::{self, DownloadState};
13use crate::format;
13use crate::format;
14use crate::inline_video;
14use crate::inline_video;
15use crate::remote_image;
15use crate::remote_image;
16use crate::render::{Block, render_blocks};
16use crate::render::{Block, render_blocks, render_table, table_images};
17use crate::rows::LocalImage;
17use crate::rows::LocalImage;
18use crate::theme;
18use crate::theme;
19
19
@@ -393,13 +393,18 @@ fn placement(url: &str, images: &[ImageRef], local_count: usize) -> Option<Place
393fn placements(blocks: &[Block], images: &[ImageRef], local_count: usize) -> Vec<Placement> {
393fn placements(blocks: &[Block], images: &[ImageRef], local_count: usize) -> Vec<Placement> {
394 blocks
394 blocks
395 .iter()
395 .iter()
396 .filter_map(|block| match block {
396 .flat_map(|block| match block {
397 Block::Image { url } => placement(url, images, local_count),
397 Block::Image { url } => vec![url.as_str()],
398 _ => None,
398 table => table_images(table),
399 })
399 })
400 .filter_map(|url| placement(url, images, local_count))
400 .collect()
401 .collect()
401}
402}
402
403
404fn draws_images(block: &Block) -> bool {
405 matches!(block, Block::Image { .. }) || !table_images(block).is_empty()
406}
407
403pub fn message_body(
408pub fn message_body(
404 blocks: &[Block],
409 blocks: &[Block],
405 images: &[ImageRef],
410 images: &[ImageRef],
@@ -410,10 +415,7 @@ pub fn message_body(
410 open_image: Option<&ImageActions>,
415 open_image: Option<&ImageActions>,
411 cx: &App,
416 cx: &App,
412) -> Vec<AnyElement> {
417) -> Vec<AnyElement> {
413 if !blocks
418 if !blocks.iter().any(draws_images) {
414 .iter()
415 .any(|block| matches!(block, Block::Image { .. }))
416 {
417 return vec![render_blocks(blocks, id, own, cx)];
419 return vec![render_blocks(blocks, id, own, cx)];
418 }
420 }
419 let mut elements = Vec::new();
421 let mut elements = Vec::new();
@@ -424,24 +426,35 @@ pub fn message_body(
424 elements.push(render_blocks(&blocks[start..end], &part_id, own, cx));
426 elements.push(render_blocks(&blocks[start..end], &part_id, own, cx));
425 }
427 }
426 };
428 };
427 for (index, block) in blocks.iter().enumerate() {
429 let placed_view = |url: &str, key: &str| -> Option<AnyElement> {
428 let Block::Image { url } = block else {
430 match placement(url, images, local_images.len())? {
429 continue;
431 Placement::Remote(position) => Some(image_view(
430 };
431 render_run(run_start, index, &mut elements);
432 run_start = index + 1;
433 match placement(url, images, local_images.len()) {
434 Some(Placement::Remote(position)) => elements.push(image_view(
435 &images[position],
432 &images[position],
436 format!("{id}-image-{position}"),
433 format!("{key}-image-{position}"),
437 directory,
434 directory,
438 open_image,
435 open_image,
439 )),
436 )),
440 Some(Placement::Local(position)) => elements.push(local_image_view(
437 Placement::Local(position) => Some(local_image_view(
441 &local_images[position],
438 &local_images[position],
442 format!("{id}-local-image-{position}"),
439 format!("{key}-local-image-{position}"),
443 )),
440 )),
444 None => {}
441 }
442 };
443 for (index, block) in blocks.iter().enumerate() {
444 if !draws_images(block) {
445 continue;
446 }
447 render_run(run_start, index, &mut elements);
448 run_start = index + 1;
449 match block {
450 Block::Image { url } => elements.extend(placed_view(url, id)),
451 Block::Table { header, rows } => {
452 let table_id = format!("{id}-table-{index}");
453 elements.push(render_table(*header, rows, &table_id, own, cx, &|url| {
454 placed_view(url, &table_id)
455 }));
456 }
457 _ => {}
445 }
458 }
446 }
459 }
447 render_run(run_start, blocks.len(), &mut elements);
460 render_run(run_start, blocks.len(), &mut elements);
@@ -502,7 +515,7 @@ mod tests {
502 IMAGE_FALLBACK, Placement, file_subtitle, fit_image, fit_image_within, local_image_index,
515 IMAGE_FALLBACK, Placement, file_subtitle, fit_image, fit_image_within, local_image_index,
503 placement, placements,
516 placement, placements,
504 };
517 };
505 use crate::render::Block;
518 use crate::render::{Block, TableCell};
506 use teams_core::{FileCard, FileKind, ImageRef};
519 use teams_core::{FileCard, FileKind, ImageRef};
507
520
508 fn image_ref(url: &str) -> ImageRef {
521 fn image_ref(url: &str) -> ImageRef {
@@ -542,6 +555,19 @@ mod tests {
542 assert_eq!(placement("a", &images, 0), Some(Placement::Remote(0)));
555 assert_eq!(placement("a", &images, 0), Some(Placement::Remote(0)));
543 }
556 }
544
557
558 #[test]
559 fn an_image_in_a_table_cell_counts_as_placed() {
560 let images = [image_ref("a"), image_ref("b")];
561 let table = Block::Table {
562 header: false,
563 rows: vec![vec![TableCell {
564 inline: Default::default(),
565 images: vec!["b".into()],
566 }]],
567 };
568 assert_eq!(placements(&[table], &images, 0), vec![Placement::Remote(1)]);
569 }
570
545 #[test]
571 #[test]
546 fn wide_images_shrink_to_the_max_width_keeping_aspect() {
572 fn wide_images_shrink_to_the_max_width_keeping_aspect() {
547 assert_eq!(fit_image(Some((720, 360))), (360., 180.));
573 assert_eq!(fit_image(Some((720, 360))), (360., 180.));