Files
PowerToys/src/settings-ui
Niels Laute 5eeb979339 fix(screen-ruler): migrate legacy measurement unit values (#49898)
## Summary of the Pull Request

Closes #49899

Fixes a PowerToys Settings crash when navigating away from the Screen
Ruler page with legacy measurement-unit settings.

Older Screen Ruler builds persisted `Measurement::Unit` enum values
(`Pixel = 1`, `Inch = 2`, `Centimetre = 4`, `Millimetre = 8`). The
current Settings page binds the persisted value directly to a four-item
`ComboBox.SelectedIndex`, which only accepts `0-3`. A persisted value
such as `4` therefore produces a WinUI `E_INVALIDARG` stowed exception
when the page is unloaded.

`MeasureToolViewModel` now migrates legacy values to the current
indices, validates all values before exposing them to XAML, and persists
the repaired setting:

- `4` (legacy centimetres) → `2`
- `8` (legacy millimetres) → `3`
- other out-of-range values → `0` (pixels)

## PR Checklist

- [x] Closes: #49899
- [x] **Tests:** Added/updated and all pass
- [x] **Localization:** N/A - no user-facing strings changed

## Detailed Description of the Pull Request / Additional comments

The failure was reproduced on `main` with
`%LOCALAPPDATA%\Microsoft\PowerToys\Measure Tool\settings.json`
containing:

```json
"UnitsOfMeasure": { "value": 4 }
```

Screen Ruler opened successfully, but navigating to Shortcut Guide
terminated `PowerToys.Settings.exe` in `Microsoft.UI.Xaml.dll` with
exception `0xc000027b` and `E_INVALIDARG` (`0x80070057`). Changing the
persisted value to `0` eliminated the crash, confirming the invalid
`SelectedIndex` as the cause.

The migration preserves the intended legacy centimetre/millimetre
selection rather than resetting it unnecessarily.

## Validation Steps Performed

- Built `PowerToys.Settings.csproj` in ARM64 Release.
- Added six Measure Tool ViewModel regression cases covering valid,
invalid, and legacy values.
- Ran the full Settings unit suite: **222 passed, 0 failed**.
- Reproduced the original UI flow with `UnitsOfMeasure = 4`:
  - opened Screen Ruler;
  - verified the setting migrated to `2`;
  - navigated to Shortcut Guide;
  - repeated Screen Ruler → Shortcut Guide navigation;
  - verified the Settings process remained alive after both transitions.

Copilot-Session: ebcedcc4-a067-4657-a6a9-046753f9d70c
2026-08-19 10:46:54 +08:00
..