mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-02 09:35:28 +00:00
use common helper for duplicated function
This commit is contained in:
parent
6806f1876c
commit
f2cdc7bc52
3 changed files with 20 additions and 36 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
use super::Page;
|
use super::Page;
|
||||||
use gtk::glib::{GString, Uri};
|
use gtk::glib::Uri;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
pub enum Text {
|
pub enum Text {
|
||||||
|
|
@ -18,24 +18,9 @@ impl Text {
|
||||||
page.search.set(Some(widget.text_view));
|
page.search.set(Some(widget.text_view));
|
||||||
page.set_title(&match widget.meta.title {
|
page.set_title(&match widget.meta.title {
|
||||||
Some(title) => title.into(), // @TODO
|
Some(title) => title.into(), // @TODO
|
||||||
None => uri_to_title(uri),
|
None => crate::tool::uri_to_title(uri),
|
||||||
});
|
});
|
||||||
page.set_progress(0.0);
|
page.set_progress(0.0);
|
||||||
page.window_action.find.simple_action.set_enabled(true);
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ use ggemini::{
|
||||||
client::{Client, Request, Response},
|
client::{Client, Request, Response},
|
||||||
gio::{file_output_stream, memory_input_stream},
|
gio::{file_output_stream, memory_input_stream},
|
||||||
};
|
};
|
||||||
use gtk::glib::GString;
|
|
||||||
use gtk::{
|
use gtk::{
|
||||||
gdk::Texture,
|
gdk::Texture,
|
||||||
gdk_pixbuf::Pixbuf,
|
gdk_pixbuf::Pixbuf,
|
||||||
|
|
@ -169,7 +168,7 @@ fn handle(
|
||||||
Feature::Download => {
|
Feature::Download => {
|
||||||
// Init download widget
|
// Init download widget
|
||||||
let status = page.content.to_status_download(
|
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
|
// format FS entities
|
||||||
&cancellable,
|
&cancellable,
|
||||||
{
|
{
|
||||||
|
|
@ -264,7 +263,7 @@ fn handle(
|
||||||
page.search.set(Some(widget.text_view));
|
page.search.set(Some(widget.text_view));
|
||||||
page.set_title(&match widget.meta.title {
|
page.set_title(&match widget.meta.title {
|
||||||
Some(title) => title.into(), // @TODO
|
Some(title) => title.into(), // @TODO
|
||||||
None => uri_to_title(&uri),
|
None => crate::tool::uri_to_title(&uri),
|
||||||
});
|
});
|
||||||
page.set_progress(0.0);
|
page.set_progress(0.0);
|
||||||
page.window_action
|
page.window_action
|
||||||
|
|
@ -330,7 +329,7 @@ fn handle(
|
||||||
move |result| {
|
move |result| {
|
||||||
match result {
|
match result {
|
||||||
Ok(buffer) => {
|
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));
|
page.content.to_image(&Texture::for_pixbuf(&buffer));
|
||||||
}
|
}
|
||||||
Err(e) => {
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
15
src/tool.rs
15
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue