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
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
//! Create macros for `tauri::Context`, invoke handler and commands leveraging the `tauri-codegen` crate.
//!
//! Don't depend on this crate directly, use the re-exported types from tauri instead.
use PathBuf;
use crateContextItems;
use TokenStream;
use ;
use ;
use CachedIcon;
/// Mark a function as a command handler. It creates a wrapper function with the necessary glue code.
///
/// The wrapped function can then be passed to [`generate_handler!`] so it can be called
/// from the frontend with `invoke()`.
///
/// ```rust,ignore
/// #[tauri::command]
/// fn greet(name: String) -> String {
/// format!("Hello, {name}!")
/// }
/// ```
///
/// # Options
///
/// The attribute accepts a comma separated list of the following options:
///
/// ## `async`
///
/// Runs the command on the async runtime instead of blocking the main thread.
///
/// `async fn` commands are always executed asynchronously, so this option is only needed
/// for synchronous functions that should not run on the main thread:
///
/// ```rust,ignore
/// #[tauri::command(async)]
/// fn expensive_computation() -> u64 {
/// // the body runs on the async runtime, so the main thread is not blocked
/// 42
/// }
/// ```
///
/// ## `rename_all`
///
/// Sets the case convention used to match the command arguments with the keys of the
/// payload sent by the frontend. Either `"camelCase"` (default) or `"snake_case"`.
///
/// ```rust,ignore
/// // called from JavaScript with `invoke("send_message", { messageBody: "Hello" })`
/// #[tauri::command]
/// fn send_message(message_body: String) {}
///
/// // called from JavaScript with `invoke("send_message", { message_body: "Hello" })`
/// #[tauri::command(rename_all = "snake_case")]
/// fn send_message_snake(message_body: String) {}
/// ```
///
/// ## `rename`
///
/// Changes the name used to call the command from the frontend.
/// By default it is the name of the function.
///
/// ```rust,ignore
/// // called from JavaScript with `invoke("greetUser")`
/// // and still registered as `generate_handler![greet_user]`
/// #[tauri::command(rename = "greetUser")]
/// fn greet_user() {}
/// ```
///
/// ## `root`
///
/// Path to the `tauri` crate, used when it is renamed in `Cargo.toml` or re-exported
/// by another crate. Defaults to `::tauri`, and the special value `"crate"` resolves
/// to `$crate` (used internally by Tauri itself).
///
/// ```rust,ignore
/// // Cargo.toml: tauri_framework = { package = "tauri", version = "2" }
/// #[tauri::command(root = "tauri_framework")]
/// fn my_command() {}
/// ```
///
/// # Inline plugins
///
/// When the command belongs to a plugin that is part of your application instead of a
/// standalone crate, annotate it in the [`generate_handler!`] list with the
/// `#![plugin(your_plugin_name)]` inner attribute so `build > removeUnusedCommands` can
/// match it against the plugin permissions.
///
/// # Stability
/// The output of this macro is managed internally by Tauri,
/// and should not be accessed directly on normal applications.
/// It may have breaking changes in the future.
/// Marks a function as the entry point of a mobile application.
///
/// It must be applied to the function that builds and runs your Tauri application on the
/// library target (`run()` on `src-tauri/src/lib.rs` for apps created by the Tauri CLI),
/// which is the function the generated Android and iOS projects call on startup.
///
/// The canonical usage only applies it on mobile targets, so the same function can be called
/// by the `main.rs` of the desktop binary:
///
/// ```rust,ignore
/// #[cfg_attr(mobile, tauri::mobile_entry_point)]
/// pub fn run() {
/// tauri::Builder::default()
/// .run(tauri::generate_context!())
/// .expect("error while running tauri application");
/// }
/// ```
///
/// The macro generates a `start_app` C symbol (checked by the Tauri CLI) that catches
/// panics instead of unwinding across the FFI boundary, blocks on the function when it is
/// `async`, sets up the stdout logger on iOS and the JNI bindings on Android using the
/// package name derived from the `identifier` in your Tauri configuration.
///
/// Because the Android package name is read from environment variables set by `tauri-build`,
/// your application must have a build script calling [`tauri_build::build`] - otherwise the
/// macro fails to compile with a `env var not set` error.
///
/// [`tauri_build::build`]: https://docs.rs/tauri-build/latest/tauri_build/fn.build.html
/// Accepts a list of command functions. Creates a handler that allows commands to be called from JS with invoke().
///
/// You can optionally annotate the commands with a inner attribute tag `#![plugin(your_plugin_name)]`
/// for `build > removeUnusedCommands` to work for plugins not defined in a standalone crate like `tauri-plugin-fs`
///
/// # Examples
///
/// ```rust,ignore
/// use tauri_macros::{command, generate_handler};
/// #[command]
/// fn command_one() {
/// println!("command one called");
/// }
/// #[command]
/// fn command_two() {
/// println!("command two called");
/// }
/// fn main() {
/// let _handler = generate_handler![command_one, command_two];
/// }
/// ```
///
/// # Stability
///
/// The output of this macro is managed internally by Tauri,
/// and should not be accessed directly on normal applications.
/// It may have breaking changes in the future.
/// Reads a Tauri config file and generates a `::tauri::Context` based on the content.
///
/// The context embeds the frontend assets, the application icons, the resolved Access Control List
/// and the parsed configuration into the binary, and is passed to
/// `tauri::Builder::run`/`tauri::Builder::build`.
///
/// ```rust,ignore
/// tauri::Builder::default()
/// .run(tauri::generate_context!())
/// .expect("error while running tauri application");
/// ```
///
/// # Options
///
/// All options are optional and can be combined in a comma separated list.
///
/// ## Configuration file path
///
/// A string literal as the first argument sets the path of the Tauri configuration file to read,
/// relative to `CARGO_MANIFEST_DIR`. Defaults to `tauri.conf.json` on the crate directory.
/// Platform specific configuration files (e.g. `tauri.windows.conf.json`) that sit next to it are
/// merged as usual.
///
/// ```rust,ignore
/// tauri::generate_context!("../tauri.conf.json");
/// ```
///
/// ## Root path
///
/// A path (any item that is not a `key = value` pair) changes the crate path the generated code
/// refers to. Defaults to `::tauri`, and is only needed when the `tauri` crate is renamed,
/// re-exported by another crate, or is the crate being compiled (`crate`).
///
/// ```rust,ignore
/// tauri::generate_context!("../tauri.conf.json", ::my_framework::tauri);
/// ```
///
/// ## `capabilities`
///
/// A list of additional capability files to include in the generated Access Control List,
/// on top of the ones defined in the `capabilities` directory and in the
/// `app > security > capabilities` configuration value.
///
/// Each item is a path (relative to the current working directory of the compiler,
/// usually the crate directory) to a JSON or TOML file containing a capability,
/// a list of capabilities or a named list of capabilities.
///
/// ```rust,ignore
/// tauri::generate_context!(capabilities = ["./capabilities/extra.json"]);
/// ```
///
/// ## `assets`
///
/// An expression resolving to a custom [`tauri::Assets`] implementation, used instead of
/// embedding the files from `build > frontendDist`. Useful for serving the frontend from a
/// custom source, or for skipping asset embedding on tests.
///
/// ```rust,ignore
/// tauri::generate_context!(assets = tauri::test::noop_assets());
/// ```
///
/// ## `test`
///
/// When `true`, skips code generation that misbehaves when the context is created inside a
/// test binary - currently the `Info.plist` embedding performed on macOS development builds.
/// Defaults to `false`.
///
/// ```rust,ignore
/// let context = tauri::generate_context!("../tauri.conf.json", test = true);
/// ```
///
/// [`tauri::Assets`]: https://docs.rs/tauri/latest/tauri/trait.Assets.html
///
/// # Stability
/// The output of this macro is managed internally by Tauri,
/// and should not be accessed directly on normal applications.
/// It may have breaking changes in the future.
/// Adds the default type for the last parameter (assumed to be runtime) for a specific feature.
///
/// e.g. To default the runtime generic to type `crate::Wry` when the `wry` feature is enabled, the
/// syntax would look like `#[default_runtime(crate::Wry, wry)`. This is **always** set for the last
/// generic, so make sure the last generic is the runtime when using this macro.
/// Accepts a closure-like syntax to call arbitrary code on a menu item
/// after matching against `kind` and retrieving it from `resources_table` using `rid`.
///
/// You can optionally pass a 5th parameter to select which item kinds
/// to match against, by providing a `|` separated list of item kinds
/// ```ignore
/// do_menu_item!(resources_table, rid, kind, |i| i.set_text(text), Check | Submenu);
/// ```
/// You could also provide a negated list
/// ```ignore
/// do_menu_item!(resources_table, rid, kind, |i| i.set_text(text), !Check);
/// do_menu_item!(resources_table, rid, kind, |i| i.set_text(text), !Check | !Submenu);
/// ```
/// but you can't have mixed negations and positive kinds.
/// ```ignore
/// do_menu_item!(resources_table, rid, kind, |i| i.set_text(text), !Check | Submenu);
/// ```
///
/// ## Examples
///
/// ```ignore
/// let rid = 23;
/// let kind = ItemKind::Check;
/// let resources_table = app.resources_table();
/// do_menu_item!(resources_table, rid, kind, |i| i.set_text(text))
/// ```
/// which will expand into:
/// ```ignore
/// let rid = 23;
/// let kind = ItemKind::Check;
/// let resources_table = app.resources_table();
/// match kind {
/// ItemKind::Submenu => {
/// let i = resources_table.get::<Submenu<R>>(rid)?;
/// i.set_text(text)
/// }
/// ItemKind::MenuItem => {
/// let i = resources_table.get::<MenuItem<R>>(rid)?;
/// i.set_text(text)
/// }
/// ItemKind::Predefined => {
/// let i = resources_table.get::<PredefinedMenuItem<R>>(rid)?;
/// i.set_text(text)
/// }
/// ItemKind::Check => {
/// let i = resources_table.get::<CheckMenuItem<R>>(rid)?;
/// i.set_text(text)
/// }
/// ItemKind::Icon => {
/// let i = resources_table.get::<IconMenuItem<R>>(rid)?;
/// i.set_text(text)
/// }
/// _ => return Err(crate::Error::UnexpectedMenuKind),
/// }
/// ```
/// Convert a .png or .ico icon to an Image
/// for things like `tauri::tray::TrayIconBuilder` to consume,
/// relative paths are resolved from `CARGO_MANIFEST_DIR`, not current file
///
/// ### Examples
///
/// ```ignore
/// const APP_ICON: Image<'_> = include_image!("./icons/32x32.png");
///
/// // then use it with tray
/// TrayIconBuilder::new().icon(APP_ICON).build().unwrap();
///
/// // or with window
/// WebviewWindowBuilder::new(app, "main", WebviewUrl::default())
/// .icon(APP_ICON)
/// .unwrap()
/// .build()
/// .unwrap();
///
/// // or with any other functions that takes `Image` struct
/// ```
///
/// Note: this stores the image in raw pixels to the final binary,
/// so keep the icon size (width and height) small
/// or else it's going to bloat your final executable