Skip to main content

gpui_material_extras/search/
scroll.rs

1use gpui::{Context, Pixels, px};
2
3/// Retained Enter Always behavior for a standalone search bar.
4pub struct SearchScrollState {
5    supplied: Pixels,
6    last: Option<Pixels>,
7    hidden: Pixels,
8    limit: Pixels,
9}
10
11impl Default for SearchScrollState {
12    fn default() -> Self {
13        Self {
14            supplied: px(0.),
15            last: None,
16            hidden: px(0.),
17            limit: px(56.),
18        }
19    }
20}
21
22impl SearchScrollState {
23    /// Creates a scroll state starting fully visible (`hidden_fraction() == 0.0`).
24    pub fn new() -> Self {
25        Self::default()
26    }
27
28    /// Supplies a positive distance from the content's top. A linked handle takes precedence.
29    pub fn set_content_offset(&mut self, offset: Pixels, cx: &mut Context<Self>) {
30        let offset = super::presentation::length(offset);
31        if self.supplied != offset {
32            self.supplied = offset;
33            cx.notify();
34        }
35    }
36
37    /// Returns the fraction of the search bar currently hidden (`0.0` visible to `1.0` hidden).
38    pub fn hidden_fraction(&self) -> f32 {
39        if self.limit > px(0.) {
40            self.hidden / self.limit
41        } else {
42            0.
43        }
44    }
45
46    /// Sets the hidden fraction directly, clamped to `0.0..=1.0`.
47    pub fn set_hidden_fraction(&mut self, fraction: f32, cx: &mut Context<Self>) {
48        if fraction.is_finite() {
49            let next = self.limit * fraction.clamp(0., 1.);
50            if self.hidden != next {
51                self.hidden = next;
52                cx.notify();
53            }
54        }
55    }
56
57    /// Snaps the bar either fully visible or fully hidden based on scroll `velocity` or midpoint.
58    pub fn settle(&mut self, velocity: Pixels, cx: &mut Context<Self>) {
59        let hide = if velocity == px(0.) || !f32::from(velocity).is_finite() {
60            self.hidden_fraction() >= 0.5
61        } else {
62            velocity > px(0.)
63        };
64        self.set_hidden_fraction(f32::from(hide), cx);
65    }
66
67    pub(super) fn measure(
68        &mut self,
69        linked: Option<Pixels>,
70        height: Pixels,
71        held_open: bool,
72    ) -> Pixels {
73        let offset = linked.map_or(self.supplied, super::presentation::length);
74        let delta = match self.last.replace(offset) {
75            Some(previous) => offset - previous,
76            None => px(0.),
77        };
78        self.limit = height;
79        self.hidden = if offset == px(0.) && delta < px(0.) {
80            px(0.)
81        } else {
82            (self.hidden + delta).clamp(px(0.), height)
83        };
84        if held_open { px(0.) } else { self.hidden }
85    }
86}
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91
92    #[test]
93    fn initial_non_zero_offset_does_not_spuriously_hide_on_first_frame() {
94        let mut state = SearchScrollState::new();
95        assert_eq!(state.measure(Some(px(240.)), px(56.), false), px(0.));
96        assert_eq!(state.hidden_fraction(), 0.);
97        // Subsequent forward scroll hides by the delta from the initial baseline.
98        assert_eq!(state.measure(Some(px(268.)), px(56.), false), px(28.));
99        assert_eq!(state.hidden_fraction(), 0.5);
100        // Scrolling back reveals the bar.
101        assert_eq!(state.measure(Some(px(254.)), px(56.), false), px(14.));
102    }
103}