@@ -521,11 +521,14 @@ ${Object.keys(size)
521
521
get mouse ( ) {
522
522
return {
523
523
click : ( x : number , y : number ) => this . mouseClick ( x , y ) ,
524
- wheel : ( deltaX : number , deltaY : number ) =>
525
- this . mouseWheel ( deltaX , deltaY ) ,
524
+ wheel : ( deltaX : number , deltaY : number , duration ?: number ) =>
525
+ this . mouseWheel ( deltaX , deltaY , duration ) ,
526
526
move : ( x : number , y : number ) => this . mouseMove ( x , y ) ,
527
- drag : ( from : { x : number ; y : number } , to : { x : number ; y : number } ) =>
528
- this . mouseDrag ( from , to ) ,
527
+ drag : (
528
+ from : { x : number ; y : number } ,
529
+ to : { x : number ; y : number } ,
530
+ duration ?: number ,
531
+ ) => this . mouseDrag ( from , to , duration ) ,
529
532
} ;
530
533
}
531
534
@@ -579,30 +582,76 @@ ${Object.keys(size)
579
582
return '' ;
580
583
}
581
584
582
- async scrollUntilTop ( ) : Promise < void > {
585
+ async scrollUntilTop ( startPoint ?: Point ) : Promise < void > {
586
+ if ( startPoint ) {
587
+ const { height } = await this . size ( ) ;
588
+ const start = { x : startPoint . left , y : startPoint . top } ;
589
+ const end = { x : start . x , y : height } ;
590
+
591
+ await repeat ( defaultScrollUntilTimes , ( ) =>
592
+ this . mouseDrag ( start , end , defaultFastScrollDuration ) ,
593
+ ) ;
594
+ await sleep ( 1000 ) ;
595
+ return ;
596
+ }
597
+
583
598
await repeat ( defaultScrollUntilTimes , ( ) =>
584
- this . mouseWheel ( 0 , 9999999 , defaultFastScrollDuration ) ,
599
+ this . mouseWheel ( 0 , - 9999999 , defaultFastScrollDuration ) ,
585
600
) ;
586
601
await sleep ( 1000 ) ;
587
602
}
588
603
589
- async scrollUntilBottom ( ) : Promise < void > {
604
+ async scrollUntilBottom ( startPoint ?: Point ) : Promise < void > {
605
+ if ( startPoint ) {
606
+ const start = { x : startPoint . left , y : startPoint . top } ;
607
+ const end = { x : start . x , y : 0 } ;
608
+
609
+ await repeat ( defaultScrollUntilTimes , ( ) =>
610
+ this . mouseDrag ( start , end , defaultFastScrollDuration ) ,
611
+ ) ;
612
+ await sleep ( 1000 ) ;
613
+ return ;
614
+ }
615
+
590
616
await repeat ( defaultScrollUntilTimes , ( ) =>
591
- this . mouseWheel ( 0 , - 9999999 , defaultFastScrollDuration ) ,
617
+ this . mouseWheel ( 0 , 9999999 , defaultFastScrollDuration ) ,
592
618
) ;
593
619
await sleep ( 1000 ) ;
594
620
}
595
621
596
- async scrollUntilLeft ( ) : Promise < void > {
622
+ async scrollUntilLeft ( startPoint ?: Point ) : Promise < void > {
623
+ if ( startPoint ) {
624
+ const { width } = await this . size ( ) ;
625
+ const start = { x : startPoint . left , y : startPoint . top } ;
626
+ const end = { x : width , y : start . y } ;
627
+
628
+ await repeat ( defaultScrollUntilTimes , ( ) =>
629
+ this . mouseDrag ( start , end , defaultFastScrollDuration ) ,
630
+ ) ;
631
+ await sleep ( 1000 ) ;
632
+ return ;
633
+ }
634
+
597
635
await repeat ( defaultScrollUntilTimes , ( ) =>
598
- this . mouseWheel ( 9999999 , 0 , defaultFastScrollDuration ) ,
636
+ this . mouseWheel ( - 9999999 , 0 , defaultFastScrollDuration ) ,
599
637
) ;
600
638
await sleep ( 1000 ) ;
601
639
}
602
640
603
- async scrollUntilRight ( ) : Promise < void > {
641
+ async scrollUntilRight ( startPoint ?: Point ) : Promise < void > {
642
+ if ( startPoint ) {
643
+ const start = { x : startPoint . left , y : startPoint . top } ;
644
+ const end = { x : 0 , y : start . y } ;
645
+
646
+ await repeat ( defaultScrollUntilTimes , ( ) =>
647
+ this . mouseDrag ( start , end , defaultFastScrollDuration ) ,
648
+ ) ;
649
+ await sleep ( 1000 ) ;
650
+ return ;
651
+ }
652
+
604
653
await repeat ( defaultScrollUntilTimes , ( ) =>
605
- this . mouseWheel ( - 9999999 , 0 , defaultFastScrollDuration ) ,
654
+ this . mouseWheel ( 9999999 , 0 , defaultFastScrollDuration ) ,
606
655
) ;
607
656
await sleep ( 1000 ) ;
608
657
}
@@ -613,13 +662,13 @@ ${Object.keys(size)
613
662
614
663
if ( startPoint ) {
615
664
const start = { x : startPoint . left , y : startPoint . top } ;
616
- const endY = Math . max ( 0 , start . y - scrollDistance ) ;
665
+ const endY = Math . min ( height , start . y + scrollDistance ) ;
617
666
const end = { x : start . x , y : endY } ;
618
667
await this . mouseDrag ( start , end ) ;
619
668
return ;
620
669
}
621
670
622
- await this . mouseWheel ( 0 , scrollDistance ) ;
671
+ await this . mouseWheel ( 0 , - scrollDistance ) ;
623
672
}
624
673
625
674
async scrollDown ( distance ?: number , startPoint ?: Point ) : Promise < void > {
@@ -628,13 +677,13 @@ ${Object.keys(size)
628
677
629
678
if ( startPoint ) {
630
679
const start = { x : startPoint . left , y : startPoint . top } ;
631
- const endY = Math . min ( height , start . y + scrollDistance ) ;
680
+ const endY = Math . max ( 0 , start . y - scrollDistance ) ;
632
681
const end = { x : start . x , y : endY } ;
633
682
await this . mouseDrag ( start , end ) ;
634
683
return ;
635
684
}
636
685
637
- await this . mouseWheel ( 0 , - scrollDistance ) ;
686
+ await this . mouseWheel ( 0 , scrollDistance ) ;
638
687
}
639
688
640
689
async scrollLeft ( distance ?: number , startPoint ?: Point ) : Promise < void > {
@@ -643,13 +692,13 @@ ${Object.keys(size)
643
692
644
693
if ( startPoint ) {
645
694
const start = { x : startPoint . left , y : startPoint . top } ;
646
- const endX = Math . max ( 0 , start . x - scrollDistance ) ;
695
+ const endX = Math . min ( width , start . x + scrollDistance ) ;
647
696
const end = { x : endX , y : start . y } ;
648
697
await this . mouseDrag ( start , end ) ;
649
698
return ;
650
699
}
651
700
652
- await this . mouseWheel ( scrollDistance , 0 ) ;
701
+ await this . mouseWheel ( - scrollDistance , 0 ) ;
653
702
}
654
703
655
704
async scrollRight ( distance ?: number , startPoint ?: Point ) : Promise < void > {
@@ -658,13 +707,13 @@ ${Object.keys(size)
658
707
659
708
if ( startPoint ) {
660
709
const start = { x : startPoint . left , y : startPoint . top } ;
661
- const endX = Math . min ( width , start . x + scrollDistance ) ;
710
+ const endX = Math . max ( 0 , start . x - scrollDistance ) ;
662
711
const end = { x : endX , y : start . y } ;
663
712
await this . mouseDrag ( start , end ) ;
664
713
return ;
665
714
}
666
715
667
- await this . mouseWheel ( - scrollDistance , 0 ) ;
716
+ await this . mouseWheel ( scrollDistance , 0 ) ;
668
717
}
669
718
670
719
private async ensureYadb ( ) {
@@ -773,20 +822,26 @@ ${Object.keys(size)
773
822
private async mouseDrag (
774
823
from : { x : number ; y : number } ,
775
824
to : { x : number ; y : number } ,
825
+ duration ?: number ,
776
826
) : Promise < void > {
777
827
const adb = await this . getAdb ( ) ;
778
828
779
829
// Use adjusted coordinates
780
830
const { x : fromX , y : fromY } = this . adjustCoordinates ( from . x , from . y ) ;
781
831
const { x : toX , y : toY } = this . adjustCoordinates ( to . x , to . y ) ;
782
832
783
- await adb . shell ( `input swipe ${ fromX } ${ fromY } ${ toX } ${ toY } 300` ) ;
833
+ // Ensure duration has a default value
834
+ const swipeDuration = duration ?? 300 ;
835
+
836
+ await adb . shell (
837
+ `input swipe ${ fromX } ${ fromY } ${ toX } ${ toY } ${ swipeDuration } ` ,
838
+ ) ;
784
839
}
785
840
786
841
private async mouseWheel (
787
842
deltaX : number ,
788
843
deltaY : number ,
789
- duration = defaultNormalScrollDuration ,
844
+ duration ?: number ,
790
845
) : Promise < void > {
791
846
const { width, height } = await this . size ( ) ;
792
847
@@ -808,8 +863,11 @@ ${Object.keys(size)
808
863
deltaY = Math . max ( - maxNegativeDeltaY , Math . min ( deltaY , maxPositiveDeltaY ) ) ;
809
864
810
865
// Calculate the end coordinates
811
- const endX = startX + deltaX ;
812
- const endY = startY + deltaY ;
866
+ // Note: For swipe, we need to reverse the delta direction
867
+ // because positive deltaY should scroll up (show top content),
868
+ // which requires swiping from bottom to top (decreasing Y)
869
+ const endX = startX - deltaX ;
870
+ const endY = startY - deltaY ;
813
871
814
872
// Adjust coordinates to fit device ratio
815
873
const { x : adjustedStartX , y : adjustedStartY } = this . adjustCoordinates (
@@ -823,9 +881,12 @@ ${Object.keys(size)
823
881
824
882
const adb = await this . getAdb ( ) ;
825
883
884
+ // Ensure duration has a default value
885
+ const swipeDuration = duration ?? defaultNormalScrollDuration ;
886
+
826
887
// Execute the swipe operation
827
888
await adb . shell (
828
- `input swipe ${ adjustedStartX } ${ adjustedStartY } ${ adjustedEndX } ${ adjustedEndY } ${ duration } ` ,
889
+ `input swipe ${ adjustedStartX } ${ adjustedStartY } ${ adjustedEndX } ${ adjustedEndY } ${ swipeDuration } ` ,
829
890
) ;
830
891
}
831
892
0 commit comments