From 90df3a1a8738d61f747c50f26359ccabf0c7b229 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Mon, 27 Jul 2026 14:46:32 +0800 Subject: new: more advanced math via kalk refactor(components): optimizations and simplify --- qml_plugin/Cargo.lock | 45 ++++++++++++++- qml_plugin/Cargo.toml | 1 + qml_plugin/build.rs | 7 ++- qml_plugin/src/launcher.rs | 1 + qml_plugin/src/launcher/mathcalc.rs | 111 ++++++++++++++++++++++++++++++++++++ 5 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 qml_plugin/src/launcher/mathcalc.rs (limited to 'qml_plugin') diff --git a/qml_plugin/Cargo.lock b/qml_plugin/Cargo.lock index b740b04..ee9fb39 100644 --- a/qml_plugin/Cargo.lock +++ b/qml_plugin/Cargo.lock @@ -78,6 +78,12 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "az" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be5eb007b7cacc6c660343e96f650fedf4b5a77512399eb952ca6642cf8d13f7" + [[package]] name = "base64" version = "0.22.1" @@ -462,9 +468,9 @@ checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" [[package]] name = "either" -version = "1.16.0" +version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91622ff5e7162018101f2fea40d6ebf4a78bbe5a49736a2020649edf9693679e" +checksum = "9e5e8f6c15a24b9a3ee5efec809ccd006d3b30e8b3bb63c39af737c7f87daa1d" [[package]] name = "encoding_rs" @@ -592,6 +598,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "gmp-mpfr-sys" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7db155b537cb791b133341f99f68371d86ee7fa4c79aacfbc376d72d23c70531" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + [[package]] name = "h2" version = "0.4.15" @@ -1288,6 +1304,18 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "kalk" +version = "3.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6be3b5fe1fc1b007b90dbb151aa48d480efece8f813f975d53597aaa4df67003" +dependencies = [ + "gmp-mpfr-sys", + "lazy_static", + "rug", + "wasm-bindgen", +] + [[package]] name = "lazy_static" version = "1.5.0" @@ -1481,6 +1509,7 @@ dependencies = [ "cxx-qt", "cxx-qt-build", "cxx-qt-lib", + "kalk", "smart-calculator", "tokio", ] @@ -1680,6 +1709,18 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rug" +version = "1.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07a8857882aec59d27254b02481c709327c13de6fad1da60bfc4f9783eaaa61e" +dependencies = [ + "az", + "gmp-mpfr-sys", + "libc", + "libm", +] + [[package]] name = "rustc-hash" version = "2.1.3" diff --git a/qml_plugin/Cargo.toml b/qml_plugin/Cargo.toml index 9f9fa88..5ae6a1c 100644 --- a/qml_plugin/Cargo.toml +++ b/qml_plugin/Cargo.toml @@ -10,6 +10,7 @@ crate-type = ["cdylib"] cxx = "1.0.198" cxx-qt = "0.9.1" cxx-qt-lib = "0.9.1" +kalk = "3.2.3" smart-calculator = "1.0.3" tokio = { version = "1.53.1", features = ["rt-multi-thread", "macros"] } diff --git a/qml_plugin/build.rs b/qml_plugin/build.rs index 8a02b92..2c73418 100644 --- a/qml_plugin/build.rs +++ b/qml_plugin/build.rs @@ -13,6 +13,11 @@ fn main() { CxxQtBuilder::new_qml_module(QmlModule::new(NAME).plugin_type(PluginType::Dynamic)) .qt_module("Qml") - .files(["src/launcher/smartcalc.rs"]) + .files( + [ + "src/launcher/smartcalc.rs", + "src/launcher/mathcalc.rs", + ], + ) .build(); } diff --git a/qml_plugin/src/launcher.rs b/qml_plugin/src/launcher.rs index 8cb8676..4274840 100644 --- a/qml_plugin/src/launcher.rs +++ b/qml_plugin/src/launcher.rs @@ -1 +1,2 @@ +pub mod mathcalc; pub mod smartcalc; 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>, +} + +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())); + } +} -- cgit v1.2.3