Skip to main content

dialog/backends/
mod.rs

1// Copyright (C) 2019 Robin Krahl <robin.krahl@ireas.org>
2// SPDX-License-Identifier: MIT
3
4mod dialog;
5mod kdialog;
6mod stdio;
7mod zenity;
8
9pub use crate::backends::dialog::Dialog;
10pub use crate::backends::kdialog::KDialog;
11pub use crate::backends::stdio::Stdio;
12pub use crate::backends::zenity::Zenity;
13
14use std::env;
15use std::path;
16
17use crate::Result;
18
19/// A dialog backend.
20///
21/// A dialog backend is a program that can be used to display dialog boxes.  Use the
22/// [`default_backend`][] function to create a new instance of the default backend, or choose a
23/// backend and create an instance manually.  To use a backend, pass it to the [`show_with`][]
24/// method of a dialog box.
25///
26/// [`default_backend`]: ../fn.default_backend.html
27/// [`show_with`]: ../trait.DialogBox.html#method.show_with
28pub trait Backend {
29    /// Shows the given input dialog and returns the input.
30    fn show_input(&self, input: &super::Input) -> Result<Option<String>>;
31
32    /// Shows the given message dialog.
33    fn show_message(&self, message: &super::Message) -> Result<()>;
34
35    /// Shows the given password dialog and returns the password.
36    fn show_password(&self, password: &super::Password) -> Result<Option<String>>;
37
38    /// Shows the given question dialog and returns the choice.
39    fn show_question(&self, question: &super::Question) -> Result<super::Choice>;
40
41    /// Shows the given file selection dialog and returns the file name.
42    fn show_file_selection(&self, file_selection: &super::FileSelection) -> Result<Option<String>>;
43}
44
45pub(crate) fn is_available(name: &str) -> bool {
46    if let Ok(path) = env::var("PATH") {
47        for part in path.split(':') {
48            if path::Path::new(part).join(name).exists() {
49                return true;
50            }
51        }
52    }
53    false
54}
55
56pub(crate) fn from_str(s: &str) -> Option<Box<dyn Backend>> {
57    match s.to_lowercase().as_ref() {
58        "dialog" => Some(Box::new(Dialog::new())),
59        "kdialog" => Some(Box::new(KDialog::new())),
60        "stdio" => Some(Box::new(Stdio::new())),
61        "zenity" => Some(Box::new(Zenity::new())),
62        _ => None,
63    }
64}