vinceglb / FileKit

Pick and save Files, Medias and Folder for Kotlin Multiplatform / KMP and Compose Multiplatform / CMP
https://vinceglb.github.io/FileKit/
MIT License
513 stars 15 forks source link

[win] Support modal dialogs (hwndOwner) #18

Open iamcalledrob opened 4 months ago

iamcalledrob commented 4 months ago

Hi there, very useful library!

It would be great to be able to open the picker dialogs modally on Windows. This is important to tie the lifecycle and visibility of the picker window to its parent window, e.g. so that closing the parent window closes the picker. I think this should probably be the default behaviour.

It looks like this could be achieved by passing an optional window handle into Picker.pickFile, and then passing that down into WindowsFileChooser so it ends up as hwndOwner?

This could tie in neatly with the Compose library as an integration with FrameWindowScope, e.g. fun FrameWindowScope.rememberFilePickerLauncher(...) that opens modally by default?

vinceglb commented 4 months ago

Thank you again for submitting an issue! You're right, it's an interesting feature to add.

I quickly sketch this PR https://github.com/vinceglb/PickerKotlin/issues/18

Here are the main modifications:

Does this solve your problem?

iamcalledrob commented 4 months ago

Your sketch looks fantastic! Absolutely solves my problem.

Apologies, I realised that my suggestion to work with FrameWindowScope might not have been a good one, since there are other window scopes, e.g. DialogWindowScope (or just plain old WindowScope). I'd consider hanging off of WindowScope?

Personally (and this is subjective), I think opening modally should be the default behaviour. Every native app I've tried on windows launches its open/save panels modally. Plus, opening modally is a less complicated user experience to handle, since you don't need to handle user interactions in the parent window while picking files.

PS: I haven't tested this library on macOS, but I imagine a similar option could be specified there, e.g. to invoke NSOpenPanel.beginSheetModalForWindow

vinceglb commented 4 months ago

Yes you're right, using WindowScope is better here. I just replace Frame and FrameWindowScope by Window and WindowScope.

I tested on macOS and it seems to open modally by default.

About your second point, I'm not sure how to handle the default behavior. How does the library get the parentWindow if the developer does not pass the argument? If the argument parentWindow is null, does the library create its own parentWindow? (I'm not sure if it is possible?). Is it interesting to be able to deactivate the default behavior?

Do you have a suggestion for implementing this default behavior?