gpui_material_extras/search/
scroll.rs1use gpui::{Context, Pixels, px};
2
3pub 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 pub fn new() -> Self {
25 Self::default()
26 }
27
28 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 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 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 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 = offset - self.last.replace(offset).unwrap_or(px(0.));
75 self.limit = height;
76 self.hidden = if offset == px(0.) && delta < px(0.) {
77 px(0.)
78 } else {
79 (self.hidden + delta).clamp(px(0.), height)
80 };
81 if held_open { px(0.) } else { self.hidden }
82 }
83}