feat(autostart): add prefered name configuration for auto start entry

- Add PreferedName enum to support different naming strategies
- Implement Default trait for PreferedName with PackageInfoName as default
- Add prefered_name builder method with documentation and examples
- Integrate prefered name into auto start configuration

This change allows users to customize the name of their application in the
auto start entry using various naming strategies like package info name,
crate name, config identifier, or custom name.
pull/2707/head
Tunglies 2 months ago
parent 6c9e08dccb
commit a227590658

@ -57,9 +57,13 @@ First you need to register the core plugin with Tauri:
`src-tauri/src/lib.rs`
```rust
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_autostart::Builder::new().args((["--flag1", "--flag2"])).build()))
.plugin(tauri_plugin_autostart::Builder::new()
.args(["--flag1", "--flag2"])
.prefered_name(tauri_plugin_autostart::PreferedName::PackageInfoName)
.build())
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

@ -13,9 +13,7 @@
use auto_launch::{AutoLaunch, AutoLaunchBuilder};
use serde::{ser::Serializer, Serialize};
use tauri::{
command,
plugin::{Builder as PluginBuilder, TauriPlugin},
Manager, Runtime, State,
command, plugin::{Builder as PluginBuilder, TauriPlugin}, AppHandle, Manager, Runtime, State
};
use std::env::current_exe;
@ -29,6 +27,16 @@ pub enum MacosLauncher {
AppleScript,
}
#[derive(Debug, Clone)]
pub enum PreferedName {
PackageInfoName,
PackageInfoCrateName,
ConfigIdentifier,
ConfigProductName,
Custom(String)
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
@ -46,6 +54,32 @@ impl Serialize for Error {
}
}
impl Default for PreferedName {
fn default() -> Self {
PreferedName::PackageInfoName
}
}
impl PreferedName {
pub fn get_value<R: Runtime>(&self, app: &AppHandle<R>) -> String {
match self {
PreferedName::PackageInfoName => {
app.package_info().name.to_string()
},
PreferedName::PackageInfoCrateName => {
app.package_info().crate_name.to_string()
},
PreferedName::ConfigIdentifier => {
app.config().identifier.to_string()
},
PreferedName::ConfigProductName => {
app.config().product_name.clone().unwrap_or("TauriAPP".to_string())
},
PreferedName::Custom(s) => s.clone(),
}
}
}
pub struct AutoLaunchManager(AutoLaunch);
impl AutoLaunchManager {
@ -103,6 +137,7 @@ pub struct Builder {
#[cfg(target_os = "macos")]
macos_launcher: MacosLauncher,
args: Vec<String>,
prefered_name: PreferedName,
}
impl Builder {
@ -154,12 +189,35 @@ impl Builder {
self
}
/// Sets the preferred name to be used for the auto start entry.
///
/// ## Examples
///
/// ```no_run
/// Builder::new()
/// .prefered_name(PreferedName::PackageInfoName)
/// .build();
/// ```
///
/// ```no_run
/// Builder::new()
/// .prefered_name(PreferedName::Custom("My Custom Name".into()))
/// .build();
/// ```
pub fn prefered_name(mut self, prefered_name: PreferedName) -> Self {
self.prefered_name = prefered_name;
self
}
pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
PluginBuilder::new("autostart")
.invoke_handler(tauri::generate_handler![enable, disable, is_enabled])
.setup(move |app, _api| {
let mut builder = AutoLaunchBuilder::new();
builder.set_app_name(&app.package_info().name);
let prefered_name = self.prefered_name.get_value(&app);
builder.set_app_name(&prefered_name);
builder.set_args(&self.args);
let current_exe = current_exe()?;
@ -216,6 +274,7 @@ impl Builder {
pub fn init<R: Runtime>(
#[allow(unused)] macos_launcher: MacosLauncher,
args: Option<Vec<&'static str>>,
prefered_name: Option<PreferedName>
) -> TauriPlugin<R> {
let mut builder = Builder::new();
if let Some(args) = args {
@ -225,5 +284,6 @@ pub fn init<R: Runtime>(
{
builder = builder.macos_launcher(macos_launcher);
}
builder = builder.prefered_name(prefered_name.unwrap_or_default());
builder.build()
}

Loading…
Cancel
Save