@@ -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
@@ -581,59 +584,74 @@ ${Object.keys(size)
581
584
582
585
async scrollUntilTop ( startPoint ?: Point ) : Promise < void > {
583
586
if ( startPoint ) {
587
+ const { height } = await this . size ( ) ;
584
588
const start = { x : startPoint . left , y : startPoint . top } ;
585
- const end = { x : start . x , y : 0 } ;
589
+ const end = { x : start . x , y : height } ;
586
590
587
- await this . mouseDrag ( start , end ) ;
591
+ await repeat ( defaultScrollUntilTimes , ( ) =>
592
+ this . mouseDrag ( start , end , defaultFastScrollDuration ) ,
593
+ ) ;
594
+ await sleep ( 1000 ) ;
588
595
return ;
589
596
}
590
597
591
598
await repeat ( defaultScrollUntilTimes , ( ) =>
592
- this . mouseWheel ( 0 , 9999999 , defaultFastScrollDuration ) ,
599
+ this . mouseWheel ( 0 , - 9999999 , defaultFastScrollDuration ) ,
593
600
) ;
594
601
await sleep ( 1000 ) ;
595
602
}
596
603
597
604
async scrollUntilBottom ( startPoint ?: Point ) : Promise < void > {
598
605
if ( startPoint ) {
599
- const { height } = await this . size ( ) ;
600
606
const start = { x : startPoint . left , y : startPoint . top } ;
601
- const end = { x : start . x , y : height } ;
602
- await this . mouseDrag ( start , end ) ;
607
+ const end = { x : start . x , y : 0 } ;
608
+
609
+ await repeat ( defaultScrollUntilTimes , ( ) =>
610
+ this . mouseDrag ( start , end , defaultFastScrollDuration ) ,
611
+ ) ;
612
+ await sleep ( 1000 ) ;
603
613
return ;
604
614
}
605
615
606
616
await repeat ( defaultScrollUntilTimes , ( ) =>
607
- this . mouseWheel ( 0 , - 9999999 , defaultFastScrollDuration ) ,
617
+ this . mouseWheel ( 0 , 9999999 , defaultFastScrollDuration ) ,
608
618
) ;
609
619
await sleep ( 1000 ) ;
610
620
}
611
621
612
622
async scrollUntilLeft ( startPoint ?: Point ) : Promise < void > {
613
623
if ( startPoint ) {
624
+ const { width } = await this . size ( ) ;
614
625
const start = { x : startPoint . left , y : startPoint . top } ;
615
- const end = { x : 0 , y : start . y } ;
616
- await this . mouseDrag ( start , end ) ;
626
+ const end = { x : width , y : start . y } ;
627
+
628
+ await repeat ( defaultScrollUntilTimes , ( ) =>
629
+ this . mouseDrag ( start , end , defaultFastScrollDuration ) ,
630
+ ) ;
631
+ await sleep ( 1000 ) ;
617
632
return ;
618
633
}
619
634
620
635
await repeat ( defaultScrollUntilTimes , ( ) =>
621
- this . mouseWheel ( 9999999 , 0 , defaultFastScrollDuration ) ,
636
+ this . mouseWheel ( - 9999999 , 0 , defaultFastScrollDuration ) ,
622
637
) ;
623
638
await sleep ( 1000 ) ;
624
639
}
625
640
626
641
async scrollUntilRight ( startPoint ?: Point ) : Promise < void > {
627
642
if ( startPoint ) {
628
- const { width } = await this . size ( ) ;
629
643
const start = { x : startPoint . left , y : startPoint . top } ;
630
- const end = { x : width , y : start . y } ;
631
- await this . mouseDrag ( start , end ) ;
644
+ const end = { x : 0 , y : start . y } ;
645
+
646
+ await repeat ( defaultScrollUntilTimes , ( ) =>
647
+ this . mouseDrag ( start , end , defaultFastScrollDuration ) ,
648
+ ) ;
649
+ await sleep ( 1000 ) ;
632
650
return ;
633
651
}
634
652
635
653
await repeat ( defaultScrollUntilTimes , ( ) =>
636
- this . mouseWheel ( - 9999999 , 0 , defaultFastScrollDuration ) ,
654
+ this . mouseWheel ( 9999999 , 0 , defaultFastScrollDuration ) ,
637
655
) ;
638
656
await sleep ( 1000 ) ;
639
657
}
@@ -644,13 +662,13 @@ ${Object.keys(size)
644
662
645
663
if ( startPoint ) {
646
664
const start = { x : startPoint . left , y : startPoint . top } ;
647
- const endY = Math . max ( 0 , start . y - scrollDistance ) ;
665
+ const endY = Math . min ( height , start . y + scrollDistance ) ;
648
666
const end = { x : start . x , y : endY } ;
649
667
await this . mouseDrag ( start , end ) ;
650
668
return ;
651
669
}
652
670
653
- await this . mouseWheel ( 0 , scrollDistance ) ;
671
+ await this . mouseWheel ( 0 , - scrollDistance ) ;
654
672
}
655
673
656
674
async scrollDown ( distance ?: number , startPoint ?: Point ) : Promise < void > {
@@ -659,13 +677,13 @@ ${Object.keys(size)
659
677
660
678
if ( startPoint ) {
661
679
const start = { x : startPoint . left , y : startPoint . top } ;
662
- const endY = Math . min ( height , start . y + scrollDistance ) ;
680
+ const endY = Math . max ( 0 , start . y - scrollDistance ) ;
663
681
const end = { x : start . x , y : endY } ;
664
682
await this . mouseDrag ( start , end ) ;
665
683
return ;
666
684
}
667
685
668
- await this . mouseWheel ( 0 , - scrollDistance ) ;
686
+ await this . mouseWheel ( 0 , scrollDistance ) ;
669
687
}
670
688
671
689
async scrollLeft ( distance ?: number , startPoint ?: Point ) : Promise < void > {
@@ -674,13 +692,13 @@ ${Object.keys(size)
674
692
675
693
if ( startPoint ) {
676
694
const start = { x : startPoint . left , y : startPoint . top } ;
677
- const endX = Math . max ( 0 , start . x - scrollDistance ) ;
695
+ const endX = Math . min ( width , start . x + scrollDistance ) ;
678
696
const end = { x : endX , y : start . y } ;
679
697
await this . mouseDrag ( start , end ) ;
680
698
return ;
681
699
}
682
700
683
- await this . mouseWheel ( scrollDistance , 0 ) ;
701
+ await this . mouseWheel ( - scrollDistance , 0 ) ;
684
702
}
685
703
686
704
async scrollRight ( distance ?: number , startPoint ?: Point ) : Promise < void > {
@@ -689,13 +707,13 @@ ${Object.keys(size)
689
707
690
708
if ( startPoint ) {
691
709
const start = { x : startPoint . left , y : startPoint . top } ;
692
- const endX = Math . min ( width , start . x + scrollDistance ) ;
710
+ const endX = Math . max ( 0 , start . x - scrollDistance ) ;
693
711
const end = { x : endX , y : start . y } ;
694
712
await this . mouseDrag ( start , end ) ;
695
713
return ;
696
714
}
697
715
698
- await this . mouseWheel ( - scrollDistance , 0 ) ;
716
+ await this . mouseWheel ( scrollDistance , 0 ) ;
699
717
}
700
718
701
719
private async ensureYadb ( ) {
@@ -804,20 +822,26 @@ ${Object.keys(size)
804
822
private async mouseDrag (
805
823
from : { x : number ; y : number } ,
806
824
to : { x : number ; y : number } ,
825
+ duration ?: number ,
807
826
) : Promise < void > {
808
827
const adb = await this . getAdb ( ) ;
809
828
810
829
// Use adjusted coordinates
811
830
const { x : fromX , y : fromY } = this . adjustCoordinates ( from . x , from . y ) ;
812
831
const { x : toX , y : toY } = this . adjustCoordinates ( to . x , to . y ) ;
813
832
814
- 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
+ ) ;
815
839
}
816
840
817
841
private async mouseWheel (
818
842
deltaX : number ,
819
843
deltaY : number ,
820
- duration = defaultNormalScrollDuration ,
844
+ duration ?: number ,
821
845
) : Promise < void > {
822
846
const { width, height } = await this . size ( ) ;
823
847
@@ -839,8 +863,11 @@ ${Object.keys(size)
839
863
deltaY = Math . max ( - maxNegativeDeltaY , Math . min ( deltaY , maxPositiveDeltaY ) ) ;
840
864
841
865
// Calculate the end coordinates
842
- const endX = startX + deltaX ;
843
- 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 ;
844
871
845
872
// Adjust coordinates to fit device ratio
846
873
const { x : adjustedStartX , y : adjustedStartY } = this . adjustCoordinates (
@@ -854,9 +881,12 @@ ${Object.keys(size)
854
881
855
882
const adb = await this . getAdb ( ) ;
856
883
884
+ // Ensure duration has a default value
885
+ const swipeDuration = duration ?? defaultNormalScrollDuration ;
886
+
857
887
// Execute the swipe operation
858
888
await adb . shell (
859
- `input swipe ${ adjustedStartX } ${ adjustedStartY } ${ adjustedEndX } ${ adjustedEndY } ${ duration } ` ,
889
+ `input swipe ${ adjustedStartX } ${ adjustedStartY } ${ adjustedEndX } ${ adjustedEndY } ${ swipeDuration } ` ,
860
890
) ;
861
891
}
862
892
0 commit comments