Skip to content

Commit

Permalink
Merge pull request #842 from marhkb/fix/panels/initial-filter-child
Browse files Browse the repository at this point in the history
Fix/panels/initial-filter-child
  • Loading branch information
marhkb authored Oct 18, 2024
2 parents f0d3915 + e85e595 commit 39431d5
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 155 deletions.
153 changes: 83 additions & 70 deletions src/view/containers_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,94 +483,107 @@ mod imp {
self.update_filter(filter_change);
}

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

ACTIONS_SELECTION
.iter()
.for_each(|action_name| obj.action_set_enabled(action_name, false));

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

container_list.connect_notify_local(
Some("running"),
clone!(
#[weak]
obj,
move |_, _| obj.imp().update_filter(gtk::FilterChange::Different)
),
);

container_list.connect_container_name_changed(clone!(
value.connect_notify_local(
Some("num-selected"),
clone!(
#[weak]
obj,
move |_, _| {
obj.imp().update_filter(gtk::FilterChange::Different);
glib::timeout_add_seconds_local_once(
1,
clone!(
#[weak]
obj,
move || obj.imp().update_sorter()
),
);
move |list, _| {
ACTIONS_SELECTION.iter().for_each(|action_name| {
obj.action_set_enabled(action_name, list.num_selected() > 0);
});
}
));

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

if let Some(view) = &*self.containers_view.borrow() {
view.set_model(Some(model.upcast_ref()));
}
),
);

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

if removed > 0 {
obj.deselect_hidden_containers(model.upcast_ref());
}
}
));
value.connect_container_name_changed(clone!(
#[weak]
obj,
move |_, _| {
obj.imp().update_filter(gtk::FilterChange::Different);
glib::timeout_add_seconds_local_once(
1,
clone!(
#[weak]
obj,
move || obj.imp().update_sorter()
),
);
}
));

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

if let Some(view) = &*self.containers_view.borrow() {
view.set_model(Some(model.upcast_ref()));
}

self.container_list.set(value);
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_containers(model.upcast_ref());
}
}
));
value.connect_initialized_notify(clone!(
#[weak]
obj,
#[weak]
model,
move |container_list| obj
.imp()
.set_filter_stack_visible_child(container_list, &model)
));

self.model.replace(Some(model.upcast()));

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

fn set_filter_stack_visible_child(
&self,
container_list: &model::ContainerList,
model: &impl IsA<gio::ListModel>,
) {
self.filter_stack.set_visible_child_name(
if model.n_items() > 0 || !container_list.initialized() {
"containers"
} else {
"empty"
},
);
}

fn update_filter(&self, filter_change: gtk::FilterChange) {
Expand Down
37 changes: 25 additions & 12 deletions src/view/images_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,28 +387,27 @@ mod imp {
view::ImageRow::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().filter_stack.set_visible_child_name(
if model.n_items() > 0
|| !obj
.image_list()
.as_ref()
.is_some_and(model::ImageList::initialized)
{
"list"
} else {
"empty"
},
);
obj.imp().set_filter_stack_visible_child(&value, model);

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

obj.action_set_enabled(ACTION_DELETE_SELECTION, false);
value.connect_notify_local(
Expand All @@ -425,6 +424,20 @@ mod imp {
self.image_list.set(Some(value));
}

fn set_filter_stack_visible_child(
&self,
image_list: &model::ImageList,
model: &impl IsA<gio::ListModel>,
) {
self.filter_stack.set_visible_child_name(
if model.n_items() > 0 || !image_list.initialized() {
"list"
} else {
"empty"
},
);
}

fn update_filter(&self, filter_change: gtk::FilterChange) {
if let Some(filter) = self.filter.get() {
filter.changed(filter_change);
Expand Down
37 changes: 25 additions & 12 deletions src/view/pods_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,28 +391,27 @@ mod imp {
view::PodRow::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().filter_stack.set_visible_child_name(
if model.n_items() > 0
|| !obj
.pod_list()
.as_ref()
.is_some_and(model::PodList::initialized)
{
"list"
} else {
"empty"
},
);
obj.imp().set_filter_stack_visible_child(&value, model);

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

ACTIONS_SELECTION
.iter()
Expand All @@ -433,6 +432,20 @@ mod imp {
self.pod_list.set(Some(value));
}

fn set_filter_stack_visible_child(
&self,
pod_list: &model::PodList,
model: &impl IsA<gio::ListModel>,
) {
self.filter_stack.set_visible_child_name(
if model.n_items() > 0 || !pod_list.initialized() {
"list"
} else {
"empty"
},
);
}

fn update_filter(&self, filter_change: gtk::FilterChange) {
if let Some(filter) = self.filter.get() {
filter.changed(filter_change);
Expand Down
Loading

0 comments on commit 39431d5

Please sign in to comment.