feat:add nativeChannel to let js call native view's method

This commit is contained in:
pengfei.zhou 2019-11-18 10:14:33 +08:00
parent ae5287fd7e
commit 8c83c3d13e
4 changed files with 62 additions and 3 deletions

View File

@ -19,12 +19,17 @@ import pub.doric.DoricContext;
import pub.doric.async.AsyncResult; import pub.doric.async.AsyncResult;
import pub.doric.extension.bridge.DoricMethod; import pub.doric.extension.bridge.DoricMethod;
import pub.doric.extension.bridge.DoricPlugin; import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.extension.bridge.DoricPromise;
import pub.doric.utils.DoricLog; import pub.doric.utils.DoricLog;
import pub.doric.utils.ThreadMode; import pub.doric.utils.ThreadMode;
import pub.doric.shader.RootNode; import pub.doric.shader.RootNode;
import com.github.pengfeizhou.jscore.ArchiveException;
import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue;
import com.github.pengfeizhou.jscore.JavaValue;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
@ -71,6 +76,37 @@ public class ShaderPlugin extends DoricJavaPlugin {
e.printStackTrace(); e.printStackTrace();
DoricLog.e("Shader.render:error%s", e.getLocalizedMessage()); DoricLog.e("Shader.render:error%s", e.getLocalizedMessage());
} }
}
@DoricMethod
public void command(JSDecoder jsDecoder, final DoricPromise doricPromise) {
try {
final JSObject jsObject = jsDecoder.decode().asObject();
final JSValue[] viewIds = jsObject.getProperty("viewIds").asArray().toArray();
final String name = jsObject.getProperty("name").asString().value();
getDoricContext().getDriver().asyncCall(new Callable<JavaValue>() {
@Override
public JavaValue call() throws Exception {
return new JavaValue("called");
}
}, ThreadMode.UI).setCallback(new AsyncResult.Callback<JavaValue>() {
@Override
public void onResult(JavaValue result) {
doricPromise.resolve(result);
}
@Override
public void onError(Throwable t) {
doricPromise.reject(new JavaValue(t.getLocalizedMessage()));
}
@Override
public void onFinish() {
} }
});
} catch (ArchiveException e) {
e.printStackTrace();
}
}
} }

View File

@ -73,6 +73,13 @@ class ListPanel extends Panel {
it.onClick = () => { it.onClick = () => {
log(`Click item at ${idx}`) log(`Click item at ${idx}`)
it.height += 10 it.height += 10
it.nativeChannel(context, "getWidth")().then(
resolve => {
log(`resolve,${resolve}`)
},
reject => {
log(`reject,${reject}`)
})
} }
}) })
}, },

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import { LayoutConfig, Group, Property, IView } from "./view"; import { Group, Property, IView } from "./view";
import { Gravity } from "../util/gravity"; import { Gravity } from "../util/gravity";
export interface IStack extends IView { export interface IStack extends IView {
@ -22,8 +22,6 @@ export interface IStack extends IView {
export class Stack extends Group implements IStack { export class Stack extends Group implements IStack {
} }
export class Root extends Stack { export class Root extends Stack {
} }

View File

@ -255,6 +255,24 @@ export abstract class View implements Modeling, IView {
in(group: Group) { in(group: Group) {
group.addChild(this) group.addChild(this)
} }
nativeChannel(context: any, name: string) {
let thisView: View | undefined = this
return function (...args: any) {
const func = context.shader.command
const viewIds = []
while (thisView != undefined) {
viewIds.push(thisView.viewId)
thisView = thisView.superview
}
const params = {
viewIds: viewIds.reverse(),
name,
args,
}
return Reflect.apply(func, undefined, [params]) as Promise<any>
}
}
} }
export abstract class Superview extends View { export abstract class Superview extends View {