From 56744fd6959b50427b6a3ff54b76ea778e95c568 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 30 Oct 2024 20:20:46 +0200 Subject: [PATCH] update content api, add common uri_to_title helper function --- src/app/browser/window/tab/item/page.rs | 36 ++++++++++++++++++------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/app/browser/window/tab/item/page.rs b/src/app/browser/window/tab/item/page.rs index cec6e770..54372518 100644 --- a/src/app/browser/window/tab/item/page.rs +++ b/src/app/browser/window/tab/item/page.rs @@ -489,9 +489,8 @@ impl Page { move |result|{ match result { Ok(buffer) => { - // Update page meta - meta.borrow_mut().status = Some(Status::Success); - meta.borrow_mut().title = content.set_text_gemini( + // + let text_gemini = content.set_text_gemini( &uri, &match GString::from_utf8(buffer.to_utf8()) { Ok(gemtext) => gemtext, @@ -499,6 +498,13 @@ impl Page { } ); + // Update page meta + meta.borrow_mut().status = Some(Status::Success); + meta.borrow_mut().title = Some(match text_gemini.meta_title() { + Some(title) => title.clone(), + None => uri_to_title(&uri) + }); + // Update window components action_update.activate(Some(&id)); } @@ -571,12 +577,7 @@ impl Page { Ok(buffer) => { // Update page meta meta.borrow_mut().status = Some(Status::Success); - meta.borrow_mut().title = Some( - match url.split('/').last() { // @TODO may be empty - Some(filename) => gformat!("{filename}"), - None => gformat!("Image") - } - ); + meta.borrow_mut().title = Some(uri_to_title(&uri)); // Update page content content.set_image(&buffer); @@ -825,3 +826,20 @@ impl Page { ); } } + +// Tools + +/// Helper function, extract readable title from [Uri](https://docs.gtk.org/glib/struct.Uri.html) +/// +/// Useful as common placeholder when page title could not be detected +/// +/// * this feature may be improved and moved outside @TODO +fn uri_to_title(uri: &Uri) -> GString { + match uri.path().split('/').last() { + Some(filename) => gformat!("{filename}"), + None => match uri.host() { + Some(host) => gformat!("{host}"), + None => gformat!("Untitled"), + }, + } +}