From f2cdc7bc52795086ea60b9226b75e81459736dd6 Mon Sep 17 00:00:00 2001 From: yggverse Date: Thu, 13 Feb 2025 18:31:34 +0200 Subject: [PATCH] use common helper for duplicated function --- .../tab/item/client/driver/file/text.rs | 19 ++-------------- .../window/tab/item/client/driver/gemini.rs | 22 +++---------------- src/tool.rs | 15 +++++++++++++ 3 files changed, 20 insertions(+), 36 deletions(-) diff --git a/src/app/browser/window/tab/item/client/driver/file/text.rs b/src/app/browser/window/tab/item/client/driver/file/text.rs index 7ad55973..18b9145d 100644 --- a/src/app/browser/window/tab/item/client/driver/file/text.rs +++ b/src/app/browser/window/tab/item/client/driver/file/text.rs @@ -1,5 +1,5 @@ use super::Page; -use gtk::glib::{GString, Uri}; +use gtk::glib::Uri; use std::rc::Rc; pub enum Text { @@ -18,24 +18,9 @@ impl Text { page.search.set(Some(widget.text_view)); page.set_title(&match widget.meta.title { Some(title) => title.into(), // @TODO - None => uri_to_title(uri), + None => crate::tool::uri_to_title(uri), }); page.set_progress(0.0); page.window_action.find.simple_action.set_enabled(true); } } - -/// 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 already duplicated! -fn uri_to_title(uri: &Uri) -> GString { - let path = uri.path(); - if path.split('/').last().unwrap_or_default().is_empty() { - match uri.host() { - Some(host) => host, - None => "Untitled".into(), - } - } else { - path - } -} diff --git a/src/app/browser/window/tab/item/client/driver/gemini.rs b/src/app/browser/window/tab/item/client/driver/gemini.rs index daa4e8ad..15237450 100644 --- a/src/app/browser/window/tab/item/client/driver/gemini.rs +++ b/src/app/browser/window/tab/item/client/driver/gemini.rs @@ -7,7 +7,6 @@ use ggemini::{ client::{Client, Request, Response}, gio::{file_output_stream, memory_input_stream}, }; -use gtk::glib::GString; use gtk::{ gdk::Texture, gdk_pixbuf::Pixbuf, @@ -169,7 +168,7 @@ fn handle( Feature::Download => { // Init download widget let status = page.content.to_status_download( - uri_to_title(&uri).trim_matches(MAIN_SEPARATOR), // grab default filename from base URI, + crate::tool::uri_to_title(&uri).trim_matches(MAIN_SEPARATOR), // grab default filename from base URI, // format FS entities &cancellable, { @@ -264,7 +263,7 @@ fn handle( page.search.set(Some(widget.text_view)); page.set_title(&match widget.meta.title { Some(title) => title.into(), // @TODO - None => uri_to_title(&uri), + None => crate::tool::uri_to_title(&uri), }); page.set_progress(0.0); page.window_action @@ -330,7 +329,7 @@ fn handle( move |result| { match result { Ok(buffer) => { - page.set_title(&uri_to_title(&uri)); + page.set_title(&crate::tool::uri_to_title(&uri)); page.content.to_image(&Texture::for_pixbuf(&buffer)); } Err(e) => { @@ -469,18 +468,3 @@ fn handle( }, ) } - -/// 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 { - let path = uri.path(); - if path.split('/').last().unwrap_or_default().is_empty() { - match uri.host() { - Some(host) => host, - None => "Untitled".into(), - } - } else { - path - } -} diff --git a/src/tool.rs b/src/tool.rs index cd18ed27..5ef2a263 100644 --- a/src/tool.rs +++ b/src/tool.rs @@ -26,3 +26,18 @@ impl Format for usize { } } } + +/// Helper function, extract readable title from [Uri](https://docs.gtk.org/glib/struct.Uri.html) +/// * this feature wants to be improved @TODO +pub fn uri_to_title(uri: >k::glib::Uri) -> gtk::glib::GString { + let path = uri.path(); + + if path.split('/').last().unwrap_or_default().is_empty() { + match uri.host() { + Some(host) => host, + None => "Untitled".into(), + } + } else { + path + } +}