diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2026-09-02 18:26:45 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2026-09-02 18:26:45 +0800 |
| commit | f7a43077fe4e139dd7e3f46f448e0951740ec148 (patch) | |
| tree | f6a68248fe42bd899e99049bab7fdb7e5d26ea60 /qml_plugin/src | |
| parent | 2f7576faf493b4667b02dcb60ddbf3aa0a9f654b (diff) | |
fix: qmlfmt
IListView: animation
Diffstat (limited to 'qml_plugin/src')
| -rw-r--r-- | qml_plugin/src/launcher/duckduckgo.rs | 98 | ||||
| -rw-r--r-- | qml_plugin/src/launcher/mathcalc.rs | 86 | ||||
| -rw-r--r-- | qml_plugin/src/launcher/smartcalc.rs | 93 | ||||
| -rw-r--r-- | qml_plugin/src/lib.rs | 15 | ||||
| -rw-r--r-- | qml_plugin/src/web/duckduckgo.rs | 118 | ||||
| -rw-r--r-- | qml_plugin/src/web/google.rs | 1 |
6 files changed, 132 insertions, 279 deletions
diff --git a/qml_plugin/src/launcher/duckduckgo.rs b/qml_plugin/src/launcher/duckduckgo.rs index 16c9102..0375cdf 100644 --- a/qml_plugin/src/launcher/duckduckgo.rs +++ b/qml_plugin/src/launcher/duckduckgo.rs @@ -16,19 +16,10 @@ mod qobject { #[qobject] #[qml_element] #[qml_singleton] - #[qproperty( - bool, ok - )] - #[qproperty( - QString, title - )] - #[qproperty( - QString, - description_html - )] - #[qproperty( - QString, image - )] + #[qproperty(bool, ok)] + #[qproperty(QString, title)] + #[qproperty(QString, description_html)] + #[qproperty(QString, image)] type DuckDuckGo = super::DuckDuckGoRs; #[qinvokable] @@ -61,60 +52,37 @@ pub struct DuckDuckGoRs { impl qobject::DuckDuckGo { fn query(self: Pin<&mut Self>, input: QString) { let qt_thread = self.qt_thread(); - self.rust_mut() - .executor - .spawn( - async move { - let qinput = input.to_string(); - let result: Result<QmlDuckDuckGo, _> = duckduckgo::query(&qinput) - .await - .map(|r| r.into()); - // consider just unwrapping since this is in a separate thread and is already - // end of life anw - _ = qt_thread - .queue( - move |mut qo| match result { - Ok(r) => { - if (r - .title - .to_lower() - == input.to_lower() - || r.title - .is_empty()) - && r.description_html - .is_empty() - && r.image - .is_empty() - { - qo.as_mut() - .set_ok(false); - } else { - qo.as_mut() - .set_ok(true); - qo.as_mut() - .set_title(r.title); - qo.as_mut() - .set_description_html(r.description_html); - qo.as_mut() - .set_image(r.image); - } - } - Err(_) => qo.set_ok(false), - }, - ) - .unwrap_print(); - }, - ); + self.rust_mut().executor.spawn(async move { + let qinput = input.to_string(); + let result: Result<QmlDuckDuckGo, _> = + duckduckgo::query(&qinput).await.map(|r| r.into()); + // consider just unwrapping since this is in a separate thread and + // is already end of life anw + _ = qt_thread + .queue(move |mut qo| match result { + Ok(r) => { + if (r.title.to_lower() == input.to_lower() || r.title.is_empty()) + && r.description_html.is_empty() + && r.image.is_empty() + { + qo.as_mut().set_ok(false); + } else { + qo.as_mut().set_ok(true); + qo.as_mut().set_title(r.title); + qo.as_mut().set_description_html(r.description_html); + qo.as_mut().set_image(r.image); + } + } + Err(_) => qo.set_ok(false), + }) + .unwrap_print(); + }); } fn reset(mut self: Pin<&mut Self>) { - self.as_mut() - .set_ok(false); - self.as_mut() - .set_title("".into()); - self.as_mut() - .set_description_html("".into()); - self.as_mut() - .set_image("".into()); + self.as_mut().set_ok(false); + self.as_mut().set_title("".into()); + self.as_mut().set_description_html("".into()); + self.as_mut().set_image("".into()); } } diff --git a/qml_plugin/src/launcher/mathcalc.rs b/qml_plugin/src/launcher/mathcalc.rs index d29239e..601dc3b 100644 --- a/qml_plugin/src/launcher/mathcalc.rs +++ b/qml_plugin/src/launcher/mathcalc.rs @@ -22,9 +22,7 @@ mod qobject { #[qobject] #[qml_element] #[qml_singleton] - #[qproperty( - QVariant, result - )] + #[qproperty(QVariant, result)] type MathCalc = super::MathCalcRs; #[qinvokable] @@ -63,61 +61,34 @@ impl Default for MathCalcRs { impl qobject::MathCalc { fn query(mut self: Pin<&mut Self>, input: QString) { let qt_thread = self.qt_thread(); - let ctx = Arc::clone( - &self - .as_mut() - .rust_mut() - .ctx, - ); + let ctx = Arc::clone(&self.as_mut().rust_mut().ctx); // TODO: consider moving to spawn_blocking - self.rust_mut() - .executor - .spawn( - async move { - let input = input.to_string(); - let result = parser::eval( - &mut *ctx - .lock() - .await, - &input, - // TODO: move to const/config - 53, - ) - .map_or_default( - |r| { - r.map_or_default( - |r| { - let r: QString = r - // to display scientific notation properly - // other ones show both version without e or shows *10^_ - // and have a = prefix - // to_string_big will result in 1/2=5.0000000000000000e − 1 - .to_string() - .into(); - if r != input.into() { - QVariant::from(&r) - } else { - QVariant::default() - } - }, - ) - }, - ); - _ = qt_thread - .queue( - |mut qo| { - qo.as_mut() - .set_result(result) - }, - ) - .unwrap_print(); - }, - ); + self.rust_mut().executor.spawn(async move { + let input = input.to_string(); + let result = parser::eval( + &mut *ctx.lock().await, + &input, + // TODO: move to const/config + 53, + ) + .map_or_default(|r| { + r.map_or_default(|r| { + let r: QString = r + // to display scientific notation properly + // other ones show both version without e or shows *10^_ + // and have a = prefix + // to_string_big will result in 1/2=5.0000000000000000e − 1 + .to_string() + .into(); + if r != input.into() { QVariant::from(&r) } else { QVariant::default() } + }) + }); + _ = qt_thread.queue(|mut qo| qo.as_mut().set_result(result)).unwrap_print(); + }); } fn reset(mut self: Pin<&mut Self>) { - self.as_mut() - .reset_result(); + self.as_mut().reset_result(); self.reset_ctx(); } @@ -126,13 +97,10 @@ impl qobject::MathCalc { } fn reset_ctx(mut self: Pin<&mut Self>) { - self.as_mut() - .rust_mut() - .ctx = Arc::new(Mutex::new(Self::new_ctx())) + self.as_mut().rust_mut().ctx = Arc::new(Mutex::new(Self::new_ctx())) } fn reset_result(mut self: Pin<&mut Self>) { - self.as_mut() - .set_result(QVariant::default()) + self.as_mut().set_result(QVariant::default()) } } diff --git a/qml_plugin/src/launcher/smartcalc.rs b/qml_plugin/src/launcher/smartcalc.rs index c23ecfa..9b5f05a 100644 --- a/qml_plugin/src/launcher/smartcalc.rs +++ b/qml_plugin/src/launcher/smartcalc.rs @@ -18,17 +18,9 @@ mod qobject { #[qobject] #[qml_element] #[qml_singleton] - #[qproperty( - QVariant, result - )] - #[qproperty( - ResultType, - result_type - )] - #[qproperty( - QVariant, - cached_query - )] + #[qproperty(QVariant, result)] + #[qproperty(ResultType, result_type)] + #[qproperty(QVariant, cached_query)] type SmartCalc = super::SmartCalcRs; #[qinvokable] @@ -89,64 +81,43 @@ impl qobject::SmartCalc { if self.cached_query == (&input).into() { return; } - self.as_mut() - .set_cached_query((&input.clone()).into()); + self.as_mut().set_cached_query((&input.clone()).into()); let qt_thread = self.qt_thread(); - self.as_mut() - .rust_mut() - .executor - .spawn( - async move { - let result = smart_calculator::calculate( - &input.to_string(), - None, + self.as_mut().rust_mut().executor.spawn(async move { + let result = smart_calculator::calculate(&input.to_string(), None).await; + _ = qt_thread + .queue(|mut qo| { + qo.as_mut().set_result_type(match &result { + Ok(value) => (&value.res_type).into(), + Err(_) => qobject::ResultType::Unset, + }); + qo.as_mut().set_result( + // TODO: do the calc outside of ts main qt ui render + // thread.(all *.rs) + match result { + Ok(value) => { + if value.formatted == value.input { + QVariant::default() + } else { + let result: QString = value.formatted.into(); + QVariant::from(&result) + } + } + Err(_) => QVariant::default(), + }, ) - .await; - _ = qt_thread - .queue( - |mut qo| { - qo.as_mut() - .set_result_type( - match &result { - Ok(value) => (&value.res_type).into(), - Err(_) => qobject::ResultType::Unset, - }, - ); - qo.as_mut() - .set_result( - // TODO: do the calc outside of ts main qt ui render - // thread.(all *.rs) - match result { - Ok(value) => { - if value.formatted == value.input { - QVariant::default() - } else { - let result: QString = value - .formatted - .into(); - QVariant::from(&result) - } - } - Err(_) => QVariant::default(), - }, - ) - }, - ) - .unwrap_print(); - }, - ); + }) + .unwrap_print(); + }); } fn reset(mut self: Pin<&mut Self>) { - self.as_mut() - .set_cached_query(QVariant::default()); + self.as_mut().set_cached_query(QVariant::default()); self.reset_result(); } fn reset_result(mut self: Pin<&mut Self>) { - self.as_mut() - .set_result(QVariant::default()); - self.as_mut() - .set_result_type(qobject::ResultType::Unset); + self.as_mut().set_result(QVariant::default()); + self.as_mut().set_result_type(qobject::ResultType::Unset); } } diff --git a/qml_plugin/src/lib.rs b/qml_plugin/src/lib.rs index f9f095e..0f1411c 100644 --- a/qml_plugin/src/lib.rs +++ b/qml_plugin/src/lib.rs @@ -2,14 +2,8 @@ use std::sync::LazyLock; use tokio::task; -static RUNTIME: LazyLock<tokio::runtime::Runtime> = LazyLock::new( - || { - tokio::runtime::Builder::new_multi_thread() - .enable_all() - .build() - .unwrap() - }, -); +static RUNTIME: LazyLock<tokio::runtime::Runtime> = + LazyLock::new(|| tokio::runtime::Builder::new_multi_thread().enable_all().build().unwrap()); /// for example duckduckgo we wanna cancel first request if user types new query /// using this guarentee that only one request is running at a time or at least @@ -32,10 +26,7 @@ impl ExclusiveExecutor { F: Future<Output = ()> + Send + 'static, F::Output: Send + 'static, { - if let Some(handle) = self - .handle - .take() - { + if let Some(handle) = self.handle.take() { handle.abort(); } diff --git a/qml_plugin/src/web/duckduckgo.rs b/qml_plugin/src/web/duckduckgo.rs index fcfa633..6b4edae 100644 --- a/qml_plugin/src/web/duckduckgo.rs +++ b/qml_plugin/src/web/duckduckgo.rs @@ -20,21 +20,9 @@ pub struct DuckDuckGoContent { impl From<DuckDuckGoContent> for QmlDuckDuckGo { fn from(value: DuckDuckGoContent) -> Self { Self { - title: value - .title - .into(), - description_html: value - .description_html - .into(), - image: value - .image - .map( - |u| { - u.to_string() - .into() - }, - ) - .unwrap_or_default(), + title: value.title.into(), + description_html: value.description_html.into(), + image: value.image.map(|u| u.to_string().into()).unwrap_or_default(), } } } @@ -86,79 +74,45 @@ pub async fn query(input: &str) -> anyhow::Result<DuckDuckGoContent> { let resp = Browser::new() .get_api_response( input, - Some( - &SearchParams::new() - .safe_search(SafeSearch::Off) - .full_urls(Toggle::On), - ), + Some(&SearchParams::new().safe_search(SafeSearch::Off).full_urls(Toggle::On)), ) .await?; - let infobox = match resp - .info_box - .map(serde_json::from_value::<Infobox>) - { + let infobox = match resp.info_box.map(serde_json::from_value::<Infobox>) { Some(Ok(o)) => Some(o), _ => None, }; - let alt_desc = resp - .entity - .unwrap_or( - match infobox { - Some(i) => i - .iter() - .find(|i| i.data_type == "wd_description" || i.data_type == "official_website") - .map_or( - String::new(), - |i| { - if i.data_type == "official_website" { - i.label - .clone() - } else { - match i.value { - InfoboxValue::Entity(_) => unreachable!(), - InfoboxValue::String(ref s) => s.to_string(), - } - } - }, - ), - // TODO: find better way to get title from infobox - None => String::new(), - }, - ); + let alt_desc = resp.entity.unwrap_or(match infobox { + Some(i) => i + .iter() + .find(|i| i.data_type == "wd_description" || i.data_type == "official_website") + .map_or(String::new(), |i| { + if i.data_type == "official_website" { + i.label.clone() + } else { + match i.value { + InfoboxValue::Entity(_) => unreachable!(), + InfoboxValue::String(ref s) => s.to_string(), + } + } + }), + // TODO: find better way to get title from infobox + None => String::new(), + }); - Ok( - DuckDuckGoContent { - title: resp - .heading - .filter(|h| !h.is_empty()) - .unwrap_or( - resp.definition - .filter(|d| !d.is_empty()) - .unwrap_or(alt_desc.clone()), - ), - description_html: resp - .answer - .filter(|a| !a.is_empty()) - .unwrap_or( - resp.r#abstract - .filter(|a| !a.is_empty()) - .unwrap_or(alt_desc), - ), - // TODO: try also Results[*].Icon.URL and RelatedTopics[*].Icon.URL - image: resp - .image - .filter(|i| !i.is_empty()) - .and_then( - |i| { - if !i.is_empty() { - Url::parse(&format!("{DUCKDUCKGO}{i}",)).ok() - } else { - None - } - }, - ), - }, - ) + Ok(DuckDuckGoContent { + title: resp + .heading + .filter(|h| !h.is_empty()) + .unwrap_or(resp.definition.filter(|d| !d.is_empty()).unwrap_or(alt_desc.clone())), + description_html: resp + .answer + .filter(|a| !a.is_empty()) + .unwrap_or(resp.r#abstract.filter(|a| !a.is_empty()).unwrap_or(alt_desc)), + // TODO: try also Results[*].Icon.URL and RelatedTopics[*].Icon.URL + image: resp.image.filter(|i| !i.is_empty()).and_then(|i| { + if !i.is_empty() { Url::parse(&format!("{DUCKDUCKGO}{i}",)).ok() } else { None } + }), + }) } diff --git a/qml_plugin/src/web/google.rs b/qml_plugin/src/web/google.rs index e69de29..8b13789 100644 --- a/qml_plugin/src/web/google.rs +++ b/qml_plugin/src/web/google.rs @@ -0,0 +1 @@ + |
