Skip to main content

slt/widgets/
responses.rs

1// Widget response types introduced in v0.20.0.
2//
3// These wrap a [`Response`] alongside widget-specific interaction data
4// (clicked segment index, drag state, search highlight info). Each implements
5// [`Deref<Target = Response>`] so callers can read the standard `hovered`,
6// `clicked`, `rect`, and `focused` fields without explicit field navigation.
7
8/// Response from [`Context::breadcrumb`](crate::Context::breadcrumb).
9///
10/// Wraps the row-level [`Response`] and exposes the index of the clicked
11/// segment (if any). Implements `Deref<Target = Response>` so `r.hovered`,
12/// `r.rect`, etc. work directly.
13///
14/// # Example
15///
16/// ```no_run
17/// # slt::run(|ui: &mut slt::Context| {
18/// let r = ui.breadcrumb(&["Home", "Settings", "Profile"]).show();
19/// if let Some(i) = r.clicked_segment {
20///     // navigate to segment `i`
21/// }
22/// if r.hovered {
23///     // whole bar hovered
24/// }
25/// # });
26/// ```
27#[derive(Debug, Clone, Default)]
28#[must_use = "BreadcrumbResponse contains interaction state — check .clicked_segment, .hovered, or .rect"]
29pub struct BreadcrumbResponse {
30    /// The row-level interaction response (hover, rect, focus).
31    pub response: Response,
32    /// Index of the clicked segment, if any.
33    pub clicked_segment: Option<usize>,
34}
35
36impl std::ops::Deref for BreadcrumbResponse {
37    type Target = Response;
38    fn deref(&self) -> &Response {
39        &self.response
40    }
41}
42
43/// Response from [`Context::gauge`](crate::Context::gauge) and
44/// [`Context::line_gauge`](crate::Context::line_gauge).
45///
46/// Wraps the row-level [`Response`] plus the rendered ratio so callers can
47/// confirm the displayed value (clamped to `0.0..=1.0`). Implements
48/// `Deref<Target = Response>` so `r.hovered` etc. work directly.
49///
50/// Note: `ratio` was widened from `f32` to `f64` in v0.20.0 so the gauge
51/// family aligns with `animate_value`, chart APIs, and `progress_bar`.
52#[derive(Debug, Clone, Default)]
53#[must_use = "GaugeResponse contains interaction state — check .hovered or .ratio"]
54pub struct GaugeResponse {
55    /// The row-level interaction response.
56    pub response: Response,
57    /// The clamped ratio that was rendered (always `0.0..=1.0`).
58    pub ratio: f64,
59}
60
61impl std::ops::Deref for GaugeResponse {
62    type Target = Response;
63    fn deref(&self) -> &Response {
64        &self.response
65    }
66}
67
68/// Response from [`Context::split_pane`](crate::Context::split_pane) and
69/// [`Context::vsplit_pane`](crate::Context::vsplit_pane).
70///
71/// Wraps the row-level [`Response`] of the outer container plus drag state.
72/// Implements `Deref<Target = Response>` so `r.hovered` etc. work directly.
73#[derive(Debug, Clone, Default)]
74#[must_use = "SplitPaneResponse contains interaction state — check .ratio, .drag_active, or .hovered"]
75pub struct SplitPaneResponse {
76    /// The row/column-level interaction response.
77    pub response: Response,
78    /// Current ratio after this frame's interaction (mirrors `state.ratio`).
79    pub ratio: f64,
80    /// Whether the handle was actively being dragged this frame.
81    pub drag_active: bool,
82}
83
84impl std::ops::Deref for SplitPaneResponse {
85    type Target = Response;
86    fn deref(&self) -> &Response {
87        &self.response
88    }
89}
90
91/// Response from [`Context::scrollable_with_gutter`](crate::Context::scrollable_with_gutter).
92///
93/// Wraps the [`Response`] for the scrollable region plus search-result
94/// metadata: the index of the currently focused highlight (if any) and the
95/// total count of registered highlights.
96#[derive(Debug, Clone, Default)]
97#[must_use = "GutterResponse contains interaction state — check .current_highlight or .hovered"]
98pub struct GutterResponse {
99    /// The scrollable region's interaction response.
100    pub response: Response,
101    /// Index of the currently focused highlight, if any.
102    pub current_highlight: Option<usize>,
103    /// Total number of active highlights.
104    pub total_highlights: usize,
105}
106
107impl std::ops::Deref for GutterResponse {
108    type Target = Response;
109    fn deref(&self) -> &Response {
110        &self.response
111    }
112}