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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
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>`.
/// To know how to use it see the [`declare` mod document](declare)
///
/// This macro implement a `XXXBuilder` struct with same field type for `XXX`
/// widget, then
///
/// - implement `Declare` for `XXX` mark `XXXBuilder` as its builder type.
/// - implement `ObjDeclarer` for `XXXBuilder` which build `XXX` and used by
/// `declare!` to build the `XXX` widget.
/// - for every field of `XXXBuilder`
/// - implement method with same name of the field and use to init the field.
///
/// [declare]: ../ribir/declare/index.html
/// Macro attribute implement the `Declare` trait with that only build a
/// `State<T>` that not extend any built-in ability, and not support `pipe!` to
/// init the field.
/// 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!(...).value_chain(|s| s.distinct_until_changed().box_it())`.
///
/// 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.
/// Includes an asset file by copying it to the application's asset directory
/// during build time and generating code to load it at runtime.
///
/// This macro manages application assets by:
/// 1. Copying the specified file to `target/{profile}/assets/` during
/// compilation
/// 2. Generating code that reads the asset from the filesystem at runtime
/// (relative to the executable)
/// 3. Automatically triggering rebuilds when the asset file changes
///
/// The asset will be placed in an `assets` folder next to your executable,
/// making it easy for bundle tools to include it in your application package.
///
/// # Asset vs Include Asset
///
/// `asset!` copies files and loads them at runtime, while `include_asset!`
/// embeds them into the binary.
///
/// | Feature | `asset!` | `include_asset!` |
/// |---------|----------|------------------|
/// | **Loading Strategy** | Runtime loading from filesystem | Compile-time embedding |
/// | **Distribution** | Must bundle `assets` folder | Single binary (easier distribution) |
/// | **Binary Size** | Smaller binary | Larger binary (contains all assets) |
/// | **Performance** | I/O overhead at runtime | Instant access (memory mapped) |
/// | **Hot Reloading** | Possible (if file changes on disk) | Requires recompilation |
///
/// # Path Resolution
///
/// Asset paths are resolved **relative to the source file** where the macro is
/// called, similar to how `#include` works in C/C++ or `include_str!` works in
/// Rust.
///
/// **Requirements:** Rust 1.88 or later
///
/// ```ignore
/// // In src/ui/widgets/button.rs
/// let icon: Svg = asset!("../icons/button.svg", "svg");
/// // Resolves to: src/ui/icons/button.svg (relative to button.rs)
/// ```
///
/// # Syntax
///
/// ```ignore
/// asset!("path/to/file.ext") // Load as binary (returns Vec<u8>)
/// asset!("path/to/file.txt", "text") // Load as text (returns String)
/// asset!("path/to/file.txt", "TEXT") // Case-insensitive type matching
/// asset!("path/to/file.svg", "svg") // Load as SVG with compression (returns Svg)
/// asset!("path/to/icon.svg", "SVG", inherit_fill = true) // SVG with parameters (key=value style)
/// asset!("path/to/icon.svg", "svg", inherit_fill = true, inherit_stroke = false) // Multiple parameters
/// ```
///
/// # Arguments
///
/// * `path` - Relative path to the asset file. On nightly with
/// `procmacro2_semver_exempt`, this is relative to the calling source file.
/// On stable, this is relative to the project root (where `Cargo.toml` is
/// located).
/// * `type` - Optional. Use `"text"` (or `"TEXT"`) to load as a `String`,
/// `"svg"` (or `"SVG"`) to load as a compressed `Svg`, otherwise loads as
/// `Vec<u8>`. Type matching is case-insensitive.
///
/// ## Type-Specific Parameters (key=value format)
///
/// ### SVG Parameters
/// * `inherit_fill` - Boolean to inherit fill style from parent (default:
/// false)
/// * `inherit_stroke` - Boolean to inherit stroke style from parent (default:
/// false)
///
/// # Output
///
/// The asset is copied to `target/{profile}/assets/{filename}` where:
/// - `{profile}` is either `debug` or `release` depending on build
/// configuration
/// - `{filename}` is the name of the input file
///
/// For SVG files (when using `"svg"` type), the file is compressed at compile
/// time before being copied, resulting in smaller asset files.
///
/// # Returns
///
/// - `Vec<u8>` - When loading binary files (default)
/// - `String` - When `"text"` parameter is specified
/// - `Svg` - When `"svg"` parameter is specified (compressed at compile time)
///
/// # Examples
///
/// ```ignore
/// // Load an image as binary data
/// let icon_data: Vec<u8> = asset!("resources/icon.png");
///
/// // Load a configuration file as text
/// let config: String = asset!("config/settings.json", "text");
///
/// // Load a shader file as text
/// let shader_source: String = asset!("shaders/fragment.glsl", "text");
///
/// // Load an SVG file with compile-time compression
/// let icon: Svg = asset!("assets/icon.svg", "svg");
///
/// // Load an SVG file with parameters (key=value style)
/// let styled_icon: Svg = asset!("assets/icon.svg", "svg", inherit_fill = true);
/// let styled_icon2: Svg = asset!("assets/icon.svg", "SVG", inherit_fill = true, inherit_stroke = false);
///
/// // Type names are case-insensitive
/// let config: String = asset!("config.json", "TEXT");
///
/// // Load multiple assets
/// let logo_svg: Svg = asset!("assets/logo.svg", "svg");
/// let font: Vec<u8> = asset!("fonts/roboto.ttf");
/// ```
///
/// # Bundling for Distribution
///
/// When packaging your application for distribution, ensure the `assets` folder
/// is included alongside your executable:
///
/// - **macOS**: Copy `assets/` to `YourApp.app/Contents/MacOS/assets/`
/// - **Windows**: Copy `assets/` next to your `.exe` file
/// - **Linux**: Copy `assets/` next to your binary
///
/// # Panics
///
/// The generated code will panic at runtime if:
/// - The asset file cannot be found at the expected location
/// - The file cannot be read (permissions, I/O errors, etc.)
/// - The file cannot be decoded as UTF-8 (when using `"text"` mode)
///
/// # Compile Errors
///
/// The macro will fail at compile time if:
/// - The specified asset file does not exist
/// - The path points to a directory instead of a file
/// - The output directory cannot be created
/// - The file cannot be copied
/// Embeds an asset file directly into the executable binary during build time.
///
/// This macro is similar to `asset!`, but instead of copying the file to the
/// assets directory and loading it at runtime, it embeds the file content
/// directly into the executable.
///
/// # Asset vs Include Asset
///
/// `asset!` copies files and loads them at runtime, while `include_asset!`
/// embeds them into the binary.
///
/// | Feature | `asset!` | `include_asset!` |
/// |---------|----------|------------------|
/// | **Loading Strategy** | Runtime loading from filesystem | Compile-time embedding |
/// | **Distribution** | Must bundle `assets` folder | Single binary (easier distribution) |
/// | **Binary Size** | Smaller binary | Larger binary (contains all assets) |
/// | **Performance** | I/O overhead at runtime | Instant access (memory mapped) |
/// | **Hot Reloading** | Possible (if file changes on disk) | Requires recompilation |
///
/// # Syntax
///
/// Same as `asset!`:
///
/// ```ignore
/// include_asset!("path/to/file.ext")
/// include_asset!("path/to/file.txt", "text")
/// include_asset!("path/to/file.svg", "svg")
/// ```
///
/// # Returns
///
/// Same types as `asset!`:
/// - `Vec<u8>` for binary
/// - `String` for text
/// - `Svg` for SVG