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

use XDG Desktop Portal on Linux #36

Closed
Be-ing opened this issue Jan 2, 2022 · 14 comments · Fixed by #41
Closed

use XDG Desktop Portal on Linux #36

Be-ing opened this issue Jan 2, 2022 · 14 comments · Fixed by #41
Labels
enhancement New feature or request

Comments

@Be-ing
Copy link
Contributor

Be-ing commented Jan 2, 2022

Using XDG Desktop Portal would solve two problems. It would automatically use the desktop environment's dialog, plus it would let rfd work in Flatpak sandboxes. IIUC this could be done with the dbus crate. If the portal isn't available, the old GTK implementation could be used as a fallback, but I think that should be turned into a Cargo feature to remove this crate's dependency on GTK.

@PolyMeilex
Copy link
Owner

PolyMeilex commented Jan 2, 2022

GTK supports XDG desktop portal by default, I use it to publish one of my apps as Flatpack.
But yeah, we could implement our own dbus version, probably using zbus crate, in case someone dos not want to depend on GTK.

EDIT: It would also remove a lot of if'y unsafe code that GTK forces us to use. DBus version would basically be 100% safe Rust.

@PolyMeilex PolyMeilex added the enhancement New feature or request label Jan 2, 2022
@Be-ing
Copy link
Contributor Author

Be-ing commented Jan 2, 2022

in case someone dos not want to depend on GTK

I think this would be quite common. I intend on distributing my application, which otherwise has no dependency on GTK, as a Flatpak. It would be silly to require the GNOME Flatpak runtime just for a file picker dialog -- even if the Flatpak gets run on KDE and GTK is only used to indirectly open KDE's file picker through the XDG Desktop Portal.

@Be-ing
Copy link
Contributor Author

Be-ing commented Jan 2, 2022

Hmm, apparently the Freedesktop Flatpak runtime has GTK3, so I suppose this wouldn't make a difference with regard to packaging Rust applications in Flatpaks. However it would still be good to remove the dependency on GTK.

@Be-ing
Copy link
Contributor Author

Be-ing commented Jan 3, 2022

Alternatively this could use the zbus crate which is a pure Rust library for dbus.

@PolyMeilex
Copy link
Owner

But yeah, we could implement our own dbus version, probably using zbus crate, in case someone dos not want to depend on GTK.

Yep, that was my plan already 😉

@Be-ing
Copy link
Contributor Author

Be-ing commented Jan 17, 2022

I just found out about the ashpd crate which provides a nice Rust API for the XDG Desktop Portal using zbus. I'll work on using that crate to implement this.

@PolyMeilex
Copy link
Owner

Oh, that's a great find! I totally forgot about its existence.

@Be-ing
Copy link
Contributor Author

Be-ing commented Jan 17, 2022

I don't know what to do to implement the generic message dialog stuff with the XDG Desktop Portal... nor am I interested in that functionality because I can use the GUI library I'm already using (SixtyFPS) to do that. I can think of a few ways to handle this, in order of my preferences:

  1. Move the message dialog code to a separate crate.
  2. Put the message dialog code behind a Cargo feature.
  3. Abuse the notifications of the XDG Desktop Portal for this... not sure if that could really work.
  4. Use XDG Desktop Portal for the file dialog and GTK for the message dialog. That would kinda defeat the purpose for me because I want to remove the dependency on GTK from my application.

@PolyMeilex
Copy link
Owner

Yeah, it makes sense, there is no point in placing msg dialogs behind desktop portal.

RFD already has differences depending on the platform, all of those are noted in docs, and for example sync dialogs are behind conditional compilation because wasm does not support them.

My idea would be to simply not compile message module when xdg desktop portal feature is enabled, and document it property to let the user know what are the downsides of using this backend of rfd.

@Be-ing
Copy link
Contributor Author

Be-ing commented Jan 17, 2022

message module

The message dialog code is within the dialog module currently. Shall I start with a PR to move it to its own module? Or a submodule of dialog?

@PolyMeilex
Copy link
Owner

PolyMeilex commented Jan 17, 2022

Yeah sure, we can split it into two modules, backends already follow this convention, so it makes sense to do the same for dialog module

@Be-ing
Copy link
Contributor Author

Be-ing commented Jan 17, 2022

I think I'm in over my head here... ashpd only has an async API, but I can't use await to implement AsyncFilePickerDialogImpl because async fn is still not supported for traits. I suppose this library could run its own async executor... which seems like an ugly hack that might not compose well with whatever executor the application using this library is running. I don't understand how async libraries can be composed with this limitation.

@PolyMeilex
Copy link
Owner

PolyMeilex commented Jan 17, 2022

Not sure if I follow, why do we need async trait fn here? Can't we just return impl Future<> like we do for every other backend?
I'll have a closer look at ashpd tomorrow, but I'm pretty sure it's not a problem async fn is just a syntax sugar around Futures.

Something like this:

 pub fn pick_file(self) -> impl Future<Output = Option<File>> {
        async move {
                whatever().await
        }
 }

Or like this:

 pub fn pick_file(self) -> impl Future<Output = Option<File>> {
       inner()
 }
 
 async fn inner() -> Option<File> {
         whatever().await
 }

Should be enough.

Or am I missing an important piece of the puzzle somewhere?

@Be-ing
Copy link
Contributor Author

Be-ing commented Jan 18, 2022

Ah, thanks for the tip. This is getting somewhere now:

impl AsyncFilePickerDialogImpl for FileDialog {
    fn pick_file_async(self) -> DialogFutureType<Option<FileHandle>> {
        Box::pin(async {
            async_function_call.await;
        })
    }
}

Be-ing added a commit to Be-ing/rfd that referenced this issue Jan 18, 2022
This new backend does not support MessageDialog nor
AsyncMessageDialog because there is no corresponding API in the
XDG Desktop Portal.

The GTK backend is still available with the new `gtk3` Cargo
feature.

Fixes PolyMeilex#36
Be-ing added a commit to Be-ing/rfd that referenced this issue Jan 18, 2022
This new backend does not support MessageDialog nor
AsyncMessageDialog because there is no corresponding API in the
XDG Desktop Portal.

The GTK backend is still available with the new `gtk3` Cargo
feature.

Fixes PolyMeilex#36
Be-ing added a commit to Be-ing/rfd that referenced this issue Jan 18, 2022
This new backend does not support MessageDialog nor
AsyncMessageDialog because there is no corresponding API in the
XDG Desktop Portal.

The GTK backend is still available with the new `gtk3` Cargo
feature.

Fixes PolyMeilex#36
Be-ing added a commit to Be-ing/rfd that referenced this issue Jan 18, 2022
This new backend does not support MessageDialog nor
AsyncMessageDialog because there is no corresponding API in the
XDG Desktop Portal.

The GTK backend is still available with the new `gtk3` Cargo
feature.

Fixes PolyMeilex#36
Be-ing added a commit to Be-ing/rfd that referenced this issue Jan 18, 2022
This new backend does not support MessageDialog nor
AsyncMessageDialog because there is no corresponding API in the
XDG Desktop Portal.

The GTK backend is still available with the new `gtk3` Cargo
feature.

Fixes PolyMeilex#36
Be-ing added a commit to Be-ing/rfd that referenced this issue Jan 18, 2022
This new backend does not support MessageDialog nor
AsyncMessageDialog because there is no corresponding API in the
XDG Desktop Portal.

The GTK backend is still available with the new `gtk3` Cargo
feature.

Fixes PolyMeilex#36
Be-ing added a commit to Be-ing/rfd that referenced this issue Jan 18, 2022
This new backend does not support MessageDialog nor
AsyncMessageDialog because there is no corresponding API in the
XDG Desktop Portal.

The GTK backend is still available with the new `gtk3` Cargo
feature.

Fixes PolyMeilex#36
Be-ing added a commit to Be-ing/rfd that referenced this issue Jan 18, 2022
This new backend does not support MessageDialog nor
AsyncMessageDialog because there is no corresponding API in the
XDG Desktop Portal.

The GTK backend is still available with the new `gtk3` Cargo
feature.

Fixes PolyMeilex#36
PolyMeilex pushed a commit that referenced this issue Jan 18, 2022
* use XDG Desktop Portal on Linux & BSDs

This new backend does not support MessageDialog nor
AsyncMessageDialog because there is no corresponding API in the
XDG Desktop Portal.

The GTK backend is still available with the new `gtk3` Cargo
feature.

Fixes #36

* replace smol with pollster

pollster is smaller than smol

* rename unwrap_or_warn to ok_or_warn

* reuse async functions to implement sync functions

* replace Option::ok with ok_or_warn

* impl From<FileHandle> for PathBuf

to reduce code duplication

* cargo fmt

* factor out file_chooser_proxy function

* add link to ashpd issue for RawWindowHandle

bilelmoussaoui/ashpd#40

* make gtk3 a default feature
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants