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

View file

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