From c119b2cf8eb2fe2cb2b204e870deb7c6dfb189ee Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sun, 26 Jul 2026 18:16:25 +0800 Subject: init: qml_plugin new(plugin): date --- qml_plugin/src/launcher.rs | 1 + qml_plugin/src/launcher/calc.rs | 122 ++++++++++++++++++++++++++++++++++++++++ qml_plugin/src/lib.rs | 12 ++++ 3 files changed, 135 insertions(+) create mode 100644 qml_plugin/src/launcher.rs create mode 100644 qml_plugin/src/launcher/calc.rs create mode 100644 qml_plugin/src/lib.rs (limited to 'qml_plugin/src') diff --git a/qml_plugin/src/launcher.rs b/qml_plugin/src/launcher.rs new file mode 100644 index 0000000..5632eea --- /dev/null +++ b/qml_plugin/src/launcher.rs @@ -0,0 +1 @@ +pub mod calc; diff --git a/qml_plugin/src/launcher/calc.rs b/qml_plugin/src/launcher/calc.rs new file mode 100644 index 0000000..196484e --- /dev/null +++ b/qml_plugin/src/launcher/calc.rs @@ -0,0 +1,122 @@ +use cxx_qt::{QObject, Threading}; +use cxx_qt_lib::{QDateTime, QString, QVariant}; +use std::{pin::Pin, sync::mpsc, time::Duration}; + +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 + )] + 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, +} + +impl qobject::SmartCalc { + fn query(self: Pin<&mut Self>, input: QString) { + 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); + } +} diff --git a/qml_plugin/src/lib.rs b/qml_plugin/src/lib.rs new file mode 100644 index 0000000..12d0684 --- /dev/null +++ b/qml_plugin/src/lib.rs @@ -0,0 +1,12 @@ +use std::sync::LazyLock; + +static RUNTIME: LazyLock = LazyLock::new( + || { + tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap() + }, +); + +mod launcher; -- cgit v1.2.3