Skip to main content

repose_core/
indication.rs

1use crate::{InteractionSource, Rect, Scene};
2
3/// Marker trait for indication implementations.
4pub trait Indication: std::fmt::Debug {}
5
6/// Factory that creates a drawable indication node bound to an interaction source.
7pub trait IndicationNodeFactory: Indication {
8    fn create(&self, interaction_source: &InteractionSource) -> Box<dyn IndicationDrawNode>;
9}
10
11/// A drawable indication node. The layout engine calls `draw()` during the paint
12/// pass to emit scene nodes for visual feedback (ripple, overlay, focus ring).
13pub trait IndicationDrawNode {
14    /// Draw the indication into `scene` at the given `rect` (in physical pixels).
15    /// `alpha` is the accumulated compositing alpha from ancestor modifiers.
16    fn draw(&self, scene: &mut Scene, rect: Rect, alpha: f32);
17}
18
19/// A debug indication that draws colored overlays for hover/press/focus.
20#[deprecated]
21#[derive(Clone, Debug)]
22pub struct DebugIndication {
23    pub press_color: crate::Color,
24    pub hover_color: crate::Color,
25    pub focus_color: crate::Color,
26}
27
28impl Default for DebugIndication {
29    fn default() -> Self {
30        Self {
31            press_color: crate::Color(0, 0, 255, 40),
32            hover_color: crate::Color::TRANSPARENT,
33            focus_color: crate::Color::TRANSPARENT,
34        }
35    }
36}
37
38impl Indication for DebugIndication {}
39
40impl IndicationNodeFactory for DebugIndication {
41    fn create(&self, interaction_source: &InteractionSource) -> Box<dyn IndicationDrawNode> {
42        Box::new(DebugIndicationDrawNode {
43            interaction_source: interaction_source.clone(),
44            press_color: self.press_color,
45            hover_color: self.hover_color,
46            focus_color: self.focus_color,
47        })
48    }
49}
50
51struct DebugIndicationDrawNode {
52    interaction_source: InteractionSource,
53    press_color: crate::Color,
54    hover_color: crate::Color,
55    focus_color: crate::Color,
56}
57
58impl IndicationDrawNode for DebugIndicationDrawNode {
59    fn draw(&self, scene: &mut Scene, rect: Rect, alpha: f32) {
60        let pressed = self.interaction_source.collect_is_pressed();
61        let hovered = self.interaction_source.collect_is_hovered();
62        let _focused = self.interaction_source.collect_is_focused();
63
64        // Draw press overlay
65        if pressed {
66            scene.nodes.push(crate::SceneNode::Rect {
67                rect,
68                brush: self
69                    .press_color
70                    .with_alpha_f32(self.press_color.3 as f32 / 255.0 * alpha)
71                    .into(),
72                radius: [0.0; 4],
73            });
74        }
75        // Draw hover overlay
76        if hovered && !pressed {
77            scene.nodes.push(crate::SceneNode::Rect {
78                rect,
79                brush: self
80                    .hover_color
81                    .with_alpha_f32(self.hover_color.3 as f32 / 255.0 * alpha)
82                    .into(),
83                radius: [0.0; 4],
84            });
85        }
86    }
87}