@@ -192,10 +192,12 @@ pub type SyncTask = Box<dyn FnOnce() + Send>;
192192
193193use serde:: Serialize ;
194194use std:: {
195+ borrow:: Cow ,
195196 collections:: HashMap ,
196197 fmt:: { self , Debug } ,
197198 sync:: MutexGuard ,
198199} ;
200+ use utils:: assets:: { AssetKey , CspHash , EmbeddedAssets } ;
199201
200202#[ cfg( feature = "wry" ) ]
201203#[ cfg_attr( docsrs, doc( cfg( feature = "wry" ) ) ) ]
@@ -224,7 +226,6 @@ pub use {
224226 } ,
225227 self :: state:: { State , StateManager } ,
226228 self :: utils:: {
227- assets:: Assets ,
228229 config:: { Config , WebviewUrl } ,
229230 Env , PackageInfo , Theme ,
230231 } ,
@@ -338,14 +339,47 @@ pub fn dev() -> bool {
338339 !cfg ! ( feature = "custom-protocol" )
339340}
340341
342+ /// Represents a container of file assets that are retrievable during runtime.
343+ pub trait Assets < R : Runtime > : Send + Sync + ' static {
344+ /// Initialize the asset provider.
345+ fn setup ( & self , app : & App < R > ) {
346+ let _ = app;
347+ }
348+
349+ /// Get the content of the passed [`AssetKey`].
350+ fn get ( & self , key : & AssetKey ) -> Option < Cow < ' _ , [ u8 ] > > ;
351+
352+ /// Iterator for the assets.
353+ fn iter ( & self ) -> Box < dyn Iterator < Item = ( & str , & [ u8 ] ) > + ' _ > ;
354+
355+ /// Gets the hashes for the CSP tag of the HTML on the given path.
356+ fn csp_hashes ( & self , html_path : & AssetKey ) -> Box < dyn Iterator < Item = CspHash < ' _ > > + ' _ > ;
357+ }
358+
359+ impl < R : Runtime > Assets < R > for EmbeddedAssets {
360+ fn get ( & self , key : & AssetKey ) -> Option < Cow < ' _ , [ u8 ] > > {
361+ EmbeddedAssets :: get ( self , key)
362+ }
363+
364+ fn iter ( & self ) -> Box < dyn Iterator < Item = ( & str , & [ u8 ] ) > + ' _ > {
365+ EmbeddedAssets :: iter ( self )
366+ }
367+
368+ fn csp_hashes ( & self , html_path : & AssetKey ) -> Box < dyn Iterator < Item = CspHash < ' _ > > + ' _ > {
369+ EmbeddedAssets :: csp_hashes ( self , html_path)
370+ }
371+ }
372+
341373/// User supplied data required inside of a Tauri application.
342374///
343375/// # Stability
344376/// This is the output of the [`generate_context`] macro, and is not considered part of the stable API.
345377/// Unless you know what you are doing and are prepared for this type to have breaking changes, do not create it yourself.
346- pub struct Context {
378+ #[ tauri_macros:: default_runtime( Wry , wry) ]
379+ pub struct Context < R : Runtime > {
347380 pub ( crate ) config : Config ,
348- pub ( crate ) assets : Box < dyn Assets > ,
381+ /// Asset provider.
382+ pub assets : Box < dyn Assets < R > > ,
349383 pub ( crate ) default_window_icon : Option < image:: Image < ' static > > ,
350384 pub ( crate ) app_icon : Option < Vec < u8 > > ,
351385 #[ cfg( all( desktop, feature = "tray-icon" ) ) ]
@@ -356,7 +390,7 @@ pub struct Context {
356390 pub ( crate ) runtime_authority : RuntimeAuthority ,
357391}
358392
359- impl fmt:: Debug for Context {
393+ impl < R : Runtime > fmt:: Debug for Context < R > {
360394 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
361395 let mut d = f. debug_struct ( "Context" ) ;
362396 d. field ( "config" , & self . config )
@@ -372,7 +406,7 @@ impl fmt::Debug for Context {
372406 }
373407}
374408
375- impl Context {
409+ impl < R : Runtime > Context < R > {
376410 /// The config the application was prepared with.
377411 #[ inline( always) ]
378412 pub fn config ( & self ) -> & Config {
@@ -387,14 +421,14 @@ impl Context {
387421
388422 /// The assets to be served directly by Tauri.
389423 #[ inline( always) ]
390- pub fn assets ( & self ) -> & dyn Assets {
424+ pub fn assets ( & self ) -> & dyn Assets < R > {
391425 self . assets . as_ref ( )
392426 }
393427
394- /// A mutable reference to the assets to be served directly by Tauri .
428+ /// Replace the [`Assets`] implementation and returns the previous value so you can use it as a fallback if desired .
395429 #[ inline( always) ]
396- pub fn assets_mut ( & mut self ) -> & mut Box < dyn Assets > {
397- & mut self . assets
430+ pub fn set_assets ( & mut self , assets : Box < dyn Assets < R > > ) -> Box < dyn Assets < R > > {
431+ std :: mem :: replace ( & mut self . assets , assets )
398432 }
399433
400434 /// The default window icon Tauri should use when creating windows.
@@ -459,7 +493,7 @@ impl Context {
459493 #[ allow( clippy:: too_many_arguments) ]
460494 pub fn new (
461495 config : Config ,
462- assets : Box < dyn Assets > ,
496+ assets : Box < dyn Assets < R > > ,
463497 default_window_icon : Option < image:: Image < ' static > > ,
464498 app_icon : Option < Vec < u8 > > ,
465499 package_info : PackageInfo ,
0 commit comments