Skip to content

feat(macos): add TrayIconAttributes::autosave_name for NSStatusItem position persistence#317

Open
ericwang915 wants to merge 1 commit into
tauri-apps:devfrom
ericwang915:feat/macos-autosave-name
Open

feat(macos): add TrayIconAttributes::autosave_name for NSStatusItem position persistence#317
ericwang915 wants to merge 1 commit into
tauri-apps:devfrom
ericwang915:feat/macos-autosave-name

Conversation

@ericwang915

Copy link
Copy Markdown

Problem

The macOS impl creates the NSStatusItem without calling setAutosaveName. AppKit therefore can't persist the user's ⌘+drag position across launches via NSUserDefaults, so the status item resets to the default leftmost slot every launch.

On notched MacBooks (M1+ Pro/Max, 14"/16") that default slot is directly under the camera cutout — the icon is partially or fully obscured, and there's no permanent fix the user can apply.

Solution

Add an opt-in autosave_name: Option<String> to TrayIconAttributes plus a with_autosave_name builder method. When set, the macOS impl forwards it to NSStatusItem.setAutosaveName. AppKit then handles position persistence automatically:

let tray = TrayIconBuilder::new()
    .with_autosave_name("com.example.app.tray")
    .with_icon(icon)
    .build()?;

The key should be a stable, app-unique string (typically reverse-DNS); changing it invalidates the saved position. No-op on Linux / Windows.

Why opt-in (not always-on)

Default-on would require synthesizing an autosave key from TrayIconId, but TrayIconId defaults to a counter-generated string (COUNTER.next().to_string()) which isn't stable across launches — using that as the autosave key would actively break position persistence in apps that don't override the id. Opt-in keeps existing behaviour unchanged and lets callers supply a stable key intentionally.

Verification

  • cargo check
  • cargo check --all-features
  • cargo clippy --all-targets ✓ (no new warnings)
  • cargo test --no-run
  • Real app verification: built downstream against this patched copy, ⌘+drag persists across launchctl kickstart restarts on a 16" M2 Max MacBook Pro.

Changefile

Included .changes/macos-autosave-name.md (minor bump — additive API).

Reference

…istence)

Currently the macOS impl creates the NSStatusItem without calling
setAutosaveName, which means AppKit can't persist the user's
⌘+drag position across app launches. Every launch the status
item resets to the default leftmost slot — and on notched
MacBooks (M1+ Pro/Max, 14"/16") that's directly under the
camera cutout, so the icon is partially or fully obscured.

This adds an opt-in autosave_name attribute. When set, AppKit
writes the preferred position to NSUserDefaults under the
provided key and re-reads it on next launch:

    let tray = TrayIconBuilder::new()
        .with_autosave_name("com.example.app.tray")
        .with_icon(icon)
        .build()?;

The key should be a stable, app-unique string (typically a
reverse-DNS identifier); changing it invalidates the saved
position. No-op on Linux / Windows.

Builds clean on `cargo check`, `cargo check --all-features`,
`cargo clippy --all-targets`, and `cargo test --no-run`.
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.

2 participants