Skip to main content

cranpose_ui/
indication.rs

1//! How a control shows that a pointer rests on it or presses it: Compose's
2//! `Indication` and `LocalIndication`.
3//!
4//! Every [`clickable`](crate::Modifier::clickable) draws the indication that
5//! [`local_indication`] holds where the modifier was made. An app sets its own
6//! look once, at its root, and every button, row and toggle under it shows it.
7
8use cranpose_core::{CompositionLocal, compositionLocalOf, try_with_composer};
9use cranpose_ui_graphics::Color;
10
11/// What a control shows while a pointer rests on it and while one presses
12/// it: a color laid over the control's surface. Compose's `Indication`.
13///
14/// The default is Compose's `DefaultDebugIndication`: black at 10% while a
15/// pointer hovers and at 30% while one presses. A dark interface takes a
16/// light overlay with [`Indication::overlay`], and
17/// [`Indication::NONE`] shows nothing. The overlay covers the control's
18/// surface: the rect and corner shape of the background of the control's
19/// modifier chain, or the control's own rect when it has no background.
20#[derive(Clone, Copy, Debug, PartialEq)]
21pub struct Indication {
22    hovered: Color,
23    pressed: Color,
24}
25
26impl Indication {
27    /// Shows nothing: Compose's `indication = null`.
28    pub const NONE: Self = Self {
29        hovered: Color::TRANSPARENT,
30        pressed: Color::TRANSPARENT,
31    };
32
33    /// Lays `hovered` over the control while a pointer rests on it, and
34    /// `pressed` while one presses it.
35    pub const fn overlay(hovered: Color, pressed: Color) -> Self {
36        Self { hovered, pressed }
37    }
38
39    /// The color over a control that a pointer hovers, or `None` for no
40    /// overlay.
41    pub fn hovered(&self) -> Option<Color> {
42        visible(self.hovered)
43    }
44
45    /// The color over a control that a pointer presses, or `None` for no
46    /// overlay.
47    pub fn pressed(&self) -> Option<Color> {
48        visible(self.pressed)
49    }
50
51    /// The color over a control in `state`, or `None` while it shows
52    /// nothing. A press shows over a hover.
53    pub fn color(&self, state: IndicationState) -> Option<Color> {
54        match state {
55            IndicationState::Idle => None,
56            IndicationState::Hovered => self.hovered(),
57            IndicationState::Pressed => self.pressed(),
58        }
59    }
60}
61
62impl Default for Indication {
63    fn default() -> Self {
64        Self::overlay(
65            Color::from_rgba_u8(0, 0, 0, 26),
66            Color::from_rgba_u8(0, 0, 0, 77),
67        )
68    }
69}
70
71fn visible(color: Color) -> Option<Color> {
72    (color.3 > 0.0).then_some(color)
73}
74
75/// Where a control's interaction stands, as its indication shows it.
76#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
77pub enum IndicationState {
78    /// No pointer rests on the control or presses it.
79    #[default]
80    Idle,
81    /// A pointer rests on the control.
82    Hovered,
83    /// A pointer presses the control, and its release would click it.
84    Pressed,
85}
86
87/// The [`CompositionLocal`] that holds the [`Indication`] the clickables
88/// under it draw. Compose's `LocalIndication`.
89///
90/// ```ignore
91/// CompositionLocalProvider(
92///     [local_indication().provides(Indication::overlay(
93///         Color::from_rgba_u8(255, 255, 255, 30),
94///         Color::from_rgba_u8(255, 255, 255, 70),
95///     ))],
96///     || App(),
97/// );
98/// ```
99pub fn local_indication() -> CompositionLocal<Indication> {
100    crate::environment_locals::cached_local(
101        |locals| &locals.indication,
102        || compositionLocalOf(Indication::default),
103    )
104}
105
106// The indication that a clickable made now takes: the local's value in a
107// composition, and the default outside one.
108pub(crate) fn current_indication() -> Indication {
109    let local = local_indication();
110    try_with_composer(|composer| composer.read_composition_local(&local))
111        .unwrap_or_else(|| local.default_value())
112}
113
114#[cfg(test)]
115#[path = "tests/indication_tests.rs"]
116mod tests;