aboutsummaryrefslogtreecommitdiff
path: root/src/gui/load.rs
blob: 618f9ddef8a2ff42527bb797c25de87beaf506f5 (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
use gtk::glib;
use gtk::prelude::*;
use super::state::{View, ViewManager};
use std::rc::Rc;
use std::thread;

use crate::index::Index;

pub struct Load {
    vm : Rc<ViewManager>
}

impl View for Load {
    fn name(&self) -> &str {
        "load"
    }

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

    fn make_current(&self) -> Option<gtk::Box> {
        let vm = Rc::clone(&self.vm);
        glib::MainContext::default().spawn_local(async move {
            let dialog = gtk::FileChooserDialog::builder()
                .action(gtk::FileChooserAction::Open)
                .transient_for(vm.get_window())
                .title("Open Index File")
                .build();

            dialog.add_button("Choose", gtk::ResponseType::Accept);

            let _ = dialog.run_future().await;
            dialog.close();
            dialog.hide();

            if let Some(uri) = dialog.uri() {
                let splash = gtk::Window::new(gtk::WindowType::Popup);
                splash.set_type_hint(gtk::gdk::WindowTypeHint::Splashscreen);
                splash.set_decorated(false);
                splash.set_position(gtk::WindowPosition::Center);
                splash.set_resizable(false);
                splash.set_size_request(300, 200);

                let container = gtk::Box::new(gtk::Orientation::Vertical, 0);
                let spinner = gtk::Spinner::builder().active(true).build();

                container.pack_start(&gtk::Label::new(Some("Loading")), false, false, 50);
                container.pack_start(&spinner, false, true, 0);

                splash.add(&container);
                splash.show_all();

                let (tx, rx) = glib::MainContext::channel(glib::Priority::default());
                let path = uri.trim_start_matches("file://").to_string();

                thread::spawn(move || {
                    let index = Index::from_file(&path);
                    tx.send(index).ok();
                });

                rx.attach(None, move |index| {
                    vm.set_index(index);
                    vm.set_current_view("search");
                    splash.close();
                    glib::Continue(false)
                });
            } else {
                vm.set_current_view("welcome");
            }
        });

        None
    }
}

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