新增 dialogs.build()支持customView属性和wrapInScrollView属性

This commit is contained in:
hyb1996
2019-01-22 20:03:03 +08:00
parent 1865ac8d0e
commit f6bab9e51d
6 changed files with 48 additions and 15 deletions

View File

@@ -194,6 +194,14 @@ module.exports = function(__runtime__, scope){
builder.getDialog().emit("check", checked, builder.getDialog()); builder.getDialog().emit("check", checked, builder.getDialog());
}); });
} }
if(properties.customView != undefined) {
let customView = properties.customView;
if(typeof(customView) == 'xml' || typeof(customView) == 'string') {
customView = ui.run(() => ui.inflate(customView));
}
let wrapInScrollView = (properties.wrapInScrollView === undefined) ? true : properties.wrapInScrollView;
builder.customView(customView, wrapInScrollView);
}
} }
function wrapNonNullString(str){ function wrapNonNullString(str){

View File

@@ -11,7 +11,7 @@ module.exports = function (runtime, global) {
ui.__defineGetter__("emitter", ()=> activity ? activity.getEventEmitter() : null); ui.__defineGetter__("emitter", ()=> activity ? activity.getEventEmitter() : null);
ui.layout = function (xml) { ui.layout = function (xml) {
if(!activity){ if(typeof(activity) == 'undefined'){
throw new Error("需要在ui模式下运行才能使用该函数"); throw new Error("需要在ui模式下运行才能使用该函数");
} }
runtime.ui.layoutInflater.setContext(activity); runtime.ui.layoutInflater.setContext(activity);
@@ -24,15 +24,18 @@ module.exports = function (runtime, global) {
} }
ui.inflate = function(xml, parent, attachToParent){ ui.inflate = function(xml, parent, attachToParent){
if(!activity){
throw new Error("需要在ui模式下运行才能使用该函数");
}
if(typeof(xml) == 'xml'){ if(typeof(xml) == 'xml'){
xml = xml.toXMLString(); xml = xml.toXMLString();
} }
parent = parent || null; parent = parent || null;
attachToParent = !!attachToParent; attachToParent = !!attachToParent;
runtime.ui.layoutInflater.setContext(activity); let ctx;
if(typeof(activity) == 'undefined') {
ctx = new android.view.ContextThemeWrapper(context, com.stardust.autojs.R.style.ScriptTheme);
} else {
ctx = activity;
}
runtime.ui.layoutInflater.setContext(ctx);
return runtime.ui.layoutInflater.inflate(xml.toString(), parent, attachToParent); return runtime.ui.layoutInflater.inflate(xml.toString(), parent, attachToParent);
} }

View File

@@ -17,10 +17,13 @@ import org.mozilla.javascript.Context;
import java.util.HashSet; import java.util.HashSet;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import androidx.annotation.Nullable;
/** /**
* Created by Stardust on 2017/7/29. * Created by Stardust on 2017/7/29.
*/ */
@SuppressWarnings("ConstantConditions")
public class Loopers implements MessageQueue.IdleHandler { public class Loopers implements MessageQueue.IdleHandler {
private static final String LOG_TAG = "Loopers"; private static final String LOG_TAG = "Loopers";
@@ -32,9 +35,27 @@ public class Loopers implements MessageQueue.IdleHandler {
private static final Runnable EMPTY_RUNNABLE = () -> { private static final Runnable EMPTY_RUNNABLE = () -> {
}; };
private volatile ThreadLocal<Boolean> waitWhenIdle = new ThreadLocal<>(); private volatile ThreadLocal<Boolean> waitWhenIdle = new ThreadLocal<Boolean>() {
private volatile ThreadLocal<HashSet<Integer>> waitIds = new ThreadLocal<>(); @Nullable
private volatile ThreadLocal<Integer> maxWaitId = new ThreadLocal<>(); @Override
protected Boolean initialValue() {
return Looper.myLooper() == Looper.getMainLooper();
}
};
private volatile ThreadLocal<HashSet<Integer>> waitIds = new ThreadLocal<HashSet<Integer>>() {
@Nullable
@Override
protected HashSet<Integer> initialValue() {
return new HashSet<>();
}
};
private volatile ThreadLocal<Integer> maxWaitId = new ThreadLocal<Integer>() {
@Nullable
@Override
protected Integer initialValue() {
return 0;
}
};
private volatile ThreadLocal<CopyOnWriteArrayList<LooperQuitHandler>> looperQuitHandlers = new ThreadLocal<>(); private volatile ThreadLocal<CopyOnWriteArrayList<LooperQuitHandler>> looperQuitHandlers = new ThreadLocal<>();
private volatile Looper mServantLooper; private volatile Looper mServantLooper;
private Timers mTimers; private Timers mTimers;
@@ -126,7 +147,7 @@ public class Loopers implements MessageQueue.IdleHandler {
return mServantLooper; return mServantLooper;
} }
public void quitServantLooper() { private void quitServantLooper() {
if (mServantLooper == null) if (mServantLooper == null)
return; return;
mServantLooper.quit(); mServantLooper.quit();
@@ -134,12 +155,14 @@ public class Loopers implements MessageQueue.IdleHandler {
public int waitWhenIdle() { public int waitWhenIdle() {
int id = maxWaitId.get(); int id = maxWaitId.get();
Log.d(LOG_TAG, "waitWhenIdle: " + id);
maxWaitId.set(id + 1); maxWaitId.set(id + 1);
waitIds.get().add(id); waitIds.get().add(id);
return id; return id;
} }
public void doNotWaitWhenIdle(int waitId) { public void doNotWaitWhenIdle(int waitId) {
Log.d(LOG_TAG, "doNotWaitWhenIdle: " + waitId);
waitIds.get().remove(waitId); waitIds.get().remove(waitId);
} }
@@ -181,9 +204,6 @@ public class Loopers implements MessageQueue.IdleHandler {
if (Looper.myLooper() == null) if (Looper.myLooper() == null)
LooperHelper.prepare(); LooperHelper.prepare();
Looper.myQueue().addIdleHandler(this); Looper.myQueue().addIdleHandler(this);
waitWhenIdle.set(Looper.myLooper() == Looper.getMainLooper());
waitIds.set(new HashSet<>());
maxWaitId.set(0);
} }
public void notifyThreadExit(TimerThread thread) { public void notifyThreadExit(TimerThread thread) {

View File

@@ -89,6 +89,10 @@ public class Timer {
} }
} }
public void post(Runnable r) {
}
public boolean clearInterval(int id) { public boolean clearInterval(int id) {
return clearCallback(id); return clearCallback(id);
} }

View File

@@ -34,7 +34,7 @@ public class JsDialogBuilder extends MaterialDialog.Builder {
super(context); super(context);
mTimer = runtime.timers.getTimerForCurrentThread(); mTimer = runtime.timers.getTimerForCurrentThread();
mLoopers = runtime.loopers; mLoopers = runtime.loopers;
mEmitter = new EventEmitter(runtime.bridges, mTimer); mEmitter = new EventEmitter(runtime.bridges);
mUiHandler = runtime.uiHandler; mUiHandler = runtime.uiHandler;
setUpEvents(); setUpEvents();
} }
@@ -70,7 +70,6 @@ public class JsDialogBuilder extends MaterialDialog.Builder {
public void onShowCalled() { public void onShowCalled() {
mTimer.postDelayed(() -> mWaitId = mLoopers.waitWhenIdle(), 0); mTimer.postDelayed(() -> mWaitId = mLoopers.waitWhenIdle(), 0);
} }
public JsDialog getDialog() { public JsDialog getDialog() {

View File

@@ -144,7 +144,6 @@ public class Dialogs {
} }
return new JsDialogBuilder(context, mRuntime) return new JsDialogBuilder(context, mRuntime)
.theme(Theme.LIGHT); .theme(Theme.LIGHT);
} }
public class NonUiDialogs { public class NonUiDialogs {