Skip to content

Commit c84b162

Browse files
docs: Fix description and add example for WebviewWindowBuilder::from_config (#13374)
* Update docs for WebviewWindowBuilder::from_config The documentation for `WebviewWindowBuilder::from_config` mentions changing the label of the new `WebviewWindowBuilder`, which is not possible. Instead, the label must be changed in the `WindowConfig` that is passed into `WebviewWindowBuilder::from_config`. This change fixes that description and adds an example code snippet for this use-case. * Add reference to function arguments so the type is correctly inferred * Remove unnecesary reference * fix tests * fix doctest --------- Co-authored-by: Lucas Nogueira <[email protected]>
1 parent 4f75bf5 commit c84b162

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

crates/tauri/src/webview/webview_window.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'a, R: Runtime, M: Manager<R>> WebviewWindowBuilder<'a, R, M> {
110110

111111
/// Initializes a webview window builder from a [`WindowConfig`] from tauri.conf.json.
112112
/// Keep in mind that you can't create 2 windows with the same `label` so make sure
113-
/// that the initial window was closed or change the label of the new [`WebviewWindowBuilder`].
113+
/// that the initial window was closed or change the label of the cloned [`WindowConfig`].
114114
///
115115
/// # Known issues
116116
///
@@ -124,7 +124,24 @@ impl<'a, R: Runtime, M: Manager<R>> WebviewWindowBuilder<'a, R, M> {
124124
/// ```
125125
/// #[tauri::command]
126126
/// async fn reopen_window(app: tauri::AppHandle) {
127-
/// let webview_window = tauri::WebviewWindowBuilder::from_config(&app, &app.config().app.windows.get(0).unwrap().clone())
127+
/// let webview_window = tauri::WebviewWindowBuilder::from_config(&app, &app.config().app.windows.get(0).unwrap())
128+
/// .unwrap()
129+
/// .build()
130+
/// .unwrap();
131+
/// }
132+
/// ```
133+
///
134+
/// - Create a window in a command from a config with a specific label, and change its label so multiple instances can exist:
135+
///
136+
/// ```
137+
/// #[tauri::command]
138+
/// async fn open_window_multiple(app: tauri::AppHandle) {
139+
/// let mut conf = app.config().app.windows.iter().find(|c| c.label == "template-for-multiwindow").unwrap().clone();
140+
/// // This should be a unique label for all windows. For example, we can use a random suffix:
141+
/// let mut buf = [0u8; 1];
142+
/// assert_eq!(getrandom::getrandom(&mut buf), Ok(()));
143+
/// conf.label = format!("my-multiwindow-{}", buf[0]);
144+
/// let webview_window = tauri::WebviewWindowBuilder::from_config(&app, &conf)
128145
/// .unwrap()
129146
/// .build()
130147
/// .unwrap();

0 commit comments

Comments
 (0)