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

Fix sort groups #111

Merged
merged 2 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use serde::{Deserialize, Serialize};
use std::sync::Arc;
use switcheroo_control::Gpu;

use crate::app_group::AppLibraryConfig;
use crate::app_group::{AppLibraryConfig, FilterType};
use crate::fl;
use crate::subscriptions::desktop_files::desktop_files;
use crate::widgets::application::ApplicationButton;
Expand Down Expand Up @@ -544,10 +544,6 @@ impl cosmic::Application for CosmicAppLibrary {
Message::FinishDndOffer(i, entry) => {
self.offer_group = None;

if self.cur_group == i {
return Command::none();
}

self.config.add_entry(i, &entry.id);
if let Some(helper) = self.helper.as_ref() {
if let Err(err) = self.config.write_entry(helper) {
Expand Down Expand Up @@ -1062,15 +1058,13 @@ impl cosmic::Application for CosmicAppLibrary {
group_height,
spacing,
);
if i != 0 {
group_button = group_button
.on_finish(move |entry| Message::FinishDndOffer(i, entry))
.on_cancel(Message::LeaveDndOffer)
.on_offer(Message::StartDndOffer(i))
.on_dnd_command_produced(|c| {
Message::DndCommandProduced(DndCommand(Arc::new(c)))
});
}
group_button = group_button
.on_finish(move |entry| Message::FinishDndOffer(i, entry))
.on_cancel(Message::LeaveDndOffer)
.on_offer(Message::StartDndOffer(i))
.on_dnd_command_produced(|c| {
Message::DndCommandProduced(DndCommand(Arc::new(c)))
});

group_row = group_row.push(group_button);
}
Expand Down Expand Up @@ -1207,7 +1201,7 @@ impl cosmic::Application for CosmicAppLibrary {
) -> (Self, iced::Command<cosmic::app::Message<Self::Message>>) {
let helper = AppLibraryConfig::helper();

let config: AppLibraryConfig = helper
let mut config: AppLibraryConfig = helper
.as_ref()
.map(|helper| {
AppLibraryConfig::get_entry(helper).unwrap_or_else(|(errors, config)| {
Expand All @@ -1218,6 +1212,8 @@ impl cosmic::Application for CosmicAppLibrary {
})
})
.unwrap_or_default();
config.groups.sort();

let self_ = Self {
locale: current_locale::current_locale().ok(),
config,
Expand Down
70 changes: 67 additions & 3 deletions src/app_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static HOME: Lazy<[AppGroup; 1]> = Lazy::new(|| {
}]
});

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub enum FilterType {
/// A list of application IDs to include in the group.
AppIds(Vec<String>),
Expand All @@ -40,15 +40,58 @@ impl Default for FilterType {
}
}

impl Ord for FilterType {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match (self, other) {
(FilterType::AppIds(_), FilterType::AppIds(_)) => std::cmp::Ordering::Equal,
(FilterType::None, FilterType::None) => std::cmp::Ordering::Equal,
(FilterType::Categories { .. }, FilterType::Categories { .. }) => {
std::cmp::Ordering::Equal
}
(FilterType::Categories { .. } | FilterType::None, FilterType::AppIds(_)) => {
std::cmp::Ordering::Less
}
(FilterType::AppIds(_), FilterType::Categories { .. } | FilterType::None) => {
std::cmp::Ordering::Greater
}
(FilterType::Categories { .. }, FilterType::None) => std::cmp::Ordering::Greater,
(FilterType::None, FilterType::Categories { .. }) => std::cmp::Ordering::Less,
}
}
}

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

// Object holding the state
#[derive(Default, Serialize, Deserialize, Clone, Debug, PartialEq)]
#[derive(Default, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct AppGroup {
pub name: String,
pub icon: String,
pub filter: FilterType,
// pub popup: bool,
}

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

impl Ord for AppGroup {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match (&self.filter, &other.filter) {
(FilterType::AppIds(_), FilterType::AppIds(_)) => {
self.name.to_lowercase().cmp(&other.name.to_lowercase())
}
(a, b) => a.cmp(b),
}
}
}

impl AppGroup {
pub fn filtered(
&self,
Expand Down Expand Up @@ -113,7 +156,7 @@ impl AppGroup {

#[derive(Debug, Clone, Serialize, Deserialize, CosmicConfigEntry)]
pub struct AppLibraryConfig {
groups: Vec<AppGroup>,
pub(crate) groups: Vec<AppGroup>,
}

impl AppLibraryConfig {
Expand All @@ -131,6 +174,7 @@ impl AppLibraryConfig {
icon: "folder-symbolic".to_string(),
filter: FilterType::AppIds(Vec::new()),
});
self.groups.sort();
}

pub fn remove(&mut self, i: usize) {
Expand Down Expand Up @@ -180,6 +224,26 @@ impl AppLibraryConfig {
exclude.retain(|conf_id| conf_id != id);
include.push(id.to_string());
}
} else {
// add to filter of all groups, forcing it to the Home group
for group in &mut self.groups {
match &mut group.filter {
FilterType::AppIds(ids) => {
ids.retain(|conf_id| conf_id != id);
}
FilterType::Categories {
categories: _,
exclude,
include,
} => {
include.retain(|conf_id| conf_id != id);
if exclude.iter().all(|conf_id| conf_id != id) {
exclude.push(id.to_string());
}
}
FilterType::None => {}
}
}
}
}

Expand Down
1 change: 0 additions & 1 deletion src/widgets/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use cosmic::{

use super::application::MIME_TYPE;

/// A widget that can be dragged and dropped.
#[allow(missing_debug_implementations)]
pub struct GroupButton<'a, Message> {
content: Element<'a, Message, cosmic::Theme, cosmic::Renderer>,
Expand Down
Loading