Skip to main content

guise/
mark.rs

1//! `Mark` — an inline highlighted span of text.
2//!
3//! ```ignore
4//! Group::new()
5//!     .gap(Size::Xs)
6//!     .child(Text::new("Highlight the"))
7//!     .child(Mark::new("important part"))
8//!     .child(Text::new("of a sentence."))
9//! ```
10
11use gpui::prelude::*;
12use gpui::{div, px, App, IntoElement, SharedString, Window};
13
14use crate::theme::{theme, ColorName, Size};
15
16/// A highlighter-pen span. The Mantine `Mark`.
17///
18/// Inherits the surrounding font size unless [`Mark::size`] is set, so it
19/// drops into a `Group` next to `Text` runs.
20#[derive(IntoElement)]
21pub struct Mark {
22    content: SharedString,
23    color: ColorName,
24    size: Option<Size>,
25}
26
27impl Mark {
28    pub fn new(content: impl Into<SharedString>) -> Self {
29        Mark {
30            content: content.into(),
31            color: ColorName::Yellow,
32            size: None,
33        }
34    }
35
36    /// The highlight tint (default `Yellow`).
37    pub fn color(mut self, color: ColorName) -> Self {
38        self.color = color;
39        self
40    }
41
42    /// Explicit font size; unset inherits from the parent.
43    pub fn size(mut self, size: Size) -> Self {
44        self.size = Some(size);
45        self
46    }
47}
48
49impl RenderOnce for Mark {
50    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
51        let t = theme(cx);
52        // Mantine's mark: a light shade-2 wash in light mode, a translucent
53        // shade-5 tint in dark mode; text stays the theme text color.
54        let bg = if t.scheme.is_dark() {
55            t.color(self.color, 5).alpha(0.35)
56        } else {
57            t.color(self.color, 2).hsla()
58        };
59        let fg = t.text().hsla();
60
61        let mut el = div()
62            .px(px(4.0))
63            .rounded(px(t.radius(Size::Xs)))
64            .bg(bg)
65            .text_color(fg)
66            .child(self.content);
67        if let Some(size) = self.size {
68            el = el.text_size(px(t.font_size(size)));
69        }
70        el
71    }
72}