fix: bug of ui.Widget; add: sample for ui.Widget

This commit is contained in:
hyb1996
2018-10-14 14:34:34 +08:00
parent 1943e39573
commit 82fa139ec0
9 changed files with 122 additions and 27 deletions

View File

@@ -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];

View File

@@ -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;
})();

View File

@@ -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]; };

View File

@@ -208,12 +208,11 @@ public class DynamicLayoutInflater {
}
}
ViewInflater<View> 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);
}

View File

@@ -23,6 +23,7 @@ public class ViewPrototype {
private final HashSet<String> 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();
}