feat(macos): add TrayIconAttributes::autosave_name for NSStatusItem position persistence#317
Open
ericwang915 wants to merge 1 commit into
Open
feat(macos): add TrayIconAttributes::autosave_name for NSStatusItem position persistence#317ericwang915 wants to merge 1 commit into
ericwang915 wants to merge 1 commit into
Conversation
…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`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The macOS impl creates the
NSStatusItemwithout callingsetAutosaveName. AppKit therefore can't persist the user's ⌘+drag position across launches viaNSUserDefaults, 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>toTrayIconAttributesplus awith_autosave_namebuilder method. When set, the macOS impl forwards it toNSStatusItem.setAutosaveName. AppKit then handles position persistence automatically: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, butTrayIconIddefaults 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✓launchctl kickstartrestarts on a 16" M2 Max MacBook Pro.Changefile
Included
.changes/macos-autosave-name.md(minor bump — additive API).Reference
NSStatusItem.autosaveName