From a227590658ffd7aba829f21def1aeac70dd64c1f Mon Sep 17 00:00:00 2001 From: Tunglies Date: Tue, 20 May 2025 01:24:03 +0800 Subject: [PATCH] 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. --- plugins/autostart/README.md | 6 +++- plugins/autostart/src/lib.rs | 68 +++++++++++++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/plugins/autostart/README.md b/plugins/autostart/README.md index bae2802a..09c969da 100644 --- a/plugins/autostart/README.md +++ b/plugins/autostart/README.md @@ -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"); } diff --git a/plugins/autostart/src/lib.rs b/plugins/autostart/src/lib.rs index 83cac89c..dabb2e9b 100644 --- a/plugins/autostart/src/lib.rs +++ b/plugins/autostart/src/lib.rs @@ -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(&self, app: &AppHandle) -> 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, + 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(self) -> TauriPlugin { 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( #[allow(unused)] macos_launcher: MacosLauncher, args: Option>, + prefered_name: Option ) -> TauriPlugin { let mut builder = Builder::new(); if let Some(args) = args { @@ -225,5 +284,6 @@ pub fn init( { builder = builder.macos_launcher(macos_launcher); } + builder = builder.prefered_name(prefered_name.unwrap_or_default()); builder.build() }