Skip to content

Commit

Permalink
Implemented sorting of items list
Browse files Browse the repository at this point in the history
  • Loading branch information
Futsch1 committed Jan 7, 2022
1 parent 7ef7778 commit ea2b152
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
28 changes: 26 additions & 2 deletions src/main_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,16 @@ pub fn populate_item_list_model(
item_list_model: &sixtyfps::VecModel<ListItem>,
filters: &Filters,
) {
for image in item_list
let mut filtered_list: Vec<&FileItem> = item_list
.items
.iter()
.filter(|item| filter_file_items(item, filters))
{
.collect();
filtered_list.sort_unstable_by(|a, b| compare_file_items(a, b, filters));
if filters.direction == "Desc" {
filtered_list.reverse();
}
for image in filtered_list {
let list_item = list_item_from_file_item(image, item_list);
item_list_model.push(list_item);
}
Expand Down Expand Up @@ -680,3 +685,22 @@ fn filter_file_items(file_item: &FileItem, filters: &Filters) -> bool {
}
visible
}

/// Compare two file items taking the current sort settings into account
fn compare_file_items(a: &FileItem, b: &FileItem, filters: &Filters) -> std::cmp::Ordering {
match filters.sort_by.as_str() {
"Date" => a.cmp(b),
"Name" => a.path.cmp(&b.path),
"Type" => {
if a.is_image() && b.is_image() {
a.cmp(b)
} else if a.is_image() && b.is_video() {
std::cmp::Ordering::Less
} else {
std::cmp::Ordering::Greater
}
}
"Size" => a.get_size().cmp(&b.get_size()),
_ => std::cmp::Ordering::Less,
}
}
40 changes: 34 additions & 6 deletions ui/sort.60
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ListView, Button, LineEdit, GroupBox, VerticalBox, HorizontalBox, ScrollView, CheckBox } from "sixtyfps_widgets.60";
import { ListView, Button, LineEdit, GroupBox, VerticalBox, HorizontalBox, ScrollView, CheckBox, ComboBox } from "sixtyfps_widgets.60";

export struct SortItem := {
image: image,
Expand All @@ -15,7 +15,14 @@ export struct ListItem := {
export struct Filters := {
sorted-out: bool,
images: bool,
videos: bool
videos: bool,
sort_by: string,
direction: string
}

export global FilterComboValues := {
property <[string]> sort_by: ["Date", "Name", "Size", "Type"];
property <[string]> direction: ["Asc", "Desc"];
}

export SortView := HorizontalBox {
Expand Down Expand Up @@ -44,7 +51,9 @@ export SortView := HorizontalBox {
property <Filters> filters : {
sorted-out: true,
images: true,
videos: true
videos: true,
sort_by: "Date",
direction: "Asc"
};

callback next-clicked <=> next-button.clicked;
Expand Down Expand Up @@ -180,21 +189,40 @@ export SortView := HorizontalBox {
if filter-visible :
VerticalBox {
alignment: start;
filter-sorted-out := CheckBox {
CheckBox {
text: "Show discarded";
checked: filters.sorted-out;
toggled => { filters.sorted-out = self.checked; filter(filters) }
}
filter-videos := CheckBox {
CheckBox {
text: "Show videos";
checked: filters.videos;
toggled => { filters.videos = self.checked; filter(filters) }
}
filter-images := CheckBox {
CheckBox {
text: "Show images";
checked: filters.images;
toggled => { filters.images = self.checked; filter(filters) }
}
HorizontalBox {
alignment: start;
Text {
vertical-alignment: center;
text: "Sort by";
}
ComboBox {
current-value: "Date";
model: FilterComboValues.sort_by;
width: 100px;
selected => { filters.sort_by = self.current-value; filter(filters) }
}
ComboBox {
current-value: "Asc";
model: FilterComboValues.direction;
width: 80px;
selected => { filters.direction = self.current-value; filter(filters) }
}
}
}

image-list := ListView {
Expand Down

0 comments on commit ea2b152

Please sign in to comment.