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#[derive(Clone, Debug)]
21pub struct DebugIndication {
22    pub press_color: crate::Color,
23    pub hover_color: crate::Color,
24    pub focus_color: crate::Color,
25}
26
27impl Default for DebugIndication {
28    fn default() -> Self {
29        Self {
30            press_color: crate::Color(0, 0, 255, 40),
31            hover_color: crate::Color(0, 0, 255, 20),
32            focus_color: crate::Color(0, 0, 255, 10),
33        }
34    }
35}
36
37impl Indication for DebugIndication {}
38
39impl IndicationNodeFactory for DebugIndication {
40    fn create(&self, interaction_source: &InteractionSource) -> Box<dyn IndicationDrawNode> {
41        Box::new(DebugIndicationDrawNode {
42            interaction_source: interaction_source.clone(),
43            press_color: self.press_color,
44            hover_color: self.hover_color,
45            focus_color: self.focus_color,
46        })
47    }
48}
49
50struct DebugIndicationDrawNode {
51    interaction_source: InteractionSource,
52    press_color: crate::Color,
53    hover_color: crate::Color,
54    focus_color: crate::Color,
55}
56
57impl IndicationDrawNode for DebugIndicationDrawNode {
58    fn draw(&self, scene: &mut Scene, rect: Rect, alpha: f32) {
59        let pressed = self.interaction_source.collect_is_pressed();
60        let hovered = self.interaction_source.collect_is_hovered();
61        let _focused = self.interaction_source.collect_is_focused();
62
63        // Draw press overlay
64        if pressed {
65            scene.nodes.push(crate::SceneNode::Rect {
66                rect,
67                brush: self
68                    .press_color
69                    .with_alpha_f32(self.press_color.3 as f32 / 255.0 * alpha)
70                    .into(),
71                radius: [0.0; 4],
72            });
73        }
74        // Draw hover overlay
75        if hovered && !pressed {
76            scene.nodes.push(crate::SceneNode::Rect {
77                rect,
78                brush: self
79                    .hover_color
80                    .with_alpha_f32(self.hover_color.3 as f32 / 255.0 * alpha)
81                    .into(),
82                radius: [0.0; 4],
83            });
84        }
85    }
86}