Skip to content

Commit

Permalink
fix(volumes-panel): Correctly reset search
Browse files Browse the repository at this point in the history
  • Loading branch information
marhkb committed Oct 18, 2024
1 parent 93b6928 commit a72e49f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 50 deletions.
124 changes: 75 additions & 49 deletions src/view/volumes_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mod imp {
pub(super) filter: OnceCell<gtk::Filter>,
pub(super) sorter: OnceCell<gtk::Sorter>,
pub(super) search_term: RefCell<String>,
#[property(get, set = Self::set_volume_list, explicit_notify, nullable)]
#[property(get, set = Self::set_volume_list)]
pub(super) volume_list: glib::WeakRef<model::VolumeList>,
#[property(get, set)]
pub(super) show_only_used_volumes: Cell<bool>,
Expand Down Expand Up @@ -109,7 +109,7 @@ mod imp {
);

klass.install_action(ACTION_SHOW_ALL_VOLUMES, None, |widget, _, _| {
widget.set_show_only_used_volumes(false);
widget.show_all_volumes();
});
}

Expand Down Expand Up @@ -299,66 +299,87 @@ mod imp {
self.update_filter(filter_change);
}

pub(super) fn set_volume_list(&self, value: Option<&model::VolumeList>) {
pub(super) fn set_volume_list(&self, value: &model::VolumeList) {
let obj = &*self.obj();
if obj.volume_list().as_ref() == value {
if obj.volume_list().as_ref() == Some(value) {
return;
}

obj.action_set_enabled(ACTION_DELETE_SELECTION, false);

if let Some(volume_list) = value {
volume_list.connect_notify_local(
Some("num-selected"),
clone!(@weak obj => move |list, _| {
value.connect_notify_local(
Some("num-selected"),
clone!(
#[weak]
obj,
move |list, _| {
obj.action_set_enabled(ACTION_DELETE_SELECTION, list.num_selected() > 0);
}),
);
}
),
);

volume_list.connect_notify_local(
Some("used"),
clone!(@weak obj => move |_, _| {
obj.imp().update_filter(gtk::FilterChange::Different);
}),
);

let model = gtk::SortListModel::new(
Some(gtk::FilterListModel::new(
Some(volume_list.to_owned()),
self.filter.get().cloned(),
)),
self.sorter.get().cloned(),
);

self.list_box.bind_model(Some(&model), |item| {
view::VolumeRow::from(item.downcast_ref().unwrap()).upcast()
});

model.connect_items_changed(clone!(
value.connect_notify_local(
Some("used"),
clone!(
#[weak]
obj,
move |model, _, removed, _| {
obj.imp().filter_stack.set_visible_child_name(
if model.n_items() > 0 || {
!obj.volume_list()
.as_ref()
.is_some_and(model::VolumeList::initialized)
} {
"list"
} else {
"empty"
},
);
move |_, _| {
obj.imp().update_filter(gtk::FilterChange::Different);
}
),
);

if removed > 0 {
obj.deselect_hidden_volumes(model.upcast_ref());
}
let model = gtk::SortListModel::new(
Some(gtk::FilterListModel::new(
Some(value.to_owned()),
self.filter.get().cloned(),
)),
self.sorter.get().cloned(),
);

self.list_box.bind_model(Some(&model), |item| {
view::VolumeRow::from(item.downcast_ref().unwrap()).upcast()
});

self.set_filter_stack_visible_child(value, &model);
model.connect_items_changed(clone!(
#[weak]
obj,
#[weak]
value,
move |model, _, removed, _| {
obj.imp().set_filter_stack_visible_child(&value, model);

if removed > 0 {
obj.deselect_hidden_volumes(model.upcast_ref());
}
));
}
}
));
value.connect_initialized_notify(clone!(
#[weak]
obj,
#[weak]
model,
move |volume_list| obj
.imp()
.set_filter_stack_visible_child(volume_list, &model)
));

self.volume_list.set(Some(value));
}

self.volume_list.set(value);
obj.notify("volume-list");
fn set_filter_stack_visible_child(
&self,
volume_list: &model::VolumeList,
model: &impl IsA<gio::ListModel>,
) {
self.filter_stack.set_visible_child_name(
if model.n_items() > 0 || !volume_list.initialized() {
"list"
} else {
"empty"
},
);
}

fn update_filter(&self, filter_change: gtk::FilterChange) {
Expand All @@ -382,6 +403,11 @@ impl Default for VolumesPanel {
}

impl VolumesPanel {
pub(crate) fn show_all_volumes(&self) {
self.set_show_only_used_volumes(false);
self.set_search_mode(false);
}

pub(crate) fn set_search_mode(&self, value: bool) {
self.imp().search_bar.set_search_mode(value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/view/volumes_panel.ui
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@
<property name="child">
<object class="AdwStatusPage">
<property name="icon-name">drive-harddisk-symbolic</property>
<property name="title" translatable="yes">No Volumes in Use</property>
<property name="title" translatable="yes">No Matching Volumes</property>

<child>
<object class="GtkButton">
Expand Down

0 comments on commit a72e49f

Please sign in to comment.