2023-08-15 17:20:19 +02:00
|
|
|
// Copyright 2023 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
|
|
//! Choose a location to save a file to.
|
|
|
|
|
//!
|
|
|
|
|
//! Check out the [open-dialog](https://github.com/pop-os/libcosmic/tree/master/examples/open-dialog)
|
|
|
|
|
//! example in our repository.
|
|
|
|
|
|
2024-01-22 08:08:45 +01:00
|
|
|
#[cfg(feature = "xdg-portal")]
|
|
|
|
|
pub use portal::{file, Response};
|
2023-08-15 17:20:19 +02:00
|
|
|
|
2024-01-22 08:08:45 +01:00
|
|
|
#[cfg(feature = "rfd")]
|
|
|
|
|
pub use rust_fd::{file, Response};
|
|
|
|
|
|
|
|
|
|
use super::Error;
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
|
|
/// A builder for an save file dialog.
|
|
|
|
|
#[derive(derive_setters::Setters)]
|
2023-08-15 17:20:19 +02:00
|
|
|
#[must_use]
|
|
|
|
|
pub struct Dialog {
|
|
|
|
|
/// The label for the dialog's window title.
|
|
|
|
|
title: String,
|
|
|
|
|
|
|
|
|
|
/// The label for the accept button. Mnemonic underlines are allowed.
|
2024-01-22 08:08:45 +01:00
|
|
|
#[cfg(feature = "xdg-portal")]
|
|
|
|
|
#[setters(skip)]
|
2023-08-15 17:20:19 +02:00
|
|
|
accept_label: Option<String>,
|
|
|
|
|
|
|
|
|
|
/// Modal dialogs require user input before continuing the program.
|
2024-01-22 08:08:45 +01:00
|
|
|
#[cfg(feature = "xdg-portal")]
|
|
|
|
|
#[setters(skip)]
|
2023-08-15 17:20:19 +02:00
|
|
|
modal: bool,
|
|
|
|
|
|
2024-01-22 08:08:45 +01:00
|
|
|
/// Set starting file name of the dialog.
|
2023-08-15 17:20:19 +02:00
|
|
|
#[setters(strip_option)]
|
2024-01-22 08:08:45 +01:00
|
|
|
file_name: Option<String>,
|
2023-08-15 17:20:19 +02:00
|
|
|
|
2024-01-22 08:08:45 +01:00
|
|
|
/// Sets the starting directory of the dialog.
|
2023-08-15 17:20:19 +02:00
|
|
|
#[setters(strip_option)]
|
2024-01-22 08:08:45 +01:00
|
|
|
directory: Option<PathBuf>,
|
2023-08-15 17:20:19 +02:00
|
|
|
|
|
|
|
|
/// Sets the absolute path of the file
|
2024-01-22 08:08:45 +01:00
|
|
|
#[cfg(feature = "xdg-portal")]
|
|
|
|
|
#[setters(skip)]
|
2023-08-15 17:20:19 +02:00
|
|
|
current_file: Option<PathBuf>,
|
|
|
|
|
|
|
|
|
|
/// Adds a list of choices.
|
2024-01-22 08:08:45 +01:00
|
|
|
#[cfg(feature = "xdg-portal")]
|
|
|
|
|
#[setters(skip)]
|
2023-08-15 17:20:19 +02:00
|
|
|
choices: Vec<super::Choice>,
|
|
|
|
|
|
|
|
|
|
/// Specifies the default file filter.
|
2024-01-22 08:08:45 +01:00
|
|
|
#[cfg(feature = "xdg-portal")]
|
|
|
|
|
#[setters(skip)]
|
2023-08-15 17:20:19 +02:00
|
|
|
current_filter: Option<super::FileFilter>,
|
|
|
|
|
|
|
|
|
|
/// A collection of file filters.
|
2024-01-22 08:08:45 +01:00
|
|
|
#[setters(skip)]
|
2023-08-15 17:20:19 +02:00
|
|
|
filters: Vec<super::FileFilter>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Dialog {
|
2024-01-22 08:08:45 +01:00
|
|
|
pub const fn new() -> Self {
|
2023-08-15 17:20:19 +02:00
|
|
|
Self {
|
|
|
|
|
title: String::new(),
|
2024-01-22 08:08:45 +01:00
|
|
|
#[cfg(feature = "xdg-portal")]
|
2023-08-15 17:20:19 +02:00
|
|
|
accept_label: None,
|
2024-01-22 08:08:45 +01:00
|
|
|
#[cfg(feature = "xdg-portal")]
|
2023-08-15 17:20:19 +02:00
|
|
|
modal: true,
|
2024-01-22 08:08:45 +01:00
|
|
|
file_name: None,
|
|
|
|
|
directory: None,
|
|
|
|
|
#[cfg(feature = "xdg-portal")]
|
2023-08-15 17:20:19 +02:00
|
|
|
current_file: None,
|
2024-01-22 08:08:45 +01:00
|
|
|
#[cfg(feature = "xdg-portal")]
|
2023-08-15 17:20:19 +02:00
|
|
|
current_filter: None,
|
2024-01-22 08:08:45 +01:00
|
|
|
#[cfg(feature = "xdg-portal")]
|
2023-08-15 17:20:19 +02:00
|
|
|
choices: Vec::new(),
|
|
|
|
|
filters: Vec::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 08:08:45 +01:00
|
|
|
/// The label for the accept button. Mnemonic underlines are allowed.
|
|
|
|
|
#[cfg(feature = "xdg-portal")]
|
|
|
|
|
pub fn accept_label(mut self, label: impl Into<String>) -> Self {
|
|
|
|
|
self.accept_label = Some(label.into());
|
|
|
|
|
self
|
2023-08-15 17:20:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Adds a choice.
|
2024-01-22 08:08:45 +01:00
|
|
|
#[cfg(feature = "xdg-portal")]
|
2023-08-15 17:20:19 +02:00
|
|
|
pub fn choice(mut self, choice: impl Into<super::Choice>) -> Self {
|
|
|
|
|
self.choices.push(choice.into());
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 08:08:45 +01:00
|
|
|
/// Set the current file filter.
|
|
|
|
|
#[cfg(feature = "xdg-portal")]
|
|
|
|
|
pub fn current_filter(mut self, filter: impl Into<super::FileFilter>) -> Self {
|
|
|
|
|
self.current_filter = Some(filter.into());
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 17:20:19 +02:00
|
|
|
/// Adds a files filter.
|
|
|
|
|
pub fn filter(mut self, filter: impl Into<super::FileFilter>) -> Self {
|
|
|
|
|
self.filters.push(filter.into());
|
|
|
|
|
self
|
|
|
|
|
}
|
2024-01-22 08:08:45 +01:00
|
|
|
|
|
|
|
|
/// Modal dialogs require user input before continuing the program.
|
|
|
|
|
#[cfg(feature = "xdg-portal")]
|
|
|
|
|
pub fn modal(mut self, modal: bool) -> Self {
|
|
|
|
|
self.modal = modal;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a save file dialog request.
|
|
|
|
|
pub async fn save_file(self) -> Result<Response, Error> {
|
|
|
|
|
file(self).await
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "xdg-portal")]
|
|
|
|
|
mod portal {
|
|
|
|
|
use super::Dialog;
|
|
|
|
|
use crate::dialog::file_chooser::Error;
|
|
|
|
|
use ashpd::desktop::file_chooser::SelectedFiles;
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
|
|
/// Create a save file dialog request.
|
|
|
|
|
pub async fn file(dialog: Dialog) -> Result<Response, Error> {
|
|
|
|
|
ashpd::desktop::file_chooser::SaveFileRequest::default()
|
|
|
|
|
.title(Some(dialog.title.as_str()))
|
|
|
|
|
.accept_label(dialog.accept_label.as_deref())
|
|
|
|
|
.modal(dialog.modal)
|
|
|
|
|
.choices(dialog.choices)
|
|
|
|
|
.filters(dialog.filters)
|
|
|
|
|
.current_filter(dialog.current_filter)
|
|
|
|
|
.current_name(dialog.file_name.as_deref())
|
|
|
|
|
.current_folder::<&Path>(dialog.directory.as_deref())
|
|
|
|
|
.map_err(Error::SetDirectory)?
|
|
|
|
|
.current_file::<&Path>(dialog.current_file.as_deref())
|
|
|
|
|
.map_err(Error::SetAbsolutePath)?
|
|
|
|
|
.send()
|
|
|
|
|
.await
|
|
|
|
|
.map_err(Error::Save)?
|
|
|
|
|
.response()
|
|
|
|
|
.map_err(Error::Save)
|
|
|
|
|
.map(Response)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A dialog response containing the selected file or folder.
|
|
|
|
|
pub struct Response(pub SelectedFiles);
|
|
|
|
|
|
|
|
|
|
impl Response {
|
|
|
|
|
pub fn choices(&self) -> &[(String, String)] {
|
|
|
|
|
self.0.choices()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn url(&self) -> Option<&Url> {
|
|
|
|
|
self.0.uris().first()
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-15 17:20:19 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-22 08:08:45 +01:00
|
|
|
#[cfg(feature = "rfd")]
|
|
|
|
|
mod rust_fd {
|
|
|
|
|
use super::Dialog;
|
|
|
|
|
use crate::dialog::file_chooser::Error;
|
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
|
|
/// Create a save file dialog request.
|
|
|
|
|
pub async fn file(dialog: Dialog) -> Result<Response, Error> {
|
|
|
|
|
let mut request = rfd::AsyncFileDialog::new().set_title(dialog.title);
|
|
|
|
|
|
|
|
|
|
if let Some(directory) = dialog.directory {
|
|
|
|
|
request = request.set_directory(directory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(file_name) = dialog.file_name {
|
|
|
|
|
request = request.set_file_name(file_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for filter in dialog.filters {
|
|
|
|
|
request = request.add_filter(filter.description, &filter.extensions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(handle) = request.save_file().await {
|
|
|
|
|
let url = Url::from_file_path(handle.path()).map_err(|_| Error::UrlAbsolute)?;
|
|
|
|
|
|
|
|
|
|
return Ok(Response(Some(url)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(Response(None))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A dialog response containing the selected file or folder.
|
|
|
|
|
pub struct Response(Option<Url>);
|
|
|
|
|
|
|
|
|
|
impl Response {
|
|
|
|
|
pub fn url(&self) -> Option<&Url> {
|
|
|
|
|
self.0.as_ref()
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-15 17:20:19 +02:00
|
|
|
}
|