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,42 +57,34 @@ impl Client {
snap_history(&self.page, None); snap_history(&self.page, None);
} }
// try autocomplete scheme if the request match local filename // run async resolver to detect Uri, scheme-less host, or search query
if std::path::Path::new(&request).exists() { lookup(&self.profile, request, self.cancellable(), {
self.page let driver = self.driver.clone();
.item_action let page = self.page.clone();
.load move |feature, cancellable, result| {
.activate(Some(&format!("file://{request}")), is_snap_history) match result {
} else { // route by scheme
// run async resolver to detect Uri, scheme-less host, or search query Ok(uri) => match uri.scheme().as_str() {
lookup(&self.profile, request, self.cancellable(), { "file" => driver.file.handle(uri, feature, cancellable),
let driver = self.driver.clone(); "gemini" | "titan" => driver.gemini.handle(uri, feature, cancellable),
let page = self.page.clone(); scheme => {
move |feature, cancellable, result| { // no scheme match driver, complete with failure message
match result { let status = page.content.to_status_failure();
// route by scheme status.set_description(Some(&format!(
Ok(uri) => match uri.scheme().as_str() { "Scheme `{scheme}` yet not supported"
"file" => driver.file.handle(uri, feature, cancellable), )));
"gemini" | "titan" => driver.gemini.handle(uri, feature, cancellable), page.set_title(&status.title());
scheme => { page.set_progress(0.0);
// no scheme match driver, complete with failure message }
let status = page.content.to_status_failure(); },
status.set_description(Some(&format!( // begin redirection to new address suggested
"Scheme `{scheme}` yet not supported" Err(query) => page
))); .item_action
page.set_title(&status.title()); .load
page.set_progress(0.0); .activate(Some(&query), is_snap_history),
}
},
// begin redirection to new address suggested
Err(uri) => page
.item_action
.load
.activate(Some(&uri.to_string()), false),
}
} }
}) }
} })
} }
/// 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
@ -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()),
),
} }
} }
} }