Skip to main content

markdown/
comment.rs

1//! The washes behind comment ranges.
2
3use gpui::{App, Global, Hsla};
4use theme::Theme;
5
6/// The washes a comment's range is painted in, by its state.
7#[derive(Debug, Clone, Copy, PartialEq)]
8pub struct CommentWashes {
9    /// The colour the washes are cut from, at full strength.
10    pub color: Hsla,
11    /// A comment still open.
12    pub open: Hsla,
13    /// The comment the reader has in front of them.
14    pub active: Hsla,
15    /// A resolved comment.
16    pub resolved: Hsla,
17}
18
19impl CommentWashes {
20    /// The shipped strengths of `color`: 20% open, 38% active and 8% resolved.
21    pub fn of(color: Hsla) -> Self {
22        Self {
23            color,
24            open: color.opacity(0.20),
25            active: color.opacity(0.38),
26            resolved: color.opacity(0.08),
27        }
28    }
29}
30
31/// Returns the comment washes for the theme. Keep the active one visibly
32/// stronger than the open ones in both appearances.
33pub type CommentPaint = fn(theme: &Theme) -> CommentWashes;
34
35struct Installed(CommentPaint);
36
37impl Global for Installed {}
38
39/// Installs the comment washes for editors and previews. Without it they use
40/// [`default_comment`]. Call again to replace the paint when preferences
41/// change.
42pub(crate) fn set_comment_paint(cx: &mut App, paint: CommentPaint) {
43    cx.set_global(Installed(paint));
44}
45
46/// The shipped washes, cut from the theme's warning colour.
47pub fn default_comment(theme: &Theme) -> CommentWashes {
48    CommentWashes::of(theme.warning)
49}
50
51pub(crate) fn comment_paint_of(cx: &App) -> CommentPaint {
52    cx.try_global::<Installed>()
53        .map_or(default_comment, |installed| installed.0)
54}