aboutsummaryrefslogtreecommitdiff
path: root/src/gui/search.rs
blob: 7d68e7dca6378080589d372bb51619b5d2858199 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use gtk::gdk::keys::constants::Return as RETURN;
use gtk::glib;
use gtk::prelude::*;
use super::state::{View, ViewManager};
use std::rc::Rc;
use std::thread;

pub struct Search {
    vm : Rc<ViewManager>
}

impl View for Search {
    fn name(&self) -> &str {
        "search"
    }

    fn set_vm(&mut self, vm : Rc<ViewManager>) {
        self.vm = vm
    }

    fn make_current(&self) -> Option<gtk::Box> {
        let main = gtk::Box::new(gtk::Orientation::Vertical, 0);
        let scroll = gtk::ScrolledWindow::new(gtk::Adjustment::NONE, gtk::Adjustment::NONE);
        let search = gtk::SearchEntry::new();
        let progress = gtk::ProgressBar::new();
        search.set_widget_name("search_entry");
        main.pack_start(&search, false, false, 0);
        main.pack_start(&progress, false, false, 0);
        let list = gtk::ListBox::new();
        scroll.add(&list);

        let empty_container = gtk::Box::new(gtk::Orientation::Vertical, 0);

        let bt = include_bytes!("icon.svg");
        let loader = gtk::gdk_pixbuf::PixbufLoader::new();
        loader.write(bt).ok();
        loader.set_size(200, 200);
        loader.close().ok();
        let pixbuf = loader.pixbuf().unwrap();
        let image = gtk::Image::from_pixbuf(Some(&pixbuf));

        empty_container.pack_start(&image, true, true, 0);
        main.pack_start(&empty_container, true, true, 0);

        let vm = Rc::clone(&self.vm);
        search.connect_key_press_event(glib::clone!(
            @strong scroll,
            @strong list,
            @weak progress,
            @weak main,
            @strong empty_container => @default-return gtk::Inhibit(false), move |entry, event| {
                match event.keyval() {
                    RETURN => {
                        let query = entry.text().to_string();

                        if query.is_empty() {
                            main.remove(&scroll);
                            main.remove(&empty_container);
                            main.pack_start(&empty_container, true, true, 0);
                            return gtk::Inhibit(false);
                        }

                        let (tx, rx) = glib::MainContext::channel(glib::Priority::default());
                        let (status_tx, status_rx) = glib::MainContext::channel(glib::Priority::default());

                        let index = vm.get_index();
                        thread::spawn(move || {
                            let searchvec = crate::splitter::split_to_words(query);
                            let results = index.lock().unwrap().search(searchvec, |p| {
                                status_tx.send(p).ok();
                            });
                            tx.send(results).ok();
                        });

                        status_rx.attach(None, glib::clone!(
                            @weak progress => @default-return glib::Continue(true), move |p| {
                                progress.set_fraction(f64::from(p) / 100.0);

                                if p == 100 {
                                    glib::Continue(false)
                                } else {
                                    glib::Continue(true)
                                }
                            }));

                        rx.attach(None, glib::clone!(
                            @weak empty_container,
                            @weak progress,
                            @weak list,
                            @weak scroll => @default-return glib::Continue(false), move |results| {
                                main.remove(&scroll);
                                main.remove(&empty_container);

                                progress.set_fraction(0.0);

                                if results.is_empty() {
                                    main.pack_start(&empty_container, true, true, 0);
                                    return glib::Continue(false);
                                } else {
                                    for child in list.children() {
                                        list.remove(&child);
                                    }

                                    for result in results.iter().rev().take(1000) {
                                        let entry = gtk::Box::new(gtk::Orientation::Horizontal, 0);
                                        entry.set_margin(10);
                                        let path_label = gtk::Label::new(Some(&result.path));
                                        path_label.set_justify(gtk::Justification::Left);
                                        entry.pack_start(&path_label, false, false, 10);
                                        list.prepend(&entry)
                                    }

                                    main.pack_start(&scroll, true, true, 0);
                                }
                                main.show_all();

                                glib::Continue(false)
                            }));
                    }
                    _ => {}
                }
                gtk::Inhibit(false)
            }));

        Some(main)
    }
}

impl Search {
    pub fn new() -> Self {
        Self { vm : Rc::new(ViewManager::empty()) }
    }
}