use crate::{
theme::color,
tokens::{ColorRole, FontFamilyKey},
typography::{INLINE_CODE_SIZES, font, inline_code_font},
};
use gpui_kit::{
Bounds, Div, Hsla, ParentElement, Pixels, Styled, StyledText, TextRun, canvas, div, fill,
point, px, size,
};
use std::ops::Range;
pub const PAD: &str = "\u{202F}";
pub fn inline_code_fill() -> Hsla {
color(ColorRole::LayerAccent01).opacity(0.6)
}
pub fn code_size(body: f32) -> f32 {
let (prose, code) = INLINE_CODE_SIZES;
if body < prose {
body
} else {
body * code / prose
}
}
pub fn box_height(body: f32) -> f32 {
code_size(body) * 1.3 + 2.
}
pub fn push_inline_code(
text: &mut String,
runs: &mut Vec<TextRun>,
code: &str,
ink: Hsla,
body: f32,
) -> Range<usize> {
let start = text.len();
let face = inline_code_font();
let code_font = if body < INLINE_CODE_SIZES.0 {
font(FontFamilyKey::Mono, 400)
} else {
face.clone()
};
for (s, f) in [(PAD, &face), (code, &code_font), (PAD, &face)] {
if s.is_empty() {
continue;
}
text.push_str(s);
runs.push(TextRun {
len: s.len(),
font: f.clone(),
color: ink,
background_color: None,
underline: None,
strikethrough: None,
});
}
start..text.len()
}
pub fn with_code_boxes(styled: StyledText, code: Vec<Range<usize>>, body: f32) -> Div {
let layout = styled.layout().clone();
let tint = inline_code_fill();
let h = px(box_height(body));
div()
.relative()
.min_w_0()
.child(
canvas(
|_, _, _| (),
move |bounds: Bounds<Pixels>, _, window, _| {
let inset = (layout.line_height() - h) / 2.;
for r in &code {
let (Some(a), Some(b)) = (
layout.position_for_index(r.start),
layout.position_for_index(r.end),
) else {
continue;
};
let mut boxes = Vec::new();
if a.y == b.y {
boxes.push((a.x, b.x, a.y));
} else {
boxes.push((a.x, bounds.right(), a.y));
boxes.push((bounds.left(), b.x, b.y));
}
for (x0, x1, y) in boxes {
if x1 > x0 {
let rect = Bounds::new(point(x0, y + inset), size(x1 - x0, h));
window.paint_quad(fill(rect, tint));
}
}
}
},
)
.absolute()
.top_0()
.left_0()
.size_full(),
)
.child(styled)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::typography::INLINE_CODE_FAMILY;
#[test]
fn inline_code_is_the_r2_desk_tint() {
crate::theme::tests_set(crate::CarbonTheme::White);
let tint = inline_code_fill();
let base = color(ColorRole::LayerAccent01);
assert!((tint.a - 0.6).abs() < 1e-3);
assert_eq!((tint.h, tint.s, tint.l), (base.h, base.s, base.l));
}
#[test]
fn code_is_13px_padded_with_no_break_spaces() {
assert_eq!(code_size(14.), 13.);
assert_eq!(code_size(13.), 13.);
assert!((box_height(14.) - 18.9).abs() < 1e-3);
let (mut text, mut runs) = ("a ".to_string(), Vec::new());
let r = push_inline_code(&mut text, &mut runs, "g90", Hsla::default(), 14.);
assert_eq!(&text[r], "\u{202F}g90\u{202F}");
assert_eq!(runs.len(), 3);
assert!(
runs.iter()
.all(|r| r.font.family.as_ref() == INLINE_CODE_FAMILY)
);
assert!(runs.iter().all(|r| r.background_color.is_none()));
let mut runs = Vec::new();
push_inline_code(&mut String::new(), &mut runs, "x", Hsla::default(), 13.);
assert_eq!(runs[1].font.family.as_ref(), "IBM Plex Mono");
}
}