Skip to content

Instantly share code, notes, and snippets.

@p1mo
Last active February 17, 2024 23:10
Show Gist options
  • Save p1mo/f37745df20d294e93ed42e03073e8277 to your computer and use it in GitHub Desktop.
Save p1mo/f37745df20d294e93ed42e03073e8277 to your computer and use it in GitHub Desktop.
wrap tauri's .plugin() into platform and os specific functions like .windows_plugin()
pub trait EnhancePlugin<R: tauri::Runtime> {
fn plugin_windows<P: tauri::plugin::Plugin<R> + 'static>(self, plugin: P) -> Self;
fn plugin_linux<P: tauri::plugin::Plugin<R> + 'static>(self, plugin: P) -> Self;
fn plugin_macos<P: tauri::plugin::Plugin<R> + 'static>(self, plugin: P) -> Self;
fn plugin_android<P: tauri::plugin::Plugin<R> + 'static>(self, plugin: P) -> Self;
fn plugin_ios<P: tauri::plugin::Plugin<R> + 'static>(self, plugin: P) -> Self;
fn plugin_desktop<P: tauri::plugin::Plugin<R> + 'static>(self, plugin: P) -> Self;
fn plugin_mobile<P: tauri::plugin::Plugin<R> + 'static>(self, plugin: P) -> Self;
}
impl<R: tauri::Runtime> EnhancePlugin<R> for tauri::Builder<R> {
/// same behavior like `.plugin()` but skips on non windows builds
fn plugin_windows<P: tauri::plugin::Plugin<R> + 'static>(self, #[allow(unused)] plugin: P) -> Self {
#[cfg(target_os = "windows")]
return self.plugin(plugin);
#[cfg(not(target_os = "windows"))]
self
}
/// same behavior like `.plugin()` but skips on non linux builds
fn plugin_linux<P: tauri::plugin::Plugin<R> + 'static>(self, #[allow(unused)] plugin: P) -> Self {
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
return self.plugin(plugin);
#[cfg(not(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
)))]
self
}
/// same behavior like `.plugin()` but skips on non macos builds
fn plugin_macos<P: tauri::plugin::Plugin<R> + 'static>(self, #[allow(unused)] plugin: P) -> Self {
#[cfg(target_os = "macos")]
return self.plugin(plugin);
#[cfg(not(target_os = "macos"))]
self
}
/// same behavior like `.plugin()` but skips on non android builds
fn plugin_android<P: tauri::plugin::Plugin<R> + 'static>(self, #[allow(unused)] plugin: P) -> Self {
#[cfg(target_os = "android")]
return self.plugin(plugin);
#[cfg(not(target_os = "android"))]
self
}
/// same behavior like `.plugin()` but skips on non ios builds
fn plugin_ios<P: tauri::plugin::Plugin<R> + 'static>(self, #[allow(unused)] plugin: P) -> Self {
#[cfg(target_os = "ios")]
return self.plugin(plugin);
#[cfg(not(target_os = "ios"))]
self
}
/// same behavior like `.plugin()` but skips on non desktop builds and ignores `target_os`
fn plugin_desktop<P: tauri::plugin::Plugin<R> + 'static>(self, #[allow(unused)] plugin: P) -> Self {
#[cfg(desktop)]
return self.plugin(plugin);
#[cfg(not(desktop))]
self
}
/// same behavior like `.plugin()` but skips on non mobile builds and ignores `target_os`
fn plugin_mobile<P: tauri::plugin::Plugin<R> + 'static>(self, #[allow(unused)] plugin: P) -> Self {
#[cfg(mobile)]
return self.plugin(plugin);
#[cfg(not(mobile))]
self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment