Skip to main content

guise/
text.rs

1//! `Text` — themed inline text with size, weight, and color controls.
2
3use gpui::prelude::*;
4use gpui::{div, px, App, FontWeight, IntoElement, SharedString, Window};
5
6use crate::theme::{theme, Color, Size};
7
8/// Themed body text. The Mantine `Text`.
9#[derive(IntoElement)]
10pub struct Text {
11    content: SharedString,
12    size: Size,
13    weight: FontWeight,
14    color: Option<Color>,
15    dimmed: bool,
16}
17
18impl Text {
19    pub fn new(content: impl Into<SharedString>) -> Self {
20        Text {
21            content: content.into(),
22            size: Size::Md,
23            weight: FontWeight::NORMAL,
24            color: None,
25            dimmed: false,
26        }
27    }
28
29    pub fn size(mut self, size: Size) -> Self {
30        self.size = size;
31        self
32    }
33
34    pub fn weight(mut self, weight: FontWeight) -> Self {
35        self.weight = weight;
36        self
37    }
38
39    /// Render at the medium font weight (500).
40    pub fn medium(self) -> Self {
41        self.weight(FontWeight::MEDIUM)
42    }
43
44    /// Render at the bold font weight (700).
45    pub fn bold(self) -> Self {
46        self.weight(FontWeight::BOLD)
47    }
48
49    /// Override the text color.
50    pub fn color(mut self, color: Color) -> Self {
51        self.color = Some(color);
52        self
53    }
54
55    /// Use the muted/secondary text color.
56    pub fn dimmed(mut self) -> Self {
57        self.dimmed = true;
58        self
59    }
60}
61
62impl RenderOnce for Text {
63    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
64        let t = theme(cx);
65        let color = match (self.color, self.dimmed) {
66            (Some(c), _) => c,
67            (None, true) => t.dimmed(),
68            (None, false) => t.text(),
69        };
70        div()
71            .text_size(px(t.font_size(self.size)))
72            .font_weight(self.weight)
73            .text_color(color.hsla())
74            .child(self.content)
75    }
76}