Skip to content

Refuse a persistAs key another toggle already remembers [patch] - #60

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/37-duplicate-persist-key
Nov 4, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/37-duplicate-persist-key

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #37

What was wrong

TrayAppBuilder.Toggle(..., persistAs:) never checked the key against the toggles already added. Preferences live in one flat dictionary, so two toggles sharing a key overwrote each other on every click. On the next launch, LoadPreferences restored both toggles to whichever value was stored, and each restore runs the toggle's real setter.

Change

  • Toggle throws ArgumentException (ParamName == "persistAs") when another toggle already uses the key. The comparison is ordinal, to match the store.
  • It also rejects an empty or white-space key (the issue's optional item).
  • The check runs before the toggle is added, so a refused call leaves the builder unchanged.
  • The XML doc and the README's preferences section state the rule.

Tests

The new tests sit next to the #18 alias-clash tests in TrayAppBuilderTests:

  • Toggle_WithAPersistAsKeyAnotherToggleTook_Throws: checks the message, the param name, and that the builder still holds one toggle.
  • Toggle_WithABlankPersistAsKey_Throws covers "" and " ".
  • Toggle_WithPersistAsKeysThatDifferOnlyInCase_KeepsBoth: awake/Awake and two unremembered toggles are all accepted.

The first two fail on main (3 cases) and pass with the fix. Full suite: 164 passed, 1 skipped, 0 failed.


Generated by Claude Code

Two toggles given the same persistAs key shared one remembered value: each
click overwrote it, and the next launch restored both toggles to it,
running both real setters with the wrong state. Toggle now throws
ArgumentException (param persistAs) for a key another toggle already uses,
compared ordinally to match the store, and for an empty or white-space key.
This is the preference-key counterpart of the alias-clash checks from #18.

Fixes #37

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW3e2AZDcKPKkvjAga2
@sonarqubecloud

sonarqubecloud Bot commented Nov 1, 2026

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit cbbc82e into main Nov 4, 2026
14 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.

Two toggles given the same persistAs key share one remembered value, so the next launch restores both to whichever was clicked last

2 participants