update content api, add common uri_to_title helper function

This commit is contained in:
yggverse 2024-10-30 20:20:46 +02:00
parent 7757bc2283
commit 56744fd695

View file

@ -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"),
},
}
}