summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: a74f0bdf03af734954041c3b1be3feae8fc278d6 (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
use gtk4::prelude::*;
use gtk4::{
    Application, ApplicationWindow, Box, Button, Entry, HeaderBar, Orientation, ScrolledWindow,
};

use webkit6::WebView;
use webkit6::prelude::*;

mod settings_window;

const APP_ID: &str = "com.namilsk.i2p-browser";

fn main() -> glib::ExitCode {
    let app = Application::builder().application_id(APP_ID).build();
    app.connect_activate(build_ui);
    app.run()
}

fn build_ui(app: &Application) {
    let window = ApplicationWindow::builder()
        .application(app)
        .title("I2P Browser")
        .default_width(1024)
        .default_height(768)
        .build();

    let header_bar = HeaderBar::new();
    window.set_titlebar(Some(&header_bar));

    let url_entry = Entry::builder()
        .placeholder_text("Enter I2P URL (e.g., http://example.i2p)")
        .build();

    let button_back = Button::with_label("<");
    let button_forward = Button::with_label(">");
    let button_reload = Button::with_label("R");
    let button_home = Button::with_label("H");

    header_bar.pack_start(&button_back);
    header_bar.pack_start(&button_forward);
    header_bar.pack_start(&button_reload);
    header_bar.pack_start(&button_home);
    header_bar.set_title_widget(Some(&url_entry));

    let main_box = Box::new(Orientation::Vertical, 0);
    window.set_child(Some(&main_box));

    let web_view = WebView::new();

    let scrolled_window = ScrolledWindow::builder().child(&web_view).build();

    main_box.append(&scrolled_window);

    let web_view_for_entry = web_view.clone();
    let url_entry_for_entry = url_entry.clone();
    url_entry.connect_activate(move |entry| {
        let url = entry.text();
        let full_url = if url.starts_with("http://") || url.starts_with("https://") {
            url.to_string()
        } else {
            format!("http://{}", url)
        };

        if glib::Uri::parse(&full_url, glib::UriFlags::NONE).is_ok() {
            web_view_for_entry.load_uri(&full_url);
        } else {
            eprintln!("Invalid URI: {}", full_url);
        }
        url_entry_for_entry.set_text(&full_url);
    });

    let web_view_for_back = web_view.clone();
    button_back.connect_clicked(move |_| {
        if web_view_for_back.can_go_back() {
            web_view_for_back.go_back();
        }
    });

    let web_view_for_forward = web_view.clone();
    button_forward.connect_clicked(move |_| {
        if web_view_for_forward.can_go_forward() {
            web_view_for_forward.go_forward();
        }
    });

    let web_view_for_reload = web_view.clone();
    button_reload.connect_clicked(move |_| {
        web_view_for_reload.reload();
    });

    let web_view_for_home = web_view.clone();
    let url_entry_for_home = url_entry.clone();
    button_home.connect_clicked(move |_| {
        let home_url = "http://reg.i2p/";
        web_view_for_home.load_uri(home_url);
        url_entry_for_home.set_text(home_url);
    });

    let url_entry_for_notify = url_entry.clone();
    web_view.connect_notify_local(Some("uri"), move |web_view, _| {
        if let Some(uri) = web_view.uri() {
            url_entry_for_notify.set_text(&uri);
        }
    });

    web_view.load_uri("http://legwork.i2p/");

    window.present();
}