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; ...@@ -7,7 +7,7 @@ use gtk;
use gtk::prelude::{BoxExt, ContainerExt, FlowBoxExt, GtkWindowExt, IconThemeExt, ImageExt, StackExt, StyleContextExt, WidgetExt}; use gtk::prelude::{BoxExt, ContainerExt, FlowBoxExt, GtkWindowExt, IconThemeExt, ImageExt, StackExt, StyleContextExt, WidgetExt};
use libhandy::{Column, ColumnExt}; use libhandy::{Column, ColumnExt};
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::HashMap;
use std::fs::File; use std::fs::File;
use std::io::BufReader; use std::io::BufReader;
use std::path::PathBuf; use std::path::PathBuf;
...@@ -150,7 +150,7 @@ struct IconsViewContext { ...@@ -150,7 +150,7 @@ struct IconsViewContext {
pub widget: gtk::Box, pub widget: gtk::Box,
pub context: String, pub context: String,
icons_container: gtk::FlowBox, icons_container: gtk::FlowBox,
icons_views: Vec<RefCell<IconView>>, icons_views: HashMap<gtk::FlowBoxChild, Icon>,
n_last_search_results: u32, n_last_search_results: u32,
} }
...@@ -158,7 +158,7 @@ impl IconsViewContext { ...@@ -158,7 +158,7 @@ impl IconsViewContext {
pub fn new(context: String, icons: Vec<Icon>, parent: &gtk::ApplicationWindow) -> IconsViewContext { pub fn new(context: String, icons: Vec<Icon>, parent: &gtk::ApplicationWindow) -> IconsViewContext {
let widget = gtk::Box::new(gtk::Orientation::Vertical, 6); let widget = gtk::Box::new(gtk::Orientation::Vertical, 6);
let icons_container = gtk::FlowBox::new(); 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 n_last_search_results: u32 = 0;
let mut icons_view_context = IconsViewContext { let mut icons_view_context = IconsViewContext {
widget, widget,
...@@ -195,54 +195,34 @@ impl IconsViewContext { ...@@ -195,54 +195,34 @@ impl IconsViewContext {
for icon in icons { for icon in icons {
let icon_view = IconView::new(icon.clone()); let icon_view = IconView::new(icon.clone());
self.icons_container.add(&icon_view.widget); 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); self.widget.add(&self.icons_container);
} }
fn connect_signals(&self, parent: &gtk::ApplicationWindow) { fn connect_signals(&self, parent: &gtk::ApplicationWindow) {
let parent_window = parent.clone(); let parent_window = parent.clone();
let icons_views = self.icons_views.to_vec().into_iter(); let icons_views = self.icons_views.clone();
self.icons_container.connect_child_activated(move |_, child| { self.icons_container.connect_child_activated(move |_, child| match icons_views.get(child) {
let icons_views = icons_views.clone(); Some(icon) => {
let views: Vec<RefCell<IconView>> = icons_views let export_dialog = ExportDialog::new(icon.clone());
.filter(|icon_view| { export_dialog.widget.set_transient_for(Some(&parent_window));
let flowbox = &icon_view.borrow_mut().widget; export_dialog.widget.show_all();
flowbox.get_name().unwrap().as_str() == child.get_name().unwrap().as_str() }
}) None => println!("Couldn't find the icon"),
.collect();
match views.get(0) {
Some(icon_view) => {
let export_dialog = ExportDialog::new(icon_view.borrow_mut().icon.clone());
export_dialog.widget.set_transient_for(Some(&parent_window));
export_dialog.widget.show_all();
}
None => println!("Couldn't find the IconView"),
};
}); });
} }
pub fn filter(&mut self, search_str: String) { pub fn filter(&mut self, search_str: String) {
self.n_last_search_results = 0; 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 filter_closure = move |child: &gtk::FlowBoxChild| -> bool {
let icons_views = icons_views.clone(); match icons_views.get(child) {
let views: Vec<RefCell<IconView>> = icons_views Some(icon) => icon.should_display(&search_str),
.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)
}
None => false, None => false,
} }
}; };
self.icons_container.set_filter_func(Some(Box::new(filter_closure))); self.icons_container.set_filter_func(Some(Box::new(filter_closure)));
for child in self.icons_container.get_children() { for child in self.icons_container.get_children() {
if child.get_child_visible() { if child.get_child_visible() {
self.n_last_search_results += 1; 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