Skip to content

Commit 8b3286b

Browse files
committed
Query instead of RemoteResource
1 parent 16b38b1 commit 8b3286b

File tree

8 files changed

+435
-224
lines changed

8 files changed

+435
-224
lines changed

src/container.rs

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::{
22
distrobox::{ContainerInfo, ExportableApp, Status},
33
distrobox_task::DistroboxTask,
4+
fakers::CommandRunner,
45
known_distros::{known_distro_by_image, KnownDistro},
5-
remote_resource::RemoteResource,
6+
query::Query,
67
root_store::RootStore,
78
fakers::Command
89
};
@@ -15,15 +16,14 @@ use gtk::{
1516
use adw::prelude::*;
1617
use glib::subclass::prelude::*;
1718
use gtk::glib;
18-
use std::{cell::RefCell, path::Path};
19+
use std::cell::RefCell;
20+
use std::path::Path;
1921

2022
mod imp {
21-
use crate::remote_resource::RemoteResource;
22-
2323
use super::*;
2424

2525
// This contains all the container informations given by distrobox, plus an associated KnownDistro struct
26-
#[derive(Default, Properties)]
26+
#[derive(Properties)]
2727
#[properties(wrapper_type=super::Container)]
2828
pub struct Container {
2929
#[property(get, set)]
@@ -38,10 +38,27 @@ mod imp {
3838
pub image: RefCell<String>,
3939
#[property(get, set)]
4040
pub distro: RefCell<Option<KnownDistro>>,
41-
#[property(get, set)]
42-
pub apps: RefCell<RemoteResource>,
43-
#[property(get, set)]
44-
pub binaries: RefCell<RemoteResource>,
41+
pub apps: Query<gio::ListStore, anyhow::Error>,
42+
pub binaries: Query<gio::ListStore, anyhow::Error>,
43+
}
44+
45+
impl Default for Container {
46+
fn default() -> Self {
47+
Self {
48+
root_store: RefCell::new(RootStore::new(CommandRunner::new_null())),
49+
name: RefCell::new(String::new()),
50+
status_tag: RefCell::new(String::new()),
51+
status_detail: RefCell::new(String::new()),
52+
image: RefCell::new(String::new()),
53+
distro: RefCell::new(None),
54+
apps: Query::new("apps".into(), || async {
55+
Ok(gio::ListStore::new::<BoxedAnyObject>())
56+
}),
57+
binaries: Query::new("binaries".into(), || async {
58+
Ok(gio::ListStore::new::<BoxedAnyObject>())
59+
}),
60+
}
61+
}
4562
}
4663

4764
#[derived_properties]
@@ -80,54 +97,54 @@ impl Container {
8097
.build();
8198

8299
let this_clone = this.clone();
83-
let loader = move |apps_list: Option<&gio::ListStore>| {
100+
this.apps().set_fetcher(move || {
84101
let this = this_clone.clone();
85-
let mut apps_list = apps_list
86-
.cloned()
87-
.unwrap_or_else(gio::ListStore::new::<BoxedAnyObject>);
88102
async move {
89103
let apps = this
90104
.root_store()
91105
.distrobox()
92106
.list_apps(&this.name())
93107
.await?;
94108

95-
apps_list.remove_all();
109+
let mut apps_list = gio::ListStore::new::<BoxedAnyObject>();
96110
apps_list.extend(apps.into_iter().map(BoxedAnyObject::new));
97111

98112
// Listing the apps starts the container, we need to update its status
99113
this.root_store().load_containers();
100114
Ok(apps_list)
101115
}
102-
};
103-
this.set_apps(RemoteResource::new::<gio::ListStore, _>(loader));
116+
});
104117

105118
let this_clone = this.clone();
106-
let binaries_loader = move |binaries_list: Option<&gio::ListStore>| {
119+
this.binaries().set_fetcher(move || {
107120
let this = this_clone.clone();
108-
let mut binaries_list = binaries_list
109-
.cloned()
110-
.unwrap_or_else(gio::ListStore::new::<BoxedAnyObject>);
111121
async move {
112122
let binaries = this
113123
.root_store()
114124
.distrobox()
115125
.get_exported_binaries(&this.name())
116126
.await?;
117127

118-
binaries_list.remove_all();
128+
let mut binaries_list = gio::ListStore::new::<BoxedAnyObject>();
119129
binaries_list.extend(binaries.into_iter().map(BoxedAnyObject::new));
120130

121131
// Listing the binaries starts the container, we need to update its status
122132
this.root_store().load_containers();
123133
Ok(binaries_list)
124134
}
125-
};
126-
this.set_binaries(RemoteResource::new::<gio::ListStore, _>(binaries_loader));
135+
});
127136

128137
this
129138
}
130139

140+
pub fn apps(&self) -> Query<gio::ListStore, anyhow::Error> {
141+
self.imp().apps.clone()
142+
}
143+
144+
pub fn binaries(&self) -> Query<gio::ListStore, anyhow::Error> {
145+
self.imp().binaries.clone()
146+
}
147+
131148
pub fn upgrade(&self) -> DistroboxTask {
132149
let this = self.clone();
133150
self.root_store()
@@ -192,7 +209,7 @@ impl Container {
192209
.distrobox()
193210
.export_app(&this.name(), &desktop_file_path)
194211
.await?;
195-
this.apps().reload();
212+
this.apps().refetch();
196213
Ok(())
197214
});
198215
}
@@ -205,7 +222,7 @@ impl Container {
205222
.distrobox()
206223
.unexport_app(&this.name(), &desktop_file_path)
207224
.await?;
208-
this.apps().reload();
225+
this.apps().refetch();
209226
Ok(())
210227
});
211228
}
@@ -218,7 +235,7 @@ impl Container {
218235
.distrobox()
219236
.export_binary(&this.name(), &binary_path)
220237
.await?;
221-
this.binaries().reload();
238+
this.binaries().refetch();
222239
Ok(())
223240
})
224241
}
@@ -231,7 +248,7 @@ impl Container {
231248
.distrobox()
232249
.unexport_binary(&this.name(), &binary_path)
233250
.await?;
234-
this.binaries().reload();
251+
this.binaries().refetch();
235252
Ok(())
236253
});
237254
}

src/dialogs/create_distrobox_dialog.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,20 +313,20 @@ impl CreateDistroboxDialog {
313313
.property("root-store", root_store)
314314
.build();
315315

316-
this.root_store().images().connect_data_changed(clone!(
316+
this.root_store()
317+
.images_query()
318+
.connect_success(clone!(
317319
#[weak]
318320
this,
319-
move |resource| {
321+
move |images| {
320322
let string_list = gtk::StringList::new(&[]);
321-
if let Some(images) = resource.data::<Vec<String>>() {
322-
for image in images {
323-
string_list.append(&image);
324-
}
323+
for image in images {
324+
string_list.append(&image);
325325
}
326326
this.imp().image_row.set_model(Some(&string_list));
327327
}
328328
));
329-
this.root_store().images().reload();
329+
this.root_store().images_query().refetch();
330330

331331
glib::MainContext::ref_thread_default().spawn_local(clone!(
332332
#[weak]

src/dialogs/exportable_apps_dialog.rs

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,33 @@ impl ExportableAppsDialog {
202202

203203
let this_clone = this.clone();
204204
let apps = this.container().apps();
205+
let this_inner = this_clone.clone();
206+
apps.connect_loading(move |is_loading| {
207+
if is_loading {
208+
this_inner.imp().stack.set_visible_child_name("loading");
209+
}
210+
});
211+
let this_inner = this_clone.clone();
212+
apps.connect_error(move |error| {
213+
this_inner.imp().error_label.set_label(&error.to_string());
214+
this_inner.imp().stack.set_visible_child_name("error");
215+
});
205216
let binaries = this.container().binaries();
206-
reaction! {
207-
(apps.error(), binaries.error()),
208-
move |(e1, e2): (Option<String>, Option<String>)| {
209-
if let Some(err) = e1.or(e2) {
210-
this_clone.imp().error_label.set_label(&err);
211-
this_clone.imp().stack.set_visible_child_name("error");
212-
}
217+
let this_inner = this_clone.clone();
218+
binaries.connect_loading(move |is_loading| {
219+
if is_loading {
220+
this_inner.imp().stack.set_visible_child_name("loading");
213221
}
214-
};
222+
});
223+
let this_inner = this_clone.clone();
224+
binaries.connect_error(move |error| {
225+
this_inner.imp().error_label.set_label(&error.to_string());
226+
this_inner.imp().stack.set_visible_child_name("error");
227+
});
215228

216229
let this_clone = this.clone();
217-
let apps = this.container().apps();
218-
let render_apps = move || {
219-
let apps = apps.data::<gio::ListStore>();
220-
let n_apps = apps.as_ref().map(|s| s.n_items()).unwrap_or(0);
230+
let render_apps = move |apps_data: &gio::ListStore| {
231+
let n_apps = apps_data.n_items();
221232

222233
// Update description based on whether there are apps
223234
if n_apps == 0 {
@@ -231,21 +242,18 @@ impl ExportableAppsDialog {
231242
this_clone
232243
.imp()
233244
.list_box
234-
.bind_model(apps.as_ref(), move |obj| {
245+
.bind_model(Some(apps_data), move |obj| {
235246
let app = obj
236247
.downcast_ref::<BoxedAnyObject>()
237248
.map(|obj| obj.borrow::<ExportableApp>())
238249
.unwrap();
239250
this.build_row(&app).upcast()
240251
});
241-
242252
};
243253

244254
let this_clone = this.clone();
245-
let binaries = this.container().binaries();
246-
let render_binaries = move || {
247-
let binaries = binaries.data::<gio::ListStore>();
248-
let n_binaries = binaries.as_ref().map(|s| s.n_items()).unwrap_or(0);
255+
let render_binaries = move |binaries_data: &gio::ListStore| {
256+
let n_binaries = binaries_data.n_items();
249257

250258
// Update description based on whether there are binaries
251259
if n_binaries == 0 {
@@ -259,7 +267,7 @@ impl ExportableAppsDialog {
259267
this_clone
260268
.imp()
261269
.binaries_list_box
262-
.bind_model(binaries.as_ref(), move |obj| {
270+
.bind_model(Some(binaries_data), move |obj| {
263271
let binary = obj
264272
.downcast_ref::<BoxedAnyObject>()
265273
.map(|obj| obj.borrow::<ExportableBinary>())
@@ -268,20 +276,16 @@ impl ExportableAppsDialog {
268276
});
269277
};
270278

271-
let this_clone = this.clone();
272279
let apps = this.container().apps();
280+
let render_apps_closure = render_apps.clone();
281+
apps.connect_success(move |apps_data| {
282+
render_apps_closure(apps_data);
283+
});
273284
let binaries = this.container().binaries();
274-
reaction! {
275-
(apps.loading(), binaries.loading()),
276-
move |(b1, b2): (bool, bool)| {
277-
if b1 || b2 {
278-
this_clone.imp().stack.set_visible_child_name("loading");
279-
} else {
280-
render_apps();
281-
render_binaries();
282-
}
283-
}
284-
};
285+
let render_binaries_closure = render_binaries.clone();
286+
binaries.connect_success(move |binaries_data| {
287+
render_binaries_closure(binaries_data);
288+
});
285289

286290

287291
// Connect the binary name entry apply signal
@@ -333,8 +337,8 @@ impl ExportableAppsDialog {
333337
}
334338
});
335339

336-
container.apps().reload();
337-
container.binaries().reload();
340+
container.apps().refetch();
341+
container.binaries().refetch();
338342

339343
this
340344
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ mod distrobox_task;
2929
mod fakers;
3030
mod gtk_utils;
3131
mod known_distros;
32-
mod remote_resource;
3332
mod sidebar_row;
3433
mod store;
3534
mod supported_terminals;
@@ -39,6 +38,7 @@ mod terminal_combo_row;
3938
mod welcome_view;
4039
mod window;
4140
pub use store::root_store;
41+
pub mod query;
4242

4343
use self::application::DistroShelfApplication;
4444
use window::DistroShelfWindow;

0 commit comments

Comments
 (0)