~exprez135/castor

ref: 193de0c2079abf371286916b814804f34dd97087 castor/src/gui.rs -rw-r--r-- 2.6 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use gtk::prelude::*;
use gtk::{ApplicationWindow, Button, Entry, TextView};

use gdk::WindowExt;

pub struct Gui {
    window: ApplicationWindow,
    url_bar: Entry,
    content_view: TextView,
    back_button: Button,
    add_bookmark_button: Button,
    show_bookmarks_button: Button,
}

impl Default for Gui {
    fn default() -> Self {
        Self::new()
    }
}

impl Gui {
    pub fn new() -> Gui {
        // Initialize the UI from the Glade XML.
        let glade_src = include_str!("castor.glade");
        let builder = gtk::Builder::new_from_string(glade_src);

        // Get handles for the various controls we need to use.
        let window: ApplicationWindow = builder.get_object("window").expect("Couldn't get window");
        let url_bar: Entry = builder.get_object("url_bar").expect("Couldn't get url_bar");
        let content_view: TextView = builder
            .get_object("content_view")
            .expect("Couldn't get content_view");
        let back_button: Button = builder
            .get_object("back_button")
            .expect("Couldn't get back_button");
        let add_bookmark_button: Button = builder
            .get_object("add_bookmark_button")
            .expect("Couldn't get add_bookmark_button");
        let show_bookmarks_button: Button = builder
            .get_object("show_bookmarks_button")
            .expect("Couldn't get show_bookmarks_button");

        Gui {
            window,
            url_bar,
            content_view,
            back_button,
            add_bookmark_button,
            show_bookmarks_button,
        }
    }

    pub fn start(&self) {
        glib::set_application_name("Castor Browser");
        self.window.set_role("Castor Browser");
        self.window.connect_delete_event(|_, _| {
            gtk::main_quit();
            Inhibit(false)
        });
        self.content_view.connect_motion_notify_event(|win, _| {
            let w = gtk::TextViewExt::get_window(win, gtk::TextWindowType::Text).unwrap();
            w.set_cursor(gdk::Cursor::new_from_name(&w.get_display(), "default").as_ref());
            Inhibit(false)
        });
        self.window.show_all();
    }

    pub fn window(&self) -> &ApplicationWindow {
        &self.window
    }

    pub fn url_bar(&self) -> &Entry {
        &self.url_bar
    }

    pub fn content_view(&self) -> &TextView {
        &self.content_view
    }

    pub fn back_button(&self) -> &Button {
        &self.back_button
    }

    pub fn add_bookmark_button(&self) -> &Button {
        &self.add_bookmark_button
    }

    pub fn show_bookmarks_button(&self) -> &Button {
        &self.show_bookmarks_button
    }
}