HumbleUI / JWM

Cross-platform window management and OS integration library for Java
Apache License 2.0
571 stars 46 forks source link

macOS: Work around for -XstartOnMainThread #210

Closed mworzala closed 2 years ago

mworzala commented 2 years ago

JavaFX appears to use performSelectorOnMainThread to start the app, perhaps it is worth doing something similar. Should also be easier to work with native image, as apparently the option does not work (though I have not tested personally)

Eg

App.init(() -> {
    Window window = App.makeWindow();
    // ...

    App.start();
});
tonsky commented 2 years ago

Interesting! That would be a huge usability improvement. Can you check if it works? We have this https://github.com/HumbleUI/JWM/blob/def339bb8c0930075deb47481d748e057dedac79/macos/cc/App.mm#L85-L97 but as far as I remember it didn’t worked without -XstartOnFirstThread for some reason anyway

mworzala commented 2 years ago

but as far as I remember it didn’t worked without -XstartOnFirstThread for some reason anyway

As far as I could tell at a glance, this requires [NSApp run] to have been called for the inner part to be executed.

I do seem to have a version working locally without -XstartOnFirstThread, although the API will need to change. There needs to be some runnable to do user init. It could be what I posted above, but I am not sure there is any reason to separate App#init and App#start, eg something like

App.init(() -> {
    Window window = App.makeWindow();

    // ...
}); // blocks for app runtime

Let me know what you think regarding API while I clean up my local version.

(I am not convinced I can explain how performSelectorOnMainThread works, but it does seem to work. Perhaps someone with more experience can comment after seeing it)

tonsky commented 2 years ago

The design you proposed is nice. So init will do what now init + start do combined, right? And init will block, in the hope you could do rest of your logic inside event handlers, right?