Skip to content

Commit b6dca99

Browse files
authored
refactor(api)!: change window label getters to be async ref #5380 (#10630)
1 parent b160f93 commit b6dca99

File tree

17 files changed

+153
-181
lines changed

17 files changed

+153
-181
lines changed

.changes/get-window-async.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@tauri-apps/api": patch:breaking
3+
---
4+
5+
Changed `WebviewWindow.getAll`, `WebviewWindow.getByLabel`, `getAllWebviewWindows`,
6+
`Window.getAll`, `Window.getByLabel`, `getAllWindows`,
7+
`Webview.getAll`, `Webview.getByLabel`, `getAllWebviews`
8+
to be async so their return value are synchronized with the state from the Rust side,
9+
meaning new and destroyed windows are reflected.

core/tauri/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const PLUGINS: &[(&str, &[(&str, bool)])] = &[
4646
&[
4747
("create", false),
4848
// getters
49+
("get_all_windows", true),
4950
("scale_factor", true),
5051
("inner_position", true),
5152
("outer_position", true),
@@ -120,6 +121,7 @@ const PLUGINS: &[(&str, &[(&str, bool)])] = &[
120121
("create_webview", false),
121122
("create_webview_window", false),
122123
// getters
124+
("get_all_webviews", true),
123125
("webview_position", true),
124126
("webview_size", true),
125127
// setters

core/tauri/permissions/webview/autogenerated/reference.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Default permissions for the plugin.
44

5+
- `allow-get-all-webviews`
56
- `allow-webview-position`
67
- `allow-webview-size`
78
- `allow-internal-toggle-devtools`
@@ -70,6 +71,32 @@ Denies the create_webview_window command without any pre-configured scope.
7071
<tr>
7172
<td>
7273

74+
`core:webview:allow-get-all-webviews`
75+
76+
</td>
77+
<td>
78+
79+
Enables the get_all_webviews command without any pre-configured scope.
80+
81+
</td>
82+
</tr>
83+
84+
<tr>
85+
<td>
86+
87+
`core:webview:deny-get-all-webviews`
88+
89+
</td>
90+
<td>
91+
92+
Denies the get_all_webviews command without any pre-configured scope.
93+
94+
</td>
95+
</tr>
96+
97+
<tr>
98+
<td>
99+
73100
`core:webview:allow-internal-toggle-devtools`
74101

75102
</td>

core/tauri/permissions/window/autogenerated/reference.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Default permissions for the plugin.
44

5+
- `allow-get-all-windows`
56
- `allow-scale-factor`
67
- `allow-inner-position`
78
- `allow-outer-position`
@@ -220,6 +221,32 @@ Denies the destroy command without any pre-configured scope.
220221
<tr>
221222
<td>
222223

224+
`core:window:allow-get-all-windows`
225+
226+
</td>
227+
<td>
228+
229+
Enables the get_all_windows command without any pre-configured scope.
230+
231+
</td>
232+
</tr>
233+
234+
<tr>
235+
<td>
236+
237+
`core:window:deny-get-all-windows`
238+
239+
</td>
240+
<td>
241+
242+
Denies the get_all_windows command without any pre-configured scope.
243+
244+
</td>
245+
</tr>
246+
247+
<tr>
248+
<td>
249+
223250
`core:window:allow-hide`
224251

225252
</td>

core/tauri/scripts/bundle.global.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tauri/src/app.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ use crate::{
88
channel::ChannelDataIpcQueue, CallbackFn, CommandArg, CommandItem, Invoke, InvokeError,
99
InvokeHandler, InvokeResponder, InvokeResponse,
1010
},
11-
manager::{
12-
webview::{UriSchemeProtocol, WebviewLabelDef},
13-
AppManager, Asset,
14-
},
11+
manager::{webview::UriSchemeProtocol, AppManager, Asset},
1512
plugin::{Plugin, PluginStore},
1613
resources::ResourceTable,
1714
runtime::{
@@ -1961,24 +1958,8 @@ impl<R: Runtime> HasDisplayHandle for App<R> {
19611958
fn setup<R: Runtime>(app: &mut App<R>) -> crate::Result<()> {
19621959
app.ran_setup = true;
19631960

1964-
let window_labels = app
1965-
.config()
1966-
.app
1967-
.windows
1968-
.iter()
1969-
.map(|p| p.label.clone())
1970-
.collect::<Vec<_>>();
1971-
let webview_labels = window_labels
1972-
.iter()
1973-
.map(|label| WebviewLabelDef {
1974-
window_label: label.clone(),
1975-
label: label.clone(),
1976-
})
1977-
.collect::<Vec<_>>();
1978-
19791961
for window_config in app.config().app.windows.clone() {
1980-
WebviewWindowBuilder::from_config(app.handle(), &window_config)?
1981-
.build_internal(&window_labels, &webview_labels)?;
1962+
WebviewWindowBuilder::from_config(app.handle(), &window_config)?.build()?;
19821963
}
19831964

19841965
app.manager.assets.setup(app);

core/tauri/src/manager/webview.rs

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,6 @@ pub struct UriSchemeProtocol<R: Runtime> {
6464
Box<dyn Fn(&AppHandle<R>, http::Request<Vec<u8>>, UriSchemeResponder) + Send + Sync>,
6565
}
6666

67-
#[derive(Clone, Serialize)]
68-
#[serde(rename_all = "camelCase")]
69-
pub struct WebviewLabelDef {
70-
pub window_label: String,
71-
pub label: String,
72-
}
73-
7467
pub struct WebviewManager<R: Runtime> {
7568
pub webviews: Mutex<HashMap<String, Webview<R>>>,
7669
/// The JS message handler.
@@ -127,8 +120,6 @@ impl<R: Runtime> WebviewManager<R> {
127120
mut pending: PendingWebview<EventLoopMessage, R>,
128121
label: &str,
129122
window_label: &str,
130-
window_labels: &[String],
131-
webview_labels: &[WebviewLabelDef],
132123
manager: &M,
133124
) -> crate::Result<PendingWebview<EventLoopMessage, R>> {
134125
let app_manager = manager.manager();
@@ -156,13 +147,6 @@ impl<R: Runtime> WebviewManager<R> {
156147
}
157148
.render_default(&Default::default())?;
158149

159-
let mut webview_labels = webview_labels.to_vec();
160-
if !webview_labels.iter().any(|w| w.label == label) {
161-
webview_labels.push(WebviewLabelDef {
162-
window_label: window_label.to_string(),
163-
label: label.to_string(),
164-
});
165-
}
166150
webview_attributes = webview_attributes
167151
.initialization_script(
168152
r"
@@ -184,15 +168,11 @@ impl<R: Runtime> WebviewManager<R> {
184168
r#"
185169
Object.defineProperty(window.__TAURI_INTERNALS__, 'metadata', {{
186170
value: {{
187-
windows: {window_labels_array}.map(function (label) {{ return {{ label: label }} }}),
188-
webviews: {webview_labels_array},
189171
currentWindow: {{ label: {current_window_label} }},
190172
currentWebview: {{ label: {current_webview_label} }}
191173
}}
192174
}})
193175
"#,
194-
window_labels_array = serde_json::to_string(&window_labels)?,
195-
webview_labels_array = serde_json::to_string(&webview_labels)?,
196176
current_window_label = serde_json::to_string(window_label)?,
197177
current_webview_label = serde_json::to_string(&label)?,
198178
))
@@ -415,8 +395,6 @@ impl<R: Runtime> WebviewManager<R> {
415395
manager: &M,
416396
mut pending: PendingWebview<EventLoopMessage, R>,
417397
window_label: &str,
418-
window_labels: &[String],
419-
webview_labels: &[WebviewLabelDef],
420398
) -> crate::Result<PendingWebview<EventLoopMessage, R>> {
421399
if self.webviews_lock().contains_key(&pending.label) {
422400
return Err(crate::Error::WebviewLabelAlreadyExists(pending.label));
@@ -509,14 +487,7 @@ impl<R: Runtime> WebviewManager<R> {
509487
}
510488

511489
let label = pending.label.clone();
512-
pending = self.prepare_pending_webview(
513-
pending,
514-
&label,
515-
window_label,
516-
window_labels,
517-
webview_labels,
518-
manager,
519-
)?;
490+
pending = self.prepare_pending_webview(pending, &label, window_label, manager)?;
520491

521492
pending.ipc_handler = Some(crate::ipc::protocol::message_handler(
522493
manager.manager_owned(),
@@ -638,12 +609,6 @@ impl<R: Runtime> WebviewManager<R> {
638609
.expect("failed to run on_webview_created hook");
639610
}
640611

641-
if let Ok(webview_labels_array) = serde_json::to_string(&webview.manager.webview.labels()) {
642-
let _ = webview.manager.webview.eval_script_all(format!(
643-
"window.__TAURI_INTERNALS__.metadata.webviews = {webview_labels_array}.map(function (label) {{ return {{ label: label }} }})",
644-
));
645-
}
646-
647612
let _ = webview.manager.emit(
648613
"tauri://webview-created",
649614
Some(crate::webview::CreatedEvent {

core/tauri/src/manager/window.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,6 @@ fn on_window_event<R: Runtime>(window: &Window<R>, event: &WindowEvent) -> crate
162162
}
163163
WindowEvent::Destroyed => {
164164
window.emit_to_window(WINDOW_DESTROYED_EVENT, ())?;
165-
let label = window.label();
166-
167-
if let Ok(webview_labels_array) = serde_json::to_string(&window.manager().webview.labels()) {
168-
let _ = window.manager().webview.eval_script_all(format!(
169-
r#"(function () {{ const metadata = window.__TAURI_INTERNALS__.metadata; if (metadata != null) {{ metadata.windows = window.__TAURI_INTERNALS__.metadata.windows.filter(w => w.label !== "{label}"); metadata.webviews = {webview_labels_array}.map(function (label) {{ return {{ label: label }} }}) }} }})()"#,
170-
));
171-
}
172165
}
173166
WindowEvent::Focused(focused) => window.emit_to_window(
174167
if *focused {

core/tauri/src/webview/mod.rs

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::{
3232
CallbackFn, CommandArg, CommandItem, Invoke, InvokeBody, InvokeError, InvokeMessage,
3333
InvokeResolver, Origin, OwnedInvokeResponder,
3434
},
35-
manager::{webview::WebviewLabelDef, AppManager},
35+
manager::AppManager,
3636
sealed::{ManagerBase, RuntimeOrDispatch},
3737
AppHandle, Emitter, Event, EventId, EventLoopMessage, Listener, Manager, ResourceTable, Runtime,
3838
Window,
@@ -548,8 +548,6 @@ tauri::Builder::default()
548548
mut self,
549549
manager: &M,
550550
window_label: &str,
551-
window_labels: &[String],
552-
webview_labels: &[WebviewLabelDef],
553551
) -> crate::Result<PendingWebview<EventLoopMessage, R>> {
554552
let mut pending = PendingWebview::new(self.webview_attributes, self.label.clone())?;
555553
pending.navigation_handler = self.navigation_handler.take();
@@ -589,13 +587,10 @@ tauri::Builder::default()
589587
}
590588
}));
591589

592-
manager.manager().webview.prepare_webview(
593-
manager,
594-
pending,
595-
window_label,
596-
window_labels,
597-
webview_labels,
598-
)
590+
manager
591+
.manager()
592+
.webview
593+
.prepare_webview(manager, pending, window_label)
599594
}
600595

601596
/// Creates a new webview on the given window.
@@ -606,27 +601,9 @@ tauri::Builder::default()
606601
position: Position,
607602
size: Size,
608603
) -> crate::Result<Webview<R>> {
609-
let window_labels = window
610-
.manager()
611-
.window
612-
.labels()
613-
.into_iter()
614-
.collect::<Vec<_>>();
615-
let webview_labels = window
616-
.manager()
617-
.webview
618-
.webviews_lock()
619-
.values()
620-
.map(|w| WebviewLabelDef {
621-
window_label: w.window().label().to_string(),
622-
label: w.label().to_string(),
623-
})
624-
.collect::<Vec<_>>();
625-
626604
let app_manager = window.manager();
627605

628-
let mut pending =
629-
self.into_pending_webview(&window, window.label(), &window_labels, &webview_labels)?;
606+
let mut pending = self.into_pending_webview(&window, window.label())?;
630607

631608
pending.webview_attributes.bounds = Some(Rect { size, position });
632609

core/tauri/src/webview/plugin.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
#[cfg(desktop)]
1313
mod desktop_commands {
1414

15-
use serde::Deserialize;
15+
use serde::{Deserialize, Serialize};
1616
use tauri_runtime::dpi::{Position, Size};
1717
use tauri_utils::config::{WebviewUrl, WindowConfig};
1818

@@ -44,6 +44,25 @@ mod desktop_commands {
4444
zoom_hotkeys_enabled: bool,
4545
}
4646

47+
#[derive(Serialize)]
48+
pub struct WebviewRef {
49+
window_label: String,
50+
label: String,
51+
}
52+
53+
#[command(root = "crate")]
54+
pub async fn get_all_webviews<R: Runtime>(app: AppHandle<R>) -> Vec<WebviewRef> {
55+
app
56+
.manager()
57+
.webviews()
58+
.values()
59+
.map(|webview| WebviewRef {
60+
window_label: webview.window().label().into(),
61+
label: webview.label().into(),
62+
})
63+
.collect()
64+
}
65+
4766
#[command(root = "crate")]
4867
pub async fn create_webview_window<R: Runtime>(
4968
app: AppHandle<R>,
@@ -232,6 +251,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
232251
desktop_commands::create_webview,
233252
desktop_commands::create_webview_window,
234253
// getters
254+
desktop_commands::get_all_webviews,
235255
desktop_commands::webview_position,
236256
desktop_commands::webview_size,
237257
// setters

0 commit comments

Comments
 (0)