make update action by item id

This commit is contained in:
yggverse 2024-10-15 08:45:44 +03:00
parent c92a5406f5
commit 6c4137f2b6
7 changed files with 48 additions and 41 deletions

View File

@ -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
}
});

View File

@ -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::<String>()
.expect("Parameter does not match `String`")
.as_str(),
);
}
});

View File

@ -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> {

View File

@ -26,7 +26,7 @@ pub struct Tab {
action_tab_page_navigation_reload: Arc<SimpleAction>,
action_update: Arc<SimpleAction>,
// Dynamically allocated reference index
index: Arc<RefCell<HashMap<Arc<GString>, Arc<Item>>>>,
index: Arc<RefCell<HashMap<GString, Arc<Item>>>>,
// GTK
widget: Arc<Widget>,
}
@ -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())
};
}
}
}

View File

@ -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<GString>,
id: GString,
// Components
page: Arc<Page>,
widget: Arc<Widget>,
@ -39,7 +39,7 @@ impl Item {
is_selected: bool,
) -> Arc<Self> {
// 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<GString> {
pub fn id(&self) -> GString {
self.id.clone()
}

View File

@ -27,7 +27,7 @@ use sqlite::Transaction;
use std::{cell::RefCell, path::Path, sync::Arc};
pub struct Page {
id: Arc<GString>,
id: GString,
// Actions
action_page_open: Arc<SimpleAction>,
action_tab_page_navigation_reload: Arc<SimpleAction>,
@ -44,7 +44,7 @@ pub struct Page {
impl Page {
// Construct
pub fn new_arc(
id: Arc<GString>,
id: GString,
action_tab_open: Arc<SimpleAction>,
action_tab_page_navigation_base: Arc<SimpleAction>,
action_tab_page_navigation_history_back: Arc<SimpleAction>,
@ -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()));
}
}
}

View File

@ -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 |_| {