Skip to content

Commit 878b8b9

Browse files
authored
fix(core): immediately listen to window-created, closes #3297 (#3298)
1 parent 2bade5e commit 878b8b9

7 files changed

Lines changed: 87 additions & 65 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Immediately listen to `tauri://window-created` event to catch it before the application triggers it.

core/tauri/scripts/core.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,11 @@
150150
}
151151
})
152152

153-
window.__TAURI_INVOKE__('tauri', {
154-
__tauriModule: 'Event',
155-
message: {
156-
cmd: 'listen',
157-
event: 'tauri://window-created',
158-
handler: window.__TAURI__.transformCallback(function (event) {
159-
if (event.payload) {
160-
var windowLabel = event.payload.label
161-
window.__TAURI_METADATA__.__windows.push({
162-
label: windowLabel
163-
})
164-
}
153+
listen('tauri://window-created', function (event) {
154+
if (event.payload) {
155+
var windowLabel = event.payload.label
156+
window.__TAURI_METADATA__.__windows.push({
157+
label: windowLabel
165158
})
166159
}
167160
})

core/tauri/scripts/init.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
__RAW_bundle_script__
1010
})()
1111

12+
__RAW_listen_function__
13+
1214
__RAW_core_script__
1315

1416
__RAW_event_initialization_script__

core/tauri/src/app.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ impl<R: Runtime> Builder<R> {
11561156

11571157
app.manager.initialize_plugins(&app.handle())?;
11581158

1159-
let pending_labels = self
1159+
let window_labels = self
11601160
.pending_windows
11611161
.iter()
11621162
.map(|p| p.label.clone())
@@ -1168,7 +1168,7 @@ impl<R: Runtime> Builder<R> {
11681168
for pending in self.pending_windows {
11691169
let pending = app
11701170
.manager
1171-
.prepare_window(app.handle.clone(), pending, &pending_labels)?;
1171+
.prepare_window(app.handle.clone(), pending, &window_labels)?;
11721172
let detached = app.runtime.as_ref().unwrap().create_window(pending)?;
11731173
let _window = app.manager.attach_window(app.handle(), detached);
11741174
#[cfg(feature = "updater")]

core/tauri/src/endpoints/event.rs

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
use super::InvokeContext;
66
use crate::{
7-
api::ipc::CallbackFn, event::is_event_name_valid, sealed::ManagerBase, Manager, Runtime, Window,
7+
api::ipc::CallbackFn,
8+
event::is_event_name_valid,
9+
event::{listen_js, unlisten_js},
10+
sealed::ManagerBase,
11+
Manager, Runtime,
812
};
913
use serde::{de::Deserializer, Deserialize};
1014
use tauri_macros::CommandModule;
@@ -54,19 +58,20 @@ impl Cmd {
5458
) -> crate::Result<u64> {
5559
let event_id = rand::random();
5660
context.window.eval(&listen_js(
57-
&context.window,
58-
event.0.clone(),
61+
context.window.manager().event_listeners_object_name(),
62+
format!("'{}'", event.0),
5963
event_id,
60-
handler,
64+
format!("window['_{}']", handler.0),
6165
))?;
6266
context.window.register_js_listener(event.0, event_id);
6367
Ok(event_id)
6468
}
6569

6670
fn unlisten<R: Runtime>(context: InvokeContext<R>, event_id: u64) -> crate::Result<()> {
67-
context
68-
.window
69-
.eval(&unlisten_js(&context.window, event_id))?;
71+
context.window.eval(&unlisten_js(
72+
context.window.manager().event_listeners_object_name(),
73+
event_id,
74+
))?;
7075
context.window.unregister_js_listener(event_id);
7176
Ok(())
7277
}
@@ -88,43 +93,3 @@ impl Cmd {
8893
Ok(())
8994
}
9095
}
91-
92-
pub fn unlisten_js<R: Runtime>(window: &Window<R>, event_id: u64) -> String {
93-
format!(
94-
"
95-
for (var event in (window['{listeners}'] || {{}})) {{
96-
var listeners = (window['{listeners}'] || {{}})[event]
97-
if (listeners) {{
98-
window['{listeners}'][event] = window['{listeners}'][event].filter(function (e) {{ return e.id !== {event_id} }})
99-
}}
100-
}}
101-
",
102-
listeners = window.manager().event_listeners_object_name(),
103-
event_id = event_id,
104-
)
105-
}
106-
107-
pub fn listen_js<R: Runtime>(
108-
window: &Window<R>,
109-
event: String,
110-
event_id: u64,
111-
handler: CallbackFn,
112-
) -> String {
113-
format!(
114-
"if (window['{listeners}'] === void 0) {{
115-
window['{listeners}'] = Object.create(null)
116-
}}
117-
if (window['{listeners}']['{event}'] === void 0) {{
118-
window['{listeners}']['{event}'] = []
119-
}}
120-
window['{listeners}']['{event}'].push({{
121-
id: {event_id},
122-
handler: window['_{handler}']
123-
}});
124-
",
125-
listeners = window.manager().event_listeners_object_name(),
126-
event = event,
127-
event_id = event_id,
128-
handler = handler.0
129-
)
130-
}

core/tauri/src/event.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,43 @@ mod test {
286286
}
287287
}
288288
}
289+
290+
pub fn unlisten_js(listeners_object_name: String, event_id: u64) -> String {
291+
format!(
292+
"
293+
for (var event in (window['{listeners}'] || {{}})) {{
294+
var listeners = (window['{listeners}'] || {{}})[event]
295+
if (listeners) {{
296+
window['{listeners}'][event] = window['{listeners}'][event].filter(function (e) {{ return e.id !== {event_id} }})
297+
}}
298+
}}
299+
",
300+
listeners = listeners_object_name,
301+
event_id = event_id,
302+
)
303+
}
304+
305+
pub fn listen_js(
306+
listeners_object_name: String,
307+
event: String,
308+
event_id: u64,
309+
handler: String,
310+
) -> String {
311+
format!(
312+
"if (window['{listeners}'] === void 0) {{
313+
window['{listeners}'] = Object.create(null)
314+
}}
315+
if (window['{listeners}'][{event}] === void 0) {{
316+
window['{listeners}'][{event}] = []
317+
}}
318+
window['{listeners}'][{event}].push({{
319+
id: {event_id},
320+
handler: {handler}
321+
}});
322+
",
323+
listeners = listeners_object_name,
324+
event = event,
325+
event_id = event_id,
326+
handler = handler
327+
)
328+
}

core/tauri/src/manager.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ impl<R: Runtime> WindowManager<R> {
395395
&self,
396396
mut pending: PendingWindow<R>,
397397
label: &str,
398-
pending_labels: &[String],
398+
window_labels: &[String],
399399
app_handle: AppHandle<R>,
400400
) -> crate::Result<PendingWindow<R>> {
401401
let is_init_global = self.inner.config.build.with_global_tauri;
@@ -422,6 +422,11 @@ impl<R: Runtime> WindowManager<R> {
422422

423423
let mut webview_attributes = pending.webview_attributes;
424424

425+
let mut window_labels = window_labels.to_vec();
426+
let l = label.to_string();
427+
if !window_labels.contains(&l) {
428+
window_labels.push(l);
429+
}
425430
webview_attributes = webview_attributes
426431
.initialization_script(&self.inner.invoke_initialization_script)
427432
.initialization_script(&format!(
@@ -433,7 +438,7 @@ impl<R: Runtime> WindowManager<R> {
433438
}}
434439
}})
435440
"#,
436-
window_labels_array = serde_json::to_string(pending_labels)?,
441+
window_labels_array = serde_json::to_string(&window_labels)?,
437442
current_window_label = serde_json::to_string(&label)?,
438443
))
439444
.initialization_script(&self.initialization_script(&ipc_init,&pattern_init,&plugin_init, is_init_global)?)
@@ -809,6 +814,9 @@ impl<R: Runtime> WindowManager<R> {
809814
ipc_script: &'a str,
810815
#[raw]
811816
bundle_script: &'a str,
817+
// A function to immediately listen to an event.
818+
#[raw]
819+
listen_function: &'a str,
812820
#[raw]
813821
core_script: &'a str,
814822
#[raw]
@@ -836,6 +844,15 @@ impl<R: Runtime> WindowManager<R> {
836844
pattern_script,
837845
ipc_script,
838846
bundle_script,
847+
listen_function: &format!(
848+
"function listen(eventName, cb) {{ {} }}",
849+
crate::event::listen_js(
850+
self.event_listeners_object_name(),
851+
"eventName".into(),
852+
0,
853+
"window['_' + window.__TAURI__.transformCallback(cb) ]".into()
854+
)
855+
),
839856
core_script: include_str!("../scripts/core.js"),
840857
event_initialization_script: &self.event_initialization_script(),
841858
plugin_initialization_script,
@@ -930,7 +947,7 @@ impl<R: Runtime> WindowManager<R> {
930947
&self,
931948
app_handle: AppHandle<R>,
932949
mut pending: PendingWindow<R>,
933-
pending_labels: &[String],
950+
window_labels: &[String],
934951
) -> crate::Result<PendingWindow<R>> {
935952
if self.windows_lock().contains_key(&pending.label) {
936953
return Err(crate::Error::WindowLabelAlreadyExists(pending.label));
@@ -982,7 +999,7 @@ impl<R: Runtime> WindowManager<R> {
982999

9831000
if is_local {
9841001
let label = pending.label.clone();
985-
pending = self.prepare_pending_window(pending, &label, pending_labels, app_handle.clone())?;
1002+
pending = self.prepare_pending_window(pending, &label, window_labels, app_handle.clone())?;
9861003
pending.ipc_handler = Some(self.prepare_ipc_handler(app_handle.clone()));
9871004
}
9881005

0 commit comments

Comments
 (0)