@@ -4,27 +4,22 @@ import {
4
4
GestureState ,
5
5
GestureStateEventData ,
6
6
GestureTouchEventData ,
7
- HandlerType ,
8
- Manager ,
7
+ HandlerType , install as installGestures , Manager ,
9
8
PanGestureHandler ,
10
- PanGestureHandlerOptions ,
11
- install as installGestures
9
+ PanGestureHandlerOptions
12
10
} from '@nativescript-community/gesturehandler' ;
13
11
import {
12
+ AbsoluteLayout ,
14
13
Animation ,
15
- AnimationDefinition ,
16
- CSSType ,
17
- Color ,
18
- CoreTypes ,
19
- EventData ,
14
+ AnimationDefinition , booleanConverter , Color ,
15
+ CoreTypes , CSSType , EventData ,
20
16
GridLayout ,
21
17
Property ,
22
18
ScrollEventData ,
23
19
ScrollView ,
24
20
TouchGestureEventData ,
25
21
Utils ,
26
- View ,
27
- booleanConverter
22
+ View
28
23
} from '@nativescript/core' ;
29
24
const OPEN_DURATION = 200 ;
30
25
const CLOSE_DURATION = 200 ;
@@ -58,7 +53,7 @@ export const bottomSheetProperty = new Property<PersistentBottomSheet, View>({
58
53
name : 'bottomSheet' ,
59
54
defaultValue : undefined ,
60
55
valueChanged : ( target , oldValue , newValue ) => {
61
- ( target as any ) . _onBottomSheetChanged ( oldValue , newValue ) ;
56
+ target . _onBottomSheetChanged ( oldValue , newValue ) ;
62
57
}
63
58
} ) ;
64
59
export const gestureEnabledProperty = new Property < PersistentBottomSheet , boolean > ( {
@@ -83,7 +78,7 @@ export const translationFunctionProperty = new Property<PersistentBottomSheet, F
83
78
} ) ;
84
79
85
80
@CSSType ( 'PersistentBottomSheet' )
86
- export class PersistentBottomSheet extends GridLayout {
81
+ export class PersistentBottomSheet extends AbsoluteLayout {
87
82
public bottomSheet : View ;
88
83
public scrollViewId : string ;
89
84
// isPanning = false;
@@ -98,7 +93,7 @@ export class PersistentBottomSheet extends GridLayout {
98
93
private isAnimating = false ;
99
94
private prevDeltaY = 0 ;
100
95
private viewHeight = 0 ;
101
- private bottomViewHeight = 0 ;
96
+ // private bottomViewHeight = 0;
102
97
103
98
private lastScrollY : number ;
104
99
private lastTouchY : number ;
@@ -150,8 +145,7 @@ export class PersistentBottomSheet extends GridLayout {
150
145
deltaY -= Utils . layout . toDeviceIndependentPixels ( this . getSafeAreaInsets ( ) . top ) ;
151
146
}
152
147
const y = data . y + deltaY ;
153
- // console.log('shouldStartGesture ', safeAreatop, data, y, this.viewHeight - (this.translationMaxOffset - this.translationY), this.translationY, this.translationMaxOffset, this.viewHeight);
154
- if ( y < this . viewHeight - ( this . bottomViewHeight - this . translationY ) ) {
148
+ if ( y < this . viewHeight + this . translationY ) {
155
149
return false ;
156
150
}
157
151
if ( this . _scrollView ) {
@@ -162,27 +156,10 @@ export class PersistentBottomSheet extends GridLayout {
162
156
}
163
157
return true ;
164
158
}
165
- // initNativeGestureHandler(newValue: View) {
166
- // // console.log('initNativeGestureHandler', newValue);
167
- // if (!this.nativeGestureHandler) {
168
- // const manager = Manager.getInstance();
169
- // const gestureHandler = manager.createGestureHandler(HandlerType.NATIVE_VIEW, NATIVE_GESTURE_TAG, {
170
- // shouldActivateOnStart: true,
171
- // shouldCancelWhenOutside: false,
172
- // // simultaneousHandlers: [PAN_GESTURE_TAG],
173
- // });
174
- // // gestureHandler.on(GestureHandlerStateEvent, this.onNativeGestureState, this);
175
- // this.nativeGestureHandler = gestureHandler as any;
176
- // }
177
- // if (this.nativeGestureHandler.getView() !== newValue) {
178
- // // this.nativeGestureHandler.attachToView(newValue);
179
- // }
180
- // }
181
159
get translationY ( ) {
182
160
return this . _translationY ;
183
161
}
184
162
set translationY ( value : number ) {
185
- // console.log('set translationY', value)
186
163
if ( this . _translationY !== - 1 ) {
187
164
this . isScrollEnabled = value === 0 ;
188
165
}
@@ -230,10 +207,10 @@ export class PersistentBottomSheet extends GridLayout {
230
207
}
231
208
}
232
209
[ stepIndexProperty . setNative ] ( value : number ) {
233
- if ( this . viewHeight !== 0 ) {
234
- // we are layed out
235
- this . animateToPosition ( this . steps [ value ] ) ;
236
- }
210
+ // if (this.viewHeight !== 0) {
211
+ // we are layed out
212
+ this . animateToPosition ( this . steps [ value ] ) ;
213
+ // }
237
214
}
238
215
[ backdropColorProperty . setNative ] ( value : Color ) {
239
216
if ( ! this . backDrop && this . bottomSheet ) {
@@ -242,7 +219,6 @@ export class PersistentBottomSheet extends GridLayout {
242
219
}
243
220
}
244
221
protected addBackdropView ( index : number ) {
245
- // console.log('addBackdropView', index);
246
222
this . backDrop = new GridLayout ( ) ;
247
223
this . backDrop . backgroundColor = this . backdropColor ;
248
224
this . backDrop . opacity = 0 ;
@@ -287,14 +263,24 @@ export class PersistentBottomSheet extends GridLayout {
287
263
}
288
264
}
289
265
290
- private _onBottomSheetChanged ( oldValue : View , newValue : View ) {
266
+ _onBottomSheetChanged ( oldValue : View , newValue : View ) {
291
267
if ( oldValue ) {
292
268
this . removeChild ( oldValue ) ;
293
269
}
294
270
if ( newValue ) {
295
271
newValue . iosOverflowSafeAreaEnabled = false ;
296
- newValue . verticalAlignment = 'bottom' ;
297
- newValue . on ( 'layoutChanged' , this . onBottomLayoutChange , this ) ;
272
+ if ( ! newValue . width ) {
273
+ newValue . width = {
274
+ unit : '%' ,
275
+ value : 100
276
+ } ;
277
+ }
278
+ // newValue.top = {
279
+ // unit: 'px',
280
+ // value: this.viewHeight
281
+ // };
282
+ // newValue.verticalAlignment = 'bottom';
283
+ // newValue.on('layoutChanged', this.onBottomLayoutChange, this);
298
284
let index ;
299
285
if ( ! newValue . parent ) {
300
286
index = this . getChildrenCount ( ) ;
@@ -311,19 +297,18 @@ export class PersistentBottomSheet extends GridLayout {
311
297
}
312
298
}
313
299
314
- computeTranslationData ( height ) {
300
+ computeTranslationData ( ty ) {
315
301
const max = this . translationMaxOffset ;
316
- const diff = height - max ;
317
302
let value = this . _translationY ;
318
- const progress = 1 - ( this . _translationY - diff ) / max ;
319
-
320
- if ( global . isIOS && progress === 0 && ! this . iosIgnoreSafeArea ) {
303
+ const diff = max - ty ;
304
+ const progress = ty / max ;
305
+ if ( __IOS__ && progress === 0 && ! this . iosIgnoreSafeArea ) {
321
306
// if this is the 0 steop ensure it gets hidden even with safeArea
322
307
const safeArea = this . getSafeAreaInsets ( ) ;
323
308
value += Utils . layout . toDeviceIndependentPixels ( safeArea . bottom ) ;
324
309
}
325
310
if ( this . translationFunction ) {
326
- return this . translationFunction ( height , value , progress ) ;
311
+ return this . translationFunction ( ty , value , progress ) ;
327
312
}
328
313
return {
329
314
bottomSheet : {
@@ -338,27 +323,18 @@ export class PersistentBottomSheet extends GridLayout {
338
323
const contentView = event . object as GridLayout ;
339
324
const height = Math . round ( Utils . layout . toDeviceIndependentPixels ( contentView . getMeasuredHeight ( ) ) ) ;
340
325
this . viewHeight = height ;
341
- if ( this . translationY === - 1 && this . bottomViewHeight !== 0 ) {
342
- const height = this . bottomViewHeight ;
343
- const steps = this . steps ;
344
- const step = steps [ this . stepIndex ] ;
345
- const ty = height - step ;
346
- this . translationY = ty ;
347
- const data = this . computeTranslationData ( height ) ;
348
- this . applyTrData ( data ) ;
326
+ if ( this . bottomSheet ) {
327
+ this . bottomSheet . top = {
328
+ unit : 'px' ,
329
+ value : contentView . getMeasuredHeight ( )
330
+ } ;
349
331
}
350
- }
351
- private onBottomLayoutChange ( event : EventData ) {
352
- const contentView = event . object as GridLayout ;
353
- const height = Math . round ( Utils . layout . toDeviceIndependentPixels ( contentView . getMeasuredHeight ( ) ) ) ;
354
- this . bottomViewHeight = height ;
355
- if ( this . translationY === - 1 && this . viewHeight !== 0 ) {
356
- const height = this . bottomViewHeight ;
332
+ if ( this . translationY === - 1 && this . bottomSheet ) {
357
333
const steps = this . steps ;
358
334
const step = steps [ this . stepIndex ] ;
359
- const ty = height - step ;
360
- this . translationY = ty ;
361
- const data = this . computeTranslationData ( height ) ;
335
+ const ty = step ;
336
+ this . translationY = - ty ;
337
+ const data = this . computeTranslationData ( ty ) ;
362
338
this . applyTrData ( data ) ;
363
339
}
364
340
}
@@ -389,10 +365,6 @@ export class PersistentBottomSheet extends GridLayout {
389
365
}
390
366
private onTouch ( event : TouchGestureEventData ) {
391
367
let touchY ;
392
- // if (this.animationTimer) {
393
- // clearTimeout(this.animationTimer);
394
- // this.animationTimer = null;
395
- // }
396
368
// touch event gives you relative touch which varies with translateY
397
369
// so we use touch location in the window
398
370
if ( global . isAndroid ) {
@@ -401,20 +373,14 @@ export class PersistentBottomSheet extends GridLayout {
401
373
touchY = ( event . ios . touches . anyObject ( ) as UITouch ) . locationInView ( null ) . y ;
402
374
}
403
375
if ( event . action === 'down' ) {
404
- // this.scrollViewTouched = true;
405
- // this.lastScrollY = this.scrollViewVerticalOffset;
406
- // this.scrollViewAtTop = this.lastScrollY === 0;
407
- // if (this.scrollViewAtTop) {
408
- // this.panGestureHandler.cancel();
409
- // }
410
376
} else if ( event . action === 'up' || event . action === 'cancel' ) {
411
377
if ( this . scrollViewTouched ) {
412
378
this . scrollViewTouched = false ;
413
379
if ( this . scrollViewAtTop ) {
414
380
this . scrollViewAtTop = this . scrollView . verticalOffset === 0 ;
415
381
const y = touchY - ( this . lastTouchY || touchY ) ;
416
382
const totalDelta = this . translationY + y ;
417
- this . computeAndAnimateEndGestureAnimation ( totalDelta ) ;
383
+ this . computeAndAnimateEndGestureAnimation ( - totalDelta ) ;
418
384
}
419
385
}
420
386
this . isScrollEnabled = true ;
@@ -433,9 +399,8 @@ export class PersistentBottomSheet extends GridLayout {
433
399
}
434
400
const y = touchY - ( this . lastTouchY || touchY ) ;
435
401
const trY = this . constrainY ( this . translationY + y ) ;
436
- const height = this . bottomViewHeight ;
437
402
this . translationY = trY ;
438
- const trData = this . computeTranslationData ( height ) ;
403
+ const trData = this . computeTranslationData ( - trY ) ;
439
404
this . applyTrData ( trData ) ;
440
405
}
441
406
this . lastTouchY = touchY ;
@@ -462,30 +427,27 @@ export class PersistentBottomSheet extends GridLayout {
462
427
const dragToss = 0.05 ;
463
428
const y = translationY - this . prevDeltaY ;
464
429
const totalDelta = this . translationY + ( y + dragToss * velocityY ) ;
465
- this . computeAndAnimateEndGestureAnimation ( totalDelta ) ;
430
+ this . computeAndAnimateEndGestureAnimation ( - totalDelta ) ;
466
431
this . prevDeltaY = 0 ;
467
432
}
468
433
}
469
434
470
435
private computeAndAnimateEndGestureAnimation ( totalDelta : number ) {
471
- const viewHeight = this . bottomViewHeight ;
472
436
const steps = this . steps ;
473
437
let stepIndex = 0 ;
474
- let destSnapPoint = viewHeight - steps [ stepIndex ] ;
438
+ let destSnapPoint = steps [ stepIndex ] ;
475
439
let distance = Math . abs ( destSnapPoint - totalDelta ) ;
476
440
for ( let i = 0 ; i < steps . length ; i ++ ) {
477
- const snapPoint = viewHeight - steps [ i ] ;
441
+ const snapPoint = steps [ i ] ;
478
442
const distFromSnap = Math . abs ( snapPoint - totalDelta ) ;
479
443
if ( distFromSnap <= Math . abs ( destSnapPoint - totalDelta ) ) {
480
444
destSnapPoint = snapPoint ;
481
445
stepIndex = i ;
482
446
distance = distFromSnap ;
483
447
}
484
448
}
485
- // stepIndexProperty.nativeValueChange
486
- // this.stepIndex = stepIndex;
487
449
stepIndexProperty . nativeValueChange ( this , stepIndex ) ;
488
- this . animateToPosition ( viewHeight - destSnapPoint , Math . min ( distance * 2 , OPEN_DURATION ) ) ;
450
+ this . animateToPosition ( destSnapPoint , Math . min ( distance * 2 , OPEN_DURATION ) ) ;
489
451
}
490
452
private onGestureTouch ( args : GestureTouchEventData ) {
491
453
const data = args . data ;
@@ -500,8 +462,7 @@ export class PersistentBottomSheet extends GridLayout {
500
462
const y = deltaY - this . prevDeltaY ;
501
463
const trY = this . constrainY ( this . translationY + y ) ;
502
464
this . translationY = trY ;
503
- const height = this . bottomViewHeight ;
504
- const trData = this . computeTranslationData ( height ) ;
465
+ const trData = this . computeTranslationData ( - trY ) ;
505
466
this . applyTrData ( trData ) ;
506
467
this . prevDeltaY = deltaY ;
507
468
}
@@ -519,7 +480,7 @@ export class PersistentBottomSheet extends GridLayout {
519
480
}
520
481
521
482
private constrainY ( y ) {
522
- return Math . max ( Math . min ( y , this . bottomViewHeight ) , this . bottomViewHeight - this . translationMaxOffset ) ;
483
+ return Math . max ( Math . min ( y , 0 ) , - this . translationMaxOffset ) ;
523
484
}
524
485
525
486
animating = false ;
@@ -539,10 +500,9 @@ export class PersistentBottomSheet extends GridLayout {
539
500
event . setAction ( android . view . MotionEvent . ACTION_CANCEL ) ;
540
501
this . scrollView . nativeViewProtected . dispatchTouchEvent ( event ) ;
541
502
}
542
- const height = this . bottomViewHeight ;
543
- this . translationY = height - position ;
544
- const trData = this . computeTranslationData ( height ) ;
545
-
503
+ // const height = this.bottomViewHeight;
504
+ this . translationY = - position ;
505
+ const trData = this . computeTranslationData ( position ) ;
546
506
const params = Object . keys ( trData )
547
507
. map ( ( k ) => {
548
508
const data = trData [ k ] ;
@@ -577,12 +537,6 @@ export class PersistentBottomSheet extends GridLayout {
577
537
this . isScrollEnabled = true ;
578
538
this . animating = false ;
579
539
this . animation = null ;
580
- // if (position !== 0) {
581
- // } else {
582
- // // if (this.backDrop) {
583
- // // this.backDrop.visibility = 'hidden';
584
- // // }
585
- // }
586
540
}
587
541
}
588
542
}
0 commit comments