inline_code.rs

 1use crate::prelude::*;
 2use gpui::{AnyElement, IntoElement, ParentElement, Styled};
 3
 4/// InlineCode mimics the way inline code is rendered when wrapped in backticks in Markdown.
 5///
 6/// # Usage Example
 7///
 8/// ```
 9/// use ui::InlineCode;
10///
11/// let InlineCode = InlineCode::new("<div>hey</div>");
12/// ```
13#[derive(IntoElement, RegisterComponent)]
14pub struct InlineCode {
15    label: SharedString,
16    label_size: LabelSize,
17}
18
19impl InlineCode {
20    pub fn new(label: impl Into<SharedString>) -> Self {
21        Self {
22            label: label.into(),
23            label_size: LabelSize::Default,
24        }
25    }
26
27    /// Sets the size of the label.
28    pub fn label_size(mut self, size: LabelSize) -> Self {
29        self.label_size = size;
30        self
31    }
32}
33
34impl RenderOnce for InlineCode {
35    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
36        h_flex()
37            .min_w_0()
38            .px_0p5()
39            .overflow_hidden()
40            .bg(cx.theme().colors().text.opacity(0.05))
41            .child(Label::new(self.label).size(self.label_size).buffer_font(cx))
42    }
43}
44
45impl Component for InlineCode {
46    fn scope() -> ComponentScope {
47        ComponentScope::DataDisplay
48    }
49
50    fn preview(_window: &mut Window, _cx: &mut App) -> Option<AnyElement> {
51        Some(
52            v_flex()
53                .gap_6()
54                .child(
55                    example_group(vec![single_example(
56                        "Simple",
57                        InlineCode::new("zed.dev").into_any_element(),
58                    )])
59                    .vertical(),
60                )
61                .into_any_element(),
62        )
63    }
64}