|
@@ -125,6 +125,16 @@ enum Target {
|
|
|
Ios,
|
|
|
}
|
|
|
|
|
|
+impl Target {
|
|
|
+ fn is_mobile(&self) -> bool {
|
|
|
+ matches!(self, Target::Android | Target::Ios)
|
|
|
+ }
|
|
|
+
|
|
|
+ fn is_desktop(&self) -> bool {
|
|
|
+ !self.is_mobile()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
/// Build a `tauri::Context` for including in application code.
|
|
|
pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsError> {
|
|
|
let ContextData {
|
|
@@ -247,7 +257,7 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
|
|
|
"icons/icon.ico",
|
|
|
);
|
|
|
if icon_path.exists() {
|
|
|
- ico_icon(&root, &out_dir, icon_path)?
|
|
|
+ ico_icon(&root, &out_dir, icon_path).map(|i| quote!(::std::option::Option::Some(#i)))?
|
|
|
} else {
|
|
|
let icon_path = find_icon(
|
|
|
&config,
|
|
@@ -255,7 +265,7 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
|
|
|
|i| i.ends_with(".png"),
|
|
|
"icons/icon.png",
|
|
|
);
|
|
|
- png_icon(&root, &out_dir, icon_path)?
|
|
|
+ png_icon(&root, &out_dir, icon_path).map(|i| quote!(::std::option::Option::Some(#i)))?
|
|
|
}
|
|
|
} else if target == Target::Linux {
|
|
|
// handle default window icons for Linux targets
|
|
@@ -265,9 +275,9 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
|
|
|
|i| i.ends_with(".png"),
|
|
|
"icons/icon.png",
|
|
|
);
|
|
|
- png_icon(&root, &out_dir, icon_path)?
|
|
|
+ png_icon(&root, &out_dir, icon_path).map(|i| quote!(::std::option::Option::Some(#i)))?
|
|
|
} else {
|
|
|
- quote!(None)
|
|
|
+ quote!(::std::option::Option::None)
|
|
|
}
|
|
|
};
|
|
|
|
|
@@ -288,7 +298,7 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
|
|
|
}
|
|
|
raw_icon(&out_dir, icon_path)?
|
|
|
} else {
|
|
|
- quote!(None)
|
|
|
+ quote!(::std::option::Option::None)
|
|
|
};
|
|
|
|
|
|
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
|
|
|
}
|
|
|
);
|
|
|
|
|
|
- let system_tray_icon = if let Some(tray) = &config.tauri.system_tray {
|
|
|
- let system_tray_icon_path = config_parent.join(&tray.icon_path);
|
|
|
- let ext = system_tray_icon_path.extension();
|
|
|
- if ext.map_or(false, |e| e == "ico") {
|
|
|
- ico_icon(&root, &out_dir, system_tray_icon_path)?
|
|
|
- } else if ext.map_or(false, |e| e == "png") {
|
|
|
- png_icon(&root, &out_dir, system_tray_icon_path)?
|
|
|
+ let with_system_tray_icon_code = if target.is_desktop() {
|
|
|
+ if let Some(tray) = &config.tauri.system_tray {
|
|
|
+ let system_tray_icon_path = config_parent.join(&tray.icon_path);
|
|
|
+ let ext = system_tray_icon_path.extension();
|
|
|
+ if ext.map_or(false, |e| e == "ico") {
|
|
|
+ ico_icon(&root, &out_dir, system_tray_icon_path)
|
|
|
+ .map(|i| quote!(context.set_system_tray_icon(#i);))?
|
|
|
+ } else if ext.map_or(false, |e| e == "png") {
|
|
|
+ png_icon(&root, &out_dir, system_tray_icon_path)
|
|
|
+ .map(|i| quote!(context.set_system_tray_icon(#i);))?
|
|
|
+ } else {
|
|
|
+ quote!(compile_error!(
|
|
|
+ "The tray icon extension must be either `.ico` or `.png`."
|
|
|
+ ))
|
|
|
+ }
|
|
|
} else {
|
|
|
- quote!(compile_error!(
|
|
|
- "The tray icon extension must be either `.ico` or `.png`."
|
|
|
- ))
|
|
|
+ quote!()
|
|
|
}
|
|
|
} else {
|
|
|
- quote!(None)
|
|
|
+ quote!()
|
|
|
};
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
@@ -409,50 +425,52 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
|
|
|
};
|
|
|
|
|
|
#[cfg(feature = "shell-scope")]
|
|
|
- let shell_scope_config = {
|
|
|
+ let with_shell_scope_code = {
|
|
|
use regex::Regex;
|
|
|
use tauri_utils::config::ShellAllowlistOpen;
|
|
|
|
|
|
let shell_scopes = get_allowed_clis(&root, &config.tauri.allowlist.shell.scope);
|
|
|
|
|
|
- let shell_scope_open = match &config.tauri.allowlist.shell.open {
|
|
|
- ShellAllowlistOpen::Flag(false) => quote!(::std::option::Option::None),
|
|
|
- ShellAllowlistOpen::Flag(true) => {
|
|
|
- quote!(::std::option::Option::Some(#root::regex::Regex::new(r#"^((mailto:\w+)|(tel:\w+)|(https?://\w+)).+"#).unwrap()))
|
|
|
- }
|
|
|
+ let shell_scope_constructor = match &config.tauri.allowlist.shell.open {
|
|
|
+ ShellAllowlistOpen::Flag(false) => quote!(#root::ShellScopeConfig::new().skip_validation()),
|
|
|
+ ShellAllowlistOpen::Flag(true) => quote!(#root::ShellScopeConfig::new()),
|
|
|
ShellAllowlistOpen::Validate(regex) => match Regex::new(regex) {
|
|
|
- Ok(_) => quote!(::std::option::Option::Some(#root::regex::Regex::new(#regex).unwrap())),
|
|
|
+ Ok(_) => {
|
|
|
+ quote!(#root::ShellScopeConfig::with_validator(#root::regex::Regex::new(#regex).unwrap()))
|
|
|
+ }
|
|
|
Err(error) => {
|
|
|
let error = error.to_string();
|
|
|
quote!({
|
|
|
compile_error!(#error);
|
|
|
- ::std::option::Option::Some(#root::regex::Regex::new(#regex).unwrap())
|
|
|
+ #root::ShellScopeConfig::with_validator(#root::regex::Regex::new(#regex).unwrap())
|
|
|
})
|
|
|
}
|
|
|
},
|
|
|
_ => panic!("unknown shell open format, unable to prepare"),
|
|
|
};
|
|
|
+ let shell_scope = quote!(#shell_scope_constructor.set_allowed_commands(#shell_scopes));
|
|
|
|
|
|
- quote!(#root::ShellScopeConfig {
|
|
|
- open: #shell_scope_open,
|
|
|
- scopes: #shell_scopes
|
|
|
- })
|
|
|
+ quote!(context.set_shell_scope(#shell_scope);)
|
|
|
};
|
|
|
|
|
|
#[cfg(not(feature = "shell-scope"))]
|
|
|
- let shell_scope_config = quote!();
|
|
|
-
|
|
|
- Ok(quote!(#root::Context::new(
|
|
|
- #config,
|
|
|
- ::std::sync::Arc::new(#assets),
|
|
|
- #default_window_icon,
|
|
|
- #app_icon,
|
|
|
- #system_tray_icon,
|
|
|
- #package_info,
|
|
|
- #info_plist,
|
|
|
- #pattern,
|
|
|
- #shell_scope_config
|
|
|
- )))
|
|
|
+ let with_shell_scope_code = quote!();
|
|
|
+
|
|
|
+ Ok(quote!({
|
|
|
+ #[allow(unused_mut, clippy::let_and_return)]
|
|
|
+ let mut context = #root::Context::new(
|
|
|
+ #config,
|
|
|
+ ::std::sync::Arc::new(#assets),
|
|
|
+ #default_window_icon,
|
|
|
+ #app_icon,
|
|
|
+ #package_info,
|
|
|
+ #info_plist,
|
|
|
+ #pattern,
|
|
|
+ );
|
|
|
+ #with_system_tray_icon_code
|
|
|
+ #with_shell_scope_code
|
|
|
+ context
|
|
|
+ }))
|
|
|
}
|
|
|
|
|
|
fn ico_icon<P: AsRef<Path>>(
|
|
@@ -483,7 +501,7 @@ fn ico_icon<P: AsRef<Path>>(
|
|
|
|
|
|
let out_path = out_path.display().to_string();
|
|
|
|
|
|
- let icon = quote!(Some(#root::Icon::Rgba { rgba: include_bytes!(#out_path).to_vec(), width: #width, height: #height }));
|
|
|
+ let icon = quote!(#root::Icon::Rgba { rgba: include_bytes!(#out_path).to_vec(), width: #width, height: #height });
|
|
|
Ok(icon)
|
|
|
}
|
|
|
|
|
@@ -501,7 +519,9 @@ fn raw_icon<P: AsRef<Path>>(out_dir: &Path, path: P) -> Result<TokenStream, Embe
|
|
|
|
|
|
let out_path = out_path.display().to_string();
|
|
|
|
|
|
- let icon = quote!(Some(include_bytes!(#out_path).to_vec()));
|
|
|
+ let icon = quote!(::std::option::Option::Some(
|
|
|
+ include_bytes!(#out_path).to_vec()
|
|
|
+ ));
|
|
|
Ok(icon)
|
|
|
}
|
|
|
|
|
@@ -533,7 +553,7 @@ fn png_icon<P: AsRef<Path>>(
|
|
|
|
|
|
let out_path = out_path.display().to_string();
|
|
|
|
|
|
- let icon = quote!(Some(#root::Icon::Rgba { rgba: include_bytes!(#out_path).to_vec(), width: #width, height: #height }));
|
|
|
+ let icon = quote!(#root::Icon::Rgba { rgba: include_bytes!(#out_path).to_vec(), width: #width, height: #height });
|
|
|
Ok(icon)
|
|
|
}
|
|
|
|