add option to configure global script behavior

pull/2019/head
amrbashir 8 months ago
parent 955697a83e
commit 23143e93fe
No known key found for this signature in database
GPG Key ID: BBD7A47A2003FF33

2
Cargo.lock generated

@ -6714,7 +6714,7 @@ dependencies = [
"thiserror 2.0.3",
"url",
"windows 0.58.0",
"zbus",
"zbus 4.4.0",
]
[[package]]

@ -4,10 +4,7 @@
use std::path::Path;
use tauri::{
plugin::{Builder, TauriPlugin},
Manager, Runtime,
};
use tauri::{plugin::TauriPlugin, Manager, Runtime};
#[cfg(mobile)]
use tauri::plugin::PluginHandle;
@ -107,28 +104,66 @@ impl<R: Runtime, T: Manager<R>> crate::OpenerExt<R> for T {
}
}
/// The opener plugin Builder.
pub struct Builder {
open_js_links_on_click: bool,
}
impl Default for Builder {
fn default() -> Self {
Self {
open_js_links_on_click: true,
}
}
}
impl Builder {
/// Create a new opener plugin Builder.
pub fn new() -> Self {
Self::default()
}
/// Whether the plugin should inject a JS script to open URLs in default browser
/// when clicking on `<a>` elements that has `_blank` target, or when pressing `Ctrl` or `Shift` while clicking it.
///
/// Enabled by default for `http:`, `https:`, `mailto:`, `tel:` links.
pub fn open_js_links_on_click(mut self, open: bool) -> Self {
self.open_js_links_on_click = open;
self
}
/// Build and Initializes the plugin.
pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
let mut builder = tauri::plugin::Builder::new("opener")
.setup(|app, _api| {
#[cfg(target_os = "android")]
let handle = _api.register_android_plugin(PLUGIN_IDENTIFIER, "OpenerPlugin")?;
#[cfg(target_os = "ios")]
let handle = _api.register_ios_plugin(init_plugin_opener)?;
app.manage(Opener {
#[cfg(not(mobile))]
_marker: std::marker::PhantomData::<fn() -> R>,
#[cfg(mobile)]
mobile_plugin_handle: handle,
});
Ok(())
})
.invoke_handler(tauri::generate_handler![
commands::open_url,
commands::open_path,
commands::reveal_item_in_dir
]);
if self.open_js_links_on_click {
builder = builder.js_init_script(include_str!("init-iife.js").to_string());
}
builder.build()
}
}
/// Initializes the plugin.
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("opener")
.js_init_script(include_str!("init-iife.js").to_string())
.setup(|app, _api| {
#[cfg(target_os = "android")]
let handle = _api.register_android_plugin(PLUGIN_IDENTIFIER, "OpenerPlugin")?;
#[cfg(target_os = "ios")]
let handle = _api.register_ios_plugin(init_plugin_opener)?;
app.manage(Opener {
#[cfg(not(mobile))]
_marker: std::marker::PhantomData::<fn() -> R>,
#[cfg(mobile)]
mobile_plugin_handle: handle,
});
Ok(())
})
.invoke_handler(tauri::generate_handler![
commands::open_url,
commands::open_path,
commands::reveal_item_in_dir
])
.build()
Builder::default().build()
}

Loading…
Cancel
Save