refactor sorting
This commit is contained in:
parent
d47002bb0d
commit
197398fe4e
1 changed files with 82 additions and 73 deletions
93
src/tab.rs
93
src/tab.rs
|
|
@ -542,15 +542,15 @@ pub struct Tab {
|
|||
pub history_i: usize,
|
||||
pub history: Vec<Location>,
|
||||
pub config: TabConfig,
|
||||
//TODO: this no longer needs to be a map
|
||||
sort_map: HashMap<HeadingOptions, bool>,
|
||||
sort_name: HeadingOptions,
|
||||
sort_direction: bool,
|
||||
}
|
||||
|
||||
impl Tab {
|
||||
pub fn new(location: Location, config: TabConfig) -> Self {
|
||||
let history = vec![location.clone()];
|
||||
let mut sort_map = HashMap::new();
|
||||
sort_map.insert(HeadingOptions::Name, true);
|
||||
let sort_name = HeadingOptions::Name;
|
||||
let sort_direction = true;
|
||||
Self {
|
||||
location,
|
||||
context_menu: None,
|
||||
|
|
@ -564,7 +564,8 @@ impl Tab {
|
|||
history_i: 0,
|
||||
history,
|
||||
config,
|
||||
sort_map,
|
||||
sort_name,
|
||||
sort_direction,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -840,9 +841,38 @@ impl Tab {
|
|||
self.view = view;
|
||||
}
|
||||
Message::ToggleSort(heading_option) => {
|
||||
let heading_sort = !self.sort_map.get(&heading_option).unwrap_or(&false);
|
||||
self.sort_map.clear();
|
||||
self.sort_map.insert(heading_option, heading_sort);
|
||||
let heading_sort = if self.sort_name == heading_option {
|
||||
!self.sort_direction
|
||||
} 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| {
|
||||
if sort {
|
||||
ord
|
||||
|
|
@ -850,8 +880,9 @@ impl Tab {
|
|||
ord.reverse()
|
||||
}
|
||||
};
|
||||
if let Some(item) = &mut self.items_opt {
|
||||
match heading_option {
|
||||
let mut item = self.items_opt.clone()?;
|
||||
let heading_sort = self.sort_direction;
|
||||
match self.sort_name {
|
||||
HeadingOptions::Size => {
|
||||
item.sort_by(|a, b| {
|
||||
// entries take precedence over size
|
||||
|
|
@ -864,9 +895,7 @@ impl Tab {
|
|||
}
|
||||
}
|
||||
ItemMetadata::Trash { metadata, .. } => match metadata.size {
|
||||
trash::TrashItemSize::Entries(entries) => {
|
||||
(true, entries as u64)
|
||||
}
|
||||
trash::TrashItemSize::Entries(entries) => (true, entries as u64),
|
||||
trash::TrashItemSize::Bytes(bytes) => (false, bytes),
|
||||
},
|
||||
};
|
||||
|
|
@ -902,29 +931,7 @@ impl Tab {
|
|||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
Some(item)
|
||||
}
|
||||
|
||||
pub fn location_view(&self, core: &Core) -> Element<Message> {
|
||||
|
|
@ -1168,11 +1175,13 @@ impl Tab {
|
|||
($name: literal, $width: expr, $msg: expr) => {
|
||||
widget::row::with_children(vec![
|
||||
widget::text::heading(fl!($name)).into(),
|
||||
widget::button(widget::icon::from_name(match self.sort_map.get(&$msg) {
|
||||
Some(true) => "go-down-symbolic",
|
||||
Some(false) => "go-up-symbolic",
|
||||
None => "list-remove-symbolic",
|
||||
}))
|
||||
widget::button(widget::icon::from_name(
|
||||
match (self.sort_name == $msg, self.sort_direction) {
|
||||
(true, true) => "go-down-symbolic",
|
||||
(true, false) => "go-up-symbolic",
|
||||
_ => "list-remove-symbolic",
|
||||
},
|
||||
))
|
||||
.on_press(Message::ToggleSort($msg))
|
||||
.style(cosmic::style::Button::Icon)
|
||||
.into(),
|
||||
|
|
@ -1200,7 +1209,7 @@ impl Tab {
|
|||
|
||||
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 hidden = 0;
|
||||
let TabConfig {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue