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