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
// make moving clones into closures more convenient
#[macro_export]
macro_rules! clone {
    (@param _) => ( _ );
    (@param $x:ident) => ( $x );
    ($($n:ident),+ => move || $body:expr) => (
        {
            $( let $n = $n.clone(); )+
            move || $body
        }
    );
    ($($n:ident),+ => move |$($p:tt),+| $body:expr) => (
        {
            $( let $n = $n.clone(); )+
            move |$(clone!(@param $p),)+| $body
        }
    );
}

#[macro_export]
macro_rules! app_id {
    () => {
        crate::globals::APP_ID.unwrap_or("com.github.Cogitri.gxi.devel")
    };
    (id => $e:expr) => {
        crate::globals::APP_ID.unwrap_or(id)
    };
}

#[macro_export]
macro_rules! setup_gtk_panic {
    () => {
        {
            #![allow(unused_imports)]
            use crate::errors::{ErrorDialog, ErrorMsg};
            use gtk::{ContainerExt, GtkWindowExt, TextBufferExt, TextViewExt, WidgetExt};
            use human_panic::{handle_dump, Metadata};
            use log::error;
            use std::panic::{self, PanicInfo};
            let meta = Metadata {
                version: env!("CARGO_PKG_VERSION").into(),
                name: env!("CARGO_PKG_NAME").into(),
                authors: env!("CARGO_PKG_AUTHORS").replace(":", ", ").into(),
                homepage: env!("CARGO_PKG_HOMEPAGE").into(),
            };

            panic::set_hook(Box::new(move |info: &PanicInfo| {
                let file_path = handle_dump(&meta, info).unwrap();

                let application = gtk::Application::new(
                    Some("com.github.Cogitri.gxi.error-reporter"),
                    Default::default(),
                )
                .unwrap();

                application.connect_activate(move |app| {
                    let text_view = gtk::TextView::new();
                    text_view.set_editable(false);
                    text_view
                        .get_buffer()
                        .unwrap()
                        .set_text(&std::fs::read_to_string(file_path.clone()).unwrap());

                    let window = gtk::ApplicationWindow::new(app);
                    window.set_title(&gettext("gxi crash reporter"));
                    let scroll_win =
                        gtk::ScrolledWindow::new(None::<&gtk::Adjustment>, None::<&gtk::Adjustment>);
                    scroll_win.add(&text_view);
                    window.add(&scroll_win);
                    window.set_border_width(5);
                    //TODO: Set a dconf value to make this the same size as the original gxi window
                    window.set_default_size(800, 400);
                    window.show_all();

                    let crash1 = gettext("It seems like gxi has crashed, sorry!");
                    let crash2 = gettext("Please send the contents of the file below to our GitHub issue tracker so we can fix it! Thank you =)");
                    let err_msg = ErrorMsg {
                        fatal: false,
                        msg: format!("{}\n{}\n\n{}: {:#?}\n{}: {}", crash1, crash2, gettext("File"), file_path, gettext("URL"), "https://github.com/Cogitri/gxi/issues"),
                    };

                    ErrorDialog::new(err_msg.clone());
                });

                application.run(&Vec::new());
            }));
        }
    }
}