This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathdialog.rs
132 lines (120 loc) · 4.43 KB
/
dialog.rs
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
//! This module contains functions for opening file dialogs using DBus.
use std::path::PathBuf;
use ashpd::{desktop::file_chooser, WindowIdentifier};
use futures::executor::block_on;
use tracing::warn;
use crate::{FileDialogOptions, FileDialogToken, FileInfo};
use super::window::IdleHandle;
pub(crate) fn open_file(
window: u32,
idle: IdleHandle,
options: FileDialogOptions,
) -> FileDialogToken {
dialog(window, idle, options, true)
}
pub(crate) fn save_file(
window: u32,
idle: IdleHandle,
options: FileDialogOptions,
) -> FileDialogToken {
dialog(window, idle, options, false)
}
fn dialog(
window: u32,
idle: IdleHandle,
mut options: FileDialogOptions,
open: bool,
) -> FileDialogToken {
let tok = FileDialogToken::next();
std::thread::spawn(move || {
if let Err(e) = block_on(async {
let id = WindowIdentifier::from_xid(window as u64);
let multi = options.multi_selection;
let title_owned = options.title.take();
let title = match (open, options.select_directories) {
(true, true) => "Open Folder",
(true, false) => "Open File",
(false, _) => "Save File",
};
let title = title_owned.as_deref().unwrap_or(title);
let response = if open {
file_chooser::OpenFileRequest::default()
.identifier(id)
.title(title)
.modal(true)
.multiple(options.multi_selection)
.directory(options.select_directories)
.accept_label(options.button_text.as_deref())
.filters(
options
.allowed_types
.unwrap_or_default()
.into_iter()
.map(From::from),
)
.current_filter(options.default_type.map(From::from))
.send()
.await?
.response()?
} else {
file_chooser::SaveFileRequest::default()
.identifier(id)
.title(title)
.modal(true)
.current_name(options.default_name.as_deref())
.current_folder::<PathBuf>(options.starting_directory)?
.accept_label(options.button_text.as_deref())
.filters(
options
.allowed_types
.unwrap_or_default()
.into_iter()
.map(From::from),
)
.current_filter(options.default_type.map(From::from))
.send()
.await?
.response()?
};
let uris = response.uris();
let mut paths = uris.iter().filter_map(|s| {
s.to_file_path().ok().or_else(|| {
warn!("Invalid file path '{s}'");
None
})
});
if multi && open {
let infos = paths.map(|path| FileInfo { path, format: None }).collect();
idle.add_idle_callback(move |handler| handler.open_files(tok, infos));
} else if !multi {
if uris.len() > 2 {
warn!(
"expected one path (got {}), returning only the first",
uris.len()
);
}
let info = paths.next().map(|path| FileInfo { path, format: None });
if open {
idle.add_idle_callback(move |handler| handler.open_file(tok, info));
} else {
idle.add_idle_callback(move |handler| handler.save_as(tok, info));
}
} else {
warn!("cannot save multiple paths");
}
ashpd::Result::Ok(())
}) {
warn!("error while opening file dialog: {}", e);
}
});
tok
}
impl From<crate::FileSpec> for file_chooser::FileFilter {
fn from(spec: crate::FileSpec) -> file_chooser::FileFilter {
let mut filter = file_chooser::FileFilter::new(spec.name);
for ext in spec.extensions {
filter = filter.glob(&format!("*.{ext}"));
}
filter
}
}