move filepath resolve to the lookup function

This commit is contained in:
yggverse 2025-02-15 15:56:05 +02:00
parent be9e55a4cf
commit 80c9a2792a

View File

@ -57,13 +57,6 @@ impl Client {
snap_history(&self.page, None); snap_history(&self.page, None);
} }
// try autocomplete scheme if the request match local filename
if std::path::Path::new(&request).exists() {
self.page
.item_action
.load
.activate(Some(&format!("file://{request}")), is_snap_history)
} else {
// run async resolver to detect Uri, scheme-less host, or search query // run async resolver to detect Uri, scheme-less host, or search query
lookup(&self.profile, request, self.cancellable(), { lookup(&self.profile, request, self.cancellable(), {
let driver = self.driver.clone(); let driver = self.driver.clone();
@ -85,15 +78,14 @@ impl Client {
} }
}, },
// begin redirection to new address suggested // begin redirection to new address suggested
Err(uri) => page Err(query) => page
.item_action .item_action
.load .load
.activate(Some(&uri.to_string()), false), .activate(Some(&query), is_snap_history),
} }
} }
}) })
} }
}
/// Get new [Cancellable](https://docs.gtk.org/gio/class.Cancellable.html) by cancel previous one /// Get new [Cancellable](https://docs.gtk.org/gio/class.Cancellable.html) by cancel previous one
fn cancellable(&self) -> Cancellable { fn cancellable(&self) -> Cancellable {
@ -117,7 +109,7 @@ fn lookup(
profile: &Rc<Profile>, profile: &Rc<Profile>,
query: &str, query: &str,
cancellable: Cancellable, cancellable: Cancellable,
callback: impl FnOnce(Rc<Feature>, Cancellable, Result<Uri, Uri>) + 'static, callback: impl FnOnce(Rc<Feature>, Cancellable, Result<Uri, String>) + 'static,
) { ) {
use gtk::{ use gtk::{
gio::{NetworkAddress, Resolver}, gio::{NetworkAddress, Resolver},
@ -157,18 +149,33 @@ fn lookup(
feature, feature,
cancellable, cancellable,
if resolve.is_ok() { if resolve.is_ok() {
match Uri::parse(&suggestion, UriFlags::NONE) { Err(match Uri::parse(&suggestion, UriFlags::NONE) {
Ok(uri) => Err(uri), Ok(uri) => uri,
Err(_) => Err(search(&profile, &query)), Err(_) => search(&profile, &query),
} }
.to_string())
} else { } else {
Err(search(&profile, &query)) const FILE_SCHEME: &str = "file://";
Err(
// try autocomplete scheme if the request match local filename
if !query.starts_with(FILE_SCHEME)
&& std::path::Path::new(&query).exists()
{
format!("{FILE_SCHEME}{query}")
} else {
search(&profile, &query).to_string()
},
)
}, },
) )
} }
}, },
), ),
Err(_) => callback(feature, cancellable, Err(search(profile, query))), Err(_) => callback(
feature,
cancellable,
Err(search(profile, query).to_string()),
),
} }
} }
} }