22// SPDX-License-Identifier: Apache-2.0
33// SPDX-License-Identifier: MIT
44
5+ use crate :: runtime:: tag:: TagRef ;
56use crate :: {
67 api:: {
78 assets:: Assets ,
@@ -13,7 +14,7 @@ use crate::{
1314 hooks:: { InvokeHandler , InvokeMessage , InvokePayload , OnPageLoad , PageLoadPayload } ,
1415 plugin:: PluginStore ,
1516 runtime:: {
16- tag:: { tags_to_javascript_array, Tag , ToJavascript } ,
17+ tag:: { tags_to_javascript_array, Tag , ToJsString } ,
1718 webview:: { Attributes , CustomProtocol , FileDropEvent , FileDropHandler , WebviewRpcHandler } ,
1819 window:: { DetachedWindow , PendingWindow } ,
1920 Dispatch , Icon , Runtime ,
@@ -23,6 +24,7 @@ use crate::{
2324} ;
2425use serde:: Serialize ;
2526use serde_json:: Value as JsonValue ;
27+ use std:: borrow:: Borrow ;
2628use std:: marker:: PhantomData ;
2729use std:: {
2830 borrow:: Cow ,
@@ -33,6 +35,20 @@ use std::{
3335} ;
3436use uuid:: Uuid ;
3537
38+ /// Parse a string representing an internal tauri event into [`Params::Event`]
39+ ///
40+ /// # Panics
41+ ///
42+ /// This will panic if the `FromStr` implementation of [`Params::Event`] returns an error.
43+ pub ( crate ) fn tauri_event < Event : Tag > ( tauri_event : & str ) -> Event {
44+ tauri_event. parse ( ) . unwrap_or_else ( |_| {
45+ panic ! (
46+ "failed to parse internal tauri event into Params::Event: {}" ,
47+ tauri_event
48+ )
49+ } )
50+ }
51+
3652pub struct InnerWindowManager < P : Params > {
3753 windows : Mutex < HashMap < P :: Label , Window < P > > > ,
3854 plugins : Mutex < PluginStore < P > > ,
@@ -168,7 +184,7 @@ impl<P: Params> WindowManager<P> {
168184 window.__TAURI__.__currentWindow = {{ label: {current_window_label} }}
169185 "# ,
170186 window_labels_array = tags_to_javascript_array( pending_labels) ?,
171- current_window_label = label. to_javascript ( ) ?,
187+ current_window_label = label. to_js_string ( ) ?,
172188 ) ) ;
173189
174190 if !attributes. has_icon ( ) {
@@ -281,14 +297,16 @@ impl<P: Params> WindowManager<P> {
281297 let window = manager. attach_window ( window) ;
282298 let _ = match event {
283299 FileDropEvent :: Hovered ( paths) => {
284- window. emit_internal ( "tauri://file-drop" . to_string ( ) , Some ( paths) )
285- }
286- FileDropEvent :: Dropped ( paths) => {
287- window. emit_internal ( "tauri://file-drop-hover" . to_string ( ) , Some ( paths) )
288- }
289- FileDropEvent :: Cancelled => {
290- window. emit_internal ( "tauri://file-drop-cancelled" . to_string ( ) , Some ( ( ) ) )
300+ window. emit_internal ( & tauri_event :: < P :: Event > ( "tauri://file-drop" ) , Some ( paths) )
291301 }
302+ FileDropEvent :: Dropped ( paths) => window. emit_internal (
303+ & tauri_event :: < P :: Event > ( "tauri://file-drop-hover" ) ,
304+ Some ( paths) ,
305+ ) ,
306+ FileDropEvent :: Cancelled => window. emit_internal (
307+ & tauri_event :: < P :: Event > ( "tauri://file-drop-cancelled" ) ,
308+ Some ( ( ) ) ,
309+ ) ,
292310 } ;
293311 } ) ;
294312 true
@@ -474,30 +492,20 @@ impl<P: Params> WindowManager<P> {
474492
475493 window
476494 }
477- pub fn emit_filter_internal < S : Serialize + Clone , F : Fn ( & Window < P > ) -> bool > (
478- & self ,
479- event : String ,
480- payload : Option < S > ,
481- filter : F ,
482- ) -> crate :: Result < ( ) > {
483- self
484- . windows_lock ( )
485- . values ( )
486- . filter ( |& w| filter ( w) )
487- . try_for_each ( |window| window. emit_internal ( event. clone ( ) , payload. clone ( ) ) )
488- }
489- pub fn emit_filter < S : Serialize + Clone , F : Fn ( & Window < P > ) -> bool > (
490- & self ,
491- event : P :: Event ,
492- payload : Option < S > ,
493- filter : F ,
494- ) -> crate :: Result < ( ) > {
495+
496+ pub fn emit_filter < E , S , F > ( & self , event : & E , payload : Option < S > , filter : F ) -> crate :: Result < ( ) >
497+ where
498+ E : TagRef < P :: Event > + ?Sized ,
499+ S : Serialize + Clone ,
500+ F : Fn ( & Window < P > ) -> bool ,
501+ {
495502 self
496503 . windows_lock ( )
497504 . values ( )
498505 . filter ( |& w| filter ( w) )
499- . try_for_each ( |window| window. emit ( & event, payload. clone ( ) ) )
506+ . try_for_each ( |window| window. emit ( event, payload. clone ( ) ) )
500507 }
508+
501509 pub fn labels ( & self ) -> HashSet < P :: Label > {
502510 self . windows_lock ( ) . keys ( ) . cloned ( ) . collect ( )
503511 }
@@ -510,9 +518,15 @@ impl<P: Params> WindowManager<P> {
510518 pub fn unlisten ( & self , handler_id : EventHandler ) {
511519 self . inner . listeners . unlisten ( handler_id)
512520 }
513- pub fn trigger ( & self , event : P :: Event , window : Option < P :: Label > , data : Option < String > ) {
521+
522+ pub fn trigger < E > ( & self , event : & E , window : Option < P :: Label > , data : Option < String > )
523+ where
524+ E : TagRef < P :: Event > + ?Sized ,
525+ P :: Event : Borrow < E > ,
526+ {
514527 self . inner . listeners . trigger ( event, window, data)
515528 }
529+
516530 pub fn listen < F : Fn ( Event ) + Send + ' static > (
517531 & self ,
518532 event : P :: Event ,
@@ -563,9 +577,15 @@ impl<P: Params> WindowManager<P> {
563577 . expect ( "poisoned salt mutex" )
564578 . remove ( & uuid)
565579 }
566- pub fn get_window ( & self , label : & P :: Label ) -> Option < Window < P > > {
580+
581+ pub fn get_window < L > ( & self , label : & L ) -> Option < Window < P > >
582+ where
583+ L : TagRef < P :: Label > + ?Sized ,
584+ P :: Label : Borrow < L > ,
585+ {
567586 self . windows_lock ( ) . get ( label) . cloned ( )
568587 }
588+
569589 pub fn windows ( & self ) -> HashMap < P :: Label , Window < P > > {
570590 self . windows_lock ( ) . clone ( )
571591 }
0 commit comments