bhoo/systems/material/
pull_refresh.rs1use crate::{Frame, PointerKind, Rect};
4
5#[derive(Default, Debug)]
8pub struct PullRefreshState {
9 origin: Option<f32>,
10 start_distance: f32,
11 distance: f32,
12 shown: f32,
13 last_time: Option<f64>,
14 dragging: bool,
15}
16impl PullRefreshState {
17 pub fn offset(&self) -> f32 { self.shown }
19 pub fn armed(&self) -> bool { self.distance * 0.5 > 80.0 }
21 fn cancel(&mut self) { self.origin = None; self.dragging = false; self.distance = 0.0; }
22}
23impl Frame<'_> {
24 pub fn pull_refresh_scroll(&mut self, id:u64, bounds:Rect, offset:&mut f32, content_height:f32, state:&mut PullRefreshState, refreshing:bool, build:impl FnOnce(&mut Self, f32)) -> bool {
28 let input=self.input;
29 let inside=self.pointer_in(bounds);
30 let reduced=self.reduced_motion();
31 let max=(content_height-bounds.height).max(0.0);
32 let wheel=self.scroll_y(bounds);
33 let scroll=self.ui.scrolls.entry(id).or_default();
34 if (scroll.offset-*offset).abs()>0.1 { scroll.cancel(); }
35 scroll.offset=offset.clamp(0.0,max);
36 if wheel!=0.0 { scroll.cancel(); scroll.offset=(scroll.offset+wheel).clamp(0.0,max); }
37 if input.pointer_kind==PointerKind::Touch {
38 if input.pressed && inside { scroll.begin(input.pointer.unwrap()[1],input.time);state.cancel(); }
39 if let Some(pointer)=input.pointer {
40 let mut distance=if refreshing { 0.0 } else { state.distance };
41 scroll.drag_with_refresh(pointer[1],input.time,max,&mut distance);
42 state.distance=if refreshing { 0.0 } else { distance };
43 state.dragging=state.distance>0.0;
44 }
45 }
46 let dragged=scroll.dragging;
47 let triggered=input.released && !refreshing && state.dragging && state.armed();
48 if input.released {
49 if state.distance>0.0 { scroll.cancel(); } else { scroll.release(input.time,reduced); }
50 state.cancel();
51 }
52 if input.cancelled || input.blurred || !self.enabled { scroll.cancel(); state.cancel(); }
53 let active=scroll.advance(input.time,max,reduced);
54 *offset=scroll.offset.clamp(0.0,max);
55 if active { self.continuous(bounds); }
56 if dragged { self.input.pressed=false;self.input.released=false;self.input.cancelled=true; }
57 self.within(bounds,|f| build(f,*offset));
58 self.paint_pull_refresh(bounds,state,refreshing);
59 triggered && !input.cancelled && !input.blurred && self.enabled
60 }
61 pub fn pull_to_refresh(&mut self, bounds: Rect, state: &mut PullRefreshState, at_top: bool, refreshing: bool) -> bool {
66 let input = self.input;
67 let mut triggered = false;
68 if input.cancelled || input.blurred || !self.enabled || refreshing || !at_top {
69 state.cancel();
70 } else {
71 if input.pointer_kind == PointerKind::Touch && input.pressed && self.pointer_in(bounds) {
72 state.origin = input.pointer.map(|p| p[1]);
73 state.start_distance = state.shown * 2.0;
74 }
75 if let (Some(origin), Some(pointer)) = (state.origin, input.pointer) {
76 let delta = pointer[1] - origin;
77 if delta > 6.0 { state.dragging = true; }
78 if state.dragging { state.distance = (state.start_distance + delta).max(0.0); }
79 }
80 if input.released {
81 triggered = state.dragging && state.armed();
82 if state.dragging { self.input.released = false; self.input.cancelled = true; }
83 state.cancel();
84 } else if state.dragging {
85 self.input.pressed = false;
86 self.input.cancelled = true;
87 }
88 }
89 self.paint_pull_refresh(bounds, state, refreshing);
90 triggered
91 }
92
93 fn paint_pull_refresh(&mut self, bounds: Rect, state: &mut PullRefreshState, refreshing: bool) {
94 let now = self.time();
95 let dt = state.last_time.map_or(0.0, |t| (now-t).clamp(0.0,0.05) as f32);
96 state.last_time = Some(now);
97 let target = if refreshing { 80.0 } else if state.dragging {
98 let adjusted = state.distance * 0.5;
99 if adjusted <= 80.0 { adjusted } else {
100 let tension = (adjusted / 80.0 - 1.0).clamp(0.0,2.0);
101 80.0 + 80.0 * (tension - tension*tension/4.0)
102 }
103 } else { 0.0 };
104 if state.dragging || self.reduced_motion() { state.shown = target; }
105 else if (state.shown-target).abs() > 0.1 {
106 state.shown += (target-state.shown) * (1.0-(-18.0*dt).exp());
107 self.continuous(bounds);
108 } else { state.shown = target; }
109 if state.shown > 0.5 {
110 let diameter = 40.0;
111 let circle = Rect { x:bounds.x+(bounds.width-diameter)*0.5, y:bounds.y+state.shown-diameter, width:diameter,height:diameter };
112 let theme = self.theme();
113 let fraction=state.shown/80.0;
114 let adjusted=(fraction.min(1.0)-0.4).max(0.0)*5.0/3.0;
115 let overshoot=(fraction.abs()-1.0).clamp(0.0,2.0);
116 let tension=overshoot-overshoot*overshoot/4.0;
117 let rotation=(-0.25+0.4*adjusted+tension)*0.5*std::f32::consts::TAU;
118 let sweep=adjusted*0.8*std::f32::consts::TAU;
119 let color=theme.on_surface_variant.opacity(if fraction >= 1.0 || refreshing { 1.0 } else { 0.3 });
120 let arc=Rect { x:circle.x+13.25,y:circle.y+13.25,width:13.5,height:13.5 };
121 let (start, busy_sweep) = if refreshing && self.continuous(circle) { crate::progress::classic_circular_sample(now) } else { (rotation*2.0,4.0) };
122 self.within(bounds, |f| {
123 f.paint_rect(circle,20.0,theme.surface_high);
124 if refreshing {
125 f.paint_arc(arc,start,busy_sweep,2.5,color);
126 } else if adjusted > 0.0 {
127 f.paint_arc(arc,rotation*2.0,sweep,2.5,color);
128 let angle=rotation*2.0+sweep-2.5_f32.to_radians();
129 let scale=adjusted.min(1.0);
130 let cx=circle.x+20.0; let cy=circle.y+20.0;
131 let rotate=|x:f32,y:f32| [cx+x*angle.cos()-y*angle.sin(),cy+x*angle.sin()+y*angle.cos()];
132 let half=5.0*scale;
133 let left=rotate(6.75-half,-2.5);
134 let tip=rotate(6.75,-2.5+5.0*scale);
135 let right=rotate(6.75+half,-2.5);
136 f.paint_line(left,tip,2.5,color);
137 f.paint_line(tip,right,2.5,color);
138 }
139 });
140 }
141 }
142}
143
144#[cfg(test)]
145#[path = "pull_refresh.spec.rs"]
146mod tests;