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 <lucas@tauri.app>
This commit is contained in:
Miguel Duarte 2025-05-05 14:37:53 +02:00 committed by GitHub
parent 4f75bf5bdb
commit c84b162374
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -110,7 +110,7 @@ impl<'a, R: Runtime, M: Manager<R>> WebviewWindowBuilder<'a, R, M> {
/// Initializes a webview window builder from a [`WindowConfig`] from tauri.conf.json.
/// Keep in mind that you can't create 2 windows with the same `label` so make sure
/// that the initial window was closed or change the label of the new [`WebviewWindowBuilder`].
/// that the initial window was closed or change the label of the cloned [`WindowConfig`].
///
/// # Known issues
///
@ -124,7 +124,24 @@ impl<'a, R: Runtime, M: Manager<R>> WebviewWindowBuilder<'a, R, M> {
/// ```
/// #[tauri::command]
/// async fn reopen_window(app: tauri::AppHandle) {
/// let webview_window = tauri::WebviewWindowBuilder::from_config(&app, &app.config().app.windows.get(0).unwrap().clone())
/// let webview_window = tauri::WebviewWindowBuilder::from_config(&app, &app.config().app.windows.get(0).unwrap())
/// .unwrap()
/// .build()
/// .unwrap();
/// }
/// ```
///
/// - Create a window in a command from a config with a specific label, and change its label so multiple instances can exist:
///
/// ```
/// #[tauri::command]
/// async fn open_window_multiple(app: tauri::AppHandle) {
/// let mut conf = app.config().app.windows.iter().find(|c| c.label == "template-for-multiwindow").unwrap().clone();
/// // This should be a unique label for all windows. For example, we can use a random suffix:
/// let mut buf = [0u8; 1];
/// assert_eq!(getrandom::getrandom(&mut buf), Ok(()));
/// conf.label = format!("my-multiwindow-{}", buf[0]);
/// let webview_window = tauri::WebviewWindowBuilder::from_config(&app, &conf)
/// .unwrap()
/// .build()
/// .unwrap();