From 6c4137f2b659675a98bddd4eede5d521ea4f9463 Mon Sep 17 00:00:00 2001 From: yggverse Date: Tue, 15 Oct 2024 08:45:44 +0300 Subject: [PATCH] make update action by item id --- src/app.rs | 9 +++-- src/app/browser.rs | 9 +++-- src/app/browser/window.rs | 4 +-- src/app/browser/window/tab.rs | 22 ++++++------ src/app/browser/window/tab/item.rs | 6 ++-- src/app/browser/window/tab/item/page.rs | 35 ++++++++++--------- .../item/page/navigation/request/widget.rs | 4 +-- 7 files changed, 48 insertions(+), 41 deletions(-) diff --git a/src/app.rs b/src/app.rs index 4f96022f..21e8ece4 100644 --- a/src/app.rs +++ b/src/app.rs @@ -9,7 +9,10 @@ use database::Database; use adw::Application; use gtk::{ glib::ExitCode, - prelude::{ActionExt, ApplicationExt, ApplicationExtManual, GtkApplicationExt, GtkWindowExt}, + prelude::{ + ActionExt, ApplicationExt, ApplicationExtManual, GtkApplicationExt, GtkWindowExt, + StaticVariantType, ToVariant, + }, }; use sqlite::{Connection, Transaction}; @@ -41,7 +44,7 @@ impl App { let action_tool_debug = Action::new("win", true, None); let action_tool_profile = Action::new("win", true, None); let action_quit = Action::new("win", true, None); - let action_update = Action::new("win", true, None); + let action_update = Action::new("win", true, Some(&String::static_variant_type())); let action_tab_append = Action::new("win", true, None); let action_tab_close = Action::new("win", true, None); let action_tab_close_all = Action::new("win", true, None); @@ -102,7 +105,7 @@ impl App { let action_update = action_update.simple(); move |_| { // Make initial update - action_update.activate(None); + action_update.activate(Some(&"".to_variant())); // @TODO } }); diff --git a/src/app/browser.rs b/src/app/browser.rs index b079567e..fa5acf2b 100644 --- a/src/app/browser.rs +++ b/src/app/browser.rs @@ -107,8 +107,13 @@ impl Browser { action_update.connect_activate({ let window = window.clone(); - move |_, _| { - window.update(); + move |_, id| { + window.update( + id.expect("Page ID required for update action") + .get::() + .expect("Parameter does not match `String`") + .as_str(), + ); } }); diff --git a/src/app/browser/window.rs b/src/app/browser/window.rs index 9c058bb1..4caca2d6 100644 --- a/src/app/browser/window.rs +++ b/src/app/browser/window.rs @@ -106,8 +106,8 @@ impl Window { self.tab.pin(); } - pub fn update(&self) { - self.tab.update(); + pub fn update(&self, id: &str) { + self.tab.update(id); } pub fn clean(&self, transaction: &Transaction, app_browser_id: &i64) -> Result<(), String> { diff --git a/src/app/browser/window/tab.rs b/src/app/browser/window/tab.rs index f55e29e1..c20f76f0 100644 --- a/src/app/browser/window/tab.rs +++ b/src/app/browser/window/tab.rs @@ -26,7 +26,7 @@ pub struct Tab { action_tab_page_navigation_reload: Arc, action_update: Arc, // Dynamically allocated reference index - index: Arc, Arc>>>, + index: Arc>>>, // GTK widget: Arc, } @@ -208,18 +208,16 @@ impl Tab { } } - pub fn update(&self) { - if let Some(id) = self.widget.current_page_keyword() { - if let Some(item) = self.index.borrow().get(&id) { - // Update item components - item.update(); + pub fn update(&self, id: &str) { + if let Some(item) = self.index.borrow().get(id) { + // Update item components + item.update(); - // Update tab title on loading indicator inactive - if !item.page_is_loading() { - if let Some(title) = item.page_meta_title() { - item.gobject().set_title(title.as_str()) - }; - } + // Update tab title on loading indicator inactive + if !item.page_is_loading() { + if let Some(title) = item.page_meta_title() { + item.gobject().set_title(title.as_str()) + }; } } } diff --git a/src/app/browser/window/tab/item.rs b/src/app/browser/window/tab/item.rs index c31e7e25..3d4ebe8a 100644 --- a/src/app/browser/window/tab/item.rs +++ b/src/app/browser/window/tab/item.rs @@ -17,7 +17,7 @@ use std::sync::Arc; pub struct Item { // Auto-generated unique item ID // useful as widget name in GTK actions callback - id: Arc, + id: GString, // Components page: Arc, widget: Arc, @@ -39,7 +39,7 @@ impl Item { is_selected: bool, ) -> Arc { // Generate unique ID for new page components - let id = Arc::new(uuid_string_random()); + let id = uuid_string_random(); // Init components let page = Page::new_arc( @@ -201,7 +201,7 @@ impl Item { } // Getters - pub fn id(&self) -> Arc { + pub fn id(&self) -> GString { self.id.clone() } diff --git a/src/app/browser/window/tab/item/page.rs b/src/app/browser/window/tab/item/page.rs index be8fb4e7..4b5ba5d9 100644 --- a/src/app/browser/window/tab/item/page.rs +++ b/src/app/browser/window/tab/item/page.rs @@ -27,7 +27,7 @@ use sqlite::Transaction; use std::{cell::RefCell, path::Path, sync::Arc}; pub struct Page { - id: Arc, + id: GString, // Actions action_page_open: Arc, action_tab_page_navigation_reload: Arc, @@ -44,7 +44,7 @@ pub struct Page { impl Page { // Construct pub fn new_arc( - id: Arc, + id: GString, action_tab_open: Arc, action_tab_page_navigation_base: Arc, action_tab_page_navigation_history_back: Arc, @@ -155,6 +155,7 @@ impl Page { let request_text = self.navigation.request_text(); // Init shared objects for async access + let id = self.id.to_variant(); let navigation = self.navigation.clone(); let content = self.content.clone(); let meta = self.meta.clone(); @@ -166,7 +167,7 @@ impl Page { meta.borrow_mut().title = Some(gformat!("Loading..")); meta.borrow_mut().description = None; - action_update.activate(None); + action_update.activate(Some(&id)); /*let _uri = */ match Uri::parse(&request_text, UriFlags::NONE) { @@ -187,7 +188,7 @@ impl Page { meta.borrow_mut().status = Some(Status::Prepare); meta.borrow_mut().description = Some(gformat!("Connect {host}..")); - action_update.activate(None); + action_update.activate(Some(&id)); // Create new connection let cancellable = Cancellable::new(); @@ -208,7 +209,7 @@ impl Page { meta.borrow_mut().status = Some(Status::Connect); meta.borrow_mut().description = Some(gformat!("Connected to {host}..")); - action_update.activate(None); + action_update.activate(Some(&id)); // Send request connection.output_stream().write_all_async( @@ -221,7 +222,7 @@ impl Page { meta.borrow_mut().status = Some(Status::Request); meta.borrow_mut().description = Some(gformat!("Request data from {host}..")); - action_update.activate(None); + action_update.activate(Some(&id)); // Read response connection.input_stream().read_all_async( @@ -239,7 +240,7 @@ impl Page { meta.borrow_mut().description = Some(host); meta.borrow_mut().title = Some(uri.path()); - action_update.activate(None); + action_update.activate(Some(&id)); // Try create short base for title let path = uri.path(); @@ -290,7 +291,7 @@ impl Page { } // Update window components - action_update.activate(None); + action_update.activate(Some(&id)); }, None => todo!(), } @@ -299,7 +300,7 @@ impl Page { meta.borrow_mut().status = Some(Status::Success); meta.borrow_mut().mime = Some(Mime::TextPlain); - action_update.activate(None); + action_update.activate(Some(&id)); todo!() }, _ => { @@ -307,7 +308,7 @@ impl Page { meta.borrow_mut().title = Some(gformat!("Oops")); meta.borrow_mut().description = Some(gformat!("Content {mime} not supported")); - action_update.activate(None); + action_update.activate(Some(&id)); }, } None => todo!(), @@ -320,7 +321,7 @@ impl Page { meta.borrow_mut().mime = Some(Mime::TextGemini); meta.borrow_mut().title = Some(gformat!("Redirect")); - action_update.activate(None); + action_update.activate(Some(&id)); // Select widget match parts.get(3) { @@ -342,7 +343,7 @@ impl Page { meta.borrow_mut().title = Some(gformat!("Oops")); meta.borrow_mut().description = Some(gformat!("Status {code} not supported")); - action_update.activate(None); + action_update.activate(Some(&id)); }, } None => todo!(), @@ -353,7 +354,7 @@ impl Page { meta.borrow_mut().title = Some(gformat!("Oops")); meta.borrow_mut().description = Some(gformat!("Failed to read buffer data: {e}")); - action_update.activate(None); + action_update.activate(Some(&id)); } } @@ -368,7 +369,7 @@ impl Page { meta.borrow_mut().title = Some(gformat!("Oops")); meta.borrow_mut().description = Some(gformat!("Failed to read response: {:?}", e)); - action_update.activate(None); + action_update.activate(Some(&id)); // Close connection if let Err(e) = connection.close(Some(&cancellable)) { @@ -384,7 +385,7 @@ impl Page { meta.borrow_mut().title = Some(gformat!("Oops")); meta.borrow_mut().description = Some(gformat!("Failed to read request: {:?}", e)); - action_update.activate(None); + action_update.activate(Some(&id)); // Close connection if let Err(e) = connection.close(Some(&cancellable)) { @@ -400,7 +401,7 @@ impl Page { meta.borrow_mut().title = Some(gformat!("Oops")); meta.borrow_mut().description = Some(gformat!("Failed to connect: {:?}", e)); - action_update.activate(None); + action_update.activate(Some(&id)); } }, ); @@ -415,7 +416,7 @@ impl Page { meta.borrow_mut().description = Some(gformat!("Protocol {scheme} not supported")); - action_update.activate(None); + action_update.activate(Some(&self.id.to_variant())); } } } diff --git a/src/app/browser/window/tab/item/page/navigation/request/widget.rs b/src/app/browser/window/tab/item/page/navigation/request/widget.rs index 8b016ad1..e975f48e 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/widget.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/widget.rs @@ -5,7 +5,7 @@ use database::Database; use gtk::{ gio::SimpleAction, glib::{timeout_add_local, ControlFlow, GString, SourceId}, - prelude::{ActionExt, EditableExt, EntryExt}, + prelude::{ActionExt, EditableExt, EntryExt, ToVariant}, Entry, }; use sqlite::Transaction; @@ -47,7 +47,7 @@ impl Widget { // Connect events gobject.connect_changed(move |_| { - action_update.activate(None); + action_update.activate(Some(&"".to_variant())); // @TODO }); gobject.connect_activate(move |_| {