refactor: get window size

This commit is contained in:
lilong.129
2024-09-02 13:41:39 +08:00
parent 4c71fc02f9
commit ee51b30b06
9 changed files with 94 additions and 86 deletions
+18 -23
View File
@@ -21,9 +21,14 @@ func assertRelative(p float64) bool {
return p >= 0 && p <= 1
}
// TODO: remove this function
func (dExt *DriverExt) SwipeUpUtil(count int64, options ...ActionOption) error {
width := dExt.WindowSize.Width
height := dExt.WindowSize.Height
windowSize, err := dExt.Driver.WindowSize()
if err != nil {
return err
}
width := windowSize.Width
height := windowSize.Height
fromX := float64(width) * directionSlice[count%3][0]
fromY := float64(height) * directionSlice[count%3][1]
@@ -35,33 +40,23 @@ func (dExt *DriverExt) SwipeUpUtil(count int64, options ...ActionOption) error {
// SwipeRelative swipe from relative position [fromX, fromY] to relative position [toX, toY]
func (dExt *DriverExt) SwipeRelative(fromX, fromY, toX, toY float64, options ...ActionOption) error {
width := dExt.WindowSize.Width
height := dExt.WindowSize.Height
orientation, err := dExt.Driver.Orientation()
if err != nil {
log.Warn().Err(err).Msgf("swipe from (%v, %v) to (%v, %v) get orientation failed, use default orientation",
fromX, fromY, toX, toY)
orientation = OrientationPortrait
}
if !assertRelative(fromX) || !assertRelative(fromY) ||
!assertRelative(toX) || !assertRelative(toY) {
return fmt.Errorf("fromX(%f), fromY(%f), toX(%f), toY(%f) must be less than 1",
fromX, fromY, toX, toY)
}
// 左转和右转都是"LANDSCAPE"
if orientation == OrientationPortrait {
fromX = float64(width) * fromX
fromY = float64(height) * fromY
toX = float64(width) * toX
toY = float64(height) * toY
} else {
fromX = float64(height) * fromX
fromY = float64(width) * fromY
toX = float64(height) * toX
toY = float64(width) * toY
}
windowSize, err := dExt.Driver.WindowSize()
if err != nil {
return err
}
width := windowSize.Width
height := windowSize.Height
fromX = float64(width) * fromX
fromY = float64(height) * fromY
toX = float64(width) * toX
toY = float64(height) * toY
return dExt.Driver.SwipeFloat(fromX, fromY, toX, toY, options...)
}