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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
extern crate proc_macro;
use TokenStream;
use quote;
use ;
pub use *;
pub
pub
pub
pub
/// Macro to implement the `Declare` trait and build a `FatObj<T>`.
///
/// This is an alternative to the `#[declare]` attribute macro.
/// Use this when you prefer running the macro as a derive, for example, to
/// coexist with other macros that might conflict with attribute macros.
///
/// For a detailed tutorial, see the `declare` module documentation in the core
/// crate.
///
/// [declare]: ../ribir_core/declare/index.html
/// Macro attribute to implement the `Declare` trait and build a `FatObj<T>`.
/// This is the preferred way to implement the `Declare` trait.
///
/// See the `declare` module documentation in the core crate for a full
/// tutorial.
/// A macro use to declare an object. This macro will use `ctx!()` to access
/// the `BuildCtx`, so it can only use in a scope that has a `BuildCtx` named as
/// `ctx!()`.
///
/// # The Syntax
///
/// `rdl` accept 3 kind of syntax:
///
/// - 1. use struct literal syntax to declare a object tree, like `rdl!{ Row {
/// wrap: true } }`, if the `Row` contain any child, its child can be embed in
/// the struct literal, but must be use `rdl!` or `@` to declare, like:
///
/// ```ignore
/// rdl!{ Row { wrap: true, rdl!{ Text { text: "hello" } } } }
/// ```
/// - 2. similar to the first, but use a variable as parent and not accept any
/// fields of the parent(the builtin fields allowed), like:
///
/// ```ignore
/// let row = rdl!{ Row { wrap: true } };
/// rdl!{ $row { rdl!{ Text { text: "hello" } } } }
/// ```
/// - 3. use expression to declare a object and not allow declare children,
/// like: `let row = rdl!{ Widget::new(Void) };`
/// The `fn_widget` macro generates a widget from a function widget based on an
/// expression.
///
/// Its syntax extends the Rust syntax, allowing the use of `@` and `$` within
/// the expression. The `@` serves as a shorthand for the `rdl` macro, while
/// `$name` is used to express a state reference to `name`.
/// This macro just return the input token stream. It's do nothing but help
/// `ribir` mark that a macro has been expanded.
/// This macro is utilized for generating a `Pipe` object that actively monitors
/// the expression's result.
///
/// The `$` symbol denotes the state reference and automatically subscribes to
/// any changes made to it. It triggers when the `$` state modifies.
/// A shorthand macro for `pipe!` can be utilized as follows:
/// `pipe!(...).transform(|s| s.distinct_until_changed())`.
///
/// It triggers when the new result differs from the previous one. The `$`
/// symbol denotes the state reference and automatically subscribes to any
/// changes made to it.
/// The `watch!` macro converts an expression into an `Observable` stream. Use
/// `$` to mark the state reference, which automatically maps its modifications
/// to the expression value.
///
/// ## Example
///
/// ```rust ignore
/// use ribir::prelude::*;
///
/// let label = Stateful::new(1);
/// watch!(*$label)
/// .subscribe(|v| println!("{v}") );
///
/// *label.write() = 2;
/// ```
/// After subscribing, the subscription remains active until the state is fully
/// dropped.
///
/// ## Notice
///
/// If you use the `writer` of the watched state downstream, it introduces a
/// circular reference, preventing the state from being dropped. You need to
/// manually call unsubscribe at the appropriate time, typically in the
/// `on_disposed` method of a widget.
///
/// ```rust ignore
/// use ribir::prelude::*;
///
/// let even = Stateful::new(1);
/// let u = watch!(*$even).subscribe(move |v| {
/// if v % 2 == 1 {
/// *even.write() = (v + 1);
/// }
/// });
///
/// // ...
///
/// // Call unsubscribe at the appropriate time to ensure the state can be dropped.
/// u.unsubscribe();
/// ```
/// The `part_writer` macro creates a partial writer from a mutable reference of
/// a writer.
///
/// This macro specifically accepts simple expressions to indicate the partial
/// of the writer, as shown in the following patterns:
///
/// - For a field: `part_writer!(&mut writer.xxx)`
/// - For a method returning a mutable reference: `part_writer!(writer.xxx())`.
///
/// Since it operates on a writer and not a state reference of the writer, the
/// use of `$` is unnecessary.
/// The `part_watcher` macro creates a partial watcher from a reference of a
/// watcher.
///
/// This macro specifically accepts simple expressions to indicate the partial
/// of the watcher, as shown in the following patterns:
///
/// - For a field: `part_watcher!(&watcher.xxx)`
/// - For a method returning a reference: `part_watcher!(watcher.xxx())`.
///
/// Since it operates on a watcher and not a state reference of the watcher, the
/// use of `$` is unnecessary.
/// The `part_reader` macro creates a partial reader from a reference of a
/// reader.
///
/// This macro specifically accepts simple expressions to indicate the partial
/// of the reader, as shown in the following patterns:
///
/// - For a field: `part_reader!(&reader.xxx)`
/// - For a method returning a reference: `part_reader!(reader.xxx())`.
///
/// Since it operates on a reader and not a state reference of the reader, the
/// use of `$` is unnecessary.
/// Loads an asset file at runtime after processing it during compilation.
///
/// This macro manages application assets by:
/// 1. Processing the asset at compile time (format conversion, compression,
/// etc.)
/// 2. Copying the processed file to `target/{profile}/assets/`
/// 3. Generating code to load it from the filesystem at runtime
/// 4. Automatically triggering rebuilds when the source file changes
///
/// # Supported Types
///
/// | Type | Syntax | Return | Processing |
/// |------|--------|--------|------------|
/// | Binary | `asset!("file.bin")` | `Vec<u8>` | None (raw copy) |
/// | Text | `asset!("file.txt", "text")` | `String` | None (raw copy) |
/// | SVG | `asset!("file.svg", "svg")` | `Svg` | Serialized/compressed |
/// | Image | `asset!("file.png", "image")` | `Image` | Converted to WebP |
///
/// # Syntax
///
/// ```ignore
/// // Binary (default)
/// asset!("path/to/file.bin")
///
/// // Text
/// asset!("path/to/file.txt", "text")
///
/// // SVG with optional parameters
/// asset!("path/to/icon.svg", "svg")
/// asset!("path/to/icon.svg", "svg", inherit_fill = true)
/// asset!("path/to/icon.svg", "svg", inherit_fill = true, inherit_stroke = true)
///
/// // Image (PNG, JPEG, GIF, BMP → WebP)
/// asset!("path/to/photo.png", "image")
/// asset!("path/to/animation.gif", "image") // Animated GIF → Animated WebP
/// ```
///
/// # Path Resolution
///
/// Paths are resolved **relative to the source file** where the macro is
/// called, similar to `include_str!` in Rust.
///
/// ```ignore
/// // In src/ui/widgets/button.rs
/// let icon: Svg = asset!("../icons/button.svg", "svg");
/// // Resolves to: src/ui/icons/button.svg
/// ```
///
/// # Type Parameters
///
/// ## SVG Parameters
/// - `inherit_fill`: Inherit fill style from parent widget (default: false)
/// - `inherit_stroke`: Inherit stroke style from parent widget (default: false)
///
/// # asset! vs include_asset!
///
/// | Feature | `asset!` | `include_asset!` |
/// |---------|----------|------------------|
/// | Loading | Runtime (filesystem) | Compile-time (embedded) |
/// | Distribution | Bundle `assets/` folder | Single binary |
/// | Binary Size | Smaller | Larger |
/// | Hot Reload | Possible | Requires recompile |
///
/// # Examples
///
/// ```ignore
/// // Binary assets
/// let font: Vec<u8> = asset!("fonts/roboto.ttf");
/// let data: Vec<u8> = asset!("data/model.bin");
///
/// // Text assets
/// let config: String = asset!("config/settings.json", "text");
/// let shader: String = asset!("shaders/fragment.glsl", "text");
///
/// // SVG assets (compressed at compile-time)
/// let icon: Svg = asset!("icons/menu.svg", "svg");
/// let themed: Svg = asset!("icons/logo.svg", "svg", inherit_fill = true);
///
/// // Image assets (converted to WebP)
/// let photo: Image = asset!("images/hero.jpg", "image");
/// let anim: Image = asset!("images/loading.gif", "image"); // Animated!
/// ```
///
/// # Bundling for Distribution
///
/// In normal builds, `asset!` expands to an absolute path under
/// `target/{profile}/assets/`.
///
/// When `ribir-cli bundle` drives the build, it sets `RIBIR_BUNDLE_MODE` to the
/// resolved base asset profile (`debug` or `release`). In that mode, `asset!`
/// still writes processed files to `target/{debug|release}/assets/`, but the
/// generated runtime code switches to bundle-relative lookups.
///
/// Include the `assets/` folder inside your application bundle:
/// - **macOS**: `YourApp.app/Contents/Resources/assets/`
/// - **Windows/Linux**: Same directory as executable
///
/// # Panics
///
/// Runtime panic if:
/// - Asset file not found at expected location
/// - File read error (permissions, I/O)
/// - UTF-8 decode error (text mode only)
///
/// # Compile Errors
///
/// Compile-time error if:
/// - Source file does not exist
/// - Path is a directory
/// - Cannot create output directory
/// - Cannot process/copy file
/// Embeds an asset directly into the executable binary.
///
/// Similar to `asset!`, but embeds the processed file content directly into
/// the binary instead of loading from filesystem at runtime. This results in
/// a larger binary but simpler distribution (single file, no external assets).
///
/// # When to Use
///
/// - Single-binary distribution is preferred
/// - Assets are small and few
/// - No need for hot-reloading
///
/// # Syntax
///
/// Same as `asset!`:
///
/// ```ignore
/// include_asset!("file.bin") // → Vec<u8>
/// include_asset!("file.txt", "text") // → String
/// include_asset!("icon.svg", "svg") // → Svg
/// include_asset!("photo.png", "image") // → Image (WebP)
/// include_asset!("anim.gif", "image") // → Image (animated WebP)
/// ```
///
/// # Returns
///
/// Same types as `asset!`:
/// - `Vec<u8>` for binary
/// - `String` for text
/// - `Svg` for SVG
/// - `Image` for image