From 397f09f684a79cdaa9978f5276f9bf1bc1bdaef4 Mon Sep 17 00:00:00 2001 From: BlueZ Date: Tue, 26 Mar 2024 18:02:27 +0100 Subject: [PATCH] Sort modlist button added --- src/gui/mod.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/gui/mod.rs b/src/gui/mod.rs index 6fe37da9..87304717 100644 --- a/src/gui/mod.rs +++ b/src/gui/mod.rs @@ -6,6 +6,7 @@ mod toggle_switch; //#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release +use std::cmp::Ordering; use std::collections::{BTreeMap, BTreeSet}; use std::ops::{Deref, RangeInclusive}; use std::time::{Duration, SystemTime}; @@ -1754,6 +1755,60 @@ impl eframe::App for App { { self.lints_toggle_window = Some(WindowLintsToggle); } + + if ui + .button("Sort modlist") + .on_hover_text("Sort mods in the current profile") + .clicked() + { + let profile = self.state.mod_data.active_profile.clone(); + let ModData { profiles, .. } = self.state.mod_data.deref_mut().deref_mut(); + + if let Some(active_profile) = profiles.get_mut(&profile) { + active_profile.mods.sort_by(|a, b| { + if matches!(a, ModOrGroup::Group { .. }) + || matches!(b, ModOrGroup::Group { .. }) + { + unimplemented!("Groups in sorting not implemented"); + } + + let ModOrGroup::Individual(mc_a) = a else { + debug!("Item is not Individual \n{:?}", a); + return Ordering::Equal; + }; + let ModOrGroup::Individual(mc_b) = b else { + debug!("Item is not Individual \n{:?}", b); + return Ordering::Equal; + }; + + let Some(info_a) = self.state.store.get_mod_info(&mc_a.spec) else { + debug!("Failed to get mod info for \n{:?}", mc_a); + return Ordering::Equal; + }; + let Some(info_b) = self.state.store.get_mod_info(&mc_b.spec) else { + debug!("Failed to get mod info for \n{:?}", mc_b); + return Ordering::Equal; + }; + + // Orders by Enabled, then by Approval / Provider, then by Name + return mc_b + .enabled + .cmp(&mc_a.enabled) + .then_with(|| { + let Some(tags_a) = info_a.modio_tags else { + return Ordering::Equal; + }; + let Some(tags_b) = info_b.modio_tags else { + return Ordering::Equal; + }; + return tags_a.approval_status.cmp(&tags_b.approval_status); + }) + .then(info_a.provider.cmp(info_b.provider)) + .then(info_a.name.to_lowercase().cmp(&info_b.name.to_lowercase())); + }); + } + } + if ui.button("⚙").on_hover_text("Open settings").clicked() { self.settings_window = Some(WindowSettings::new(&self.state)); }