1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//! # The `Declare` Macro System
//!
//! The `#[declare]` macro is the backbone of Ribir's widget system. It
//! generates the builder pattern implementation required to construct widgets
//! in a declarative style.
//!
//! This module contains the helper types used by the generated code.
//!
//! ## Core Concepts
//!
//! When you annotate a struct with `#[derive(Declare)]` or `#[declare]`, the
//! macro generates:
//! 1. A **Builder Struct** (e.g., `ButtonDeclarer` for `Button`).
//! 2. Implementations of [`Declare`] and [`ObjDeclarer`].
//! 3. **Setter Methods** for each field, enabling the chaining syntax.
//! 4. Integration with `FatObj` for built-in widget features, unless disabled.
//!
//! ## `#[declare]` vs `#[derive(Declare)]`
//!
//! There are two ways to use the macro:
//!
//! 1. **`#[declare]` (Attribute Macro)**: This is the modern and preferred
//! style. It supports all features, including `simple` and `stateless`
//! modes.
//! 2. **`#[derive(Declare)]` (Derive Macro)**: Use this only when you need
//! compatibility with other macros that might conflict with attribute
//! macros, or when you specifically want to use the derive syntax. Field
//! attributes like `#[declare(...)]` work the same way in both cases.
//!
//! ## Field Attributes
//!
//! The macro supports several field-level attributes to customize the builder
//! behavior:
//!
//! ### `#[declare(default = ...)]`
//! Specifies a custom default value expression for the field.
//!
//! ### `#[declare(skip)]`
//! Skips generating a setter for this field. The field relies on `Default` or
//! `#[declare(default = ...)]`.
//!
//! ### `#[declare(strict)]`
//! By default, setters are generic and accept `impl Into<FieldType>` or `Pipe`.
//! Adding `strict` restricts the setter to accept only the exact field type.
//!
//! ### `#[declare(event = ...)]`
//! Enables two-way data binding. This tells the builder how to sync values back
//! from events.
//! - `event = EventType`: Uses `Into` to convert event data to the field type.
//! - `event = EventType.field`: Binds to a specific field.
//! - `event = EventType.method()`: Binds via a method call.
//!
//! ### `#[declare(setter = ...)]`
//! Specifies the method to use for setting the field value on the widget,
//! instead of direct field assignment. This is useful when the field is private
//! or requires special logic during setting.
//! - `setter = method_name`: Calls `widget.method_name(value)`.
//! - `setter = method_name(Type)`: The `method_name` accepts a value of `Type`
//! and converts it to the field's type.
//!
//! ### `#[declare(custom)]`
//! Tags a field to indicate you will implement a custom setter manually.
//!
//! ## Struct Attributes
//!
//! ### `#[declare(validate)]` or `#[declare(validate = method_name)]`
//!
//! When present, the builder calls the specified validation method before
//! building.
//! - `#[declare(validate)]`: Calls `self.declare_validate()`. you must
//! implement this method.
//! - `#[declare(validate = method_name)]`: Calls `self.method_name()`.
//!
//! ### `#[declare(simple)]`
//! Generates a lightweight builder without the `FatObj` wrapper.
//!
//! - **Use case**: For helper widgets, internal structures, or when you don't
//! need universal widget attributes (like `margin`, `on_click`, etc.).
//! - **Outcome**: The builder returns `Stateful<T>` by default, or `T` if
//! combined with `stateless` or if the struct is empty.
//! - **Fields**: Setters accept static values (no pipes).
//!
//! ### `#[declare(stateless)]`
//! Optimizes the widget by removing the internal `Stateful` wrapper, treating
//! the widget as immutable after creation.
//!
//! - **Use case**: For widgets that perform a one-time build and don't need to
//! update their internal fields later (e.g., purely specific layout
//! structures).
//! - **Outcome**: Returns `FatObj<T>` (supporting standard attributes) instead
//! of `FatObj<Stateful<T>>`.
//! - **Fields**: Setters accept static values (no pipes).
//!
//! ## Example
//!
//! ```ignore
//! #[declare]
//! pub struct MyWidget {
//! #[declare(default = 1.0)]
//! opacity: f32,
//! #[declare(event = ValueChanged)]
//! value: TwoWay<f32>,
//! }
//! ```
use Infallible;
use LocalBoxedObservable;
use crate*;
/// Trait used to create a widget declarer that can interact with the `BuildCtx`
/// to create a widget.
///
/// This is the entry point for the builder pattern generated by `#[declare]`.
/// An object declarer is a type that can be used to create a object with the
/// given context.
/// A enum that represents a value that can be either a constant value or a
/// pipe.
///
/// This is the underlying storage for most fields in a `#[declare]` builder.
/// It allows a field to be initialized with receiving a static value,
/// or a dynamic stream (Pipe) that updates the field automatically.
pub type ValueStream<V> = ;
/// Helper type for implementing `RInto` traits to support flexible builder
/// arguments.
///
/// This allows setters to accept both `V` and `Pipe<V>` by utilizing the logic
/// in `RFrom`.
>);
/// Two-way binding wrapper for automatic read/write synchronization.
///
/// When passed to a field marked with `#[declare(event = ...)]` in the
/// widget macro, the builder auto-generates logic to synchronize changes.
///
/// The `event` attribute supports several syntaxes to define how values flow
/// back:
/// - `event = EventType`: Uses `Into` to convert event data to the field type.
/// - `event = EventType.field`: Extracts a field from the event data.
/// - `event = EventType.method()`: Calls a method on the event data.
///
/// # Example
/// ```ignore
/// #[declare(event = SliderChanged)]
/// pub struct Slider {
/// #[declare(convert = output.value)]
/// pub value: f32,
/// }
///
/// // Usage in `fn_widget!`:
/// @Slider {
/// // Binds `state` to `value`.
/// // When `state` changes, `value` updates.
/// // When `Slider` emits `SliderChanged`, `state` is updated with the new value.
/// value: TwoWay::new(state)
/// }
/// ```
;
/// Value type for fields with two-way binding support.
///
/// Used for fields with `#[declare(event = ...)]` attribute.
/// It can hold either a standard `PipeValue` (one-way or static)
/// or a `TwoWay` writer that enables automatic write-back on events.
/// Kind marker for TwoWayValue value conversion.
>);
/// Kind marker for TwoWay conversion from `TwoWay<W>` to `TwoWayValue<V>`.
;