Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call the macos file dialog from Main thread only #46

Closed
AChep opened this issue Nov 29, 2021 · 8 comments
Closed

Call the macos file dialog from Main thread only #46

AChep opened this issue Nov 29, 2021 · 8 comments

Comments

@AChep
Copy link
Contributor

AChep commented Nov 29, 2021

The latest macos versions will crash if you try to call its UI components from a non-main thread. Therefore it would be nice if we dispatch all of the calls to the main thread.

@btzy
Copy link
Owner

btzy commented Nov 29, 2021

I'm thinking this might be more of the responsibility of the user instead of the library. Most UI libraries assume that UI updates are only invoked on the main thread.

As far as I know, both GTK and Windows require all UI updates to be from one specific thread only. Some GTK functions may be called by other threads, but you cannot make a UI update from those threads. So if we want your behaviour for MacOS, we ought to implement it for Windows and Linux too.

But I think we should not provide this feature at all - most people do not need such a feature, and it complicates the implementations. Currently, the calling thread gets blocked until the file is selected. But if the API is meant to be called from multiple threads, we will need to determine what we should do when multiple threads call it simultaneously, and ensure that it can be implemented consistently across all platforms - I think this will be over-complicated or impossible.

Those who want such a feature can easily implement the synchronisation on their own - just dispatch the call on your own, and invoke NFDe inside the callback that runs on the main thread.

@AChep
Copy link
Contributor Author

AChep commented Nov 29, 2021

Makes sense, even if it hurts my soul knowing that I can not get the main thread from Java -- therefore need another native layer that dispatches the stuff. 😢 ☕

@AChep AChep closed this as completed Nov 29, 2021
@btzy
Copy link
Owner

btzy commented Nov 29, 2021

Hmm, I thought you already have to write some wrapper C code to invoke anything from Java - is this not the case? Could you give me a link to your project where you use this (if it's on GitHub)?

@AChep
Copy link
Contributor Author

AChep commented Nov 29, 2021

I can not give you the full source code, but we Java developers have this little library called Java Native Access that helps with that issue.

An example:

interface NfdLibraryPure {
    /* initialize NFD - call this for every thread that might use NFD, before calling any other NFD
     * functions on that thread */
    fun NFD_Init(
    ): NfdResult

    /* call this to de-initialize NFD, if NFD_Init returned NFD_OKAY */
    fun NFD_Quit()

    /*
     * Select folder dialog
     *
     * It is the caller's responsibility to free `outPath` via NFD_FreePathN() if
     * this function returns NFD_OKAY.
     *
     * If defaultPath is NULL, the operating system will decide
     */
    fun NFD_PickFolderN(outPath: PointerByReference, defaultPath: WString?): NfdResult

    fun NFD_FreePathN(filePath: PointerByReference)
}

@AChep
Copy link
Contributor Author

AChep commented Nov 29, 2021

it is basically a C wrapper in disguise 😄

@btzy
Copy link
Owner

btzy commented Nov 29, 2021

I see, interesting :)

@AChep
Copy link
Contributor Author

AChep commented Nov 29, 2021

I guess if I'll have some free time, I will create a Java wrapper library for this one :) For JetPack Compose it is gonna be a perfect match.

@btzy
Copy link
Owner

btzy commented Nov 29, 2021

Thanks! Maybe you could also take a look at the original NFD (that this project was forked from), I think it has some kind of Java bindings (https://javadoc.lwjgl.org/org/lwjgl/util/nfd/NativeFileDialog.html). But I think they also don't have support for calling the function via a non-main thread.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants