Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/better sorting #848

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
125 changes: 96 additions & 29 deletions data/com.github.marhkb.Pods.gschema.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,6 @@
<summary>The last used view</summary>
<description></description>
</key>
<key name="containers-view" type="s">
<choices>
<choice value='grid'/>
<choice value='list'/>
</choices>
<default>'grid'</default>
<summary>Containers representation</summary>
<description></description>
</key>
<key name="hide-intermediate-images" type="b">
<default>false</default>
<summary>Whether to hide intermediate images</summary>
<description></description>
</key>
<key name="last-used-container-file-path" type="s">
<default>''</default>
<summary>The last used container file path</summary>
Expand All @@ -55,21 +41,6 @@
<summary>Remove images even when they are used by external containers (e.g, by build containers)</summary>
<description></description>
</key>
<key name="show-only-running-containers" type="b">
<default>false</default>
<summary>Whether to show only running containers</summary>
<description></description>
</key>
<key name="show-only-running-pods" type="b">
<default>false</default>
<summary>Whether to show only running pods</summary>
<description></description>
</key>
<key name="show-only-used-volumes" type="b">
<default>false</default>
<summary>Whether to show only used volumes</summary>
<description></description>
</key>
<key name="show-log-timestamps" type="b">
<default>false</default>
<summary>Whether to show a timestamp for each log line</summary>
Expand Down Expand Up @@ -106,4 +77,100 @@
<description></description>
</key>
</schema>

<schema path="/com/github/marhkb/Pods/view/panels/containers/" id="@[email protected]" gettext-domain="@gettext-package@">
<key name="view" type="s">
<choices>
<choice value='grid'/>
<choice value='list'/>
</choices>
<default>'grid'</default>
<summary>Containers representation</summary>
<description></description>
</key>
<key name="sort-attribute" type="s">
<choices>
<choice value='name-asc'/>
<choice value='name-desc'/>
</choices>
<default>'name-asc'</default>
<summary>sort attribute for containers</summary>
<description></description>
</key>
<key name="show-running-first" type="b">
<default>false</default>
<summary>Whether to show running containers first</summary>
<description></description>
</key>
</schema>

<schema path="/com/github/marhkb/Pods/view/panels/pods/" id="@[email protected]" gettext-domain="@gettext-package@">
<key name="sort-direction" type="s">
<choices>
<choice value='asc'/>
<choice value='desc'/>
</choices>
<default>'asc'</default>
<summary>Sort direction for pods</summary>
<description></description>
</key>
<key name="sort-attribute" type="s">
<choices>
<choice value='name'/>
<choice value='containers'/>
</choices>
<default>'name'</default>
<summary>Sort attribute for pods</summary>
<description></description>
</key>
<key name="show-running-first" type="b">
<default>false</default>
<summary>Whether to show running pods first</summary>
<description></description>
</key>
</schema>

<schema path="/com/github/marhkb/Pods/view/panels/images/" id="@[email protected]" gettext-domain="@gettext-package@">
<key name="sort-direction" type="s">
<choices>
<choice value='asc'/>
<choice value='desc'/>
</choices>
<default>'asc'</default>
<summary>Sort direction for images</summary>
<description></description>
</key>
<key name="sort-attribute" type="s">
<choices>
<choice value='name'/>
<choice value='tag'/>
<choice value='containers'/>
</choices>
<default>'name'</default>
<summary>Sort attribute for images</summary>
<description></description>
</key>
</schema>

<schema path="/com/github/marhkb/Pods/view/panels/volumes/" id="@[email protected]" gettext-domain="@gettext-package@">
<key name="sort-direction" type="s">
<choices>
<choice value='asc'/>
<choice value='desc'/>
</choices>
<default>'asc'</default>
<summary>Sort direction for volumes</summary>
<description></description>
</key>
<key name="sort-attribute" type="s">
<choices>
<choice value='name'/>
<choice value='age'/>
<choice value='containers'/>
</choices>
<default>'name'</default>
<summary>Sort attribute for volumes</summary>
<description></description>
</key>
</schema>
</schemalist>
29 changes: 29 additions & 0 deletions src/model/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,35 @@ pub(crate) enum Status {
Unknown,
}

impl std::cmp::Ord for Status {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self {
Self::Running => {
if let Self::Running = other {
std::cmp::Ordering::Equal
} else {
std::cmp::Ordering::Greater
}
}
Self::Paused => match other {
Self::Running => std::cmp::Ordering::Less,
Self::Paused => std::cmp::Ordering::Equal,
_ => std::cmp::Ordering::Greater,
},
_ => match other {
Self::Running | Self::Paused => std::cmp::Ordering::Less,
_ => std::cmp::Ordering::Equal,
},
}
}
}

impl std::cmp::PartialOrd for Status {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl FromStr for Status {
type Err = Self;

Expand Down
43 changes: 28 additions & 15 deletions src/model/image_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ mod imp {
Signal::builder("image-removed")
.param_types([model::Image::static_type()])
.build(),
Signal::builder("containers-of-image-changed")
.param_types([model::Image::static_type()])
.build(),
Signal::builder("tags-of-image-changed")
.param_types([model::Image::static_type()])
.build(),
]
})
}
Expand Down Expand Up @@ -98,9 +104,6 @@ mod imp {
model::SelectableList::bootstrap(obj.upcast_ref());

obj.connect_items_changed(|self_, _, _, _| self_.notify("len"));

obj.connect_image_added(|list, _| list.notify_num_images());
obj.connect_image_removed(|list, _| list.notify_num_images());
}
}

Expand Down Expand Up @@ -209,7 +212,8 @@ impl ImageList {
drop(list);

self.items_changed(idx as u32, 1, 0);
self.image_removed(&image);
self.emit_by_name::<()>("image-removed", &[&image]);
self.notify_num_images();
image.emit_deleted();
}
}
Expand Down Expand Up @@ -333,7 +337,11 @@ impl ImageList {
clone!(
#[weak(rename_to = obj)]
self,
move |_, _| obj.notify_num_images()
move |image, _| {
obj.notify_num_images();
obj.emit_by_name::<()>("containers-of-image-changed", &[&image]);
obj.emit_by_name::<()>("tags-of-image-changed", &[&image]);
}
),
);
self.emit_by_name::<()>("image-added", &[image]);
Expand All @@ -343,24 +351,29 @@ impl ImageList {
&self,
f: F,
) -> glib::SignalHandlerId {
self.connect_local("image-added", true, move |values| {
let obj = values[0].get::<Self>().unwrap();
let image = values[1].get::<model::Image>().unwrap();
f(&obj, &image);
self.connect_signal("image-added", f)
}

None
})
pub(crate) fn connect_containers_of_image_changed<F: Fn(&Self, &model::Image) + 'static>(
&self,
f: F,
) -> glib::SignalHandlerId {
self.connect_signal("containers-of-image-changed", f)
}

fn image_removed(&self, image: &model::Image) {
self.emit_by_name::<()>("image-removed", &[image]);
pub(crate) fn connect_tags_of_image_changed<F: Fn(&Self, &model::Image) + 'static>(
&self,
f: F,
) -> glib::SignalHandlerId {
self.connect_signal("tags-of-image-changed", f)
}

pub(crate) fn connect_image_removed<F: Fn(&Self, &model::Image) + 'static>(
fn connect_signal<F: Fn(&Self, &model::Image) + 'static>(
&self,
signal: &str,
f: F,
) -> glib::SignalHandlerId {
self.connect_local("image-removed", true, move |values| {
self.connect_local(signal, true, move |values| {
let obj = values[0].get::<Self>().unwrap();
let image = values[1].get::<model::Image>().unwrap();
f(&obj, &image);
Expand Down
29 changes: 29 additions & 0 deletions src/model/pod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,35 @@ pub(crate) enum Status {
Unknown,
}

impl std::cmp::Ord for Status {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self {
Self::Running => {
if let Self::Running = other {
std::cmp::Ordering::Equal
} else {
std::cmp::Ordering::Greater
}
}
Self::Paused => match other {
Self::Running => std::cmp::Ordering::Less,
Self::Paused => std::cmp::Ordering::Equal,
_ => std::cmp::Ordering::Greater,
},
_ => match other {
Self::Running | Self::Paused => std::cmp::Ordering::Less,
_ => std::cmp::Ordering::Equal,
},
}
}
}

impl std::cmp::PartialOrd for Status {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl FromStr for Status {
type Err = Self;

Expand Down
36 changes: 32 additions & 4 deletions src/model/pod_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ mod imp {
fn signals() -> &'static [Signal] {
static SIGNALS: OnceLock<Vec<Signal>> = OnceLock::new();
SIGNALS.get_or_init(|| {
vec![Signal::builder("pod-added")
.param_types([model::Pod::static_type()])
.build()]
vec![
Signal::builder("pod-added")
.param_types([model::Pod::static_type()])
.build(),
Signal::builder("containers-in-pod-changed")
.param_types([model::Pod::static_type()])
.build(),
]
})
}

Expand Down Expand Up @@ -94,6 +99,14 @@ mod imp {
let obj = &*self.obj();
model::SelectableList::bootstrap(obj.upcast_ref());
obj.connect_items_changed(|self_, _, _, _| self_.notify("len"));

obj.connect_pod_added(|list, pod| {
pod.connect_num_containers_notify(clone!(
#[weak]
list,
move |pod| list.emit_by_name::<()>("containers-in-pod-changed", &[pod])
));
});
}
}

Expand Down Expand Up @@ -314,7 +327,22 @@ impl PodList {
&self,
f: F,
) -> glib::SignalHandlerId {
self.connect_local("pod-added", true, move |values| {
self.connect_signal("pod-added", f)
}

pub(crate) fn connect_containers_in_pod_changed<F: Fn(&Self, &model::Pod) + 'static>(
&self,
f: F,
) -> glib::SignalHandlerId {
self.connect_signal("containers-in-pod-changed", f)
}

fn connect_signal<F: Fn(&Self, &model::Pod) + 'static>(
&self,
signal: &str,
f: F,
) -> glib::SignalHandlerId {
self.connect_local(signal, true, move |values| {
let obj = values[0].get::<Self>().unwrap();
let pod = values[1].get::<model::Pod>().unwrap();
f(&obj, &pod);
Expand Down
Loading
Loading