qtbridge_interfaces/qparser_status/proxy_rust.rs
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only
3
4use qtbridge_runtime::{DispatchMetaCall, QObjectHolder};
5use crate::call_rust_trait_impl;
6use crate::genericrustproxy::GenericRustProxy;
7use super::proxy_cpp_bridge::QParserStatusProxyCpp;
8
9/// A trait for hooking into QML component construction stages.
10///
11/// [`QParserStatus::class_begin`] is called first when the
12/// object is created, followed by [`QParserStatus::component_complete`] once
13/// all QML properties have been set. This allows you to defer expensive
14/// initialization until the component is fully constructed.
15///
16/// This trait is the Rust equivalent of
17/// [`QQmlParserStatus`](https://doc.qt.io/qt-6/qqmlparserstatus.html).
18///
19/// This trait requires the `qobject` macro with `Base = QParserStatus` to set
20/// up the correct Qt proxy.
21///
22/// ## Example
23///
24/// ```rust
25/// use qtbridge::{QApp, qobject};
26///
27/// #[qobject(Base = QParserStatus)]
28/// pub mod backend {
29/// use qtbridge::QParserStatus;
30///
31/// #[derive(Default)]
32/// pub struct Status {
33/// somewhat_ready: bool,
34/// }
35///
36/// impl Status {
37/// #[qsignal]
38/// fn ready(&mut self);
39/// }
40///
41/// impl QParserStatus for Status {
42/// fn class_begin(&mut self) {
43/// self.somewhat_ready = true;
44/// }
45///
46/// fn component_complete(&mut self) {
47/// if self.somewhat_ready {
48/// self.ready();
49/// }
50/// }
51/// }
52/// }
53///
54/// const QML_CODE: &str =
55/// r#"
56/// import QtQuick
57/// import QtQuick.Controls
58///# import qtbridge_interfaces
59/// // import your module
60///
61/// ApplicationWindow {
62/// visible: true
63/// Status {
64/// onReady: closeTimer.start()
65/// }
66/// Timer {
67/// id: closeTimer
68/// interval: 1
69/// onTriggered: Qt.quit()
70/// }
71/// }
72/// "#;
73///
74/// fn main() {
75/// QApp::new()
76/// .register::<backend::Status>()
77/// .load_qml(QML_CODE.as_bytes())
78/// .run();
79/// }
80/// ```
81pub trait QParserStatus {
82 /// Called when the object is first created, before any properties are set.
83 ///
84 /// Use this to perform any initialization that must happen before
85 /// properties are applied.
86 fn class_begin(&mut self) {}
87
88 /// Called after all QML properties have been set.
89 ///
90 /// Use this to perform initialization that depends on property values,
91 /// or to emit signals that notify QML the object is ready.
92 fn component_complete(&mut self) {}
93}
94
95#[doc(hidden)]
96pub trait QParserStatusAdapter: DispatchMetaCall {
97 fn class_begin(&mut self);
98 fn component_complete(&mut self);
99}
100
101impl<T> QParserStatusAdapter for T
102where T: QParserStatus + QObjectHolder<ProxyRust = QParserStatusProxyRust> {
103 fn class_begin(&mut self) {
104 <Self as QParserStatus>::class_begin(self)
105 }
106
107 fn component_complete(&mut self) {
108 <Self as QParserStatus>::component_complete(self)
109 }
110}
111
112pub type QParserStatusProxyRust = GenericRustProxy<QParserStatusProxyCpp, dyn QParserStatusAdapter>;
113
114impl QParserStatusProxyRust {
115 pub fn class_begin(&mut self) {
116 call_rust_trait_impl!(mut self, class_begin())
117 }
118
119 pub fn component_complete(&mut self) {
120 call_rust_trait_impl!(mut self, component_complete())
121 }
122}