1use gpui::prelude::*;
4use gpui::{div, px, App, IntoElement, SharedString, Window};
5
6use crate::theme::{theme, ColorName, Size};
7
8#[derive(IntoElement)]
11pub struct Code {
12 content: SharedString,
13 color: Option<ColorName>,
14}
15
16impl Code {
17 pub fn new(content: impl Into<SharedString>) -> Self {
18 Code {
19 content: content.into(),
20 color: None,
21 }
22 }
23
24 pub fn color(mut self, color: ColorName) -> Self {
26 self.color = Some(color);
27 self
28 }
29}
30
31impl RenderOnce for Code {
32 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
33 let t = theme(cx);
34 let (bg, fg) = match self.color {
35 Some(name) => (
36 t.color(name, if t.scheme.is_dark() { 8 } else { 0 }).hsla(),
37 t.color(name, if t.scheme.is_dark() { 2 } else { 8 }).hsla(),
38 ),
39 None => (
40 t.color(ColorName::Gray, if t.scheme.is_dark() { 8 } else { 1 })
41 .hsla(),
42 t.text().hsla(),
43 ),
44 };
45
46 div()
47 .px(px(6.0))
48 .py(px(2.0))
49 .rounded(px(t.radius(Size::Sm)))
50 .bg(bg)
51 .text_color(fg)
52 .text_size(px(t.font_size(Size::Sm)))
53 .child(self.content)
54 }
55}