-
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathScrollView.re
More file actions
294 lines (255 loc) · 9.19 KB
/
ScrollView.re
File metadata and controls
294 lines (255 loc) · 9.19 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
open Revery_Core;
open Revery_UI;
open Revery_UI.Transform;
open Revery_UI_Primitives;
module Hooks = Revery_UI_Hooks;
module Log = (val Log.withNamespace("Revery.ScrollView"));
type bouncingState =
| Bouncing(int)
| Idle;
let empty = React.empty;
let scrollTrackColor = Color.rgba(0.0, 0.0, 0.0, 0.4);
let scrollThumbColor = Color.rgba(0.5, 0.5, 0.5, 0.4);
let defaultBounce = Environment.os === Mac ? true : false;
type action =
| ScrollUpdated(int);
let reducer = (action, _state) => {
switch (action) {
| ScrollUpdated(v) => v
};
};
let bounceAnimation = (~origin, ~force) =>
Animation.(
{
let bounceAway =
animate(Time.ms(100))
|> ease(Easing.cubicBezier(0.23, 1., 0.32, 1.))
|> tween(float(origin), float(origin + force));
let bounceBack =
Animation.animate(Time.ms(800))
|> ease(Easing.cubicBezier(0.23, 1., 0.32, 1.))
|> tween(float(origin + force), float(origin));
bounceAway |> andThen(~next=bounceBack) |> map(int_of_float);
}
);
let%component make =
(
~style,
~scrollLeft=0,
~scrollTop=0,
~bounce=defaultBounce,
~children=React.empty,
(),
) => {
let%hook (actualScrollTop, dispatch) =
Hooks.reducer(~initialState=scrollTop, reducer);
let%hook (outerRef: option(Revery_UI.node), setOuterRef) =
Hooks.state(None);
let%hook (actualScrollLeft, setScrollLeft) = Hooks.state(scrollLeft);
let%hook (bouncingState, setBouncingState) = Hooks.state(Idle);
//let%hook (scrollview, setScrollview) = Hooks.state(() => Libscroll.scrollview_new());
//setScrollview(_ => scrollview);
let%hook (scrollViewRef) = Hooks.ref(None);
let%hook () = Hooks.effect(OnMount, () => {
let scrollView = Libscroll.scrollview_new();
scrollViewRef := Some(scrollView);
let dispose = () => {
scrollViewRef := None;
};
Some(dispose);
});
let%hook (actualScrollTop, _bounceAnimationState, resetBouncingAnimation) =
switch (bouncingState) {
| Idle =>
// TODO: Why isn't Animation.const always sufficient to stop the timer?
Hooks.animation(~active=false, Animation.const(actualScrollTop))
| Bouncing(force) =>
Hooks.animation(
bounceAnimation(~origin=actualScrollTop, ~force), ~onComplete=() =>
setBouncingState(_ => Idle)
)
};
let setBouncingState = state => {
resetBouncingAnimation();
setBouncingState(state);
};
let scrollBarThickness = 10;
let innerViewTransform = [
TranslateX((-1.) *. float_of_int(actualScrollLeft)),
TranslateY((-1.) *. float_of_int(actualScrollTop)),
];
let (horizontalScrollBar, verticalScrollBar, scroll) =
switch (outerRef) {
| Some(outer) =>
let inner = outer#firstChild();
let childMeasurements = inner#measurements();
let outerMeasurements = outer#measurements();
let maxHeight =
max(0, childMeasurements.height - outerMeasurements.height);
let maxWidth = childMeasurements.width - outerMeasurements.width;
/*
* TODO: #287
* prerr_endline ("Child width: " ++ string_of_int(childMeasurements.width));
* prerr_endline ("Container width: " ++ string_of_int(outerMeasurements.width));
* This can be removed once #287 is fixed
*/
let verticalThumbHeight =
childMeasurements.height > 0
? outerMeasurements.height
* outerMeasurements.height
/ childMeasurements.height
: 1;
let horizontalThumbHeight =
childMeasurements.width > 0
? outerMeasurements.width
* outerMeasurements.width
/ childMeasurements.width
: 1;
let isVerticalScrollbarVisible = maxHeight > 0;
let isHorizontalScrollbarVisible = maxWidth > 0;
let verticalScrollBar =
isVerticalScrollbarVisible
? <Slider
onValueChanged={v => dispatch(ScrollUpdated(int_of_float(v)))}
minimumValue=0.
maximumValue={float_of_int(maxHeight)}
sliderLength={outerMeasurements.height}
thumbLength=verticalThumbHeight
value={float_of_int(actualScrollTop)}
trackThickness=scrollBarThickness
thumbThickness=scrollBarThickness
minimumTrackColor=scrollTrackColor
maximumTrackColor=scrollTrackColor
thumbColor=scrollThumbColor
vertical=true
/>
: empty;
/* TODO: #287
* Need to investigate why the child width is not being reported (expanded) correctly.
* Currently, the child width is clamped to the parent.
* Is this a bug in flex?
* Or something we need to fix in our styling?
*/
let horizontalScrollbar =
isHorizontalScrollbarVisible
? <Slider
onValueChanged={v => setScrollLeft(_ => - int_of_float(v))}
minimumValue=0.
maximumValue={float_of_int(maxWidth)}
sliderLength={outerMeasurements.width}
thumbLength=horizontalThumbHeight
trackThickness=scrollBarThickness
thumbThickness=scrollBarThickness
minimumTrackColor=scrollTrackColor
maximumTrackColor=scrollTrackColor
thumbColor=scrollThumbColor
/>
: empty;
/*let pan = (panEvent: NodeEvents.panEventParams) => {
switch (scrollViewRef^) {
| None => ()
| Some(scrollview) => {
let timestamp = wheelEvent.timestamp;
let delta = wheelEvent.delta;
let axis = wheelEvent.axis;
}
}*/
let scrollForFrameFlip = (timestamp: int) => {
// call into libscroll to sample new position
}
let scroll = (wheelEvent: NodeEvents.mouseWheelEventParams) => {
switch (scrollViewRef^) {
| Some(scrollview) => {//Libscroll.push_pan(scrollview, Libscroll.Axis.Vertical, 10.0, 0)
Log.info("Scrollview existed");
Libscroll.set_source(scrollview, wheelEvent.source);
switch(wheelEvent.deltaX) {
| Some(delta) => {
Libscroll.push_pan(scrollview, Libscroll.Axis.Horizontal, delta, wheelEvent.timestamp);
}
| None => ()
};
switch(wheelEvent.deltaY) {
| Some(delta) => {
Libscroll.push_pan(scrollview, Libscroll.Axis.Vertical, delta, wheelEvent.timestamp);
}
| None => ()
};
switch(wheelEvent.isFling) {
| true => Libscroll.push_fling(scrollview, wheelEvent.timestamp);
| false => ()
};
switch(wheelEvent.isInterrupt) {
| true => Libscroll.push_interrupt(scrollview, wheelEvent.timestamp);
| false => ()
};
}
| None => Log.error("Scrollview not present on event dispatch");
}
let delta = switch(wheelEvent.deltaY) {
| Some(value) => value *. 25.
| None => 0.0
};
//let delta = int_of_float(wheelEvent.deltaY *. 25.);
let delta_s = delta;
let delta = int_of_float(delta /. -400.0);
let newScrollTop = actualScrollTop - delta;
let isAtTop = newScrollTop < 0;
let isAtBottom = newScrollTop > maxHeight;
switch (bouncingState) {
| Bouncing(force) when force < 0 && delta_s < 0. =>
setBouncingState(_ => Idle)
| Bouncing(force) when force > 0 && delta_s > 0. =>
setBouncingState(_ => Idle)
| Bouncing(_) => ()
| Idle when !bounce && (isAtTop || isAtBottom) =>
dispatch(ScrollUpdated(newScrollTop));
//let clampedScrollTop = isAtTop ? 0 : maxHeight;
//dispatch(ScrollUpdated(clampedScrollTop));
()
| Idle when bounce && (isAtTop || isAtBottom) =>
//setBouncingState(_ => Bouncing(- delta * 2));
dispatch(ScrollUpdated(isAtTop ? 0 : maxHeight));
| Idle => dispatch(ScrollUpdated(newScrollTop))
};
};
(horizontalScrollbar, verticalScrollBar, scroll);
| _ => (empty, empty, (_ => ()))
};
let innerStyle =
Style.[
transform(innerViewTransform),
position(`Absolute),
top(0),
/* TODO: #287 This styling will need to be adjusted to handle horizontal scrolling */
left(0),
right(verticalScrollBar == empty ? 0 : scrollBarThickness),
];
let verticalScrollbarContainerStyle =
Style.[
position(`Absolute),
right(0),
top(0),
bottom(0),
width(scrollBarThickness),
];
let horizontalScrollbarContainerStyle =
Style.[
position(`Absolute),
right(0),
left(0),
bottom(0),
height(scrollBarThickness),
];
<View style>
<View
onMouseWheel=scroll
ref={r => setOuterRef(_ => Some(r))}
style=Style.[flexGrow(1), position(`Relative), overflow(`Scroll)]>
<View style=innerStyle> children </View>
<View style=verticalScrollbarContainerStyle> verticalScrollBar </View>
<View style=horizontalScrollbarContainerStyle>
horizontalScrollBar
</View>
</View>
</View>;
};