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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use gtk::prelude::{Cast, FileChooserExt, FileExt, ListModelExt, NativeDialogExt};
use relm4::{gtk, ComponentParts, ComponentSender, SimpleComponent};
use std::{fmt::Debug, marker::PhantomData, path::PathBuf};
pub type OpenDialog = OpenDialogInner<SingleSelection>;
pub type OpenDialogMulti = OpenDialogInner<MultiSelection>;
pub trait Select: Debug {
type Selection: Debug;
const SELECT_MULTIPLE: bool;
fn select(dialog: >k::FileChooserNative) -> Self::Selection;
}
#[derive(Debug)]
pub struct SingleSelection;
impl Select for SingleSelection {
type Selection = PathBuf;
const SELECT_MULTIPLE: bool = false;
fn select(dialog: >k::FileChooserNative) -> Self::Selection {
dialog
.file()
.expect("No file selected")
.path()
.expect("No path")
}
}
#[derive(Debug)]
pub struct MultiSelection;
impl Select for MultiSelection {
type Selection = Vec<PathBuf>;
const SELECT_MULTIPLE: bool = true;
fn select(dialog: >k::FileChooserNative) -> Self::Selection {
let list_model = dialog.files();
(0..list_model.n_items())
.filter_map(|index| list_model.item(index))
.filter_map(|obj| obj.downcast::<gtk::gio::File>().ok())
.filter_map(|file| file.path())
.collect()
}
}
#[derive(Clone, Debug)]
pub struct OpenDialogSettings {
pub folder_mode: bool,
pub cancel_label: String,
pub accept_label: String,
pub create_folders: bool,
pub is_modal: bool,
pub filters: Vec<gtk::FileFilter>,
}
impl Default for OpenDialogSettings {
fn default() -> Self {
OpenDialogSettings {
folder_mode: false,
accept_label: String::from("Open"),
cancel_label: String::from("Cancel"),
create_folders: true,
is_modal: true,
filters: Vec::new(),
}
}
}
#[derive(Debug)]
pub struct OpenDialogInner<S: Select> {
visible: bool,
_phantom: PhantomData<S>,
}
#[derive(Debug, Clone)]
pub enum OpenDialogMsg {
Open,
#[doc(hidden)]
Hide,
}
#[derive(Debug, Clone)]
pub enum OpenDialogResponse<S: Select> {
Accept(S::Selection),
Cancel,
}
#[relm4::component(pub)]
impl<S: Select + 'static> SimpleComponent for OpenDialogInner<S> {
type Init = OpenDialogSettings;
type Input = OpenDialogMsg;
type Output = OpenDialogResponse<S>;
view! {
gtk::FileChooserNative {
set_action: if settings.folder_mode {
gtk::FileChooserAction::SelectFolder
} else {
gtk::FileChooserAction::Open
},
set_select_multiple: S::SELECT_MULTIPLE,
set_create_folders: settings.create_folders,
set_modal: settings.is_modal,
set_accept_label: Some(&settings.accept_label),
set_cancel_label: Some(&settings.cancel_label),
#[iterate]
add_filter: &settings.filters,
#[watch]
set_visible: model.visible,
connect_response[sender] => move |dialog, res_ty| {
match res_ty {
gtk::ResponseType::Accept => {
let selection = S::select(dialog);
sender.output(OpenDialogResponse::Accept(selection)).unwrap();
}
_ => sender.output(OpenDialogResponse::Cancel).unwrap(),
}
sender.input(OpenDialogMsg::Hide);
}
}
}
fn init(
settings: Self::Init,
root: &Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let model = OpenDialogInner {
visible: false,
_phantom: PhantomData::default(),
};
let widgets = view_output!();
ComponentParts { model, widgets }
}
fn update(&mut self, message: Self::Input, _sender: ComponentSender<Self>) {
match message {
OpenDialogMsg::Open => {
self.visible = true;
}
OpenDialogMsg::Hide => {
self.visible = false;
}
}
}
}