diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2026-09-18 15:13:01 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2026-10-01 15:10:27 +0800 |
| commit | 3cea94b72dbfdd2077a59fc52a7460e200b6061e (patch) | |
| tree | 294a8cb1ef3aadc189073492bb041f6af2b17f70 /qml_plugin/src/qml_interface/network.rs | |
| parent | 8a71bd72f53e3629fb1d23d7c1b153207ba44789 (diff) | |
refactor: qml_pluginmain
Diffstat (limited to 'qml_plugin/src/qml_interface/network.rs')
| -rw-r--r-- | qml_plugin/src/qml_interface/network.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/qml_plugin/src/qml_interface/network.rs b/qml_plugin/src/qml_interface/network.rs new file mode 100644 index 0000000..7ece121 --- /dev/null +++ b/qml_plugin/src/qml_interface/network.rs @@ -0,0 +1,55 @@ +use cxx_qt::{CxxQtType, Threading}; +use cxx_qt_lib::{QString, QVariant}; +use std::pin::Pin; +use unwrap_print::PrintableResult; +use zbus::zvariant::NoneValue; + +use crate::{ExclusiveExecutor, services::network}; + +#[cxx_qt::bridge] +mod qobject { + extern "C++" { + include!("cxx-qt-lib/qvariant.h"); + type QVariant = cxx_qt_lib::QVariant; + } + + extern "RustQt" { + #[qobject] + #[qml_element] + #[qml_singleton] + #[qproperty( + QVariant, result + )] + type Network = super::NetworkRs; + + #[qinvokable] + fn query_primary_device(self: Pin<&mut Network>); + } + + impl cxx_qt::Threading for Network {} +} + +#[derive(Default)] +pub struct NetworkRs { + result: QVariant, + + executor: ExclusiveExecutor, +} + +impl qobject::Network { + fn query_primary_device(self: Pin<&mut Self>) { + let qt_thread = self.qt_thread(); + self.rust_mut().executor.spawn( + async move { + let result = match network::get().await { + Ok(o) => { + let r: QString = o.into(); + QVariant::from(&r) + } + Err(_) => QVariant::null_value(), + }; + _ = qt_thread.queue(|mut qo| qo.as_mut().set_result(result)).unwrap_print(); + }, + ); + } +} |
