From b6368b403d77a2bd4d5e75174abf7e1dffb66635 Mon Sep 17 00:00:00 2001 From: SuperMonster003 Date: Wed, 5 Feb 2025 21:07:48 +0800 Subject: [PATCH] =?UTF-8?q?6.6.2=20-=20Alpha2=20-=20ui.inflate=20=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E5=80=BC=E4=B8=A2=E5=A4=B1=20attr/on/click=20?= =?UTF-8?q?=E7=AD=89=E5=8E=9F=E5=9E=8B=E6=96=B9=E6=B3=95=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changelog/lang_zh-Hans.json | 1 + .../autojs/runtime/api/augment/ui/UI.kt | 21 +++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.changelog/lang_zh-Hans.json b/.changelog/lang_zh-Hans.json index 15211f52..aadd2c9a 100644 --- a/.changelog/lang_zh-Hans.json +++ b/.changelog/lang_zh-Hans.json @@ -8,6 +8,7 @@ "ScriptRuntime 使用 require 引用内置模块时可能出现的同步状态异常 (试修) _[`issue #298`](http://issues.autojs6.com/298)_", "notice 模块缺失 getBuilder 等扩展方法的问题 _[`issue #301`](http://issues.autojs6.com/301)_", "floaty.window/floaty.rawWindow 无法在子线程执行的问题", + "ui.inflate 返回值丢失 attr/on/click 等原型方法的问题", "代码编辑器跳转到行尾时可能跳转到下一行起始位置的问题" ], "improvement": [ diff --git a/app/src/main/java/org/autojs/autojs/runtime/api/augment/ui/UI.kt b/app/src/main/java/org/autojs/autojs/runtime/api/augment/ui/UI.kt index d66cc0e2..c7402a3a 100644 --- a/app/src/main/java/org/autojs/autojs/runtime/api/augment/ui/UI.kt +++ b/app/src/main/java/org/autojs/autojs/runtime/api/augment/ui/UI.kt @@ -14,6 +14,7 @@ import org.autojs.autojs.core.ui.inflater.InflateContext import org.autojs.autojs.core.ui.inflater.LayoutInflaterDelegate import org.autojs.autojs.core.ui.inflater.inflaters.ViewGroupInflater import org.autojs.autojs.core.ui.inflater.inflaters.ViewInflater +import org.autojs.autojs.core.ui.nativeview.NativeView import org.autojs.autojs.core.ui.widget.JsListView import org.autojs.autojs.execution.ScriptExecuteActivity import org.autojs.autojs.extension.AnyExtensions.isJsNullish @@ -341,14 +342,14 @@ class UI(private val scriptRuntime: ScriptRuntime) : AugmentableProxy(scriptRunt @JvmStatic @RhinoRuntimeFunctionInterface - fun inflate(scriptRuntime: ScriptRuntime, args: Array): View = ensureArgumentsLengthInRange(args, 1..3) { argList -> + fun inflate(scriptRuntime: ScriptRuntime, args: Array): NativeView = ensureArgumentsLengthInRange(args, 1..3) { argList -> val (xml, parent, isAttachedToParent) = argList inflateRhinoRuntime(scriptRuntime, xml, parent, isAttachedToParent) } @JvmStatic @RhinoFunctionBody - fun inflateRhinoRuntime(scriptRuntime: ScriptRuntime, xml: Any?, parent: Any? = null, isAttachedToParent: Any? = false): View { + fun inflateRhinoRuntime(scriptRuntime: ScriptRuntime, xml: Any?, parent: Any? = null, isAttachedToParent: Any? = false): NativeView { val parentView = parent.jsSanitize() require(parentView is ViewGroup?) { "Augment parentView for ui.inflate must be a ViewGroup instead of ${parentView.jsBrief()}" @@ -359,7 +360,8 @@ class UI(private val scriptRuntime: ScriptRuntime) : AugmentableProxy(scriptRunt activity is ScriptExecuteActivity -> activity else -> throw WrappedIllegalArgumentException("Global activity ${activity.jsBrief()} must be a ScriptExecuteActivity") } - return scriptRuntime.ui.layoutInflater.inflate(toXMLString(xml), parentView, coerceBoolean(isAttachedToParent, false)) + val inflatedView = scriptRuntime.ui.layoutInflater.inflate(toXMLString(xml), parentView, coerceBoolean(isAttachedToParent, false)) + return ViewExtras.getNativeView(scriptRuntime.topLevelScope, inflatedView, inflatedView::class.java, scriptRuntime) } @JvmStatic @@ -544,9 +546,9 @@ class UI(private val scriptRuntime: ScriptRuntime) : AugmentableProxy(scriptRunt @JvmStatic @RhinoFunctionBody - fun findByIdRhinoWithRuntime(scriptRuntime: ScriptRuntime, id: String?): View? { + fun findByIdRhinoWithRuntime(scriptRuntime: ScriptRuntime, id: String?): NativeView? { val view = scriptRuntime.ui.view ?: return null - return findByStringIdRhino(view, id) + return findByStringIdRhinoRuntime(scriptRuntime, view, id) } @JvmStatic @@ -556,19 +558,20 @@ class UI(private val scriptRuntime: ScriptRuntime) : AugmentableProxy(scriptRunt require(view is View) { "Argument view for ui.findByStringId must be a View instead of ${view.jsBrief()}" } when { id.isJsNullish() -> { - findByStringIdRhino(view, null) + findByStringIdRhinoRuntime(scriptRuntime, view, null) } else -> { // require(id is String) { "Argument id for ui.findByStringId must be a string instead of ${id.jsBrief()}" } - findByStringIdRhino(view, coerceString(id)) + findByStringIdRhinoRuntime(scriptRuntime, view, coerceString(id)) } } } @JvmStatic @RhinoFunctionBody - fun findByStringIdRhino(view: View, id: String?): View? { - return JsViewHelper.findViewByStringId(view, id) + fun findByStringIdRhinoRuntime(scriptRuntime: ScriptRuntime, view: View, id: String?): NativeView? { + val foundView = JsViewHelper.findViewByStringId(view, id) ?: return null + return ViewExtras.getNativeView(scriptRuntime.topLevelScope, foundView, foundView::class.java, scriptRuntime) } @JvmStatic