diff --git a/examples/Example.java b/examples/Example.java index 19150430..e8016523 100644 --- a/examples/Example.java +++ b/examples/Example.java @@ -202,6 +202,6 @@ public void accept(Event e) { public static void main(String[] args) { App.init(); new Example(); - App.run(); + App.start(); } } diff --git a/macos/cc/App.mm b/macos/cc/App.mm index fc747e07..358fdef0 100644 --- a/macos/cc/App.mm +++ b/macos/cc/App.mm @@ -34,7 +34,7 @@ [pool release]; } -extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_jwm_App_run +extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_jwm_App_start (JNIEnv* env, jclass jclass) { [NSApp run]; return EXIT_SUCCESS; @@ -44,4 +44,18 @@ (JNIEnv* env, jclass jclass) { [NSApp stop:nil]; [NSApp terminate:nil]; +} + +extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_jwm_App_runOnUIThread + (JNIEnv* env, jclass jclass, jobject callback) { + JavaVM* javaVM; + env->GetJavaVM(&javaVM); + auto callbackRef = env->NewGlobalRef(callback); + dispatch_async(dispatch_get_main_queue(), ^{ + JNIEnv* env2; + if (javaVM->GetEnv(reinterpret_cast(&env2), JNI_VERSION_1_8) == JNI_OK) { + jwm::classes::Runnable::run(env2, callbackRef); + env2->DeleteGlobalRef(callbackRef); + } + }); } \ No newline at end of file diff --git a/shared/cc/impl/Library.cc b/shared/cc/impl/Library.cc index 27edcb43..311d3cbf 100644 --- a/shared/cc/impl/Library.cc +++ b/shared/cc/impl/Library.cc @@ -35,10 +35,26 @@ namespace jwm { Throwable::exceptionThrown(env); } - void accept(JNIEnv* env, jobject consumer, jobject event) { + bool accept(JNIEnv* env, jobject consumer, jobject event) { env->CallVoidMethod(consumer, kAccept, event); + return Throwable::exceptionThrown(env); + } + } + + namespace Runnable { + jmethodID kRun; + + void onLoad(JNIEnv* env) { + jclass cls = env->FindClass("java/lang/Runnable"); + Throwable::exceptionThrown(env); + kRun = env->GetMethodID(cls, "run", "()V"); Throwable::exceptionThrown(env); } + + bool run(JNIEnv* env, jobject runnable) { + env->CallVoidMethod(runnable, kRun); + return Throwable::exceptionThrown(env); + } } namespace Native { @@ -147,6 +163,7 @@ extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_jwm_impl_Library__1nAfterLo (JNIEnv* env, jclass jclass) { jwm::classes::Throwable::onLoad(env); jwm::classes::Consumer::onLoad(env); + jwm::classes::Runnable::onLoad(env); jwm::classes::Native::onLoad(env); jwm::classes::EventClose::onLoad(env); jwm::classes::EventKeyboard::onLoad(env); diff --git a/shared/cc/impl/Library.hh b/shared/cc/impl/Library.hh index 2867b566..c312f095 100644 --- a/shared/cc/impl/Library.hh +++ b/shared/cc/impl/Library.hh @@ -19,7 +19,12 @@ namespace jwm { namespace Consumer { extern jmethodID kAccept; - void accept(JNIEnv* env, jobject consumer, jobject event); + bool accept(JNIEnv* env, jobject consumer, jobject event); + } + + namespace Runnable { + extern jmethodID kRun; + bool run(JNIEnv* env, jobject runnable); } namespace Native { diff --git a/shared/java/App.java b/shared/java/App.java index 16bf857f..8e53ef92 100644 --- a/shared/java/App.java +++ b/shared/java/App.java @@ -36,7 +36,9 @@ public static Window makeWindow() { /** * Will block until you call {@link #terminate()} */ - public static native int run(); + public static native int start(); + + public static native void runOnUIThread(Runnable callback); public static native void terminate();