Skip to content

Conversation

Tunglies
Copy link
Contributor

@Tunglies Tunglies commented May 19, 2025

Add Prefered Name Configuration for AutoStart Entry

This PR introduces the ability to customize the application name used in auto start entries by adding a PreferedName enum to support different naming strategies.

Key Changes:

  • Added PreferedName enum with variants:
    • PackageInfoName (default) – Uses name from Tauri's package info
    • PackageInfoCrateName – Uses the crate name from Cargo.toml
    • ConfigIdentifier – Uses the identifier from Tauri's runtime config
    • ConfigProductName Uses the product_name from Tauri's runtime config
    • Custom(String) – Allows specifying a custom name
  • Implemented Default trait for PreferedName (defaults to PackageInfoName)
  • Added prefered_name builder method with documentation and usage examples
  • Integrated prefered name selection into auto start configuration

Usage Example:

Builder::new()
     .prefered_name(PreferedName::PackageInfoName)
     .build();

Builder::new()
    .prefered_name(PreferedName::Custom("My Custom Name".into()))
     .build();

This enhancement provides flexibility for apps that need to control their display name in system auto start entries, supporting use cases like branding adjustments or multi-instance differentiation.

- 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.
@Tunglies Tunglies requested a review from a team as a code owner May 19, 2025 17:35
Copy link
Contributor

github-actions bot commented May 19, 2025

Package Changes Through c5b28c0

There are 2 changes which include autostart with minor, autostart-js with minor

Planned Package Versions

The following package releases are the planned based on the context of changes in this pull request.

package current next
autostart 2.3.0 2.4.0
autostart-js 2.3.0 2.4.0

Add another change file through the GitHub UI by following this link.


Read about change files or the docs at github.com/jbolda/covector

…ted logic

- Change prefered_name to app_name in the Builder struct and methods
- Update PreferedName enum to include new variants and set AppName as default
- Modify documentation to reflect the new app_name usage for auto start entry

This refactor improves clarity in naming conventions and enhances the configuration options for auto start entries.
@Tunglies
Copy link
Contributor Author

Thanks @Legend-Master for review.

I kept the original init api which not brings a breaking change.

pub fn init<R: Runtime>(
    #[allow(unused)] macos_launcher: MacosLauncher,
    args: Option<Vec<&'static str>>,
)

To setup a app name with builder method

fn main() {
    tauri::Builder::default()
        .setup(|app| {
            app.app_handle().plugin(
                tauri_plugin_autostart::Builder::new()
                    .app_name(PreferedName::CrateName)
                    // .app_name("MyTauriAPP")
                    .build(),
            )?;

            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

It supports 5 naming variants: default (AppName) (package.json), CrateName (Cargo.toml), Identifier and ProductName (Tauri config), and a customizable String override.

#[derive(Default)]
pub enum PreferedName {
   #[default]
   AppName,
   CrateName,
   Identifier,
   ProductName,
   Custom(String),
}

The extra variants improve configuration file synchronization, which is a useful feature.

@Legend-Master
Copy link
Contributor

The extra variants improve configuration file synchronization, which is a useful feature.

I can see it's more ergonomic to use when you need them, but I can't see when would you want to use them to be honest, for the crate name, you can just do .app_name(env!("CARGO_CRATE_NAME")), for the id, normally we should use a display name instead of a code name, and for the product name, isn't it just the app name but fallback to TauriAPP?

@Tunglies
Copy link
Contributor Author

Tunglies commented May 20, 2025

The extra variants improve configuration file synchronization, which is a useful feature.

I can see it's more ergonomic to use when you need them, but I can't see when would you want to use them to be honest, for the crate name, you can just do .app_name(env!("CARGO_CRATE_NAME")), for the id, normally we should use a display name instead of a code name, and for the product name, isn't it just the app name but fallback to TauriAPP?

The PreferedName::CrateName variant is redundant since it can be directly achieved via .app_name(env!("CARGO_CRATE_NAME")), making it removable.

On MacOS, the plist file path is generated based on app_name, resulting in /Users/user/Library/LaunchAgents/{app_name}.plist .
https://github.com/zzzgydi/auto-launch/blob/fedaeba7a5ee6a5124b056907e03c399acd6df27/src/macos.rs#L179-L181

and for LaunchAgent, the Label field typically uses the app's identifier for unique identification. Applications control their LaunchAgent naming through the CFBundleIdentifier specified in Info.plist .
https://github.com/zzzgydi/auto-launch/blob/fedaeba7a5ee6a5124b056907e03c399acd6df27/src/macos.rs#L95-L111

The Config structure is defines product_name: Option<String> so I added fallback just in case.
https://github.com/tauri-apps/tauri/blob/dbcfaa18d7a0a4b41fbdde5c16616cce1b702ac6/crates/tauri-utils/src/config.rs#L2941-L2948


#[derive(Default)]
pub enum PreferedName {
   #[default]
   AppName,
   Identifier,
   Custom(String),
}

Keep only these three variants: default to AppName (package.json), a variant using Identifier(Tauri config, particularly useful for macOS), and a Custom option allowing users to define their own logic.

Alternatively, remove the Identifier variant if you think it’s too cumbersome to handle.
So there would be just None or String, None is default to AppName or setting by String .app_name(String)

@FabianLars
Copy link
Member

The thing here is that devs themselves can use app.config().identifier as well. It's not like we don't want them to not have a way to use the identifier without hardcoding it, it's just that the code logic of the enum can be used in their code just as well.
Imo that's good enough to keep our api small and simple 🤷

- Change app_name usage in README to a custom string
- Simplify app_name handling in lib.rs by removing PreferedName enum
- Update Builder struct to accept app_name as an optional string

This refactor enhances the clarity and usability of the auto start configuration.
@Tunglies
Copy link
Contributor Author

Tunglies commented May 20, 2025

Thanks @Legend-Master and @FabianLars for reviews.

Let's keep the api small and simple.

The only one additional function for builder is

/// Sets the app name to be used for the auto start entry.
///
/// ## Examples
///
/// ```no_run
/// Builder::new()
///     .app_name("My Custom Name"))
///     .build();
/// ```
pub fn app_name(mut self, app_name: &str) -> Self {
    self.app_name = Some(app_name.to_string());
    self
}

no more, no less.

@FabianLars
Copy link
Member

Thank you! And sorry for being kinda harsh about your ideas. In theory I like them but with the very limited resources we have for maintenance I try to keep things simple like that.

Copy link
Contributor

@Legend-Master Legend-Master left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good, just a few nit picks, thanks for the contribution

Also could you add a change file?

https://github.com/tauri-apps/plugins-workspace/blob/v2/.changes/readme.md

…aming

- Introduced a new builder method app_name() to customize the application name in the autostart entry.
- Updated app_name handling in the Builder struct to accept a generic string type.

This enhancement allows for greater flexibility in defining application names for autostart configurations.
@Tunglies
Copy link
Contributor Author

Generally looks good, just a few nit picks, thanks for the contribution

Also could you add a change file?

https://github.com/tauri-apps/plugins-workspace/blob/v2/.changes/readme.md

I added the change file. Let's go

Copy link
Contributor

@Legend-Master Legend-Master left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Legend-Master Legend-Master merged commit 764e8f7 into tauri-apps:v2 May 20, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants