summaryrefslogtreecommitdiff
path: root/qml_plugin/src/launcher
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2026-08-03 20:11:48 +0800
committerlittleblack111 <littleblack11111@gmail.com>2026-08-04 23:07:51 +0800
commitf36d416853696b103934b56ee57118c733191dfa (patch)
tree29c824be162d51dffcbbb35137dcd4596d7a15bd /qml_plugin/src/launcher
parent029ff8999a5d3c1613302b805228cd6927b4dddb (diff)
feat: make use of ExclusiveExecutor
mathcalc: use to_string_big() // to display scientific notation properly // other ones show both version without e or shows *10^_ // and have a = prefix unwrap_print queueing to qt main thread
Diffstat (limited to 'qml_plugin/src/launcher')
-rw-r--r--qml_plugin/src/launcher/duckduckgo.rs69
-rw-r--r--qml_plugin/src/launcher/mathcalc.rs105
-rw-r--r--qml_plugin/src/launcher/smartcalc.rs88
3 files changed, 151 insertions, 111 deletions
diff --git a/qml_plugin/src/launcher/duckduckgo.rs b/qml_plugin/src/launcher/duckduckgo.rs
index aa1ce2d..bb0f806 100644
--- a/qml_plugin/src/launcher/duckduckgo.rs
+++ b/qml_plugin/src/launcher/duckduckgo.rs
@@ -1,8 +1,9 @@
-use cxx_qt::Threading;
+use cxx_qt::{CxxQtType, Threading};
use cxx_qt_lib::QString;
use std::pin::Pin;
+use unwrap_print::PrintableResult;
-use crate::{RUNTIME, web::duckduckgo};
+use crate::{ExclusiveExecutor, web::duckduckgo};
#[cxx_qt::bridge]
mod qobject {
@@ -16,7 +17,7 @@ mod qobject {
#[qml_element]
#[qml_singleton]
#[qproperty(
- bool, valid
+ bool, ok
)]
#[qproperty(
QString, title
@@ -50,43 +51,53 @@ pub struct QmlDuckDuckGo {
#[derive(Default)]
pub struct DuckDuckGoRs {
- valid: bool,
+ ok: bool,
title: QString,
description_html: QString,
preview_image: QString,
+
+ executor: ExclusiveExecutor,
}
impl qobject::DuckDuckGo {
- fn query(self: Pin<&mut Self>, input: QString) {
+ fn query(mut self: Pin<&mut Self>, input: QString) {
let qt_thread = self.qt_thread();
- RUNTIME.spawn(
- async move {
- let input = input.to_string();
- let result: Result<QmlDuckDuckGo, _> = duckduckgo::query(&input)
- .await
- .map(|r| r.into());
- qt_thread.queue(
- |mut qo| match result {
- Ok(r) => {
- qo.as_mut()
- .set_valid(true);
- qo.as_mut()
- .set_title(r.title);
- qo.as_mut()
- .set_description_html(r.description_html);
- qo.as_mut()
- .set_preview_image(r.preview_image);
- }
- Err(_) => qo.set_valid(false),
- },
- )
- },
- );
+ // self.as_mut()
+ // .reset();
+ self.rust_mut()
+ .executor
+ .spawn(
+ async move {
+ let input = input.to_string();
+ let result: Result<QmlDuckDuckGo, _> = duckduckgo::query(&input)
+ .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(
+ |mut qo| match result {
+ Ok(r) => {
+ 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_preview_image(r.preview_image);
+ }
+ Err(_) => qo.set_ok(false),
+ },
+ )
+ .unwrap_print();
+ },
+ );
}
fn reset(mut self: Pin<&mut Self>) {
self.as_mut()
- .set_valid(false);
+ .set_ok(false);
self.as_mut()
.set_title("".into());
self.as_mut()
diff --git a/qml_plugin/src/launcher/mathcalc.rs b/qml_plugin/src/launcher/mathcalc.rs
index 4ac625d..8c24a92 100644
--- a/qml_plugin/src/launcher/mathcalc.rs
+++ b/qml_plugin/src/launcher/mathcalc.rs
@@ -1,10 +1,11 @@
use cxx_qt::{CxxQtType, Threading};
use cxx_qt_lib::{QString, QVariant};
-use kalk::parser;
+use kalk::{calculation_result::CalculationResult, parser};
use std::{pin::Pin, sync::Arc};
use tokio::sync::Mutex;
+use unwrap_print::PrintableResult;
-use crate::RUNTIME;
+use crate::ExclusiveExecutor;
const DEFAULT_ANGLE_UNIT: &str = "deg";
@@ -42,58 +43,75 @@ mod qobject {
impl cxx_qt::Threading for MathCalc {}
}
-#[derive(Default)]
pub struct MathCalcRs {
result: QVariant,
-
ctx: Arc<Mutex<parser::Context>>,
+
+ executor: ExclusiveExecutor,
+}
+
+impl Default for MathCalcRs {
+ fn default() -> Self {
+ Self {
+ result: QVariant::default(),
+ ctx: Arc::new(Mutex::new(qobject::MathCalc::new_ctx())),
+ executor: ExclusiveExecutor::default(),
+ }
+ }
}
impl qobject::MathCalc {
- fn query(self: Pin<&mut Self>, input: QString) {
+ 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,
);
// TODO: consider moving to spawn_blocking
- RUNTIME.spawn(
- async move {
- let input = input.to_string();
- let result = parser::eval(
- &mut *ctx
- .lock()
- .await,
- &input,
- // TODO: move to const/config
- 53,
- );
- qt_thread.queue(
- |mut qo| {
- qo.as_mut()
- .set_result(
- result.map_or_default(
- |r| {
- r.map_or_default(
- |r| {
- let result: QString = r
- .to_string()
- .into();
- if result != input.into() {
- QVariant::from(&result)
- } else {
- QVariant::default()
- }
- },
- )
- },
- ),
+ 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()
+ .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>) {
@@ -102,11 +120,14 @@ impl qobject::MathCalc {
self.reset_ctx();
}
+ fn new_ctx() -> parser::Context {
+ parser::Context::default().set_angle_unit(DEFAULT_ANGLE_UNIT)
+ }
+
fn reset_ctx(mut self: Pin<&mut Self>) {
self.as_mut()
.rust_mut()
- .ctx =
- Arc::new(Mutex::new(parser::Context::default().set_angle_unit(DEFAULT_ANGLE_UNIT)));
+ .ctx = Arc::new(Mutex::new(Self::new_ctx()))
}
fn reset_result(mut self: Pin<&mut Self>) {
diff --git a/qml_plugin/src/launcher/smartcalc.rs b/qml_plugin/src/launcher/smartcalc.rs
index 9e94f77..98cb037 100644
--- a/qml_plugin/src/launcher/smartcalc.rs
+++ b/qml_plugin/src/launcher/smartcalc.rs
@@ -1,8 +1,9 @@
-use cxx_qt::Threading;
+use cxx_qt::{CxxQtType, Threading};
use cxx_qt_lib::{QString, QVariant};
use std::pin::Pin;
+use unwrap_print::PrintableResult;
-use crate::RUNTIME;
+use crate::{ExclusiveExecutor, RUNTIME};
#[cxx_qt::bridge]
mod qobject {
@@ -79,6 +80,8 @@ pub struct SmartCalcRs {
result_type: qobject::ResultType,
cached_query: QVariant,
+
+ executor: ExclusiveExecutor,
}
impl qobject::SmartCalc {
@@ -89,44 +92,49 @@ impl qobject::SmartCalc {
self.as_mut()
.set_cached_query((&input.clone()).into());
let qt_thread = self.qt_thread();
- RUNTIME.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(),
- },
- )
- },
- )
- },
- );
+ 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(),
+ },
+ )
+ },
+ )
+ .unwrap_print();
+ },
+ );
}
fn reset(mut self: Pin<&mut Self>) {