BlueGit

ict rusty-teams

fix(messages): keycap emoji render in the emoji font

- font fallback put the digit in the text font and left the keycap as a box

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

metis Daniel Däschle · pushed by metis

Authored 2026-11-05 · 3 weeks ago

Hash
758bbe2e2b6ccb8b5c75a1a321c2c8129d86d1fa
Parent
9b18c939
1 file changed +38 −1
crates/app-ui-kit/src/render/elements.rs +38 −1
@@ -358,6 +358,31 @@ fn code_font(cx: &App) -> SharedString {
358 358 cx.theme().mono_font_family.clone()
359 359 }
360 360
361 #[cfg(target_os = "windows")]
362 const EMOJI_FONT_FAMILY: &str = "Segoe UI Emoji";
363 #[cfg(target_os = "macos")]
364 const EMOJI_FONT_FAMILY: &str = "Apple Color Emoji";
365 #[cfg(not(any(target_os = "windows", target_os = "macos")))]
366 const EMOJI_FONT_FAMILY: &str = "Noto Color Emoji";
367 const KEYCAP: char = '\u{20e3}';
368 const VARIATION_SELECTOR: char = '\u{fe0f}';
369
370 /// Font fallback maps the keycap base digit to the text font and leaves the combining keycap as a box.
371 pub fn keycap_ranges(text: &str) -> Vec<std::ops::Range<usize>> {
372 let mut ranges = Vec::new();
373 for (keycap_at, _) in text.match_indices(KEYCAP) {
374 let before = &text[..keycap_at];
375 let before = before.strip_suffix(VARIATION_SELECTOR).unwrap_or(before);
376 let Some(base) = before.chars().next_back() else {
377 continue;
378 };
379 if base.is_ascii_digit() || base == '#' || base == '*' {
380 ranges.push(before.len() - base.len_utf8()..keycap_at + KEYCAP.len_utf8());
381 }
382 }
383 ranges
384 }
385
361 386 fn render_inline(inline: &Inline, id: String, own: bool, cx: &App) -> AnyElement {
362 387 let highlights = inline
363 388 .segments
@@ -371,6 +396,11 @@ fn render_inline(inline: &Inline, id: String, own: bool, cx: &App) -> AnyElement
371 396 .iter()
372 397 .filter(|segment| segment.style.code)
373 398 .map(|segment| (segment.range.clone(), mono.clone()))
399 .chain(
400 keycap_ranges(&inline.text)
401 .into_iter()
402 .map(|range| (range, SharedString::from(EMOJI_FONT_FAMILY))),
403 )
374 404 .collect();
375 405 let emoji = inline.emoji();
376 406 let highlights = highlights
@@ -567,9 +597,16 @@ fn luminance(color: Rgba) -> f32 {
567 597 mod tests {
568 598 use gpui_kit::{Rgba, rgb};
569 599
570 use super::{MIN_CONTRAST, contrast, readable};
600 use super::{MIN_CONTRAST, contrast, keycap_ranges, readable};
571 601 use app_base::theme;
572 602
603 #[test]
604 fn keycap_sequences_cover_the_base_the_selector_and_the_keycap() {
605 let text = "\u{1f522} 4\u{fe0f}\u{20e3} After #\u{20e3} x\u{20e3}";
606 let found: Vec<&str> = keycap_ranges(text).into_iter().map(|range| &text[range]).collect();
607 assert_eq!(found, ["4\u{fe0f}\u{20e3}", "#\u{20e3}"]);
608 }
609
573 610 #[test]
574 611 fn dark_text_color_is_lifted_to_readable_contrast() {
575 612 let lifted = Rgba::from(readable(0x000080, false));
@@ -358,6 +358,31 @@ fn code_font(cx: &App) -> SharedString {
358 cx.theme().mono_font_family.clone()
358 cx.theme().mono_font_family.clone()
359}
359}
360
360
361#[cfg(target_os = "windows")]
362const EMOJI_FONT_FAMILY: &str = "Segoe UI Emoji";
363#[cfg(target_os = "macos")]
364const EMOJI_FONT_FAMILY: &str = "Apple Color Emoji";
365#[cfg(not(any(target_os = "windows", target_os = "macos")))]
366const EMOJI_FONT_FAMILY: &str = "Noto Color Emoji";
367const KEYCAP: char = '\u{20e3}';
368const VARIATION_SELECTOR: char = '\u{fe0f}';
369
370/// Font fallback maps the keycap base digit to the text font and leaves the combining keycap as a box.
371pub fn keycap_ranges(text: &str) -> Vec<std::ops::Range<usize>> {
372 let mut ranges = Vec::new();
373 for (keycap_at, _) in text.match_indices(KEYCAP) {
374 let before = &text[..keycap_at];
375 let before = before.strip_suffix(VARIATION_SELECTOR).unwrap_or(before);
376 let Some(base) = before.chars().next_back() else {
377 continue;
378 };
379 if base.is_ascii_digit() || base == '#' || base == '*' {
380 ranges.push(before.len() - base.len_utf8()..keycap_at + KEYCAP.len_utf8());
381 }
382 }
383 ranges
384}
385
361fn render_inline(inline: &Inline, id: String, own: bool, cx: &App) -> AnyElement {
386fn render_inline(inline: &Inline, id: String, own: bool, cx: &App) -> AnyElement {
362 let highlights = inline
387 let highlights = inline
363 .segments
388 .segments
@@ -371,6 +396,11 @@ fn render_inline(inline: &Inline, id: String, own: bool, cx: &App) -> AnyElement
371 .iter()
396 .iter()
372 .filter(|segment| segment.style.code)
397 .filter(|segment| segment.style.code)
373 .map(|segment| (segment.range.clone(), mono.clone()))
398 .map(|segment| (segment.range.clone(), mono.clone()))
399 .chain(
400 keycap_ranges(&inline.text)
401 .into_iter()
402 .map(|range| (range, SharedString::from(EMOJI_FONT_FAMILY))),
403 )
374 .collect();
404 .collect();
375 let emoji = inline.emoji();
405 let emoji = inline.emoji();
376 let highlights = highlights
406 let highlights = highlights
@@ -567,9 +597,16 @@ fn luminance(color: Rgba) -> f32 {
567mod tests {
597mod tests {
568 use gpui_kit::{Rgba, rgb};
598 use gpui_kit::{Rgba, rgb};
569
599
570 use super::{MIN_CONTRAST, contrast, readable};
600 use super::{MIN_CONTRAST, contrast, keycap_ranges, readable};
571 use app_base::theme;
601 use app_base::theme;
572
602
603 #[test]
604 fn keycap_sequences_cover_the_base_the_selector_and_the_keycap() {
605 let text = "\u{1f522} 4\u{fe0f}\u{20e3} After #\u{20e3} x\u{20e3}";
606 let found: Vec<&str> = keycap_ranges(text).into_iter().map(|range| &text[range]).collect();
607 assert_eq!(found, ["4\u{fe0f}\u{20e3}", "#\u{20e3}"]);
608 }
609
573 #[test]
610 #[test]
574 fn dark_text_color_is_lifted_to_readable_contrast() {
611 fn dark_text_color_is_lifted_to_readable_contrast() {
575 let lifted = Rgba::from(readable(0x000080, false));
612 let lifted = Rgba::from(readable(0x000080, false));