summaryrefslogtreecommitdiff
path: root/qml_plugin/src/launcher
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2026-07-27 14:46:32 +0800
committerlittleblack111 <littleblack11111@gmail.com>2026-07-27 14:57:41 +0800
commit90df3a1a8738d61f747c50f26359ccabf0c7b229 (patch)
tree8df5a8ce0bef4f7cb831ba517ac54969c46fbe19 /qml_plugin/src/launcher
parent1f9873b40fee2d33c5b7103a25077ec61a387063 (diff)
new: more advanced math via kalk
refactor(components): optimizations and simplify
Diffstat (limited to 'qml_plugin/src/launcher')
-rw-r--r--qml_plugin/src/launcher/mathcalc.rs111
1 files changed, 111 insertions, 0 deletions
diff --git a/qml_plugin/src/launcher/mathcalc.rs b/qml_plugin/src/launcher/mathcalc.rs
new file mode 100644
index 0000000..fd39ed1
--- /dev/null
+++ b/qml_plugin/src/launcher/mathcalc.rs
@@ -0,0 +1,111 @@
+use cxx_qt::{CxxQtType, Threading};
+use cxx_qt_lib::{QString, QVariant};
+use kalk::parser;
+use std::{pin::Pin, sync::Arc};
+use tokio::sync::Mutex;
+
+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
+ )]
+ type MathCalc = super::MathCalcRs;
+
+ #[qinvokable]
+ fn query(self: Pin<&mut MathCalc>, input: QString);
+
+ #[qinvokable]
+ fn reset(self: Pin<&mut MathCalc>);
+
+ #[qinvokable]
+ fn reset_ctx(self: Pin<&mut MathCalc>);
+ }
+
+ impl cxx_qt::Threading for MathCalc {}
+}
+
+#[derive(Default)]
+pub struct MathCalcRs {
+ result: QVariant,
+
+ ctx: Arc<Mutex<parser::Context>>,
+}
+
+impl qobject::MathCalc {
+ fn query(self: Pin<&mut Self>, input: QString) {
+ let qt_thread = self.qt_thread();
+ let ctx = Arc::clone(
+ &self
+ .rust_mut()
+ .ctx,
+ );
+ // TODO: consider moving to spawn_blocking
+ RUNTIME.spawn(
+ async move {
+ let result = parser::eval(
+ &mut *ctx
+ .lock()
+ .await,
+ &input.to_string(),
+ // 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_big()
+ .into();
+ QVariant::from(&result)
+ },
+ )
+ },
+ ), /* match result {
+ * Ok(value) => {
+ * let result: QString = value
+ * .to_string()
+ * .into();
+ * QVariant::from(&result)
+ * }
+ * // TODO: add some logging
+ * Err(_) => QVariant::default(),
+ * }, */
+ )
+ },
+ )
+ },
+ );
+ }
+
+ fn reset(mut self: Pin<&mut Self>) {
+ self.as_mut()
+ .set_result(QVariant::default());
+ self.as_mut()
+ .rust_mut()
+ .ctx = Arc::new(Mutex::new(parser::Context::default()));
+ }
+
+ fn reset_ctx(mut self: Pin<&mut Self>) {
+ self.as_mut()
+ .rust_mut()
+ .ctx = Arc::new(Mutex::new(parser::Context::default()));
+ }
+}