summaryrefslogtreecommitdiff
path: root/qml_plugin/src/launcher/smartcalc.rs
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2026-07-27 13:14:10 +0800
committerlittleblack111 <littleblack11111@gmail.com>2026-07-27 13:14:10 +0800
commit1f9873b40fee2d33c5b7103a25077ec61a387063 (patch)
tree29a8b875c1a44e3781ab3f486c74ee6b7a9df078 /qml_plugin/src/launcher/smartcalc.rs
parentdf74bc4c98036f6b0bddf1c9baf772274e33606a (diff)
refactor: ISmartCalc
new(SmartCalc): Unit
Diffstat (limited to 'qml_plugin/src/launcher/smartcalc.rs')
-rw-r--r--qml_plugin/src/launcher/smartcalc.rs135
1 files changed, 135 insertions, 0 deletions
diff --git a/qml_plugin/src/launcher/smartcalc.rs b/qml_plugin/src/launcher/smartcalc.rs
new file mode 100644
index 0000000..7b1b066
--- /dev/null
+++ b/qml_plugin/src/launcher/smartcalc.rs
@@ -0,0 +1,135 @@
+use cxx_qt::Threading;
+use cxx_qt_lib::{QString, QVariant};
+use std::pin::Pin;
+
+use crate::RUNTIME;
+
+#[cxx_qt::bridge]
+mod qobject {
+ extern "C++" {
+ include!("cxx-qt-lib/qstring.h");
+ include!("cxx-qt-lib/qvariant.h");
+ type QString = cxx_qt_lib::QString;
+ type QVariant = cxx_qt_lib::QVariant;
+ }
+
+ extern "RustQt" {
+ #[qobject]
+ #[qml_element]
+ #[qml_singleton]
+ #[qproperty(
+ QVariant, result
+ )]
+ #[qproperty(
+ ResultType,
+ result_type
+ )]
+ #[qproperty(
+ QVariant,
+ cached_query
+ )]
+ type SmartCalc = super::SmartCalcRs;
+
+ #[qinvokable]
+ fn query(self: Pin<&mut SmartCalc>, input: QString);
+
+ #[qinvokable]
+ fn reset(self: Pin<&mut SmartCalc>);
+ }
+
+ impl cxx_qt::Threading for SmartCalc {}
+
+ #[qenum(SmartCalc)]
+ pub enum ResultType {
+ Math,
+ Unit,
+ Currency,
+ Crypto,
+ Time,
+ Date,
+ Unset,
+ }
+}
+
+impl Default for qobject::ResultType {
+ fn default() -> Self {
+ qobject::ResultType::Unset
+ }
+}
+
+impl From<&smart_calculator::types::ResultType> for qobject::ResultType {
+ fn from(value: &smart_calculator::types::ResultType) -> Self {
+ match value {
+ smart_calculator::types::ResultType::Math => qobject::ResultType::Math,
+ smart_calculator::types::ResultType::Unit => qobject::ResultType::Unit,
+ smart_calculator::types::ResultType::Currency => qobject::ResultType::Currency,
+ smart_calculator::types::ResultType::Crypto => qobject::ResultType::Crypto,
+ smart_calculator::types::ResultType::Time => qobject::ResultType::Time,
+ smart_calculator::types::ResultType::Date => qobject::ResultType::Date,
+ }
+ }
+}
+
+#[derive(Default)]
+pub struct SmartCalcRs {
+ result: QVariant,
+ result_type: qobject::ResultType,
+
+ cached_query: QVariant,
+}
+
+impl qobject::SmartCalc {
+ fn query(mut self: Pin<&mut Self>, input: QString) {
+ if self.cached_query == (&input).into() {
+ return;
+ }
+ 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(
+ match result {
+ Ok(value) => {
+ if value.formatted == value.input {
+ QVariant::default()
+ } else {
+ let result: QString = value
+ .formatted
+ .into();
+ QVariant::from(&result)
+ }
+ }
+ Err(_) => QVariant::default(),
+ },
+ )
+ },
+ )
+ },
+ );
+ }
+
+ fn reset(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_cached_query(QVariant::default());
+ }
+}