diff options
author | Reyk Floeter <reyk.floeter@googlemail.com> | 2019-12-10 15:24:36 +0000 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2019-12-10 19:56:09 +0100 |
commit | 2f3e2b5474834e3d733edf09b831c5607d451f49 (patch) | |
tree | 4b94e0e6874ae300f595e8dd9bffd06aec867543 /src/lib.rs | |
parent | 3b76a0a46ce38eef7fe3e5a969cec6ffee47f227 (diff) | |
download | dialog-rs-2f3e2b5474834e3d733edf09b831c5607d451f49.tar.gz dialog-rs-2f3e2b5474834e3d733edf09b831c5607d451f49.tar.bz2 |
Add FileSelection dialog type
This patch adds the FileSelection struct representing a file selection
dialog. It can be displayed using the backend’s show_file_selection
function.
Currently, we only support file open dialogs (i. e. choosing an
existing file). Support for save dialogs should be added in the future.
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 82 |
1 files changed, 81 insertions, 1 deletions
@@ -7,6 +7,7 @@ //! //! The `dialog` crate can be used to display different types of dialog boxes. The supported types //! are: +//! - [`FileSelection`][]: a file chooser dialog box //! - [`Input`][]: a text input dialog //! - [`Message`][]: a simple message box //! - [`Password`][]: a password input dialog @@ -70,6 +71,7 @@ //! ``` //! //! [`Dialog`]: backends/struct.Dialog.html +//! [`FileSelection`]: struct.FileSelection.html //! [`Input`]: struct.Input.html //! [`Message`]: struct.Message.html //! [`Password`]: struct.Password.html @@ -92,7 +94,11 @@ mod error; /// [`Backend`]: trait.Backend.html pub mod backends; -use std::env; +use dirs; +use std::{ + env, + path::{Path, PathBuf}, +}; pub use crate::error::{Error, Result}; @@ -342,6 +348,80 @@ impl DialogBox for Question { } } +/// A file chooser dialog box. +/// +/// This dialog box opens a file chooser with an optional title in the specified path. If the path +/// is not specified, it defaults to the user’s home directory. +/// +/// # Example +/// +/// ```no_run +/// use dialog::DialogBox; +/// +/// let choice = dialog::FileSelection::new("Please select a file") +/// .title("File Selection") +/// .path("/home/user/Downloads") +/// .show() +/// .expect("Could not display dialog box"); +/// println!("The user chose: {:?}", choice); +/// ``` +pub struct FileSelection { + text: String, + title: Option<String>, + path: Option<PathBuf>, +} + +impl FileSelection { + /// Creates a new file chooser with the given path. + pub fn new(text: impl Into<String>) -> FileSelection { + FileSelection { + text: text.into(), + title: None, + path: dirs::home_dir(), + } + } + + /// Sets the title of this file chooser dialog box. + /// + /// This method returns a reference to `self` to enable chaining. + pub fn title(&mut self, title: impl Into<String>) -> &mut FileSelection { + self.title = Some(title.into()); + self + } + + /// Sets the path of this file chooser dialog box. + /// + /// This method returns a reference to `self` to enable chaining. + pub fn path(&mut self, path: impl AsRef<Path>) -> &mut FileSelection { + self.path = Some(path.as_ref().to_path_buf()); + self + } + + /// Gets the path of this file chooser dialog box. + /// + /// This method returns the validated directory as a `String`. + pub fn path_to_string(&self) -> Option<String> { + match self.path { + Some(ref path) if path.is_dir() => { + // The backends expect a trailing / after the directory + path.to_str().map(|s| s.to_string() + "/") + } + _ => None, + } + } +} + +impl DialogBox for FileSelection { + type Output = Option<String>; + + fn show_with<B>(&self, backend: impl AsRef<B>) -> Result<Self::Output> + where + B: backends::Backend + ?Sized, + { + backend.as_ref().show_file_selection(self) + } +} + /// Creates a new instance of the default backend. /// /// The following steps are performed to determine the default backend: |