web_view/
dialog.rs

1use std::path::PathBuf;
2use tfd::MessageBoxIcon;
3use {WVResult, WebView};
4
5/// A builder for opening a new dialog window.
6#[deprecated(
7    note = "Please use crates like 'tinyfiledialogs' for dialog handling, see example in examples/dialog.rs"
8)]
9#[derive(Debug)]
10pub struct DialogBuilder<'a: 'b, 'b, T: 'a> {
11    webview: &'b mut WebView<'a, T>,
12}
13
14impl<'a: 'b, 'b, T: 'a> DialogBuilder<'a, 'b, T> {
15    /// Creates a new dialog builder for a WebView.
16    pub fn new(webview: &'b mut WebView<'a, T>) -> DialogBuilder<'a, 'b, T> {
17        DialogBuilder { webview }
18    }
19
20    /// Opens a new open file dialog and returns the chosen file path.
21    pub fn open_file<S, P>(&mut self, title: S, default_file: P) -> WVResult<Option<PathBuf>>
22    where
23        S: Into<String>,
24        P: Into<PathBuf>,
25    {
26        let default_file = default_file.into().into_os_string();
27        let default_file = default_file
28            .to_str()
29            .expect("default_file is not valid utf-8");
30
31        let result = tfd::open_file_dialog(&title.into(), default_file, None).map(|p| p.into());
32        Ok(result)
33    }
34
35    /// Opens a new save file dialog and returns the chosen file path.
36    pub fn save_file(&mut self) -> WVResult<Option<PathBuf>> {
37        Ok(tfd::save_file_dialog("", "").map(|p| p.into()))
38    }
39
40    /// Opens a new choose directory dialog as returns the chosen directory path.
41    pub fn choose_directory<S, P>(
42        &mut self,
43        title: S,
44        default_directory: P,
45    ) -> WVResult<Option<PathBuf>>
46    where
47        S: Into<String>,
48        P: Into<PathBuf>,
49    {
50        let default_directory = default_directory.into().into_os_string();
51        let default_directory = default_directory
52            .to_str()
53            .expect("default_directory is not valid utf-8");
54
55        let result = tfd::select_folder_dialog(&title.into(), default_directory).map(|p| p.into());
56        Ok(result)
57    }
58
59    /// Opens an info alert dialog.
60    pub fn info<TS, MS>(&mut self, title: TS, message: MS) -> WVResult
61    where
62        TS: Into<String>,
63        MS: Into<String>,
64    {
65        tfd::message_box_ok(&title.into(), &message.into(), MessageBoxIcon::Info);
66        Ok(())
67    }
68
69    /// Opens a warning alert dialog.
70    pub fn warning<TS, MS>(&mut self, title: TS, message: MS) -> WVResult
71    where
72        TS: Into<String>,
73        MS: Into<String>,
74    {
75        tfd::message_box_ok(&title.into(), &message.into(), MessageBoxIcon::Warning);
76        Ok(())
77    }
78
79    /// Opens an error alert dialog.
80    pub fn error<TS, MS>(&mut self, title: TS, message: MS) -> WVResult
81    where
82        TS: Into<String>,
83        MS: Into<String>,
84    {
85        tfd::message_box_ok(&title.into(), &message.into(), MessageBoxIcon::Error);
86        Ok(())
87    }
88}