1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#[allow(unused)]
use crate::*;
use pax_engine::api::{Event, Wheel};
use pax_engine::api::{Property, Size};
use pax_engine::*;
use pax_runtime::api::{NodeContext, TouchEnd, TouchMove, TouchStart, OS};
use std::cell::RefCell;
use std::collections::HashMap;

/// A scrolling container for arbitrary content.
#[pax]
#[engine_import_path("pax_engine")]
#[inlined(
    <Frame _clip_content={self._clip_content}>
        <Scrollbar
            scroll_y=bind:scroll_pos_y
            width=20px
            anchor_x=100%
            x=100%
            size_inner_pane_y={self.scroll_height}
        />
        <Scrollbar
            scroll_x=bind:scroll_pos_x
            height=20px
            anchor_y=100%
            y=100%
            size_inner_pane_x={self.scroll_width}
        />
        <Group x={(-self.scroll_pos_x)px} y={(-self.scroll_pos_y)px} width={self.scroll_width} height={self.scroll_height}>
            for i in 0..self._slot_children_count {
                slot(i)
            }
        </Group>
        <Rectangle fill=TRANSPARENT/>
    </Frame>

    @settings {
        @mount: on_mount
        @wheel: handle_wheel
        @pre_render: update,
        @touch_move: touch_move,
        @touch_start: touch_start,
        @touch_end: touch_end,
    }

)]
#[custom(Default)]
pub struct Scroller {
    pub scroll_pos_x: Property<f64>,
    pub scroll_pos_y: Property<f64>,
    pub scroll_width: Property<Size>,
    pub scroll_height: Property<Size>,

    // used by pax create (might want to just make public at some point)
    pub _clip_content: Property<bool>,

    // private fields
    pub _platform_params: Property<PlatformSpecificScrollParams>,
    pub _momentum_x: Property<f64>,
    pub _momentum_y: Property<f64>,
    pub _damping: Property<f64>,
    pub _slot_children_count: Property<usize>,
}

impl Default for Scroller {
    fn default() -> Self {
        Self {
            scroll_pos_x: Default::default(),
            scroll_pos_y: Default::default(),
            scroll_width: Default::default(),
            scroll_height: Default::default(),
            _clip_content: Property::new(true),
            _platform_params: Default::default(),
            _momentum_x: Default::default(),
            _momentum_y: Default::default(),
            _damping: Default::default(),
            _slot_children_count: Default::default(),
        }
    }
}

#[pax]
#[engine_import_path("pax_engine")]
pub struct PlatformSpecificScrollParams {
    // constant decrease in momentum over time
    pub deacceleration: f64,
    // friction (decreases momentum proportionally to current value)
    pub damping: f64,
    // if user "flings", set to high momentum to quickly scroll
    pub fling: bool,
}

pub struct TouchInfo {
    x: f64,
    y: f64,
}

thread_local! {
    static TOUCH_TRACKER: RefCell<HashMap<i64, TouchInfo>> = RefCell::new(HashMap::new());
}
pub fn no_touches() -> bool {
    TOUCH_TRACKER.with_borrow(|touches| touches.len() == 0)
}

impl Scroller {
    pub fn on_mount(&mut self, ctx: &NodeContext) {
        let slot_children_count = ctx.slot_children_count.clone();
        let deps = [slot_children_count.untyped()];
        self._slot_children_count
            .replace_with(Property::computed(move || slot_children_count.get(), &deps));
        let scroll_params = match ctx.os {
            OS::Android => PlatformSpecificScrollParams {
                deacceleration: 0.02,
                damping: 0.03,
                fling: true,
            },
            OS::IPhone => PlatformSpecificScrollParams {
                deacceleration: 0.00,
                damping: 0.04,
                fling: false,
            },
            // just choose some hopefully sane default
            OS::Windows | OS::Mac | OS::Linux | OS::Unknown => PlatformSpecificScrollParams {
                deacceleration: 0.01,
                damping: 0.04,
                fling: false,
            },
        };
        self._platform_params.set(scroll_params);
    }

    pub fn update(&mut self, ctx: &NodeContext) {
        let mom_x = self._momentum_x.get();
        let mom_y = self._momentum_y.get();
        if no_touches() {
            self.add_position(ctx, mom_x, mom_y);
        }
        let platform_data = self._platform_params.get();
        let damping = self._damping.get();

        let mut new_mom_x;
        let mut new_mom_y;

        // damping
        let falloff_factor = 1.0 - damping;
        new_mom_x = mom_x * falloff_factor;
        new_mom_y = mom_y * falloff_factor;

        // decelleration
        if new_mom_x > 0.0 {
            new_mom_x = (new_mom_x - platform_data.deacceleration).max(0.0);
        }
        if new_mom_x < 0.0 {
            new_mom_x = (new_mom_x + platform_data.deacceleration).min(0.0);
        }
        if new_mom_y > 0.0 {
            new_mom_y = (new_mom_y - platform_data.deacceleration).max(0.0);
        }
        if new_mom_y < 0.0 {
            new_mom_y = (new_mom_y + platform_data.deacceleration).min(0.0);
        }

        // stop if close to 0
        if new_mom_x.abs() < 0.1 {
            new_mom_x = 0.0;
        }
        if new_mom_y.abs() < 0.1 {
            new_mom_y = 0.0;
        }

        self._momentum_x.set(new_mom_x);
        self._momentum_y.set(new_mom_y);
    }

    pub fn add_position(&self, ctx: &NodeContext, dx: f64, dy: f64) {
        let (bounds_x, bounds_y) = ctx.bounds_self.get();
        let (max_bounds_x, max_bounds_y) = (
            self.scroll_width.get().get_pixels(bounds_x),
            self.scroll_height.get().get_pixels(bounds_y),
        );
        let old_x = self.scroll_pos_x.get();
        let old_y = self.scroll_pos_y.get();
        let target_x = old_x + dx;
        let target_y = old_y + dy;
        let clamped_target_x = target_x.clamp(0.0, (max_bounds_x - bounds_x).max(0.0));
        let clamped_target_y = target_y.clamp(0.0, (max_bounds_y - bounds_y).max(0.0));
        self.scroll_pos_x.set(clamped_target_x);
        self.scroll_pos_y.set(clamped_target_y);
    }

    pub fn add_momentum(&self, ddx: f64, ddy: f64) {
        let mom_x = self._momentum_x.get();
        let mom_y = self._momentum_y.get();
        self._momentum_x.set(mom_x + ddx);
        self._momentum_y.set(mom_y + ddy);
    }

    pub fn process_new_touch_pos(&self, ctx: &NodeContext, x: f64, y: f64, ident: i64) {
        TOUCH_TRACKER.with_borrow_mut(|touches| {
            let last = touches
                .get_mut(&ident)
                .expect("should have recieved touch down before touch move");
            let delta_x = last.x - x;
            let delta_y = last.y - y;
            last.x = x;
            last.y = y;
            self.add_position(ctx, delta_x, delta_y);
            self.add_momentum(delta_x, delta_y);
        });
    }

    pub fn handle_wheel(&mut self, ctx: &NodeContext, args: Event<Wheel>) {
        self.add_position(ctx, args.delta_x, args.delta_y);
    }

    pub fn touch_move(&mut self, ctx: &NodeContext, args: Event<TouchMove>) {
        for touch in &args.touches {
            self.process_new_touch_pos(ctx, touch.x, touch.y, touch.identifier);
        }
    }

    pub fn touch_start(&mut self, _ctx: &NodeContext, args: Event<TouchStart>) {
        if no_touches() {
            // this is first touch
            let cached_damping = self._platform_params.get().damping;
            let temp_damping = cached_damping.max(0.5);
            self._damping.set(temp_damping);
        }
        self._momentum_x.set(0.0);
        self._momentum_y.set(0.0);
        TOUCH_TRACKER.with_borrow_mut(|touches| {
            touches.extend(
                args.touches
                    .iter()
                    .map(|e| (e.identifier, TouchInfo { x: e.x, y: e.y })),
            );
        });
    }

    pub fn touch_end(&mut self, ctx: &NodeContext, args: Event<TouchEnd>) {
        for touch in &args.touches {
            self.process_new_touch_pos(ctx, touch.x, touch.y, touch.identifier);
        }
        TOUCH_TRACKER.with_borrow_mut(|touches| {
            let idents: Vec<_> = args.touches.iter().map(|e| e.identifier).collect();
            touches.retain(|k, _| !idents.contains(k));
        });
        if no_touches() {
            let params = self._platform_params.get();
            self._damping.set(params.damping);

            let mut mom_x = self._momentum_x.get();
            let mut mom_y = self._momentum_y.get();
            if params.fling && mom_x.abs() > 50.0 {
                mom_x *= 1.7;
            }
            if params.fling && mom_y.abs() > 50.0 {
                mom_y *= 1.7;
            }
            self._momentum_x.set(mom_x);
            self._momentum_y.set(mom_y);
        }
    }
}