Skip to main content

guise/
code.rs

1//! `Code` — inline monospace-style code text.
2
3use gpui::prelude::*;
4use gpui::{div, px, App, IntoElement, SharedString, Window};
5
6use crate::devtools::Probed;
7use crate::theme::{theme, ColorName, Size};
8
9/// Inline code. (Uses the window font; gpui has no generic
10/// monospace fallback, so style a real mono family at the app level if needed.)
11#[derive(IntoElement)]
12pub struct Code {
13  content: SharedString,
14  color: Option<ColorName>,
15}
16
17impl Code {
18  pub fn new(content: impl Into<SharedString>) -> Self {
19    Code {
20      content: content.into(),
21      color: None,
22    }
23  }
24
25  /// Tint the code chip with a named color instead of the neutral surface.
26  pub fn color(mut self, color: ColorName) -> Self {
27    self.color = Some(color);
28    self
29  }
30}
31
32impl RenderOnce for Code {
33  fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
34    let t = theme(cx);
35    let (bg, fg) = match self.color {
36      Some(name) => (
37        t.color(name, if t.scheme.is_dark() { 8 } else { 0 }).hsla(),
38        t.color(name, if t.scheme.is_dark() { 2 } else { 8 }).hsla(),
39      ),
40      None => (
41        t.color(ColorName::Gray, if t.scheme.is_dark() { 8 } else { 1 })
42          .hsla(),
43        t.text().hsla(),
44      ),
45    };
46
47    div()
48      .px(px(6.0))
49      .py(px(2.0))
50      .rounded(px(t.radius(Size::Sm)))
51      .bg(bg)
52      .text_color(fg)
53      .text_size(px(t.font_size(Size::Sm)))
54      .child(self.content)
55      .probe("Code")
56  }
57}