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

Update file dialog example to be non-blocking. #5697

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

hydra
Copy link

@hydra hydra commented Feb 8, 2025

This PR updates the file dialog example to be non-blocking, additionally:

  • the 'Open file...' button is disabled while picking.
  • the main windows is responsive while picking - you can still drag files onto it and resize it just fine.
  • the picked file message is removed if the dialog is cancelled (try picking, then picking again and cancelling)
  • added 'try_lock' to mutex API, with tests.

The result of a pick is recorded and kept in the state as before.

Video:
https://github.com/user-attachments/assets/29dd4cd9-b769-44f2-abe2-bd533728cdba

Related issues:

Related discussions:

hydra added 6 commits February 8, 2025 17:45
* the button is disabled while picking.
* the main windows is responsive while picking - you can still drag files onto it.
* the result of a pick is recorded and kept in the state as before.
…c::Mutex.

* std::sync::Mutex is disallowed by clippy config.
@hydra hydra force-pushed the file-dialog-non-blocking-1 branch from de79199 to e3d9d53 Compare February 8, 2025 16:46
@hydra hydra marked this pull request as ready for review February 8, 2025 16:49
@hydra
Copy link
Author

hydra commented Feb 8, 2025

This is my first PR for egui, be gentle 😄

@hydra
Copy link
Author

hydra commented Feb 8, 2025

Also, I know this is just an example, but perhaps after merging this you might be interested in a PR to move the FilePicker from this PR into the main code base somewhere and expanding the API for the common use cases (files, directories, specification of path and file types).

I note that other GUI frameworks I've used have 'rfd' features and similar APIs.
Cushy - https://github.com/khonsulabs/cushy/blob/main/src/dialog.rs#L352-L464

@YgorSouza
Copy link
Contributor

Also, I know this is just an example, but perhaps after merging this you might be interested in a PR to move the FilePicker from this PR into the main code base somewhere and expanding the API for the common use cases (files, directories, specification of path and file types).

I note that other GUI frameworks I've used have 'rfd' features and similar APIs. Cushy - https://github.com/khonsulabs/cushy/blob/main/src/dialog.rs#L352-L464

I think it would be hard to implement this in a way that covers all the use cases of egui. For example, on wasm you can't spawn threads, so you have to use async, and then in rfd you have to pick which async backend you're using (async-std or tokio). Then there's the game engines and such. There are a couple of file pickers implemented as egui widgets instead of using rfd, as third-party crates. They are listed in the wiki.

@hydra
Copy link
Author

hydra commented Feb 9, 2025

Also, I know this is just an example, but perhaps after merging this you might be interested in a PR to move the FilePicker from this PR into the main code base somewhere and expanding the API for the common use cases (files, directories, specification of path and file types).

I note that other GUI frameworks I've used have 'rfd' features and similar APIs.
Cushy - https://github.com/khonsulabs/cushy/blob/main/src/dialog.rs#L352-L464

Also, I know this is just an example, but perhaps after merging this you might be interested in a PR to move the FilePicker from this PR into the main code base somewhere and expanding the API for the common use cases (files, directories, specification of path and file types).
I note that other GUI frameworks I've used have 'rfd' features and similar APIs. Cushy - https://github.com/khonsulabs/cushy/blob/main/src/dialog.rs#L352-L464

I think it would be hard to implement this in a way that covers all the use cases of egui. For example, on wasm you can't spawn threads, so you have to use async, and then in rfd you have to pick which async backend you're using (async-std or tokio). Then there's the game engines and such. There are a couple of file pickers implemented as egui widgets instead of using rfd, as third-party crates. They are listed in the wiki.

Yes, I understand that there's various backends for async and limitations with wasm. I feel it would still be ok to provide solution for the common native desktop use-case however, along with appropriate module documentation containing links to the wiki of course. This feels like it would serve the community better rather than requiring each egui developer to implement, test and debug their own API for a common use-case no?

@landaire
Copy link
Contributor

IMO this should supplement the existing blocking example, possibly as a button indicating a blocking dialog and another indicating non-blocking.

Copy link
Owner

@emilk emilk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The blocking rfd calls need to be on the main thread: https://docs.rs/rfd/latest/rfd/#macos-non-windowed-applications-async-and-threading

To get non-blocking file dialogs, you should use the async versions instead.

In any case, I'd like to keep the original example simple. If you want to make an async version, I suggest you create a separate example for it.

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

Successfully merging this pull request may close these issues.

4 participants