6.7.0 - Alpha13 - 修复 UiObject#isShifted 方法内部逻辑错误; 新增 UiObject#snapshot 方法 (issue #469)

This commit is contained in:
SuperMonster003
2025-12-27 23:18:01 +08:00
parent ebf4489655
commit f8ef030f94
3 changed files with 36 additions and 22 deletions

View File

@@ -559,27 +559,40 @@ open class UiObject(
"${lastChild() ?: ""}" == "${other.lastChild()}"
}
fun isShifted(other: UiObject) = bounds() == other.bounds()
// Captures current state; refreshing it won't affect the original.
// zh-CN: 捕获当前状态; 刷新它不会影响原始对象.
fun snapshot(): UiObject {
val raw = unwrap()
val rawCopy = AccessibilityNodeInfo.obtain(raw)
return UiObject(rawCopy, allocator, depth, indexInParent)
}
fun isShifted(other: UiObject, tolerance: Double): Boolean {
val a = bounds()
val b = other.bounds()
return when {
tolerance < 0 -> throw WrappedIllegalArgumentException(
"Argument \"tolerance\" ${tolerance.jsBrief()} for UiObject#isShifted must be greater than or equal to 0",
)
tolerance < 1 -> (width() to height()).let { (w, h) ->
return@let abs(a.left - b.left) > tolerance * w
|| abs(a.top - b.top) > tolerance * h
|| abs(a.right - b.right) > tolerance * w
|| abs(a.bottom - b.bottom) > tolerance * h
}
else -> let {
return@let abs(a.left - b.left) > tolerance
|| abs(a.top - b.top) > tolerance
|| abs(a.right - b.right) > tolerance
|| abs(a.bottom - b.bottom) > tolerance
@JvmOverloads
fun isShifted(tolerance: Double = 0.0): Boolean {
val probe = snapshot()
try {
val a = probe.bounds() /* Before. */
if (!probe.refresh()) return true
val b = probe.bounds() /* After. */
return when {
tolerance < 0 -> throw WrappedIllegalArgumentException(
"Argument \"tolerance\" ${tolerance.jsBrief()} for UiObject#isShifted must be greater than or equal to 0",
)
tolerance < 1 -> (width() to height()).let { (w, h) ->
return@let abs(a.left - b.left) > tolerance * w
|| abs(a.top - b.top) > tolerance * h
|| abs(a.right - b.right) > tolerance * w
|| abs(a.bottom - b.bottom) > tolerance * h
}
else -> let {
return@let abs(a.left - b.left) > tolerance
|| abs(a.top - b.top) > tolerance
|| abs(a.right - b.right) > tolerance
|| abs(a.bottom - b.bottom) > tolerance
}
}
} finally {
probe.recycle()
}
}