~exprez135/castor

ref: 193de0c2079abf371286916b814804f34dd97087 castor/src/dialog.rs -rw-r--r-- 1.9 KiB
193de0c2Julien Blanchard [gopher] Fix parsing of h type 2 years ago
                                                                                
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
use gtk::prelude::*;
use gtk::ResponseType;
use std::sync::Arc;

use url::{Position, Url};

use crate::gui::Gui;
use crate::protocols::Gemini;

pub fn info(gui: &Arc<Gui>, message: &str) {
    let dialog = gtk::Dialog::new_with_buttons(
        Some("Info"),
        Some(gui.window()),
        gtk::DialogFlags::MODAL,
        &[("Close", ResponseType::Close)],
    );
    dialog.set_default_response(ResponseType::Close);
    dialog.connect_response(|dialog, _| dialog.destroy());

    let content_area = dialog.get_content_area();
    let message = gtk::Label::new(Some(message));
    content_area.add(&message);

    dialog.show_all();
}

pub fn error(gui: &Arc<Gui>, message: &str) {
    let dialog = gtk::Dialog::new_with_buttons(
        Some("Error"),
        Some(gui.window()),
        gtk::DialogFlags::MODAL,
        &[("Close", ResponseType::Close)],
    );
    dialog.set_default_response(ResponseType::Close);
    dialog.connect_response(|dialog, _| dialog.destroy());

    let content_area = dialog.get_content_area();
    let message = gtk::Label::new(Some(message));
    content_area.add(&message);

    dialog.show_all();
}

pub fn input(gui: &Arc<Gui>, url: Url, message: &str) {
    let dialog = gtk::Dialog::new_with_buttons(
        Some(message),
        Some(gui.window()),
        gtk::DialogFlags::MODAL,
        &[
            ("Close", ResponseType::Close),
            ("Send", ResponseType::Accept),
        ],
    );

    let content_area = dialog.get_content_area();
    let entry = gtk::Entry::new();
    content_area.add(&entry);

    dialog.show_all();

    if dialog.run() == gtk::ResponseType::Accept {
        let response = entry.get_text().expect("get_text failed").to_string();
        let cleaned: &str = &url[..Position::AfterPath];
        let full_url = format!("{}?{}", cleaned.to_string(), response);

        crate::visit_url(&gui, Gemini { source: full_url });
    }

    dialog.destroy();
}