From fb5455ae891c30cdf55fca393b70eb8dd907dd16 Mon Sep 17 00:00:00 2001 From: Namilskyy Date: Sat, 6 Dec 2025 23:13:31 +0300 Subject: Implemented bookmarks and some fixes --- src/main.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 287ec42..48fb653 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,10 @@ mod redirecting_i2p; mod settings_window; +mod bookmarks; use gtk4::prelude::*; use gtk4::{ - Application, ApplicationWindow, Box, Button, CheckButton, Entry, HeaderBar, Image, Orientation, + Application, ApplicationWindow, Box, Button, CheckButton, Entry, HeaderBar, Orientation, ScrolledWindow, }; use webkit6::prelude::*; @@ -50,6 +51,7 @@ fn build_ui(app: &Application) { let settings_button = Button::with_label("Settings"); let i2p_proxy = CheckButton::with_label("Enable i2p proxy"); let i2p_panel = Button::with_label("Panel"); + let bookmark_button = Button::with_label("Bookmark"); header_bar.pack_start(&button_back); header_bar.pack_start(&button_forward); @@ -58,10 +60,19 @@ fn build_ui(app: &Application) { header_bar.pack_end(&settings_button); header_bar.pack_end(&i2p_proxy); header_bar.pack_end(&i2p_panel); + header_bar.pack_end(&bookmark_button); let main_box = Box::new(Orientation::Vertical, 0); window.set_child(Some(&main_box)); + // Create bookmarks bar + let bookmarks_bar = bookmarks::BookmarksBar::new(); + + // Use Rc for shared mutable access + use std::rc::Rc; + use std::cell::RefCell; + let bookmarks_bar_shared = Rc::new(RefCell::new(bookmarks_bar)); + if let Some(enabled) = settings_window::load_i2p_proxy_enabled() { i2p_proxy.set_active(enabled); configure_i2p_proxy(enabled); @@ -80,7 +91,11 @@ fn build_ui(app: &Application) { web_view.set_vexpand(true); web_view.set_hexpand(true); + // Set webview for bookmarks bar + bookmarks_bar_shared.borrow_mut().set_webview(&web_view); + main_box.append(&url_entry); + main_box.append(bookmarks_bar_shared.borrow().get_container()); if let Some(enable_js) = settings_window::load_js_enabled() && let Some(web_settings) = webkit6::prelude::WebViewExt::settings(&web_view) { @@ -162,6 +177,49 @@ fn build_ui(app: &Application) { let i2p_panel_url = format!("http://127.0.0.1:{}/", panel_port); web_view_for_panel.load_uri(&i2p_panel_url); }); + + let web_view_for_bookmark = web_view.clone(); + let bookmarks_bar_for_bookmark = bookmarks_bar_shared.clone(); + bookmark_button.connect_clicked(move |_| { + if let Some(uri) = web_view_for_bookmark.uri() { + let dialog = gtk4::Dialog::builder() + .title("Add Bookmark") + .modal(true) + .default_width(300) + .build(); + + let content_area = dialog.content_area(); + let entry_box = gtk4::Box::new(Orientation::Vertical, 5); + entry_box.set_margin_top(10); + entry_box.set_margin_bottom(10); + entry_box.set_margin_start(10); + entry_box.set_margin_end(10); + + let label = gtk4::Label::new(Some("Bookmark name:")); + let name_entry = gtk4::Entry::new(); + name_entry.set_text(&uri); + + entry_box.append(&label); + entry_box.append(&name_entry); + content_area.append(&entry_box); + + dialog.add_button("Cancel", gtk4::ResponseType::Cancel); + dialog.add_button("Add", gtk4::ResponseType::Accept); + + let name_entry_for_response = name_entry.clone(); + let uri_for_response = uri.clone(); + let bookmarks_bar_clone = bookmarks_bar_for_bookmark.clone(); + dialog.connect_response(move |dialog, response| { + if response == gtk4::ResponseType::Accept { + let name = name_entry_for_response.text().to_string(); + bookmarks_bar_clone.borrow_mut().add_current_page(name, uri_for_response.to_string()); + } + dialog.close(); + }); + + dialog.present(); + } + }); web_view.load_uri("http://legwork.i2p/"); window.present(); } -- cgit v1.2.3