From 82fa139ec03a4e3e3182e328db70a8861b7deb39 Mon Sep 17 00:00:00 2001
From: hyb1996 <946994919@qq.com>
Date: Sun, 14 Oct 2018 14:34:34 +0800
Subject: [PATCH] fix: bug of ui.Widget; add: sample for ui.Widget
---
.../界面控件/自定义控件-使用配置勾选框.js | 18 ++++++++
.../sample/界面控件/自定义控件-布局模板.js | 41 +++++++++++++++++++
.../sample/界面控件/自定义控件-带颜色按钮.js | 2 +-
...件-配置勾选框.js => 自定义控件-模块-配置勾选框.js} | 31 ++++++--------
autojs/src/main/assets/modules/__app__.js | 5 +++
autojs/src/main/assets/modules/__ui__.js | 30 ++++++++++++++
autojs/src/main/assets/modules/__util__.js | 2 +-
.../ui/inflater/DynamicLayoutInflater.java | 11 +++--
.../core/ui/nativeview/ViewPrototype.java | 9 ++++
9 files changed, 122 insertions(+), 27 deletions(-)
create mode 100644 app/src/main/assets/sample/界面控件/自定义控件-使用配置勾选框.js
create mode 100644 app/src/main/assets/sample/界面控件/自定义控件-布局模板.js
rename app/src/main/assets/sample/界面控件/{自定义控件-配置勾选框.js => 自定义控件-模块-配置勾选框.js} (63%)
diff --git a/app/src/main/assets/sample/界面控件/自定义控件-使用配置勾选框.js b/app/src/main/assets/sample/界面控件/自定义控件-使用配置勾选框.js
new file mode 100644
index 00000000..154db38e
--- /dev/null
+++ b/app/src/main/assets/sample/界面控件/自定义控件-使用配置勾选框.js
@@ -0,0 +1,18 @@
+"ui";
+
+var PrefCheckBox = require('./自定义控件-模块-配置勾选框.js');
+
+ui.layout(
+
+
+
+
+
+);
+
+ui.btn.on("click", function(){
+ toast("配置1为" + PrefCheckBox.getPref().get("perf1"));
+ toast("配置2为" + PrefCheckBox.getPref().get("perf2"));
+});
+
+
diff --git a/app/src/main/assets/sample/界面控件/自定义控件-布局模板.js b/app/src/main/assets/sample/界面控件/自定义控件-布局模板.js
new file mode 100644
index 00000000..73a7a481
--- /dev/null
+++ b/app/src/main/assets/sample/界面控件/自定义控件-布局模板.js
@@ -0,0 +1,41 @@
+"ui";
+
+var InputLayout = (function() {
+ //继承至ui.Widget
+ util.extend(InputLayout, ui.Widget);
+
+ function InputLayout() {
+ ui.Widget.call(this);
+ this.defineAttr("hint", (view, attr, value, defineSetter)=>{
+ view._hint.setText(value);
+ });
+ this.defineAttr("text", (view, attr, value, defineSetter)=>{
+ view._input.setText(value);
+ });
+ }
+ InputLayout.prototype.render = function() {
+ return (
+
+
+
+
+ );
+ }
+ InputLayout.prototype.getInput = function() {
+ return this.view._input.getText();
+ };
+ ui.registerWidget("input-layout", InputLayout);
+ return InputLayout;
+})();
+
+ui.layout(
+
+
+
+
+
+);
+
+ui.ok.on("click", function(){
+ toast("名字是:" + ui.name.widget.getInput() + ", 年龄是:" + ui.age.widget.getInput());
+});
\ No newline at end of file
diff --git a/app/src/main/assets/sample/界面控件/自定义控件-带颜色按钮.js b/app/src/main/assets/sample/界面控件/自定义控件-带颜色按钮.js
index b4a61181..3a7b5167 100644
--- a/app/src/main/assets/sample/界面控件/自定义控件-带颜色按钮.js
+++ b/app/src/main/assets/sample/界面控件/自定义控件-带颜色按钮.js
@@ -2,7 +2,7 @@
var ColoredButton = (function() {
//继承至ui.Widget
- util.extends(ColoredButton, ui.Widget);
+ util.extend(ColoredButton, ui.Widget);
function ColoredButton() {
//调用父类构造函数
diff --git a/app/src/main/assets/sample/界面控件/自定义控件-配置勾选框.js b/app/src/main/assets/sample/界面控件/自定义控件-模块-配置勾选框.js
similarity index 63%
rename from app/src/main/assets/sample/界面控件/自定义控件-配置勾选框.js
rename to app/src/main/assets/sample/界面控件/自定义控件-模块-配置勾选框.js
index 622741f3..5ac0c983 100644
--- a/app/src/main/assets/sample/界面控件/自定义控件-配置勾选框.js
+++ b/app/src/main/assets/sample/界面控件/自定义控件-模块-配置勾选框.js
@@ -1,9 +1,8 @@
-"ui";
//这个自定义控件是一个勾选框checkbox,能够保存自己的勾选状态,在脚本重新启动时能恢复状态
var PrefCheckBox = (function() {
//继承至ui.Widget
- util.extends(PrefCheckBox, ui.Widget);
+ util.extend(PrefCheckBox, ui.Widget);
function PrefCheckBox() {
//调用父类构造函数
@@ -16,21 +15,28 @@ var PrefCheckBox = (function() {
);
}
- PrefCheckBox.prototype.onViewCreated = function(view) {
+ PrefCheckBox.prototype.onFinishInflation = function(view) {
view.setChecked(PrefCheckBox.getPref().get(this.getKey(), false));
view.on("check", (checked) => {
PrefCheckBox.getPref().put(this.getKey(), checked);
});
}
PrefCheckBox.prototype.getKey = function() {
- return this.key || view.attr("id").replace("@+id/", "");
+ if(this.key){
+ return this.key;
+ }
+ let id = this.view.attr("id");
+ if(!id){
+ throw new Error("should set a id or key to the checkbox");
+ }
+ return id.replace("@+id/", "");
}
PrefCheckBox.setPref = function(pref) {
PrefCheckBox._pref = pref;
}
PrefCheckBox.getPref = function(){
if(!PrefCheckBox._pref){
- PrefCheckBox._pref = storages.create("pref_pref");
+ PrefCheckBox._pref = storages.create("pref");
}
return PrefCheckBox._pref;
}
@@ -38,17 +44,4 @@ var PrefCheckBox = (function() {
return PrefCheckBox;
})();
-ui.layout(
-
-
-
-
-
-);
-
-ui.btn.on("click", function(){
- toast("配置1为" + PrefCheckBox.getPref().get("perf1"));
- toast("配置2为" + PrefCheckBox.getPref().get("perf2"));
-});
-
-
+module.exports = PrefCheckBox;
\ No newline at end of file
diff --git a/autojs/src/main/assets/modules/__app__.js b/autojs/src/main/assets/modules/__app__.js
index 178ebf39..5159d415 100644
--- a/autojs/src/main/assets/modules/__app__.js
+++ b/autojs/src/main/assets/modules/__app__.js
@@ -82,6 +82,11 @@ module.exports = function(runtime, global){
context.startActivity(Intent.createChooser(i, "发送邮件").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
+ app.getUriForFile = function(file){
+ return android.support.v4.content.FileProvider.getUriForFile(context,
+ "org.autojs.autojs.fileprovider", new java.io.File(files.path(path)));
+ };
+
function toArray(arg){
if(typeof(arg) == 'string'){
return [arg];
diff --git a/autojs/src/main/assets/modules/__ui__.js b/autojs/src/main/assets/modules/__ui__.js
index 9e25c723..5863283b 100644
--- a/autojs/src/main/assets/modules/__ui__.js
+++ b/autojs/src/main/assets/modules/__ui__.js
@@ -138,6 +138,10 @@ module.exports = function (runtime, global) {
return null;
},
afterInflateView: function (context, view, node, parent, attachToParent) {
+ let widget = view.widget;
+ if(widget && context.get("root") != widget){
+ widget.notifyAfterInflation(view);
+ }
return view;
},
beforeCreateView: function (context, node, viewName, parent, attrs) {
@@ -145,6 +149,7 @@ module.exports = function (runtime, global) {
let Widget = ui.__widgets__[viewName];
let widget = new Widget();
let ctx = layoutInflater.newInflateContext();
+ ctx.put("root", widget);
ctx.put("widget", widget);
let view = ui.__inflate__(ctx, widget.renderInternal(), parent, false);
return view;
@@ -159,6 +164,7 @@ module.exports = function (runtime, global) {
var widget = context.get("widget");
if(widget != null){
widget.view = view;
+ view.widget = widget;
let viewAttrs = com.stardust.autojs.core.ui.ViewExtras.getViewAttributes(view, layoutInflater.resourceParser);
viewAttrs.setViewAttributeDelegate({
has: function(name) {
@@ -291,6 +297,25 @@ module.exports = function (runtime, global) {
return (< />)
};
Widget.prototype.defineAttr = function(attrName, getter, setter){
+ var attrAlias = attrName;
+ var applier = null;
+ if(typeof(arguments[1]) == 'string'){
+ attrAlias = arguments[1];
+ if(arguments.length >= 3){
+ applier = arguments[2];
+ }
+ } else if(typeof(arguments[1]) == 'function' && typeof(arguments[2]) != 'function'){
+ applier = arguments[1];
+ }
+ if(!(typeof(arguments[1]) == 'function' && typeof(arguments[2]) == 'function')){
+ getter = ()=> {
+ return this[attrAlias];
+ };
+ setter = (view, attrName, value, setter)=> {
+ this[attrAlias] = value;
+ applier && applier(view, attrName, value, setter);
+ };
+ }
this.__attrs__[attrName] = {
getter: getter,
setter: setter
@@ -310,6 +335,11 @@ module.exports = function (runtime, global) {
this.onViewCreated(view);
}
};
+ Widget.prototype.notifyAfterInflation = function(view){
+ if(typeof(this.onFinishInflation) == 'function'){
+ this.onFinishInflation(view);
+ }
+ }
return Widget;
})();
diff --git a/autojs/src/main/assets/modules/__util__.js b/autojs/src/main/assets/modules/__util__.js
index 0ec00710..81f1e5e8 100644
--- a/autojs/src/main/assets/modules/__util__.js
+++ b/autojs/src/main/assets/modules/__util__.js
@@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var formatRegExp = /%[sdj%]/g;
-exports.extends = (function () {
+exports.extend = (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
diff --git a/autojs/src/main/java/com/stardust/autojs/core/ui/inflater/DynamicLayoutInflater.java b/autojs/src/main/java/com/stardust/autojs/core/ui/inflater/DynamicLayoutInflater.java
index 6557ea7c..299a13fb 100644
--- a/autojs/src/main/java/com/stardust/autojs/core/ui/inflater/DynamicLayoutInflater.java
+++ b/autojs/src/main/java/com/stardust/autojs/core/ui/inflater/DynamicLayoutInflater.java
@@ -208,12 +208,11 @@ public class DynamicLayoutInflater {
}
}
ViewInflater inflater = applyAttributes(context, view, attrs, parent);
- if (!(view instanceof ViewGroup) || !node.hasChildNodes()) {
- return view;
- }
- inflateChildren(context, inflater, node, (ViewGroup) view);
- if (inflater instanceof ViewGroupInflater) {
- applyPendingAttributesOfChildren(context, (ViewGroupInflater) inflater, (ViewGroup) view);
+ if (view instanceof ViewGroup && node.hasChildNodes()) {
+ inflateChildren(context, inflater, node, (ViewGroup) view);
+ if (inflater instanceof ViewGroupInflater) {
+ applyPendingAttributesOfChildren(context, (ViewGroupInflater) inflater, (ViewGroup) view);
+ }
}
return mLayoutInflaterDelegate.afterInflateView(context, view, node, parent, attachToParent);
}
diff --git a/autojs/src/main/java/com/stardust/autojs/core/ui/nativeview/ViewPrototype.java b/autojs/src/main/java/com/stardust/autojs/core/ui/nativeview/ViewPrototype.java
index 2f3b7852..e4007e61 100644
--- a/autojs/src/main/java/com/stardust/autojs/core/ui/nativeview/ViewPrototype.java
+++ b/autojs/src/main/java/com/stardust/autojs/core/ui/nativeview/ViewPrototype.java
@@ -23,6 +23,7 @@ public class ViewPrototype {
private final HashSet mRegisteredEvents = new HashSet<>();
private final Scriptable mScope;
private final ViewAttributes mViewAttributes;
+ private Object mWidget;
public ViewPrototype(View view, ViewAttributes viewAttributes, Scriptable scope, ScriptRuntime runtime) {
mView = view;
@@ -50,6 +51,14 @@ public class ViewPrototype {
}
}
+ public void setWidget(Object widget) {
+ mWidget = widget;
+ }
+
+ public Object getWidget() {
+ return mWidget;
+ }
+
public void click() {
mView.performClick();
}