Skip to content

Commit 5021800

Browse files
authored
Merge pull request #1557 from runhey/dev
Automated PR 2026.05.14
2 parents 15c0a25 + 27ce720 commit 5021800

1 file changed

Lines changed: 22 additions & 15 deletions

File tree

module/device/handle.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import re
55

66
from enum import Enum
7+
from time import sleep
78
from cached_property import cached_property
89
from anytree import NodeMixin, RenderTree, PreOrderIter
910
from win32api import GetSystemMetrics, SendMessage, MAKELONG, PostMessage
@@ -168,11 +169,8 @@ def __init__(self, config) -> None:
168169
self.config = Config(config, task=None)
169170
else:
170171
self.config = config
171-
if not self.config.script.device.handle:
172-
logger.info('Handle is empty. oas not use handle')
173-
return
174-
if self.config.script.device.handle == '':
175-
logger.info('Handle is empty. oas not use handle')
172+
if not self.config.script.device.handle or self.config.script.device.handle == '':
173+
logger.info('Handle is empty, oas not use handle')
176174
return
177175

178176
# 获取根的句柄
@@ -187,20 +185,32 @@ def __init__(self, config) -> None:
187185
if isinstance(self.root_handle, str):
188186
try:
189187
self.root_handle_num = int(self.root_handle)
190-
logger.info('Handle is handle num. oas use it as root handle num')
188+
logger.info('Handle is a number, using it as root handle num')
191189
if is_handle_valid(self.root_handle_num):
192190
logger.info(f'Handle number {self.root_handle_num} is valid')
193191
self.root_handle_title = handle_num2title(self.root_handle_num)
194192
except ValueError:
195-
logger.info('Handle is handle string. oas use it as root handle title')
193+
logger.info('Handle is a string, looking up window by title')
196194
if handle_title2num(self.root_handle) != 0:
197195
self.root_handle_num = handle_title2num(self.root_handle)
198196
self.root_handle_title = self.root_handle
199197
logger.info(f'The root handle title is {self.root_handle_title} and num is {self.root_handle_num}')
200198

201-
# 获取句柄树
199+
# 获取句柄树(加重试,等待子窗口渲染就绪)
202200
self.root_node = WindowNode(name=self.root_handle_title, num=self.root_handle_num)
203201
Handle.handle_tree(self.root_handle_num, self.root_node)
202+
if not self.root_node.children:
203+
logger.info('Window child tree not ready, waiting for emulator to finish initializing')
204+
for i in range(9):
205+
sleep(1)
206+
self.root_node = WindowNode(name=self.root_handle_title, num=self.root_handle_num)
207+
Handle.handle_tree(self.root_handle_num, self.root_node)
208+
if self.root_node.children:
209+
logger.info(f'Window child tree ready after {i + 2} attempts')
210+
break
211+
else:
212+
logger.warning('Window child tree still not ready after 10 attempts, will use title-based fallback')
213+
204214
logger.info('Emulator handle structure:')
205215
for pre, fill, node in RenderTree(self.root_node):
206216
logger.info("%s%s" % (pre, node.name))
@@ -209,12 +219,9 @@ def __init__(self, config) -> None:
209219

210220
# 判断是哪一个模拟器 通过句柄树结构
211221
logger.info(f'Emulator family: {self.emulator_family}')
212-
213222
# window系统的缩放
214-
logger.info(f'Your window screen scale rate: {window_scale_rate()}')
215-
_ = self.screenshot_handle_num
216-
logger.info(f'Screenshot handle num: {self.screenshot_handle_num}')
217-
logger.info(f'Emulator screenshot size: {self.screenshot_size}')
223+
logger.info(f'Window screen scale rate: {window_scale_rate()}')
224+
# screenshot_handle_num 和 screenshot_size 延迟到首次截屏时按需求值,不在初始化时预计算
218225

219226
@staticmethod
220227
def all_windows() -> list:
@@ -240,7 +247,7 @@ def auto_handle_title(cls, windows: list) -> str:
240247
:return:
241248
"""
242249
if windows is None:
243-
logger.error("handle_auto not get all wnidow")
250+
logger.error("auto_handle_title: windows list is None")
244251

245252
emu_list = []
246253
for window_title in windows:
@@ -270,7 +277,7 @@ def auto_handle_title(cls, windows: list) -> str:
270277
if len(emu_list) == 1:
271278
emulator_title = emu_list[0]
272279

273-
logger.info(f'Handle auto seclect to find {emulator_title} and use it as root_title')
280+
logger.info(f'Auto-detected emulator window: {emulator_title}')
274281
return emulator_title
275282

276283
@staticmethod

0 commit comments

Comments
 (0)