Commit 105844d3 authored by Bilal Elmoussaoui's avatar Bilal Elmoussaoui

use hashmap collections for data structure.

parent e4bc262a
Pipeline #98171 passed with stages
in 9 minutes and 36 seconds
......@@ -7,7 +7,7 @@ use gtk;
use gtk::prelude::{BoxExt, ContainerExt, FlowBoxExt, GtkWindowExt, IconThemeExt, ImageExt, StackExt, StyleContextExt, WidgetExt};
use libhandy::{Column, ColumnExt};
use std::cell::RefCell;
use std::collections::HashMap;
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;
......@@ -150,7 +150,7 @@ struct IconsViewContext {
pub widget: gtk::Box,
pub context: String,
icons_container: gtk::FlowBox,
icons_views: Vec<RefCell<IconView>>,
icons_views: HashMap<gtk::FlowBoxChild, Icon>,
n_last_search_results: u32,
}
......@@ -158,7 +158,7 @@ impl IconsViewContext {
pub fn new(context: String, icons: Vec<Icon>, parent: &gtk::ApplicationWindow) -> IconsViewContext {
let widget = gtk::Box::new(gtk::Orientation::Vertical, 6);
let icons_container = gtk::FlowBox::new();
let icons_views: Vec<RefCell<IconView>> = Vec::new();
let icons_views: HashMap<gtk::FlowBoxChild, Icon> = HashMap::new();
let n_last_search_results: u32 = 0;
let mut icons_view_context = IconsViewContext {
widget,
......@@ -195,54 +195,34 @@ impl IconsViewContext {
for icon in icons {
let icon_view = IconView::new(icon.clone());
self.icons_container.add(&icon_view.widget);
self.icons_views.push(RefCell::new(icon_view));
self.icons_views.insert(icon_view.widget, icon);
}
self.widget.add(&self.icons_container);
}
fn connect_signals(&self, parent: &gtk::ApplicationWindow) {
let parent_window = parent.clone();
let icons_views = self.icons_views.to_vec().into_iter();
self.icons_container.connect_child_activated(move |_, child| {
let icons_views = icons_views.clone();
let views: Vec<RefCell<IconView>> = icons_views
.filter(|icon_view| {
let flowbox = &icon_view.borrow_mut().widget;
flowbox.get_name().unwrap().as_str() == child.get_name().unwrap().as_str()
})
.collect();
match views.get(0) {
Some(icon_view) => {
let export_dialog = ExportDialog::new(icon_view.borrow_mut().icon.clone());
let icons_views = self.icons_views.clone();
self.icons_container.connect_child_activated(move |_, child| match icons_views.get(child) {
Some(icon) => {
let export_dialog = ExportDialog::new(icon.clone());
export_dialog.widget.set_transient_for(Some(&parent_window));
export_dialog.widget.show_all();
}
None => println!("Couldn't find the IconView"),
};
None => println!("Couldn't find the icon"),
});
}
pub fn filter(&mut self, search_str: String) {
self.n_last_search_results = 0;
let icons_views = self.icons_views.to_vec().into_iter();
let icons_views = self.icons_views.clone();
let filter_closure = move |child: &gtk::FlowBoxChild| -> bool {
let icons_views = icons_views.clone();
let views: Vec<RefCell<IconView>> = icons_views
.filter(|icon_view| {
let flowbox = &icon_view.borrow_mut().widget;
flowbox.get_name().unwrap().as_str() == child.get_name().unwrap().as_str()
})
.collect();
match views.get(0) {
Some(icon_view) => {
let icon: &Icon = &icon_view.borrow_mut().icon;
icon.should_display(&search_str)
}
match icons_views.get(child) {
Some(icon) => icon.should_display(&search_str),
None => false,
}
};
self.icons_container.set_filter_func(Some(Box::new(filter_closure)));
for child in self.icons_container.get_children() {
if child.get_child_visible() {
self.n_last_search_results += 1;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment