Refactor sorting option to TabConfig

This commit is contained in:
Josh Megnauth 2024-03-13 23:23:00 -04:00 committed by Jeremy Soller
parent dc3c4eb731
commit a4b38c4983
2 changed files with 20 additions and 17 deletions

View file

@ -8,6 +8,8 @@ use cosmic::{
};
use serde::{Deserialize, Serialize};
use super::tab::HeadingOptions;
pub const CONFIG_VERSION: u64 = 1;
// Default icon sizes
@ -58,9 +60,10 @@ impl Default for Config {
pub struct TabConfig {
/// Show hidden files and folders
pub show_hidden: bool,
// TODO: Other possible options
// pub sort_by: fn(&PathBuf, &PathBuf) -> Ordering,
// Icon zoom
/// Sorter
pub sort_name: HeadingOptions,
pub sort_direction: bool,
/// Icon zoom
pub icon_sizes: IconSizes,
}
@ -68,6 +71,8 @@ impl Default for TabConfig {
fn default() -> Self {
Self {
show_hidden: false,
sort_name: HeadingOptions::Name,
sort_direction: true,
icon_sizes: IconSizes::default(),
}
}

View file

@ -22,6 +22,7 @@ use cosmic::{
};
use mime_guess::{mime, Mime};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::{
cell::Cell,
cmp::Ordering,
@ -617,7 +618,7 @@ pub enum View {
Grid,
List,
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, PartialOrd, Ord, Eq)]
#[derive(Clone, Copy, Debug, Hash, PartialEq, PartialOrd, Ord, Eq, Deserialize, Serialize)]
pub enum HeadingOptions {
Name,
Modified,
@ -642,15 +643,11 @@ pub struct Tab {
scrollable_id: widget::Id,
select_focus: Option<usize>,
select_shift: Option<usize>,
sort_name: HeadingOptions,
sort_direction: bool,
}
impl Tab {
pub fn new(location: Location, config: TabConfig) -> Self {
let history = vec![location.clone()];
let sort_name = HeadingOptions::Name;
let sort_direction = true;
Self {
location,
context_menu: None,
@ -667,8 +664,6 @@ impl Tab {
scrollable_id: widget::Id::unique(),
select_focus: None,
select_shift: None,
sort_name,
sort_direction,
}
}
@ -1206,13 +1201,13 @@ impl Tab {
self.view = view;
}
Message::ToggleSort(heading_option) => {
let heading_sort = if self.sort_name == heading_option {
!self.sort_direction
let heading_sort = if self.config.sort_name == heading_option {
!self.config.sort_direction
} else {
true
};
self.sort_direction = heading_sort;
self.sort_name = heading_option;
self.config.sort_direction = heading_sort;
self.config.sort_name = heading_option;
}
}
if let Some(location) = cd {
@ -1247,8 +1242,8 @@ impl Tab {
}
};
let mut items: Vec<_> = self.items_opt.as_ref()?.iter().enumerate().collect();
let heading_sort = self.sort_direction;
match self.sort_name {
let heading_sort = self.config.sort_direction;
match self.config.sort_name {
HeadingOptions::Size => {
items.sort_by(|a, b| {
// entries take precedence over size
@ -1482,6 +1477,7 @@ impl Tab {
let TabConfig {
show_hidden,
icon_sizes,
..
} = self.config;
let text_height = 40; // Height of two lines of text
@ -1627,6 +1623,8 @@ impl Tab {
let TabConfig {
show_hidden,
icon_sizes,
sort_name,
sort_direction,
} = self.config;
let size = self.size_opt.unwrap_or_else(|| Size::new(0.0, 0.0));
@ -1648,7 +1646,7 @@ impl Tab {
.spacing(space_xxs)
.width(width);
row = row.push(widget::text::heading(name));
match (self.sort_name == msg, self.sort_direction) {
match (sort_name == msg, sort_direction) {
(true, true) => {
row = row.push(widget::icon::from_name("pan-down-symbolic").size(16));
}