BlueGit

ict rusty-teams

fix(chat): show message toolbar only over the bubble, paint wrapped inline code per line

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

metis Daniel Däschle · pushed by metis

Authored 2026-10-08 · 7 weeks ago

Hash
87343831bc025070a2d467ff31fda769975ae60e
Parent
3da1ddf6
2 files changed +113 −15
crates/app/src/render/selectable.rs +104 −13
@@ -73,22 +73,19 @@ impl SelectableRichText {
73 73 }
74 74 }
75 75
76 fn paint_pills(pills: &[Pill], layout: &TextLayout, window: &mut Window) {
76 fn paint_pills(pills: &[Pill], text: &str, layout: &TextLayout, window: &mut Window) {
77 77 let line_height = layout.line_height();
78 78 let bounds = layout.bounds();
79 79 for pill in pills {
80 let (Some(start), Some(end)) = (
81 layout.position_for_index(pill.range.start),
82 layout.position_for_index(pill.range.end),
83 ) else {
84 continue;
85 };
86 for line in selection_quads(start, end, bounds, line_height) {
80 let lines = pill_lines(text, pill.range.clone(), bounds.left(), |index| {
81 layout.position_for_index(index)
82 });
83 for line in lines {
87 84 let line = Bounds::from_corners(
88 point(line.left() - px(PILL_OUTSET), line.top() + px(PILL_INSET_Y)),
85 point(line.left - px(PILL_OUTSET), line.top + px(PILL_INSET_Y)),
89 86 point(
90 line.right() + px(PILL_OUTSET),
91 line.top() + line_height - px(PILL_INSET_Y),
87 line.right + px(PILL_OUTSET),
88 line.top + line_height - px(PILL_INSET_Y),
92 89 ),
93 90 );
94 91 let (border_width, border_color) = match pill.border {
@@ -107,6 +104,53 @@ fn paint_pills(pills: &[Pill], layout: &TextLayout, window: &mut Window) {
107 104 }
108 105 }
109 106
107 #[derive(Debug, PartialEq)]
108 struct PillLine {
109 left: Pixels,
110 right: Pixels,
111 top: Pixels,
112 }
113
114 fn pill_lines(
115 text: &str,
116 range: Range<usize>,
117 line_left: Pixels,
118 position: impl Fn(usize) -> Option<Point<Pixels>>,
119 ) -> Vec<PillLine> {
120 let mut lines: Vec<PillLine> = Vec::new();
121 let Some(slice) = text.get(range.clone()) else {
122 return lines;
123 };
124 let mut before = position(range.start);
125 for (offset, character) in slice.char_indices() {
126 let after = position(range.start + offset + character.len_utf8());
127 let (Some(start), Some(end)) = (before, after) else {
128 before = after;
129 continue;
130 };
131 before = after;
132 if character == '\n' {
133 continue;
134 }
135 // gpui places a wrap-boundary index at the end of the previous line.
136 let left = if start.y == end.y { start.x } else { line_left };
137 let right = if character.is_whitespace() {
138 left
139 } else {
140 end.x
141 };
142 match lines.last_mut() {
143 Some(line) if line.top == end.y => line.right = right.max(line.right),
144 _ => lines.push(PillLine {
145 left,
146 right,
147 top: end.y,
148 }),
149 }
150 }
151 lines
152 }
153
110 154 fn link_at(
111 155 layout: &TextLayout,
112 156 links: &[(Range<usize>, String)],
@@ -242,7 +286,7 @@ impl Element for SelectableRichText {
242 286 if selected_before != TextSelection::selected_text(window, cx) {
243 287 window.refresh();
244 288 }
245 paint_pills(&self.pills, &layout, window);
289 paint_pills(&self.pills, &self.text, &layout, window);
246 290 let color = Theme::global(cx).tokens.colors.selection;
247 291 let content_end = self.text.trim_end_matches(TIME_ROOM).len();
248 292 let mut copied = String::new();
@@ -316,7 +360,7 @@ mod tests {
316 360 WindowOptions, div, point, px, size,
317 361 };
318 362
319 use super::SelectableRichText;
363 use super::{PillLine, SelectableRichText, pill_lines};
320 364
321 365 const TEXT: &str = "plain bold link";
322 366
@@ -385,4 +429,51 @@ mod tests {
385 429 assert!(link_x.is_some());
386 430 assert_eq!(cx.opened_url().as_deref(), Some("https://example.com"));
387 431 }
432
433 #[test]
434 fn pill_wrapped_whole_onto_next_line_paints_only_there() {
435 let text = "the code";
436 let position = |index: usize| {
437 Some(if index <= 4 {
438 point(px(index as f32 * 10.), px(0.))
439 } else {
440 point(px((index - 4) as f32 * 10.), px(20.))
441 })
442 };
443 assert_eq!(
444 pill_lines(text, 4..8, px(0.), position),
445 vec![PillLine {
446 left: px(0.),
447 right: px(40.),
448 top: px(20.),
449 }]
450 );
451 }
452
453 #[test]
454 fn pill_split_by_a_wrap_paints_one_piece_per_line_without_the_trailing_space() {
455 let text = "ab cd";
456 let position = |index: usize| {
457 Some(if index <= 3 {
458 point(px(index as f32 * 10.), px(0.))
459 } else {
460 point(px((index - 3) as f32 * 10.), px(20.))
461 })
462 };
463 assert_eq!(
464 pill_lines(text, 0..5, px(0.), position),
465 vec![
466 PillLine {
467 left: px(0.),
468 right: px(20.),
469 top: px(0.),
470 },
471 PillLine {
472 left: px(0.),
473 right: px(20.),
474 top: px(20.),
475 },
476 ]
477 );
478 }
388 479 }
@@ -73,22 +73,19 @@ impl SelectableRichText {
73 }
73 }
74}
74}
75
75
76fn paint_pills(pills: &[Pill], layout: &TextLayout, window: &mut Window) {
76fn paint_pills(pills: &[Pill], text: &str, layout: &TextLayout, window: &mut Window) {
77 let line_height = layout.line_height();
77 let line_height = layout.line_height();
78 let bounds = layout.bounds();
78 let bounds = layout.bounds();
79 for pill in pills {
79 for pill in pills {
80 let (Some(start), Some(end)) = (
80 let lines = pill_lines(text, pill.range.clone(), bounds.left(), |index| {
81 layout.position_for_index(pill.range.start),
81 layout.position_for_index(index)
82 layout.position_for_index(pill.range.end),
82 });
83 ) else {
83 for line in lines {
84 continue;
85 };
86 for line in selection_quads(start, end, bounds, line_height) {
87 let line = Bounds::from_corners(
84 let line = Bounds::from_corners(
88 point(line.left() - px(PILL_OUTSET), line.top() + px(PILL_INSET_Y)),
85 point(line.left - px(PILL_OUTSET), line.top + px(PILL_INSET_Y)),
89 point(
86 point(
90 line.right() + px(PILL_OUTSET),
87 line.right + px(PILL_OUTSET),
91 line.top() + line_height - px(PILL_INSET_Y),
88 line.top + line_height - px(PILL_INSET_Y),
92 ),
89 ),
93 );
90 );
94 let (border_width, border_color) = match pill.border {
91 let (border_width, border_color) = match pill.border {
@@ -107,6 +104,53 @@ fn paint_pills(pills: &[Pill], layout: &TextLayout, window: &mut Window) {
107 }
104 }
108}
105}
109
106
107#[derive(Debug, PartialEq)]
108struct PillLine {
109 left: Pixels,
110 right: Pixels,
111 top: Pixels,
112}
113
114fn pill_lines(
115 text: &str,
116 range: Range<usize>,
117 line_left: Pixels,
118 position: impl Fn(usize) -> Option<Point<Pixels>>,
119) -> Vec<PillLine> {
120 let mut lines: Vec<PillLine> = Vec::new();
121 let Some(slice) = text.get(range.clone()) else {
122 return lines;
123 };
124 let mut before = position(range.start);
125 for (offset, character) in slice.char_indices() {
126 let after = position(range.start + offset + character.len_utf8());
127 let (Some(start), Some(end)) = (before, after) else {
128 before = after;
129 continue;
130 };
131 before = after;
132 if character == '\n' {
133 continue;
134 }
135 // gpui places a wrap-boundary index at the end of the previous line.
136 let left = if start.y == end.y { start.x } else { line_left };
137 let right = if character.is_whitespace() {
138 left
139 } else {
140 end.x
141 };
142 match lines.last_mut() {
143 Some(line) if line.top == end.y => line.right = right.max(line.right),
144 _ => lines.push(PillLine {
145 left,
146 right,
147 top: end.y,
148 }),
149 }
150 }
151 lines
152}
153
110fn link_at(
154fn link_at(
111 layout: &TextLayout,
155 layout: &TextLayout,
112 links: &[(Range<usize>, String)],
156 links: &[(Range<usize>, String)],
@@ -242,7 +286,7 @@ impl Element for SelectableRichText {
242 if selected_before != TextSelection::selected_text(window, cx) {
286 if selected_before != TextSelection::selected_text(window, cx) {
243 window.refresh();
287 window.refresh();
244 }
288 }
245 paint_pills(&self.pills, &layout, window);
289 paint_pills(&self.pills, &self.text, &layout, window);
246 let color = Theme::global(cx).tokens.colors.selection;
290 let color = Theme::global(cx).tokens.colors.selection;
247 let content_end = self.text.trim_end_matches(TIME_ROOM).len();
291 let content_end = self.text.trim_end_matches(TIME_ROOM).len();
248 let mut copied = String::new();
292 let mut copied = String::new();
@@ -316,7 +360,7 @@ mod tests {
316 WindowOptions, div, point, px, size,
360 WindowOptions, div, point, px, size,
317 };
361 };
318
362
319 use super::SelectableRichText;
363 use super::{PillLine, SelectableRichText, pill_lines};
320
364
321 const TEXT: &str = "plain bold link";
365 const TEXT: &str = "plain bold link";
322
366
@@ -385,4 +429,51 @@ mod tests {
385 assert!(link_x.is_some());
429 assert!(link_x.is_some());
386 assert_eq!(cx.opened_url().as_deref(), Some("https://example.com"));
430 assert_eq!(cx.opened_url().as_deref(), Some("https://example.com"));
387 }
431 }
432
433 #[test]
434 fn pill_wrapped_whole_onto_next_line_paints_only_there() {
435 let text = "the code";
436 let position = |index: usize| {
437 Some(if index <= 4 {
438 point(px(index as f32 * 10.), px(0.))
439 } else {
440 point(px((index - 4) as f32 * 10.), px(20.))
441 })
442 };
443 assert_eq!(
444 pill_lines(text, 4..8, px(0.), position),
445 vec![PillLine {
446 left: px(0.),
447 right: px(40.),
448 top: px(20.),
449 }]
450 );
451 }
452
453 #[test]
454 fn pill_split_by_a_wrap_paints_one_piece_per_line_without_the_trailing_space() {
455 let text = "ab cd";
456 let position = |index: usize| {
457 Some(if index <= 3 {
458 point(px(index as f32 * 10.), px(0.))
459 } else {
460 point(px((index - 3) as f32 * 10.), px(20.))
461 })
462 };
463 assert_eq!(
464 pill_lines(text, 0..5, px(0.), position),
465 vec![
466 PillLine {
467 left: px(0.),
468 right: px(20.),
469 top: px(0.),
470 },
471 PillLine {
472 left: px(0.),
473 right: px(20.),
474 top: px(20.),
475 },
476 ]
477 );
478 }
388}
479}
crates/app/src/views/message_row.rs +9 −2
@@ -80,9 +80,13 @@ fn bubble(
80 80 directory: &Directory,
81 81 extras: BubbleExtras,
82 82 cx: &App,
83 ) -> Div {
83 ) -> Stateful<Div> {
84 84 let corners = bubble_corners(own, row.series);
85 85 let mut element = div()
86 .id(ElementId::Name(format!("bubble-{index}").into()))
87 .when_some(extras.hover.clone(), |element, hover| {
88 element.on_hover(move |is_hovered, _, cx| hover(*is_hovered, cx))
89 })
86 90 .relative()
87 91 .rounded_tl(px(corners.top_left))
88 92 .rounded_tr(px(corners.top_right))
@@ -191,6 +195,7 @@ fn bubble(
191 195
192 196 #[derive(Default)]
193 197 struct BubbleExtras {
198 hover: Option<HoverChange>,
194 199 menu: Option<MessageMenu>,
195 200 react: Option<PickHandler>,
196 201 controls: Option<ReactionControls>,
@@ -575,7 +580,9 @@ pub fn render_message_row(
575 580 files,
576 581 highlighted,
577 582 } = actions;
583 let card_hover = hovered.clone().filter(|_| row.card);
578 584 let extras = BubbleExtras {
585 hover: hovered,
579 586 menu,
580 587 react,
581 588 controls: reaction_controls,
@@ -616,7 +623,7 @@ pub fn render_message_row(
616 623 }
617 624 })
618 625 })
619 .when_some(hovered, |element, hovered| {
626 .when_some(card_hover, |element, hovered| {
620 627 element.on_hover(move |is_hovered, _, cx| hovered(*is_hovered, cx))
621 628 })
622 629 .when_some(reply, |element, reply| {
@@ -80,9 +80,13 @@ fn bubble(
80 directory: &Directory,
80 directory: &Directory,
81 extras: BubbleExtras,
81 extras: BubbleExtras,
82 cx: &App,
82 cx: &App,
83) -> Div {
83) -> Stateful<Div> {
84 let corners = bubble_corners(own, row.series);
84 let corners = bubble_corners(own, row.series);
85 let mut element = div()
85 let mut element = div()
86 .id(ElementId::Name(format!("bubble-{index}").into()))
87 .when_some(extras.hover.clone(), |element, hover| {
88 element.on_hover(move |is_hovered, _, cx| hover(*is_hovered, cx))
89 })
86 .relative()
90 .relative()
87 .rounded_tl(px(corners.top_left))
91 .rounded_tl(px(corners.top_left))
88 .rounded_tr(px(corners.top_right))
92 .rounded_tr(px(corners.top_right))
@@ -191,6 +195,7 @@ fn bubble(
191
195
192#[derive(Default)]
196#[derive(Default)]
193struct BubbleExtras {
197struct BubbleExtras {
198 hover: Option<HoverChange>,
194 menu: Option<MessageMenu>,
199 menu: Option<MessageMenu>,
195 react: Option<PickHandler>,
200 react: Option<PickHandler>,
196 controls: Option<ReactionControls>,
201 controls: Option<ReactionControls>,
@@ -575,7 +580,9 @@ pub fn render_message_row(
575 files,
580 files,
576 highlighted,
581 highlighted,
577 } = actions;
582 } = actions;
583 let card_hover = hovered.clone().filter(|_| row.card);
578 let extras = BubbleExtras {
584 let extras = BubbleExtras {
585 hover: hovered,
579 menu,
586 menu,
580 react,
587 react,
581 controls: reaction_controls,
588 controls: reaction_controls,
@@ -616,7 +623,7 @@ pub fn render_message_row(
616 }
623 }
617 })
624 })
618 })
625 })
619 .when_some(hovered, |element, hovered| {
626 .when_some(card_hover, |element, hovered| {
620 element.on_hover(move |is_hovered, _, cx| hovered(*is_hovered, cx))
627 element.on_hover(move |is_hovered, _, cx| hovered(*is_hovered, cx))
621 })
628 })
622 .when_some(reply, |element, reply| {
629 .when_some(reply, |element, reply| {