@@ -125,6 +125,16 @@ enum Target {
125125 Ios ,
126126}
127127
128+ impl Target {
129+ fn is_mobile ( & self ) -> bool {
130+ matches ! ( self , Target :: Android | Target :: Ios )
131+ }
132+
133+ fn is_desktop ( & self ) -> bool {
134+ !self . is_mobile ( )
135+ }
136+ }
137+
128138/// Build a `tauri::Context` for including in application code.
129139pub fn context_codegen ( data : ContextData ) -> Result < TokenStream , EmbeddedAssetsError > {
130140 let ContextData {
@@ -247,15 +257,15 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
247257 "icons/icon.ico" ,
248258 ) ;
249259 if icon_path. exists ( ) {
250- ico_icon ( & root, & out_dir, icon_path) ?
260+ ico_icon ( & root, & out_dir, icon_path) . map ( |i| quote ! ( :: std :: option :: Option :: Some ( #i ) ) ) ?
251261 } else {
252262 let icon_path = find_icon (
253263 & config,
254264 & config_parent,
255265 |i| i. ends_with ( ".png" ) ,
256266 "icons/icon.png" ,
257267 ) ;
258- png_icon ( & root, & out_dir, icon_path) ?
268+ png_icon ( & root, & out_dir, icon_path) . map ( |i| quote ! ( :: std :: option :: Option :: Some ( #i ) ) ) ?
259269 }
260270 } else if target == Target :: Linux {
261271 // handle default window icons for Linux targets
@@ -265,9 +275,9 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
265275 |i| i. ends_with ( ".png" ) ,
266276 "icons/icon.png" ,
267277 ) ;
268- png_icon ( & root, & out_dir, icon_path) ?
278+ png_icon ( & root, & out_dir, icon_path) . map ( |i| quote ! ( :: std :: option :: Option :: Some ( #i ) ) ) ?
269279 } else {
270- quote ! ( None )
280+ quote ! ( :: std :: option :: Option :: None )
271281 }
272282 } ;
273283
@@ -288,7 +298,7 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
288298 }
289299 raw_icon ( & out_dir, icon_path) ?
290300 } else {
291- quote ! ( None )
301+ quote ! ( :: std :: option :: Option :: None )
292302 } ;
293303
294304 let package_name = if let Some ( product_name) = & config. package . product_name {
@@ -312,20 +322,26 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
312322 }
313323 ) ;
314324
315- let system_tray_icon = if let Some ( tray) = & config. tauri . system_tray {
316- let system_tray_icon_path = config_parent. join ( & tray. icon_path ) ;
317- let ext = system_tray_icon_path. extension ( ) ;
318- if ext. map_or ( false , |e| e == "ico" ) {
319- ico_icon ( & root, & out_dir, system_tray_icon_path) ?
320- } else if ext. map_or ( false , |e| e == "png" ) {
321- png_icon ( & root, & out_dir, system_tray_icon_path) ?
325+ let with_system_tray_icon_code = if target. is_desktop ( ) {
326+ if let Some ( tray) = & config. tauri . system_tray {
327+ let system_tray_icon_path = config_parent. join ( & tray. icon_path ) ;
328+ let ext = system_tray_icon_path. extension ( ) ;
329+ if ext. map_or ( false , |e| e == "ico" ) {
330+ ico_icon ( & root, & out_dir, system_tray_icon_path)
331+ . map ( |i| quote ! ( context. set_system_tray_icon( #i) ; ) ) ?
332+ } else if ext. map_or ( false , |e| e == "png" ) {
333+ png_icon ( & root, & out_dir, system_tray_icon_path)
334+ . map ( |i| quote ! ( context. set_system_tray_icon( #i) ; ) ) ?
335+ } else {
336+ quote ! ( compile_error!(
337+ "The tray icon extension must be either `.ico` or `.png`."
338+ ) )
339+ }
322340 } else {
323- quote ! ( compile_error!(
324- "The tray icon extension must be either `.ico` or `.png`."
325- ) )
341+ quote ! ( )
326342 }
327343 } else {
328- quote ! ( None )
344+ quote ! ( )
329345 } ;
330346
331347 #[ cfg( target_os = "macos" ) ]
@@ -409,50 +425,52 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
409425 } ;
410426
411427 #[ cfg( feature = "shell-scope" ) ]
412- let shell_scope_config = {
428+ let with_shell_scope_code = {
413429 use regex:: Regex ;
414430 use tauri_utils:: config:: ShellAllowlistOpen ;
415431
416432 let shell_scopes = get_allowed_clis ( & root, & config. tauri . allowlist . shell . scope ) ;
417433
418- let shell_scope_open = match & config. tauri . allowlist . shell . open {
419- ShellAllowlistOpen :: Flag ( false ) => quote ! ( :: std:: option:: Option :: None ) ,
420- ShellAllowlistOpen :: Flag ( true ) => {
421- quote ! ( :: std:: option:: Option :: Some ( #root:: regex:: Regex :: new( r#"^((mailto:\w+)|(tel:\w+)|(https?://\w+)).+"# ) . unwrap( ) ) )
422- }
434+ let shell_scope_constructor = match & config. tauri . allowlist . shell . open {
435+ ShellAllowlistOpen :: Flag ( false ) => quote ! ( #root:: ShellScopeConfig :: new( ) . skip_validation( ) ) ,
436+ ShellAllowlistOpen :: Flag ( true ) => quote ! ( #root:: ShellScopeConfig :: new( ) ) ,
423437 ShellAllowlistOpen :: Validate ( regex) => match Regex :: new ( regex) {
424- Ok ( _) => quote ! ( :: std:: option:: Option :: Some ( #root:: regex:: Regex :: new( #regex) . unwrap( ) ) ) ,
438+ Ok ( _) => {
439+ quote ! ( #root:: ShellScopeConfig :: with_validator( #root:: regex:: Regex :: new( #regex) . unwrap( ) ) )
440+ }
425441 Err ( error) => {
426442 let error = error. to_string ( ) ;
427443 quote ! ( {
428444 compile_error!( #error) ;
429- :: std :: option :: Option :: Some ( #root:: regex:: Regex :: new( #regex) . unwrap( ) )
445+ #root :: ShellScopeConfig :: with_validator ( #root:: regex:: Regex :: new( #regex) . unwrap( ) )
430446 } )
431447 }
432448 } ,
433449 _ => panic ! ( "unknown shell open format, unable to prepare" ) ,
434450 } ;
451+ let shell_scope = quote ! ( #shell_scope_constructor. set_allowed_commands( #shell_scopes) ) ;
435452
436- quote ! ( #root:: ShellScopeConfig {
437- open: #shell_scope_open,
438- scopes: #shell_scopes
439- } )
453+ quote ! ( context. set_shell_scope( #shell_scope) ; )
440454 } ;
441455
442456 #[ cfg( not( feature = "shell-scope" ) ) ]
443- let shell_scope_config = quote ! ( ) ;
444-
445- Ok ( quote ! ( #root:: Context :: new(
446- #config,
447- :: std:: sync:: Arc :: new( #assets) ,
448- #default_window_icon,
449- #app_icon,
450- #system_tray_icon,
451- #package_info,
452- #info_plist,
453- #pattern,
454- #shell_scope_config
455- ) ) )
457+ let with_shell_scope_code = quote ! ( ) ;
458+
459+ Ok ( quote ! ( {
460+ #[ allow( unused_mut, clippy:: let_and_return) ]
461+ let mut context = #root:: Context :: new(
462+ #config,
463+ :: std:: sync:: Arc :: new( #assets) ,
464+ #default_window_icon,
465+ #app_icon,
466+ #package_info,
467+ #info_plist,
468+ #pattern,
469+ ) ;
470+ #with_system_tray_icon_code
471+ #with_shell_scope_code
472+ context
473+ } ) )
456474}
457475
458476fn ico_icon < P : AsRef < Path > > (
@@ -483,7 +501,7 @@ fn ico_icon<P: AsRef<Path>>(
483501
484502 let out_path = out_path. display ( ) . to_string ( ) ;
485503
486- let icon = quote ! ( Some ( #root:: Icon :: Rgba { rgba: include_bytes!( #out_path) . to_vec( ) , width: #width, height: #height } ) ) ;
504+ let icon = quote ! ( #root:: Icon :: Rgba { rgba: include_bytes!( #out_path) . to_vec( ) , width: #width, height: #height } ) ;
487505 Ok ( icon)
488506}
489507
@@ -501,7 +519,9 @@ fn raw_icon<P: AsRef<Path>>(out_dir: &Path, path: P) -> Result<TokenStream, Embe
501519
502520 let out_path = out_path. display ( ) . to_string ( ) ;
503521
504- let icon = quote ! ( Some ( include_bytes!( #out_path) . to_vec( ) ) ) ;
522+ let icon = quote ! ( :: std:: option:: Option :: Some (
523+ include_bytes!( #out_path) . to_vec( )
524+ ) ) ;
505525 Ok ( icon)
506526}
507527
@@ -533,7 +553,7 @@ fn png_icon<P: AsRef<Path>>(
533553
534554 let out_path = out_path. display ( ) . to_string ( ) ;
535555
536- let icon = quote ! ( Some ( #root:: Icon :: Rgba { rgba: include_bytes!( #out_path) . to_vec( ) , width: #width, height: #height } ) ) ;
556+ let icon = quote ! ( #root:: Icon :: Rgba { rgba: include_bytes!( #out_path) . to_vec( ) , width: #width, height: #height } ) ;
537557 Ok ( icon)
538558}
539559
0 commit comments