refactor sorting

This commit is contained in:
Dominic Gerhauser 2024-02-29 03:46:14 +01:00 • committed by Jeremy Soller
parent d47002bb0d
commit 197398fe4e

View file

@ -542,15 +542,15 @@ pub struct Tab {
pub history_i: usize, pub history_i: usize,
pub history: Vec<Location>, pub history: Vec<Location>,
pub config: TabConfig, pub config: TabConfig,
//TODO: this no longer needs to be a map sort_name: HeadingOptions,
sort_map: HashMap<HeadingOptions, bool>, 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 mut sort_map = HashMap::new(); let sort_name = HeadingOptions::Name;
sort_map.insert(HeadingOptions::Name, true); let sort_direction = true;
Self { Self {
location, location,
context_menu: None, context_menu: None,
@ -564,7 +564,8 @@ impl Tab {
history_i: 0, history_i: 0,
history, history,
config, config,
sort_map, sort_name,
sort_direction,
} }
} }
@ -840,9 +841,38 @@ impl Tab {
self.view = view; self.view = view;
} }
Message::ToggleSort(heading_option) => { Message::ToggleSort(heading_option) => {
let heading_sort = !self.sort_map.get(&heading_option).unwrap_or(&false); let heading_sort = if self.sort_name == heading_option {
self.sort_map.clear(); !self.sort_direction
self.sort_map.insert(heading_option, heading_sort); } else {
true
};
self.sort_direction = heading_sort;
self.sort_name = heading_option;
}
}
if let Some(location) = cd {
if location != self.location {
self.location = location.clone();
self.items_opt = None;
self.edit_location = None;
if let Some(history_i) = history_i_opt {
// Navigating in history
self.history_i = history_i;
} else {
// Truncate history to remove next entries
self.history.truncate(self.history_i + 1);
// Push to the front of history
self.history_i = self.history.len();
self.history.push(location.clone());
}
commands.push(Command::ChangeLocation(self.title(), location));
}
}
commands
}
fn column_sort(&self) -> Option<Vec<Item>> {
let check_reverse = |ord: Ordering, sort: bool| { let check_reverse = |ord: Ordering, sort: bool| {
if sort { if sort {
ord ord
@ -850,8 +880,9 @@ impl Tab {
ord.reverse() ord.reverse()
} }
}; };
if let Some(item) = &mut self.items_opt { let mut item = self.items_opt.clone()?;
match heading_option { let heading_sort = self.sort_direction;
match self.sort_name {
HeadingOptions::Size => { HeadingOptions::Size => {
item.sort_by(|a, b| { item.sort_by(|a, b| {
// entries take precedence over size // entries take precedence over size
@ -864,9 +895,7 @@ impl Tab {
} }
} }
ItemMetadata::Trash { metadata, .. } => match metadata.size { ItemMetadata::Trash { metadata, .. } => match metadata.size {
trash::TrashItemSize::Entries(entries) => { trash::TrashItemSize::Entries(entries) => (true, entries as u64),
(true, entries as u64)
}
trash::TrashItemSize::Bytes(bytes) => (false, bytes), trash::TrashItemSize::Bytes(bytes) => (false, bytes),
}, },
}; };
@ -902,29 +931,7 @@ impl Tab {
}); });
} }
} }
} Some(item)
}
}
if let Some(location) = cd {
if location != self.location {
self.location = location.clone();
self.items_opt = None;
self.edit_location = None;
if let Some(history_i) = history_i_opt {
// Navigating in history
self.history_i = history_i;
} else {
// Truncate history to remove next entries
self.history.truncate(self.history_i + 1);
// Push to the front of history
self.history_i = self.history.len();
self.history.push(location.clone());
}
commands.push(Command::ChangeLocation(self.title(), location));
}
}
commands
} }
pub fn location_view(&self, core: &Core) -> Element<Message> { pub fn location_view(&self, core: &Core) -> Element<Message> {
@ -1168,11 +1175,13 @@ impl Tab {
($name: literal, $width: expr, $msg: expr) => { ($name: literal, $width: expr, $msg: expr) => {
widget::row::with_children(vec![ widget::row::with_children(vec![
widget::text::heading(fl!($name)).into(), widget::text::heading(fl!($name)).into(),
widget::button(widget::icon::from_name(match self.sort_map.get(&$msg) { widget::button(widget::icon::from_name(
Some(true) => "go-down-symbolic", match (self.sort_name == $msg, self.sort_direction) {
Some(false) => "go-up-symbolic", (true, true) => "go-down-symbolic",
None => "list-remove-symbolic", (true, false) => "go-up-symbolic",
})) _ => "list-remove-symbolic",
},
))
.on_press(Message::ToggleSort($msg)) .on_press(Message::ToggleSort($msg))
.style(cosmic::style::Button::Icon) .style(cosmic::style::Button::Icon)
.into(), .into(),
@ -1200,7 +1209,7 @@ impl Tab {
children.push(horizontal_rule(1).into()); children.push(horizontal_rule(1).into());
if let Some(ref items) = self.items_opt { if let Some(ref items) = self.column_sort() {
let mut count = 0; let mut count = 0;
let mut hidden = 0; let mut hidden = 0;
let TabConfig { let TabConfig {