Skip to main content

theme/
selection.rs

1//! How a selected row is drawn against the focus.
2
3use gpui::{BoxShadow, Hsla};
4
5use crate::{paint, theme::Theme};
6
7/// A row's selected state. Emphasized while the area it is in holds the
8/// keyboard focus and unemphasized otherwise, as AppKit's `isEmphasized`.
9/// Both are neutral washes, never the accent.
10#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
11pub enum Selection {
12    #[default]
13    None,
14    Emphasized,
15    Unemphasized,
16}
17
18impl Selection {
19    pub fn new(selected: bool, emphasized: bool) -> Self {
20        match (selected, emphasized) {
21            (false, _) => Self::None,
22            (true, true) => Self::Emphasized,
23            (true, false) => Self::Unemphasized,
24        }
25    }
26
27    pub fn is_selected(self) -> bool {
28        self != Self::None
29    }
30
31    /// The row's fill: [`Theme::card_selected_bg`] emphasized,
32    /// [`Theme::element_hover`](Theme#structfield.element_hover) unemphasized.
33    pub fn fill(self, theme: &Theme) -> Option<Hsla> {
34        match self {
35            Self::None => None,
36            Self::Emphasized => Some(theme.card_selected_bg()),
37            Self::Unemphasized => Some(theme.element_hover),
38        }
39    }
40
41    /// The ring over the fill, for a row that carries one: emphasized only.
42    pub fn shadows(self) -> Vec<BoxShadow> {
43        match self {
44            Self::Emphasized => paint::glass_selected_shadows(),
45            Self::None | Self::Unemphasized => Vec::new(),
46        }
47    }
48}