[Image Resizer] Automatically reload settings changes#45266
[Image Resizer] Automatically reload settings changes#45266daverayment wants to merge 3 commits intomicrosoft:mainfrom
Conversation
…ion or manual user-edits of the settings.json file). Update and simplify IdRecoveryHelper. Add unit tests to exercise updated settings code and IdRecoveryHelper.
This comment has been minimized.
This comment has been minimized.
…deletes the currently-selected preset.
This comment has been minimized.
This comment has been minimized.
@check-spelling-bot Report🔴 Please reviewSee the 📂 files view, the 📜action log, or 📝 job summary for details.Unrecognized words (4)Gotchas These words are not needed and should be removedgotcha ROOTOWNERSome files were automatically ignored 🙈These sample patterns would exclude them: You should consider adding them to: File matching is via Perl regular expressions. To check these files, more of their words need to be in the dictionary than not. You can use To accept these unrecognized words as correct, update file exclusions, and remove the previously acknowledged and now absent words, you could run the following commands... in a clone of the git@github.com:daverayment/PowerToys.git repository curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/c635c2f3f714eec2fcf27b643a1919b9a811ef2e/apply.pl' |
perl - 'https://github.com/microsoft/PowerToys/actions/runs/21559588762/attempts/1' &&
git commit -m 'Update check-spelling metadata'Warnings
|
| Count | |
|---|---|
| 2 | |
| 2 |
See
If the flagged items are 🤯 false positives
If items relate to a ...
-
binary file (or some other file you wouldn't want to check at all).
Please add a file path to the
excludes.txtfile matching the containing file.File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.
^refers to the file's path from the root of the repository, so^README\.md$would exclude README.md (on whichever branch you're using). -
well-formed pattern.
If you can write a pattern that would match it,
try adding it to thepatterns.txtfile.Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.
Note that patterns can't match multiline strings.
Summary of the Pull Request
This PR introduces real-time settings synchronisation for Image Resizer. External changes to
settings.json(via the Settings application or manual edits to the file) are detected and reloaded immediately without requiring a restart.PR Checklist
Detailed Description of the Pull Request / Additional comments
Reload updates
The reload detection is via an
IFileSystemWatcher(as file system operations are abstracted in Image Resizer), which monitors thesettings.jsonfile for creation and changes. There is a half second debounce for changes, as the settings file can rapidly change when the user is editing the preset name field.Hot reloading of the properties required refactoring
ReloadCore, which replaces theSizespreset collection and updates the Custom and AI presets, in addition to the application-wide properties like compression options and the fallback encoder choice.I changed the combo box data binding from the
SelectedSizeobject to the intSelectedSizeIndex. This was required to resolve a specific WPF issue where reloading theSizescollection would cause issues with restoring the current combo box selection to the prior value. Binding to the index decouples the selection state from the object lifecycle during the reload process.Selection preservation is based on the preset ID (for user presets) and preset type (for Custom and AI presets). This ensures that matches are robust even if the user is in the process of renaming an entry in the Settings application. If the preset cannot be matched (for example, if the user deletes the item or changes the ID manually in the file), the Custom preset is selected.
Selection range checks are maintained from the old code, and additional checks have been added to ensure that Custom and AI presets will be present even if they're deleted from the settings file.
The AI preset check from before has been inlined; this guarantees that the AI resize option will not display if the facility is unavailable on the current PC, even if it's present in the settings file.
The reload routine is dispatched to the UI thread, as changes involve updates to the combo box.
ID recovery
Preset IDs are key to preserving the combo box selection between reloads, and a couple of changes were necessary to ensure round-tripping changes were robust.
New ID assignment is now based on a monotonically increasing ID (seeded at application start) rather than
Current Maximum ID + 1. This means that IDs cannot be reused in the same Settings application session. This makes the matching process in the client application more reliable.A small update was made to the ID Recovery Helper to use
HashSet.Add()instead of splitting up the check and addition steps (Addwill return false if the value already exists), which saves a massive one line of code.There were comments about the ID Recovery Helper resolving "empty" or "invalid" entries; this was inaccurate, as IDs are ints, which must by definition always have a valid value. The routine only guards against duplicates, so the comments have been updated to reflect this.
Miscellaneous
The
DefaultSettings property getter previously calledReloadevery time it was accessed. This is fine when the file is only read at application startup, but I changed this to lazily instantiate and callReloada single time.I refactored duplicate code related to settings file/folder strings, and also the creation of the Custom and AI size instances.
Validation Steps Performed
Manual testing:
imageresizer_shrinkOnly) or "Overwrite files" (imageresizer_replace). Upon saving, confirm the checkbox changes immediately in the client application.settings.jsonfile manually by e.g. adding a new Size preset or editing the width or height property of an existing preset. Upon saving, the change(s) should be reflected in the client application.14 new unit tests have been added to
SetingsTests.csto exercise theSettingsandIDRecoveryHelperfunctionality.