@@ -21,6 +21,7 @@ use tauri_runtime::{
2121 } ,
2222 DeviceEventFilter , Dispatch , Error , EventLoopProxy , ExitRequestedEventAction , Icon , Result ,
2323 RunEvent , RunIteration , Runtime , RuntimeHandle , RuntimeInitArgs , UserAttentionType , UserEvent ,
24+ WindowEventId ,
2425} ;
2526
2627#[ cfg( windows) ]
@@ -41,7 +42,6 @@ use wry::webview::WebViewBuilderExtWindows;
4142#[ cfg( target_os = "macos" ) ]
4243use tauri_utils:: TitleBarStyle ;
4344use tauri_utils:: { config:: WindowConfig , debug_eprintln, Theme } ;
44- use uuid:: Uuid ;
4545use wry:: {
4646 application:: {
4747 dpi:: {
@@ -93,13 +93,14 @@ use std::{
9393 path:: PathBuf ,
9494 rc:: Rc ,
9595 sync:: {
96+ atomic:: { AtomicU32 , Ordering } ,
9697 mpsc:: { channel, Sender } ,
9798 Arc , Mutex , Weak ,
9899 } ,
99100 thread:: { current as current_thread, ThreadId } ,
100101} ;
101102
102- pub type WebviewId = u64 ;
103+ pub type WebviewId = u32 ;
103104type IpcHandler = dyn Fn ( & Window , String ) + ' static ;
104105type FileDropHandler = dyn Fn ( & Window , WryFileDropEvent ) -> bool + ' static ;
105106
@@ -109,7 +110,7 @@ pub use webview::Webview;
109110pub type WebContextStore = Arc < Mutex < HashMap < Option < PathBuf > , WebContext > > > ;
110111// window
111112pub type WindowEventHandler = Box < dyn Fn ( & WindowEvent ) + Send > ;
112- pub type WindowEventListeners = Arc < Mutex < HashMap < Uuid , WindowEventHandler > > > ;
113+ pub type WindowEventListeners = Arc < Mutex < HashMap < WindowEventId , WindowEventHandler > > > ;
113114
114115#[ derive( Debug , Clone , Default ) ]
115116pub struct WebviewIdStore ( Arc < Mutex < HashMap < WindowId , WebviewId > > > ) ;
@@ -171,6 +172,9 @@ pub struct Context<T: UserEvent> {
171172 pub proxy : WryEventLoopProxy < Message < T > > ,
172173 main_thread : DispatcherMainThreadContext < T > ,
173174 plugins : Arc < Mutex < Vec < Box < dyn Plugin < T > + Send > > > > ,
175+ next_window_id : Arc < AtomicU32 > ,
176+ next_window_event_id : Arc < AtomicU32 > ,
177+ next_webcontext_id : Arc < AtomicU32 > ,
174178}
175179
176180impl < T : UserEvent > Context < T > {
@@ -184,6 +188,18 @@ impl<T: UserEvent> Context<T> {
184188 None
185189 } )
186190 }
191+
192+ fn next_window_id ( & self ) -> WebviewId {
193+ self . next_window_id . fetch_add ( 1 , Ordering :: Relaxed )
194+ }
195+
196+ fn next_window_event_id ( & self ) -> WebviewId {
197+ self . next_window_event_id . fetch_add ( 1 , Ordering :: Relaxed )
198+ }
199+
200+ fn next_webcontext_id ( & self ) -> WebviewId {
201+ self . next_webcontext_id . fetch_add ( 1 , Ordering :: Relaxed )
202+ }
187203}
188204
189205impl < T : UserEvent > Context < T > {
@@ -194,7 +210,7 @@ impl<T: UserEvent> Context<T> {
194210 ) -> Result < DetachedWindow < T , Wry < T > > > {
195211 let label = pending. label . clone ( ) ;
196212 let context = self . clone ( ) ;
197- let window_id = rand :: random ( ) ;
213+ let window_id = self . next_window_id ( ) ;
198214
199215 send_user_message (
200216 self ,
@@ -909,7 +925,7 @@ pub enum ApplicationMessage {
909925
910926pub enum WindowMessage {
911927 WithWebview ( Box < dyn FnOnce ( Webview ) + Send > ) ,
912- AddEventListener ( Uuid , Box < dyn Fn ( & WindowEvent ) + Send > ) ,
928+ AddEventListener ( WindowEventId , Box < dyn Fn ( & WindowEvent ) + Send > ) ,
913929 // Devtools
914930 #[ cfg( any( debug_assertions, feature = "devtools" ) ) ]
915931 OpenDevTools ,
@@ -1056,8 +1072,8 @@ impl<T: UserEvent> Dispatch<T> for WryDispatcher<T> {
10561072 send_user_message ( & self . context , Message :: Task ( Box :: new ( f) ) )
10571073 }
10581074
1059- fn on_window_event < F : Fn ( & WindowEvent ) + Send + ' static > ( & self , f : F ) -> Uuid {
1060- let id = Uuid :: new_v4 ( ) ;
1075+ fn on_window_event < F : Fn ( & WindowEvent ) + Send + ' static > ( & self , f : F ) -> WindowEventId {
1076+ let id = self . context . next_window_event_id ( ) ;
10611077 let _ = self . context . proxy . send_event ( Message :: Window (
10621078 self . window_id ,
10631079 WindowMessage :: AddEventListener ( id, Box :: new ( f) ) ,
@@ -1640,11 +1656,9 @@ impl<T: UserEvent> WryHandle<T> {
16401656 & self ,
16411657 f : F ,
16421658 ) -> Result < Weak < Window > > {
1659+ let id = self . context . next_window_id ( ) ;
16431660 let ( tx, rx) = channel ( ) ;
1644- send_user_message (
1645- & self . context ,
1646- Message :: CreateWindow ( rand:: random ( ) , Box :: new ( f) , tx) ,
1647- ) ?;
1661+ send_user_message ( & self . context , Message :: CreateWindow ( id, Box :: new ( f) , tx) ) ?;
16481662 rx. recv ( ) . unwrap ( )
16491663 }
16501664
@@ -1794,6 +1808,9 @@ impl<T: UserEvent> Wry<T> {
17941808 windows,
17951809 } ,
17961810 plugins : Default :: default ( ) ,
1811+ next_window_id : Default :: default ( ) ,
1812+ next_window_event_id : Default :: default ( ) ,
1813+ next_webcontext_id : Default :: default ( ) ,
17971814 } ;
17981815
17991816 Ok ( Self {
@@ -1850,7 +1867,7 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
18501867 before_webview_creation : Option < F > ,
18511868 ) -> Result < DetachedWindow < T , Self > > {
18521869 let label = pending. label . clone ( ) ;
1853- let window_id = rand :: random ( ) ;
1870+ let window_id = self . context . next_window_id ( ) ;
18541871
18551872 let webview = create_webview (
18561873 window_id,
@@ -2661,7 +2678,7 @@ fn create_webview<T: UserEvent, F: Fn(RawWindow) + Send + 'static>(
26612678
26622679 if let Some ( handler) = ipc_handler {
26632680 webview_builder =
2664- webview_builder. with_ipc_handler ( create_ipc_handler ( context, label. clone ( ) , handler) ) ;
2681+ webview_builder. with_ipc_handler ( create_ipc_handler ( context. clone ( ) , label. clone ( ) , handler) ) ;
26652682 }
26662683
26672684 for ( scheme, protocol) in uri_scheme_protocols {
@@ -2686,8 +2703,9 @@ fn create_webview<T: UserEvent, F: Fn(RawWindow) + Send + 'static>(
26862703 if automation_enabled {
26872704 webview_attributes. data_directory . clone ( )
26882705 } else {
2689- // random unique key
2690- Some ( Uuid :: new_v4 ( ) . as_hyphenated ( ) . to_string ( ) . into ( ) )
2706+ // unique key
2707+ let key = context. next_webcontext_id ( ) . to_string ( ) . into ( ) ;
2708+ Some ( key)
26912709 } ;
26922710 let entry = web_context. entry ( web_context_key. clone ( ) ) ;
26932711 let web_context = match entry {
0 commit comments