fix(messages): keycap emoji render as one emoji glyph again
- gpui applies a font override only to a run inside its range - highlights are split at keycap boundaries, so 1️⃣ gets the emoji font in plain and bold text - demo release chat shows keycaps in bold and plain text Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -1997,6 +1997,16 @@ fn release_messages() -> Vec<MessageRecord> {
1997
1997
"[]",
1998
1998
false,
1999
1999
),
2000
message(
2001
chat,
2002
"m12",
2003
None,
2004
(LEA_ID, lea_name),
2005
at(0, 13, 52),
2006
"<p><strong>1\u{fe0f}\u{20e3}Freeze the branch</strong></p><p><strong>2\u{fe0f}\u{20e3}Tag the release</strong></p><p>3\u{fe0f}\u{20e3} Publish the notes</p>",
2007
"[]",
2008
false,
2009
),
2000
2010
]
2001
2011
}
2002
2012
@@ -1997,6 +1997,16 @@ fn release_messages() -> Vec<MessageRecord> {1997
"[]",1997
"[]",1998
false,1998
false,1999
),1999
),2000
message(2001
chat,2002
"m12",2003
None,2004
(LEA_ID, lea_name),2005
at(0, 13, 52),2006
"<p><strong>1\u{fe0f}\u{20e3}Freeze the branch</strong></p><p><strong>2\u{fe0f}\u{20e3}Tag the release</strong></p><p>3\u{fe0f}\u{20e3} Publish the notes</p>",2007
"[]",2008
false,2009
),2000
]2010
]2001
}2011
}2002
2012
@@ -392,6 +392,47 @@ pub fn keycap_ranges(text: &str) -> Vec<std::ops::Range<usize>> {
392
392
ranges
393
393
}
394
394
395
/// gpui applies a font override only to a run that lies inside it, so every cut range gets runs of its own.
396
pub fn split_highlights(
397
highlights: Vec<(std::ops::Range<usize>, HighlightStyle)>,
398
cuts: &[std::ops::Range<usize>],
399
text_len: usize,
400
) -> Vec<(std::ops::Range<usize>, HighlightStyle)> {
401
if cuts.is_empty() {
402
return highlights;
403
}
404
let mut covered = Vec::new();
405
let mut cursor = 0;
406
for (range, style) in highlights {
407
if cursor < range.start {
408
covered.push((cursor..range.start, HighlightStyle::default()));
409
}
410
cursor = cursor.max(range.end);
411
covered.push((range, style));
412
}
413
if cursor < text_len {
414
covered.push((cursor..text_len, HighlightStyle::default()));
415
}
416
let mut points: Vec<usize> = cuts.iter().flat_map(|cut| [cut.start, cut.end]).collect();
417
points.sort_unstable();
418
covered
419
.into_iter()
420
.flat_map(|(range, style)| {
421
let mut start = range.start;
422
let mut pieces = Vec::new();
423
for &point in points
424
.iter()
425
.filter(|&&point| range.start < point && point < range.end)
426
{
427
pieces.push((start..point, style));
428
start = point;
429
}
430
pieces.push((start..range.end, style));
431
pieces
432
})
433
.collect()
434
}
435
395
436
fn render_inline(inline: &Inline, id: String, own: bool, cx: &App) -> AnyElement {
396
437
let highlights = inline
397
438
.segments
@@ -420,6 +461,7 @@ fn render_inline(inline: &Inline, id: String, own: bool, cx: &App) -> AnyElement
420
461
)
421
462
.collect();
422
463
font_overrides.sort_by_key(|(range, _)| range.start);
464
let keycaps = keycap_ranges(&inline.text);
423
465
let emoji = inline.emoji();
424
466
let highlights = highlights
425
467
.into_iter()
@@ -482,11 +524,15 @@ fn render_inline(inline: &Inline, id: String, own: bool, cx: &App) -> AnyElement
482
524
.w_full()
483
525
.text_color(theme::text())
484
526
.child(
485
SelectableRichText::new(ElementId::Name(id.into()), inline.text.clone(), highlights)
486
.links(inline.links())
487
.font_overrides(font_overrides)
488
.pills(pills)
489
.inline_images(inline_images),
527
SelectableRichText::new(
528
ElementId::Name(id.into()),
529
inline.text.clone(),
530
split_highlights(highlights, &keycaps, inline.text.len()),
531
)
532
.links(inline.links())
533
.font_overrides(font_overrides)
534
.pills(pills)
535
.inline_images(inline_images),
490
536
)
491
537
.into_any_element()
492
538
}
@@ -619,8 +665,22 @@ fn luminance(color: Rgba) -> f32 {
619
665
mod tests {
620
666
use gpui_kit::{Rgba, rgb};
621
667
622
use super::{MIN_CONTRAST, contrast, keycap_ranges, readable};
668
use super::{MIN_CONTRAST, contrast, keycap_ranges, readable, split_highlights};
623
669
use app_base::theme;
670
use gpui_kit::{FontWeight, HighlightStyle};
671
672
#[test]
673
fn a_keycap_inside_bold_text_gets_runs_of_its_own() {
674
let bold = HighlightStyle {
675
font_weight: Some(FontWeight::BOLD),
676
..Default::default()
677
};
678
let split = split_highlights(vec![(0..10, bold)], &[2..9, 12..15], 20);
679
let ranges: Vec<_> = split.iter().map(|(range, _)| range.clone()).collect();
680
assert_eq!(ranges, [0..2, 2..9, 9..10, 10..12, 12..15, 15..20]);
681
assert_eq!(split[1].1, bold);
682
assert_eq!(split[4].1, HighlightStyle::default());
683
}
624
684
625
685
#[test]
626
686
fn keycap_sequences_cover_the_base_the_selector_and_the_keycap() {
@@ -392,6 +392,47 @@ pub fn keycap_ranges(text: &str) -> Vec<std::ops::Range<usize>> {392
ranges392
ranges393
}393
}394
394
395
/// gpui applies a font override only to a run that lies inside it, so every cut range gets runs of its own.396
pub fn split_highlights(397
highlights: Vec<(std::ops::Range<usize>, HighlightStyle)>,398
cuts: &[std::ops::Range<usize>],399
text_len: usize,400
) -> Vec<(std::ops::Range<usize>, HighlightStyle)> {401
if cuts.is_empty() {402
return highlights;403
}404
let mut covered = Vec::new();405
let mut cursor = 0;406
for (range, style) in highlights {407
if cursor < range.start {408
covered.push((cursor..range.start, HighlightStyle::default()));409
}410
cursor = cursor.max(range.end);411
covered.push((range, style));412
}413
if cursor < text_len {414
covered.push((cursor..text_len, HighlightStyle::default()));415
}416
let mut points: Vec<usize> = cuts.iter().flat_map(|cut| [cut.start, cut.end]).collect();417
points.sort_unstable();418
covered419
.into_iter()420
.flat_map(|(range, style)| {421
let mut start = range.start;422
let mut pieces = Vec::new();423
for &point in points424
.iter()425
.filter(|&&point| range.start < point && point < range.end)426
{427
pieces.push((start..point, style));428
start = point;429
}430
pieces.push((start..range.end, style));431
pieces432
})433
.collect()434
}435
395
fn render_inline(inline: &Inline, id: String, own: bool, cx: &App) -> AnyElement {436
fn render_inline(inline: &Inline, id: String, own: bool, cx: &App) -> AnyElement {396
let highlights = inline437
let highlights = inline397
.segments438
.segments@@ -420,6 +461,7 @@ fn render_inline(inline: &Inline, id: String, own: bool, cx: &App) -> AnyElement420
)461
)421
.collect();462
.collect();422
font_overrides.sort_by_key(|(range, _)| range.start);463
font_overrides.sort_by_key(|(range, _)| range.start);464
let keycaps = keycap_ranges(&inline.text);423
let emoji = inline.emoji();465
let emoji = inline.emoji();424
let highlights = highlights466
let highlights = highlights425
.into_iter()467
.into_iter()@@ -482,11 +524,15 @@ fn render_inline(inline: &Inline, id: String, own: bool, cx: &App) -> AnyElement482
.w_full()524
.w_full()483
.text_color(theme::text())525
.text_color(theme::text())484
.child(526
.child(485
SelectableRichText::new(ElementId::Name(id.into()), inline.text.clone(), highlights)527
SelectableRichText::new(486
.links(inline.links())528
ElementId::Name(id.into()),487
.font_overrides(font_overrides)529
inline.text.clone(),488
.pills(pills)530
split_highlights(highlights, &keycaps, inline.text.len()),489
.inline_images(inline_images),531
)532
.links(inline.links())533
.font_overrides(font_overrides)534
.pills(pills)535
.inline_images(inline_images),490
)536
)491
.into_any_element()537
.into_any_element()492
}538
}@@ -619,8 +665,22 @@ fn luminance(color: Rgba) -> f32 {619
mod tests {665
mod tests {620
use gpui_kit::{Rgba, rgb};666
use gpui_kit::{Rgba, rgb};621
667
622
use super::{MIN_CONTRAST, contrast, keycap_ranges, readable};668
use super::{MIN_CONTRAST, contrast, keycap_ranges, readable, split_highlights};623
use app_base::theme;669
use app_base::theme;670
use gpui_kit::{FontWeight, HighlightStyle};671
672
#[test]673
fn a_keycap_inside_bold_text_gets_runs_of_its_own() {674
let bold = HighlightStyle {675
font_weight: Some(FontWeight::BOLD),676
..Default::default()677
};678
let split = split_highlights(vec![(0..10, bold)], &[2..9, 12..15], 20);679
let ranges: Vec<_> = split.iter().map(|(range, _)| range.clone()).collect();680
assert_eq!(ranges, [0..2, 2..9, 9..10, 10..12, 12..15, 15..20]);681
assert_eq!(split[1].1, bold);682
assert_eq!(split[4].1, HighlightStyle::default());683
}624
684
625
#[test]685
#[test]626
fn keycap_sequences_cover_the_base_the_selector_and_the_keycap() {686
fn keycap_sequences_cover_the_base_the_selector_and_the_keycap() {