Skip to content

Commit cca6ac9

Browse files
authored
Merge pull request #5 from kkppa/master
Add swipe ext function
2 parents f86f6ee + 052669a commit cca6ac9

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-2
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,25 @@ d.swipe(x1, y1, x2, y2, spped)
375375
d.swipe(600, 2600, 600, 1200, speed=2000) # 上滑
376376
d.swipe(0.5, 0.8, 0.5, 0.4, speed=2000)
377377
```
378-
参数`x1`, `y1`表示滑动的起始点,`x2`, `y2`表示滑动的终点,`speed`为滑动速率, 范围:200~40000, 不在范围内设为默认值为2000, 单位: 像素点/秒
378+
- `x1`, `y1`表示滑动的起始点,`x2`, `y2`表示滑动的终点
379+
- `speed`为滑动速率, 范围:200~40000, 不在范围内设为默认值为2000, 单位: 像素点/秒
380+
381+
#### 滑动 ext
382+
```python
383+
384+
d.swipe_ext("up") # 向上滑动,"left", "right", "up", "down"
385+
d.swipe_ext("right", scale=0.8) # 向右滑动,滑动距离为屏幕宽度的80%
386+
d.swipe_ext("up", box=(0.2, 0.2, 0.8, 0.8)) # 在屏幕 (0.2, 0.2) -> (0.8, 0.8) 这个区域上滑
387+
388+
# 使用枚举作为参数
389+
from hmdriver2.proto import SwipeDirection
390+
d.swipe_ext(SwipeDirection.DOWN) # 向下滑动
391+
```
392+
- `direction`表示滑动方向,可以为`up`, `down`, `left`, `right`, 也可以为`SwipeDirection`的枚举值
393+
- `scale`表示滑动距离百分比,范围:0.1~1.0, 默认值为0.8
394+
- `box`表示滑动区域,格式为`(x1, y1, x2, y2)`, 表示滑动区域的左上角和右下角的坐标,可以为绝对坐标值,也可以为相当坐标(屏幕百分比)
395+
396+
Notes: `swipe_ext``swipe`的区别在于swipe_ext可以指定滑动区域,并且可以指定滑动方向,更简洁灵活
379397

380398
#### 输入
381399
```python
@@ -645,4 +663,4 @@ See [DEVELOP.md](/docs/DEVELOP.md)
645663
- https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ut-V5
646664
- https://github.com/codematrixer/awesome-hdc
647665
- https://github.com/openatx/uiautomator2
648-
- https://github.com/mrx1203/hmdriver
666+
- https://github.com/mrx1203/hmdriver

hmdriver2/_swipe.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from typing import Union, Tuple
4+
5+
from .driver import Driver
6+
from .proto import SwipeDirection
7+
8+
9+
class SwipeExt(object):
10+
def __init__(self, d: Driver):
11+
self._d = d
12+
13+
def __call__(self,
14+
direction: Union[SwipeDirection, str],
15+
scale: float = 0.8,
16+
box: Union[Tuple, None] = None,
17+
speed=2000):
18+
"""
19+
Args:
20+
direction (str): one of "left", "right", "up", "bottom" or SwipeDirection.LEFT
21+
scale (float): percent of swipe, range (0, 1.0]
22+
box (Tuple): None or (x1, x1, y1, x2, y2)
23+
speed (int, optional): The swipe speed in pixels per second. Default is 2000. Range: 200-40000. If not within the range, set to default value of 2000.
24+
Raises:
25+
ValueError
26+
"""
27+
def _swipe(_from, _to):
28+
self._d.swipe(_from[0], _from[1], _to[0], _to[1], speed=speed)
29+
30+
if scale <= 0 or scale > 1.0 or not isinstance(scale, (float, int)):
31+
raise ValueError("scale must be in range (0, 1.0]")
32+
33+
if box:
34+
x1, y1, x2, y2 = self._validate_and_convert_box(box)
35+
else:
36+
x1, y1 = 0, 0
37+
x2, y2 = self._d.display_size
38+
39+
width, height = x2 - x1, y2 - y1
40+
41+
h_offset = int(width * (1 - scale) / 2)
42+
v_offset = int(height * (1 - scale) / 2)
43+
44+
if direction == SwipeDirection.LEFT:
45+
start = (x2 - h_offset, y1 + height // 2)
46+
end = (x1 + h_offset, y1 + height // 2)
47+
elif direction == SwipeDirection.RIGHT:
48+
start = (x1 + h_offset, y1 + height // 2)
49+
end = (x2 - h_offset, y1 + height // 2)
50+
elif direction == SwipeDirection.UP:
51+
start = (x1 + width // 2, y2 - v_offset)
52+
end = (x1 + width // 2, y1 + v_offset)
53+
elif direction == SwipeDirection.DOWN:
54+
start = (x1 + width // 2, y1 + v_offset)
55+
end = (x1 + width // 2, y2 - v_offset)
56+
else:
57+
raise ValueError("Unknown SwipeDirection:", direction)
58+
59+
_swipe(start, end)
60+
61+
def _validate_and_convert_box(self, box: Tuple) -> Tuple[int, int, int, int]:
62+
"""
63+
Validate and convert the box coordinates if necessay.
64+
65+
Args:
66+
box (Tuple): The box coordinates as a tuple (x1, y1, x2, y2).
67+
68+
Returns:
69+
Tuple[int, int, int, int]: The validated and converted box coordinates.
70+
"""
71+
if not isinstance(box, tuple) or len(box) != 4:
72+
raise ValueError("Box must be a tuple of length 4.")
73+
x1, y1, x2, y2 = box
74+
if not (x1 >= 0 and y1 >= 0 and x2 > 0 and y2 > 0):
75+
raise ValueError("Box coordinates must be greater than 0.")
76+
if not (x1 < x2 and y1 < y2):
77+
raise ValueError("Box coordinates must satisfy x1 < x2 and y1 < y2.")
78+
79+
from .driver import Point
80+
p1: Point = self._d._to_abs_pos(x1, y1)
81+
p2: Point = self._d._to_abs_pos(x2, y2)
82+
x1, y1, x2, y2 = p1.x, p1.y, p2.x, p2.y
83+
84+
return x1, y1, x2, y2

hmdriver2/driver.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,11 @@ def swipe(self, x1, y1, x2, y2, speed=2000):
313313
api = "Driver.swipe"
314314
self._invoke(api, args=[point1.x, point1.y, point2.x, point2.y, speed])
315315

316+
@cached_property
317+
def swipe_ext(self):
318+
from ._swipe import SwipeExt
319+
return SwipeExt(self)
320+
316321
@delay
317322
def input_text(self, text: str):
318323
"""

0 commit comments

Comments
 (0)