Skip to content

Commit

Permalink
Make Zenity backend optional
Browse files Browse the repository at this point in the history
Saves some Rust dependencies for users who are willing to require gtk3
(the default) or who don't want message dialogs at all.
  • Loading branch information
Ralith committed Nov 28, 2023
1 parent 81aa5d6 commit 6d8375a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ documentation = "https://docs.rs/rfd"
default = ["gtk3"]
file-handle-inner = []
gtk3 = ["gtk-sys", "glib-sys", "gobject-sys"]
xdg-portal = ["ashpd", "urlencoding", "pollster", "async-io", "futures-util"]
xdg-portal = ["ashpd", "urlencoding", "pollster"]
# Enables message dialogs on Linux when gtk3 is disabled and the end
# user has zenity installed
zenity = ["async-io", "futures-util"]
common-controls-v6 = ["windows-sys/Win32_UI_Controls"]

[dev-dependencies]
Expand Down
2 changes: 2 additions & 0 deletions src/backend/linux/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#[cfg(feature = "zenity")]
mod child_stdout;
#[cfg(feature = "zenity")]
pub(crate) mod zenity;
9 changes: 8 additions & 1 deletion src/backend/xdg_desktop_portal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ use std::path::PathBuf;

use crate::backend::DialogFutureType;
use crate::file_dialog::Filter;
#[cfg(feature = "zenity")]
use crate::message_dialog::MessageDialog;
use crate::{FileDialog, FileHandle, MessageButtons, MessageDialogResult};
use crate::{FileDialog, FileHandle};
#[cfg(feature = "zenity")]
use crate::{MessageButtons, MessageDialogResult};

use ashpd::desktop::file_chooser::{FileFilter, OpenFileRequest, SaveFileRequest};
// TODO: convert raw_window_handle::RawWindowHandle to ashpd::WindowIdentifier
Expand Down Expand Up @@ -192,14 +195,18 @@ impl AsyncFileSaveDialogImpl for FileDialog {
}
}

#[cfg(feature = "zenity")]
use crate::backend::MessageDialogImpl;
#[cfg(feature = "zenity")]
impl MessageDialogImpl for MessageDialog {
fn show(self) -> MessageDialogResult {
block_on(self.show_async())
}
}

#[cfg(feature = "zenity")]
use crate::backend::AsyncMessageDialogImpl;
#[cfg(feature = "zenity")]
impl AsyncMessageDialogImpl for MessageDialog {
fn show_async(self) -> DialogFutureType<MessageDialogResult> {
Box::pin(async move {
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@
//! they can all be installed simultaneously).
//!
//! The XDG Desktop Portal has no API for message dialogs, so the [MessageDialog] and
//! [AsyncMessageDialog] structs will not build with this backend.
//! [AsyncMessageDialog] structs will not build with this backend unless the `zenity` feature is
//! enabled. However, `zenity`-based dialogs will silently fail if the end user does not have the
//! `zenity` binary in `$PATH`.
//!
//! # macOS non-windowed applications, async, and threading
//!
Expand Down
9 changes: 7 additions & 2 deletions src/message_dialog.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#[cfg(any(not(target_os = "linux"), feature = "zenity"))]
use crate::backend::AsyncMessageDialogImpl;
#[cfg(any(not(target_os = "linux"), feature = "zenity"))]
use crate::backend::MessageDialogImpl;
use std::fmt::{Display, Formatter};

#[cfg(any(not(target_os = "linux"), feature = "zenity"))]
use std::future::Future;

use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};

/// Synchronous Message Dialog. Supported platforms:
/// * Windows
/// * macOS
/// * Linux (GTK only)
/// * Linux (GTK or Zenity only)
/// * WASM
#[derive(Default, Debug, Clone)]
pub struct MessageDialog {
Expand Down Expand Up @@ -71,6 +74,7 @@ impl MessageDialog {
}

/// Shows a message dialog and returns the button that was pressed.
#[cfg(any(not(target_os = "linux"), any(feature = "zenity", feature = "gtk3")))]
pub fn show(self) -> MessageDialogResult {
MessageDialogImpl::show(self)
}
Expand All @@ -79,7 +83,7 @@ impl MessageDialog {
/// Asynchronous Message Dialog. Supported platforms:
/// * Windows
/// * macOS
/// * Linux (GTK only)
/// * Linux (GTK or Zenity only)
/// * WASM
#[derive(Default, Debug, Clone)]
pub struct AsyncMessageDialog(MessageDialog);
Expand Down Expand Up @@ -131,6 +135,7 @@ impl AsyncMessageDialog {
}

/// Shows a message dialog and returns the button that was pressed.
#[cfg(any(not(target_os = "linux"), any(feature = "zenity", feature = "gtk3")))]
pub fn show(self) -> impl Future<Output = MessageDialogResult> {
AsyncMessageDialogImpl::show_async(self.0)
}
Expand Down

0 comments on commit 6d8375a

Please sign in to comment.