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 }).hsla(),
41 t.text().hsla(),
42 ),
43 };
44
45 div()
46 .px(px(6.0))
47 .py(px(2.0))
48 .rounded(px(t.radius(Size::Sm)))
49 .bg(bg)
50 .text_color(fg)
51 .text_size(px(t.font_size(Size::Sm)))
52 .child(self.content)
53 }
54}