1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use ffi::{self, DialogFlags, DialogType};
use std::{ffi::CString, path::PathBuf};
use {read_str, WVResult, WebView};
const STR_BUF_SIZE: usize = 4096;
#[derive(Debug)]
pub struct DialogBuilder<'a: 'b, 'b, T: 'a> {
webview: &'b mut WebView<'a, T>,
}
impl<'a: 'b, 'b, T: 'a> DialogBuilder<'a, 'b, T> {
pub fn new(webview: &'b mut WebView<'a, T>) -> DialogBuilder<'a, 'b, T> {
DialogBuilder { webview }
}
fn dialog(
&mut self,
title: String,
arg: String,
dialog_type: DialogType,
dialog_flags: DialogFlags,
) -> WVResult<String> {
let mut s = [0u8; STR_BUF_SIZE];
let title_cstr = CString::new(title)?;
let arg_cstr = CString::new(arg)?;
unsafe {
ffi::webview_dialog(
self.webview.inner,
dialog_type,
dialog_flags,
title_cstr.as_ptr(),
arg_cstr.as_ptr(),
s.as_mut_ptr() as _,
s.len(),
);
}
Ok(read_str(&s))
}
pub fn open_file<S, P>(&mut self, title: S, default_file: P) -> WVResult<Option<PathBuf>>
where
S: Into<String>,
P: Into<PathBuf>,
{
self.dialog(
title.into(),
default_file.into().to_string_lossy().into_owned(),
DialogType::Open,
DialogFlags::FILE,
)
.map(|path| {
if path.is_empty() {
None
} else {
Some(PathBuf::from(path))
}
})
}
pub fn choose_directory<S, P>(
&mut self,
title: S,
default_directory: P,
) -> WVResult<Option<PathBuf>>
where
S: Into<String>,
P: Into<PathBuf>,
{
self.dialog(
title.into(),
default_directory.into().to_string_lossy().into_owned(),
DialogType::Open,
DialogFlags::DIRECTORY,
)
.map(|path| {
if path.is_empty() {
None
} else {
Some(PathBuf::from(path))
}
})
}
pub fn info<TS, MS>(&mut self, title: TS, message: MS) -> WVResult
where
TS: Into<String>,
MS: Into<String>,
{
self.dialog(
title.into(),
message.into(),
DialogType::Alert,
DialogFlags::INFO,
)
.map(|_| ())
}
pub fn warning<TS, MS>(&mut self, title: TS, message: MS) -> WVResult
where
TS: Into<String>,
MS: Into<String>,
{
self.dialog(
title.into(),
message.into(),
DialogType::Alert,
DialogFlags::WARNING,
)
.map(|_| ())
}
pub fn error<TS, MS>(&mut self, title: TS, message: MS) -> WVResult
where
TS: Into<String>,
MS: Into<String>,
{
self.dialog(
title.into(),
message.into(),
DialogType::Alert,
DialogFlags::ERROR,
)
.map(|_| ())
}
}