Skip to content

Commit

Permalink
App.run -> start, App.runOnUIThread
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed May 17, 2021
1 parent 39fc5f8 commit 5e1e838
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,6 @@ public void accept(Event e) {
public static void main(String[] args) {
App.init();
new Example();
App.run();
App.start();
}
}
16 changes: 15 additions & 1 deletion macos/cc/App.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<void**>(&env2), JNI_VERSION_1_8) == JNI_OK) {
jwm::classes::Runnable::run(env2, callbackRef);
env2->DeleteGlobalRef(callbackRef);
}
});
}
19 changes: 18 additions & 1 deletion shared/cc/impl/Library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion shared/cc/impl/Library.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion shared/java/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 5e1e838

Please sign in to comment.