From ac28b1c29fa3087bd93b17a18660b2a039bdbf39 Mon Sep 17 00:00:00 2001 From: moooyo <42196638+moooyo@users.noreply.github.com> Date: Wed, 1 Apr 2026 16:22:33 +0800 Subject: [PATCH] Migrate ImageResizer to WinUI3 (#45288) ## Summary of the Pull Request Migrate WPF/WinForms utility to WinUI3 can give us many benefit. 1. Only WinUI3 support AOT. By this change, we can remove the blocker to make imageResizer publish with AOT enabled to improve the performance. Through the previous testing in CmdPal, it can improve about 1.5x to 3x perf. 2. WinUI 3 provides a modern UI and makes sure that our experiences fit in with the Windows 11 look and feel. 3. We can merge many redundant code to the same one and reduce more codebase and installed size in the future if we successfully migrate all remaining WPF/WinForms utility to WinUI3. ## PR Checklist - [x] Closes: #46465 - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [x] **Tests:** Added/updated and all pass - [x] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx ## Detailed Description of the Pull Request / Additional comments ## Validation Steps Performed 1. Set up the ImageResizer as the startup project. 2. Start in visual studio. --------- Co-authored-by: Yu Leng (from Dev Box) Co-authored-by: Niels Laute Co-authored-by: Claude Opus 4.6 (1M context) --- .github/actions/spell-check/expect.txt | 3 + .pipelines/ESRPSigning_core.json | 10 +- installer/PowerToysSetupVNext/Resources.wxs | 9 +- .../generateAllFileComponents.ps1 | 18 + .../ImageResizerCLI/ImageResizerCLI.csproj | 5 - .../ImageResizerContextMenu.vcxproj | 16 +- .../codeAnalysis/GlobalSuppressions.cs | 21 - .../imageresizer/dll/ImageResizerExt.base.rc | 2 +- .../tests/ImageResizer.UnitTests.csproj | 116 +- .../tests/Models/CustomSizeTests.cs | 26 - .../tests/Models/ResizeBatchTests.cs | 32 +- .../tests/Models/ResizeOperationTests.cs | 328 ++--- .../tests/Models/ResizeSizeTests.cs | 41 +- .../tests/Properties/SettingsTests.cs | 23 +- .../imageresizer/tests/Test/AssertEx.cs | 44 +- .../tests/Test/BitmapSourceExtensions.cs | 32 +- .../Views/TimeRemainingConverterTests.cs | 50 - src/modules/imageresizer/ui/App.xaml | 28 - src/modules/imageresizer/ui/App.xaml.cs | 243 ---- .../ImageResizer}/ImageResizer.ico | Bin .../ImageResizer}/ImageResizer.png | Bin .../ui/Cli/ImageResizerCliExecutor.cs | 7 +- .../ui/Converters/AutoDoubleConverter.cs | 28 + .../ui/Converters/EnumToIntConverter.cs | 32 + .../EnumValueConverter.cs | 28 +- .../ui/Extensions/BitmapEncoderExtensions.cs | 25 - .../ui/Extensions/BitmapMetadataExtension.cs | 258 ---- .../ui/Extensions/ICollectionExtensions.cs | 4 +- .../ui/Extensions/TimeSpanExtensions.cs | 14 - .../imageresizer/ui/Helpers/Observable.cs | 27 - .../imageresizer/ui/Helpers/RelayCommand.cs | 61 - .../ui/Helpers/ResourceLoaderInstance.cs | 27 + .../imageresizer/ui/ImageResizerUI.csproj | 123 +- .../ui/ImageResizerUI.dev.manifest | 8 - .../ui/ImageResizerUI.prod.manifest | 8 - .../imageresizer/ui/ImageResizerXAML/App.xaml | 33 + .../ui/ImageResizerXAML/App.xaml.cs | 244 ++++ .../ui/ImageResizerXAML/MainWindow.xaml | 33 + .../ui/ImageResizerXAML/MainWindow.xaml.cs | 294 +++++ .../ui/ImageResizerXAML/Views/IMainView.cs | 17 + .../ui/ImageResizerXAML/Views/InputPage.xaml | 346 +++++ .../ImageResizerXAML/Views/InputPage.xaml.cs | 52 + .../ImageResizerXAML/Views/ProgressPage.xaml | 42 + .../Views/ProgressPage.xaml.cs | 26 + .../ImageResizerXAML/Views/ResultsPage.xaml | 60 + .../Views/ResultsPage.xaml.cs | 20 + .../Views/SizeDataTemplateSelector.cs | 37 + src/modules/imageresizer/ui/Models/AiSize.cs | 27 +- .../imageresizer/ui/Models/CliOptions.cs | 136 +- .../imageresizer/ui/Models/CustomSize.cs | 9 +- .../imageresizer/ui/Models/ImagingEnums.cs | 32 + .../imageresizer/ui/Models/ResizeBatch.cs | 44 +- .../imageresizer/ui/Models/ResizeError.cs | 11 +- .../imageresizer/ui/Models/ResizeFit.cs | 4 +- .../imageresizer/ui/Models/ResizeOperation.cs | 630 +++++---- .../imageresizer/ui/Models/ResizeSize.cs | 106 +- .../imageresizer/ui/Models/ResizeUnit.cs | 4 +- src/modules/imageresizer/ui/Program.cs | 27 + .../ui/Properties/InternalsVisibleTo.cs | 9 - .../ui/Properties/Resources.Designer.cs | 1152 ----------------- .../imageresizer/ui/Properties/Resources.cs | 75 ++ .../imageresizer/ui/Properties/Settings.cs | 157 ++- .../ui/Properties/SettingsWrapper.cs | 4 +- .../ui/Properties/WrappedJsonConverter`1.cs | 4 +- .../Properties/WrappedJsonValueConverter.cs | 4 +- .../ui/Services/IAISuperResolutionService.cs | 4 +- .../Services/NoOpAiSuperResolutionService.cs | 4 +- .../Services/WinAiSuperResolutionService.cs | 157 +-- .../en-us/Resources.resw} | 294 ++--- .../imageresizer/ui/Utilities/CodecHelper.cs | 91 ++ .../imageresizer/ui/Utilities/MathHelpers.cs | 16 - .../ui/Utilities/NativeMethods.cs | 21 - .../ui/ViewModels/AdvancedViewModel.cs | 89 -- .../ui/ViewModels/ITabViewModel.cs | 13 - .../ui/ViewModels/InputViewModel.cs | 364 ++---- .../ui/ViewModels/MainViewModel.cs | 38 +- .../ui/ViewModels/ProgressViewModel.cs | 121 +- .../ui/ViewModels/ResultsViewModel.cs | 16 +- .../ui/Views/AccessTextToTextConverter.cs | 23 - .../ui/Views/AutoDoubleConverter.cs | 64 - .../ui/Views/BoolValueConverter.cs | 33 - .../ui/Views/EnumToIntConverter.cs | 23 - .../imageresizer/ui/Views/IMainView.cs | 17 - .../imageresizer/ui/Views/InputPage.xaml | 408 ------ .../imageresizer/ui/Views/InputPage.xaml.cs | 69 - .../imageresizer/ui/Views/MainWindow.xaml | 65 - .../imageresizer/ui/Views/MainWindow.xaml.cs | 61 - .../ui/Views/NumberBoxValueConverter.cs | 32 - .../imageresizer/ui/Views/ProgressPage.xaml | 46 - .../ui/Views/ProgressPage.xaml.cs | 16 - .../imageresizer/ui/Views/ResultsPage.xaml | 44 - .../imageresizer/ui/Views/ResultsPage.xaml.cs | 16 - .../ui/Views/SizeTypeToHelpTextConverter.cs | 46 - .../ui/Views/SizeTypeToVisibilityConverter.cs | 27 - .../ui/Views/TiffCompressOptionConverter.cs | 27 - .../ui/Views/TimeRemainingConverter.cs | 51 - .../ui/Views/VisibilityBoolConverter.cs | 23 - .../Views/ZeroToEmptyStringNumberFormatter.cs | 37 - src/modules/imageresizer/ui/app.manifest | 21 + 99 files changed, 2937 insertions(+), 4776 deletions(-) delete mode 100644 src/modules/imageresizer/tests/Models/CustomSizeTests.cs delete mode 100644 src/modules/imageresizer/tests/Views/TimeRemainingConverterTests.cs delete mode 100644 src/modules/imageresizer/ui/App.xaml delete mode 100644 src/modules/imageresizer/ui/App.xaml.cs rename src/modules/imageresizer/ui/{Resources => Assets/ImageResizer}/ImageResizer.ico (100%) rename src/modules/imageresizer/ui/{Resources => Assets/ImageResizer}/ImageResizer.png (100%) create mode 100644 src/modules/imageresizer/ui/Converters/AutoDoubleConverter.cs create mode 100644 src/modules/imageresizer/ui/Converters/EnumToIntConverter.cs rename src/modules/imageresizer/ui/{Views => Converters}/EnumValueConverter.cs (63%) delete mode 100644 src/modules/imageresizer/ui/Extensions/BitmapEncoderExtensions.cs delete mode 100644 src/modules/imageresizer/ui/Extensions/BitmapMetadataExtension.cs delete mode 100644 src/modules/imageresizer/ui/Extensions/TimeSpanExtensions.cs delete mode 100644 src/modules/imageresizer/ui/Helpers/Observable.cs delete mode 100644 src/modules/imageresizer/ui/Helpers/RelayCommand.cs create mode 100644 src/modules/imageresizer/ui/Helpers/ResourceLoaderInstance.cs delete mode 100644 src/modules/imageresizer/ui/ImageResizerUI.dev.manifest delete mode 100644 src/modules/imageresizer/ui/ImageResizerUI.prod.manifest create mode 100644 src/modules/imageresizer/ui/ImageResizerXAML/App.xaml create mode 100644 src/modules/imageresizer/ui/ImageResizerXAML/App.xaml.cs create mode 100644 src/modules/imageresizer/ui/ImageResizerXAML/MainWindow.xaml create mode 100644 src/modules/imageresizer/ui/ImageResizerXAML/MainWindow.xaml.cs create mode 100644 src/modules/imageresizer/ui/ImageResizerXAML/Views/IMainView.cs create mode 100644 src/modules/imageresizer/ui/ImageResizerXAML/Views/InputPage.xaml create mode 100644 src/modules/imageresizer/ui/ImageResizerXAML/Views/InputPage.xaml.cs create mode 100644 src/modules/imageresizer/ui/ImageResizerXAML/Views/ProgressPage.xaml create mode 100644 src/modules/imageresizer/ui/ImageResizerXAML/Views/ProgressPage.xaml.cs create mode 100644 src/modules/imageresizer/ui/ImageResizerXAML/Views/ResultsPage.xaml create mode 100644 src/modules/imageresizer/ui/ImageResizerXAML/Views/ResultsPage.xaml.cs create mode 100644 src/modules/imageresizer/ui/ImageResizerXAML/Views/SizeDataTemplateSelector.cs create mode 100644 src/modules/imageresizer/ui/Models/ImagingEnums.cs create mode 100644 src/modules/imageresizer/ui/Program.cs delete mode 100644 src/modules/imageresizer/ui/Properties/InternalsVisibleTo.cs delete mode 100644 src/modules/imageresizer/ui/Properties/Resources.Designer.cs create mode 100644 src/modules/imageresizer/ui/Properties/Resources.cs rename src/modules/imageresizer/ui/{Properties/Resources.resx => Strings/en-us/Resources.resw} (80%) create mode 100644 src/modules/imageresizer/ui/Utilities/CodecHelper.cs delete mode 100644 src/modules/imageresizer/ui/Utilities/MathHelpers.cs delete mode 100644 src/modules/imageresizer/ui/Utilities/NativeMethods.cs delete mode 100644 src/modules/imageresizer/ui/ViewModels/AdvancedViewModel.cs delete mode 100644 src/modules/imageresizer/ui/ViewModels/ITabViewModel.cs delete mode 100644 src/modules/imageresizer/ui/Views/AccessTextToTextConverter.cs delete mode 100644 src/modules/imageresizer/ui/Views/AutoDoubleConverter.cs delete mode 100644 src/modules/imageresizer/ui/Views/BoolValueConverter.cs delete mode 100644 src/modules/imageresizer/ui/Views/EnumToIntConverter.cs delete mode 100644 src/modules/imageresizer/ui/Views/IMainView.cs delete mode 100644 src/modules/imageresizer/ui/Views/InputPage.xaml delete mode 100644 src/modules/imageresizer/ui/Views/InputPage.xaml.cs delete mode 100644 src/modules/imageresizer/ui/Views/MainWindow.xaml delete mode 100644 src/modules/imageresizer/ui/Views/MainWindow.xaml.cs delete mode 100644 src/modules/imageresizer/ui/Views/NumberBoxValueConverter.cs delete mode 100644 src/modules/imageresizer/ui/Views/ProgressPage.xaml delete mode 100644 src/modules/imageresizer/ui/Views/ProgressPage.xaml.cs delete mode 100644 src/modules/imageresizer/ui/Views/ResultsPage.xaml delete mode 100644 src/modules/imageresizer/ui/Views/ResultsPage.xaml.cs delete mode 100644 src/modules/imageresizer/ui/Views/SizeTypeToHelpTextConverter.cs delete mode 100644 src/modules/imageresizer/ui/Views/SizeTypeToVisibilityConverter.cs delete mode 100644 src/modules/imageresizer/ui/Views/TiffCompressOptionConverter.cs delete mode 100644 src/modules/imageresizer/ui/Views/TimeRemainingConverter.cs delete mode 100644 src/modules/imageresizer/ui/Views/VisibilityBoolConverter.cs delete mode 100644 src/modules/imageresizer/ui/Views/ZeroToEmptyStringNumberFormatter.cs create mode 100644 src/modules/imageresizer/ui/app.manifest diff --git a/.github/actions/spell-check/expect.txt b/.github/actions/spell-check/expect.txt index da93694080..749a25a379 100644 --- a/.github/actions/spell-check/expect.txt +++ b/.github/actions/spell-check/expect.txt @@ -52,6 +52,7 @@ Apm APPBARDATA APPEXECLINK APPLICATIONFRAMEHOST +apphost appmanifest APPMODEL APPNAME @@ -1329,6 +1330,7 @@ rundll rungameid RUNLEVEL runtimeclass +runtimeconfig runtimepack ruuid rvm @@ -1583,6 +1585,7 @@ TILEDWINDOW TILLSON timedate timediff +timespan timeutil TITLEBARINFO Titlecase diff --git a/.pipelines/ESRPSigning_core.json b/.pipelines/ESRPSigning_core.json index 09d65893d7..8851a7ffac 100644 --- a/.pipelines/ESRPSigning_core.json +++ b/.pipelines/ESRPSigning_core.json @@ -141,13 +141,13 @@ "WinUI3Apps\\PowerToys.EnvironmentVariables.dll", "WinUI3Apps\\PowerToys.EnvironmentVariables.exe", - "PowerToys.ImageResizer.exe", - "PowerToys.ImageResizer.dll", + "WinUI3Apps\\PowerToys.ImageResizer.exe", + "WinUI3Apps\\PowerToys.ImageResizer.dll", "WinUI3Apps\\PowerToys.ImageResizerCLI.exe", "WinUI3Apps\\PowerToys.ImageResizerCLI.dll", - "PowerToys.ImageResizerExt.dll", - "PowerToys.ImageResizerContextMenu.dll", - "ImageResizerContextMenuPackage.msix", + "WinUI3Apps\\PowerToys.ImageResizerExt.dll", + "WinUI3Apps\\PowerToys.ImageResizerContextMenu.dll", + "WinUI3Apps\\ImageResizerContextMenuPackage.msix", "PowerToys.LightSwitchModuleInterface.dll", "LightSwitchService\\PowerToys.LightSwitchService.exe", diff --git a/installer/PowerToysSetupVNext/Resources.wxs b/installer/PowerToysSetupVNext/Resources.wxs index a392004320..808883920b 100644 --- a/installer/PowerToysSetupVNext/Resources.wxs +++ b/installer/PowerToysSetupVNext/Resources.wxs @@ -9,7 +9,7 @@ - + @@ -171,12 +171,6 @@ - - - - - - @@ -459,7 +453,6 @@ - diff --git a/installer/PowerToysSetupVNext/generateAllFileComponents.ps1 b/installer/PowerToysSetupVNext/generateAllFileComponents.ps1 index bd6604ad46..8144575369 100644 --- a/installer/PowerToysSetupVNext/generateAllFileComponents.ps1 +++ b/installer/PowerToysSetupVNext/generateAllFileComponents.ps1 @@ -131,7 +131,25 @@ if ($platform -ceq "arm64") { } #BaseApplications +# WORKAROUND: Exclude ImageResizer files that leak into the root output directory. +# ImageResizerCLI (Exe, SelfContained) has a ProjectReference to ImageResizerUI (WinExe, SelfContained). +# MSBuild copies the referenced WinExe's apphost (.exe, .deps.json, .runtimeconfig.json) to the root +# output directory as a side effect. These files are incomplete (missing the managed .dll) and should +# not be included in the installer. The complete ImageResizer files are in WinUI3Apps/ and are handled +# by WinUI3ApplicationsFiles. TODO: Refactor ImageResizer to use a shared Library project instead. Generate-FileList -fileDepsJson "" -fileListName BaseApplicationsFiles -wxsFilePath $PSScriptRoot\BaseApplications.wxs -depsPath "$PSScriptRoot..\..\..\$platform\Release" + +# Remove leaked ImageResizer artifacts from BaseApplications +$baseAppWxsPath = "$PSScriptRoot\BaseApplications.wxs" +$baseAppWxs = Get-Content $baseAppWxsPath -Raw +$baseAppWxs = $baseAppWxs -replace 'PowerToys\.ImageResizer\.exe;?', '' +$baseAppWxs = $baseAppWxs -replace 'PowerToys\.ImageResizer\.deps\.json;?', '' +$baseAppWxs = $baseAppWxs -replace 'PowerToys\.ImageResizer\.runtimeconfig\.json;?', '' +# Clean up trailing/double semicolons left after removal +$baseAppWxs = $baseAppWxs -replace ';;+', ';' +$baseAppWxs = $baseAppWxs -replace '=;', '=' +$baseAppWxs = $baseAppWxs -replace ';"', '"' +Set-Content -Path $baseAppWxsPath -Value $baseAppWxs Generate-FileComponents -fileListName "BaseApplicationsFiles" -wxsFilePath $PSScriptRoot\BaseApplications.wxs #WinUI3Applications diff --git a/src/modules/imageresizer/ImageResizerCLI/ImageResizerCLI.csproj b/src/modules/imageresizer/ImageResizerCLI/ImageResizerCLI.csproj index 05efacafd8..4d3cd8a18b 100644 --- a/src/modules/imageresizer/ImageResizerCLI/ImageResizerCLI.csproj +++ b/src/modules/imageresizer/ImageResizerCLI/ImageResizerCLI.csproj @@ -20,9 +20,4 @@ - - - - - diff --git a/src/modules/imageresizer/ImageResizerContextMenu/ImageResizerContextMenu.vcxproj b/src/modules/imageresizer/ImageResizerContextMenu/ImageResizerContextMenu.vcxproj index 5e63e8f1d8..ff6d92708e 100644 --- a/src/modules/imageresizer/ImageResizerContextMenu/ImageResizerContextMenu.vcxproj +++ b/src/modules/imageresizer/ImageResizerContextMenu/ImageResizerContextMenu.vcxproj @@ -50,10 +50,10 @@ false Source.def - - del $(OutDir)\ImageResizerContextMenuPackage.msix /q -MakeAppx.exe pack /d . /p $(OutDir)ImageResizerContextMenuPackage.msix /nv - + + if exist "$(OutDir)ImageResizerContextMenuPackage.msix" del "$(OutDir)ImageResizerContextMenuPackage.msix" /q +MakeAppx.exe pack /d "$(MSBuildThisFileDirectory)." /p "$(OutDir)ImageResizerContextMenuPackage.msix" /nv + @@ -73,10 +73,10 @@ MakeAppx.exe pack /d . /p $(OutDir)ImageResizerContextMenuPackage.msix /nvfalse Source.def - - del $(OutDir)\ImageResizerContextMenuPackage.msix /q -MakeAppx.exe pack /d . /p $(OutDir)ImageResizerContextMenuPackage.msix /nv - + + if exist "$(OutDir)ImageResizerContextMenuPackage.msix" del "$(OutDir)ImageResizerContextMenuPackage.msix" /q +MakeAppx.exe pack /d "$(MSBuildThisFileDirectory)." /p "$(OutDir)ImageResizerContextMenuPackage.msix" /nv + diff --git a/src/modules/imageresizer/codeAnalysis/GlobalSuppressions.cs b/src/modules/imageresizer/codeAnalysis/GlobalSuppressions.cs index 34ceb00539..2f4b0c9216 100644 --- a/src/modules/imageresizer/codeAnalysis/GlobalSuppressions.cs +++ b/src/modules/imageresizer/codeAnalysis/GlobalSuppressions.cs @@ -25,24 +25,3 @@ using System.Diagnostics.CodeAnalysis; [assembly: SuppressMessage("StyleCop.CSharp.SpecialRules", "SA0001:XmlCommentAnalysisDisabled", Justification = "Not enabled as we don't want or need XML documentation.")] [assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1629:DocumentationTextMustEndWithAPeriod", Justification = "Not enabled as we don't want or need XML documentation.")] -[assembly: SuppressMessage("Microsoft.Design", "CA1009:DeclareEventHandlersCorrectly", Scope = "member", Target = "Microsoft.Templates.Core.Locations.TemplatesSynchronization.#SyncStatusChanged", Justification = "Using an Action does not allow the required notation")] - -// Non general suppressions -[assembly: SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "This is part of the markdown processing", MessageId = "System.Windows.Documents.Run.#ctor(System.String)", Scope = "member", Target = "Microsoft.Templates.UI.Controls.Markdown.#ImageInlineEvaluator(System.Text.RegularExpressions.Match)")] -[assembly: SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Justification = "We need to have the names of these keys in lowercase to be able to compare with the keys becoming form the template json. ContainsKey does not allow StringComparer specification to IgnoreCase", Scope = "member", Target = "Microsoft.Templates.Core.ITemplateInfoExtensions.#GetQueryableProperties(Microsoft.TemplateEngine.Abstractions.ITemplateInfo)")] -[assembly: SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Justification = "We need to have the names of these keys in lowercase to be able to compare with the keys becoming form the template json. ContainsKey does not allow StringComparer specification to IgnoreCase", Scope = "member", Target = "Microsoft.Templates.Core.Composition.CompositionQuery.#Match(System.Collections.Generic.IEnumerable`1,Microsoft.Templates.Core.Composition.QueryablePropertyDictionary)")] -[assembly: SuppressMessage("Usage", "VSTHRD103:Call async methods when in an async method", Justification = "Resource DictionaryWriter does not implement flush async", Scope = "member", Target = "~M:Microsoft.Templates.Core.PostActions.Catalog.Merge.MergeResourceDictionaryPostAction.ExecuteInternalAsync~System.Threading.Tasks.Task")] - -// Threading suppressions -[assembly: SuppressMessage("Microsoft.VisualStudio.Threading.Analyzers", "VSTHRD100:Avoid async void methods", Justification = "Event handlers needs async void", Scope = "member", Target = "~M:Microsoft.Templates.UI.Controls.Notification.OnClose")] -[assembly: SuppressMessage("Microsoft.VisualStudio.Threading.Analyzers", "VSTHRD100:Avoid async void methods", Justification = "Event handlers needs async void", Scope = "member", Target = "~M:Microsoft.Templates.UI.ViewModels.Common.SavedTemplateViewModel.OnDelete")] -[assembly: SuppressMessage("Microsoft.VisualStudio.Threading.Analyzers", "VSTHRD100:Avoid async void methods", Justification = "Event handlers needs async void", Scope = "member", Target = "~M:Microsoft.Templates.UI.ViewModels.Common.WizardNavigation.GoBack")] -[assembly: SuppressMessage("Microsoft.VisualStudio.Threading.Analyzers", "VSTHRD100:Avoid async void methods", Justification = "Event handlers needs async void", Scope = "member", Target = "~M:Microsoft.Templates.UI.ViewModels.Common.WizardNavigation.GoForward")] -[assembly: SuppressMessage("Microsoft.VisualStudio.Threading.Analyzers", "VSTHRD100:Avoid async void methods", Justification = "Event handlers needs async void", Scope = "member", Target = "~M:Microsoft.Templates.UI.ViewModels.Common.SavedTemplateViewModel.OnDelete(Microsoft.Templates.UI.ViewModels.Common.SavedTemplateViewModel)")] - -// Localization suppressions -[assembly: SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "Microsoft.Templates.Core.Locations.JunctionNativeMethods.ThrowLastWin32Error(System.String)", Scope = "member", Target = "Microsoft.Templates.Core.Locations.JunctionNativeMethods.#CreateJunction(System.String,System.String,System.Boolean)", Justification = "Only used for local generation")] -[assembly: SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "Microsoft.Templates.Core.Locations.JunctionNativeMethods.ThrowLastWin32Error(System.String)", Scope = "member", Target = "Microsoft.Templates.Core.Locations.JunctionNativeMethods.#DeleteJunction(System.String)", Justification = "Only used for local generation")] -[assembly: SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "Microsoft.Templates.Core.Locations.JunctionNativeMethods.ThrowLastWin32Error(System.String)", Scope = "member", Target = "Microsoft.Templates.Core.Locations.JunctionNativeMethods.#InternalGetTarget(Microsoft.Win32.SafeHandles.SafeFileHandle)", Justification = "Only used for local generation")] -[assembly: SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "Microsoft.Templates.Core.Locations.JunctionNativeMethods.ThrowLastWin32Error(System.String)", Scope = "member", Target = "Microsoft.Templates.Core.Locations.JunctionNativeMethods.#OpenReparsePoint(System.String,Microsoft.Templates.Core.Locations.JunctionNativeMethods+EFileAccess)", Justification = "Only used for local generation")] -[assembly: SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "System.Windows.Documents.InlineCollection.Add(System.String)", Scope = "member", Target = "Microsoft.Templates.UI.Extensions.TextBlockExtensions.#OnSequentialFlowStepChanged(System.Windows.DependencyObject,System.Windows.DependencyPropertyChangedEventArgs)", Justification = "No text here")] diff --git a/src/modules/imageresizer/dll/ImageResizerExt.base.rc b/src/modules/imageresizer/dll/ImageResizerExt.base.rc index f545420287..a2cf0dc44c 100644 --- a/src/modules/imageresizer/dll/ImageResizerExt.base.rc +++ b/src/modules/imageresizer/dll/ImageResizerExt.base.rc @@ -82,4 +82,4 @@ IDR_CONTEXTMENUHANDLER REGISTRY "ContextMenuHandler.rgs" // Icon with lowest ID value placed first to ensure application icon // remains consistent on all systems. -IDI_RESIZE_PICTURES ICON "..\ui\Resources\ImageResizer.ico" +IDI_RESIZE_PICTURES ICON "..\\ui\\Assets\\ImageResizer\\ImageResizer.ico" diff --git a/src/modules/imageresizer/tests/ImageResizer.UnitTests.csproj b/src/modules/imageresizer/tests/ImageResizer.UnitTests.csproj index 4588574c62..e83fc93998 100644 --- a/src/modules/imageresizer/tests/ImageResizer.UnitTests.csproj +++ b/src/modules/imageresizer/tests/ImageResizer.UnitTests.csproj @@ -1,62 +1,64 @@ - - - - - - {E0CC7526-D85E-43AC-844F-D5DF0D2F5AB8} + + + + + + {E0CC7526-D85E-43AC-844F-D5DF0D2F5AB8} Exe true win-x64 win-arm64 Properties - ImageResizer - ImageResizer.Test - - $(SolutionDir)$(Platform)\$(Configuration)\tests\$(AssemblyName)\ - - - - - - - - - - - - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - - - - - - - runtime - - - + ImageResizer + ImageResizer.Test + + $(SolutionDir)$(Platform)\$(Configuration)\tests\$(AssemblyName)\ + + true + + + + + + + + + + + + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + + + + + + + runtime + + + diff --git a/src/modules/imageresizer/tests/Models/CustomSizeTests.cs b/src/modules/imageresizer/tests/Models/CustomSizeTests.cs deleted file mode 100644 index 10b905bfc4..0000000000 --- a/src/modules/imageresizer/tests/Models/CustomSizeTests.cs +++ /dev/null @@ -1,26 +0,0 @@ -#pragma warning disable IDE0073 -// Copyright (c) Brice Lambson -// The Brice Lambson licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 - -using ImageResizer.Properties; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace ImageResizer.Models -{ - [TestClass] - public class CustomSizeTests - { - [TestMethod] - public void NameWorks() - { - var size = new CustomSize - { - Name = "Ignored", - }; - - Assert.AreEqual(Resources.Input_Custom, size.Name); - } - } -} diff --git a/src/modules/imageresizer/tests/Models/ResizeBatchTests.cs b/src/modules/imageresizer/tests/Models/ResizeBatchTests.cs index bd6031cad4..12b2cb4830 100644 --- a/src/modules/imageresizer/tests/Models/ResizeBatchTests.cs +++ b/src/modules/imageresizer/tests/Models/ResizeBatchTests.cs @@ -10,6 +10,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; +using System.Threading.Tasks; using ImageResizer.Properties; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; @@ -50,29 +51,14 @@ namespace ImageResizer.Models Assert.AreEqual("OutputDir", result.DestinationDirectory); } - /*[Fact] - public void Process_executes_in_parallel() - { - var batch = CreateBatch(_ => Thread.Sleep(50)); - batch.Files.AddRange( - Enumerable.Range(0, Environment.ProcessorCount) - .Select(i => "Image" + i + ".jpg")); - - var stopwatch = Stopwatch.StartNew(); - batch.Process(CancellationToken.None, (_, __) => { }); - stopwatch.Stop(); - - Assert.InRange(stopwatch.ElapsedMilliseconds, 50, 99); - }*/ - [TestMethod] - public void ProcessAggregatesErrors() + public async Task ProcessAggregatesErrors() { var batch = CreateBatch(file => throw new InvalidOperationException("Error: " + file)); batch.Files.Add("Image1.jpg"); batch.Files.Add("Image2.jpg"); - var errors = batch.Process((_, __) => { }, CancellationToken.None).ToList(); + var errors = (await batch.ProcessAsync((_, __) => { }, CancellationToken.None)).ToList(); Assert.AreEqual(2, errors.Count); @@ -91,14 +77,14 @@ namespace ImageResizer.Models } [TestMethod] - public void ProcessReportsProgress() + public async Task ProcessReportsProgress() { var batch = CreateBatch(_ => { }); batch.Files.Add("Image1.jpg"); batch.Files.Add("Image2.jpg"); var calls = new ConcurrentBag<(int I, double Count)>(); - batch.Process( + await batch.ProcessAsync( (i, count) => calls.Add((i, count)), CancellationToken.None); @@ -109,8 +95,12 @@ namespace ImageResizer.Models { var mock = new Mock { CallBase = true }; mock.Protected() - .Setup("Execute", ItExpr.IsAny(), ItExpr.IsAny()) - .Callback((string file, Settings settings) => executeAction(file)); + .Setup("ExecuteAsync", ItExpr.IsAny(), ItExpr.IsAny()) + .Returns((string file, Settings settings) => + { + executeAction(file); + return Task.CompletedTask; + }); return mock.Object; } diff --git a/src/modules/imageresizer/tests/Models/ResizeOperationTests.cs b/src/modules/imageresizer/tests/Models/ResizeOperationTests.cs index d91a4e4879..d4ab696f32 100644 --- a/src/modules/imageresizer/tests/Models/ResizeOperationTests.cs +++ b/src/modules/imageresizer/tests/Models/ResizeOperationTests.cs @@ -7,10 +7,8 @@ using System; using System.IO; using System.Linq; -using System.Windows.Media; -using System.Windows.Media.Imaging; +using System.Threading.Tasks; -using ImageResizer.Extensions; using ImageResizer.Properties; using ImageResizer.Test; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -20,45 +18,61 @@ namespace ImageResizer.Models [TestClass] public class ResizeOperationTests : IDisposable { + // Known legacy container format GUID for PNG, used as FallbackEncoder value in settings JSON + private static readonly Guid PngContainerFormatGuid = new Guid("1b7cfaf4-713f-473c-bbcd-6137425faeaf"); + + private static readonly string[] CommentPropertyQuery = new[] { "System.Comment" }; + private static readonly string[] DateTakenPropertyQuery = new[] { "System.Photo.DateTaken" }; + private static readonly string[] CameraModelPropertyQuery = new[] { "System.Photo.CameraModel" }; + private readonly TestDirectory _directory = new TestDirectory(); private bool disposedValue; [TestMethod] - public void ExecuteCopiesFrameMetadata() + public async Task ExecuteCopiesFrameMetadata() { var operation = new ResizeOperation("Test.jpg", _directory, Settings()); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image( + await AssertEx.ImageAsync( _directory.File(), - image => Assert.AreEqual("Test", ((BitmapMetadata)image.Frames[0].Metadata).Comment)); + async decoder => + { + var props = await decoder.BitmapProperties.GetPropertiesAsync(CommentPropertyQuery); + Assert.IsTrue(props.ContainsKey("System.Comment"), "Comment metadata should be preserved during transcode"); + Assert.AreEqual("Test", (string)props["System.Comment"].Value, "Comment value should be preserved"); + }); } [TestMethod] - public void ExecuteCopiesFrameMetadataEvenWhenMetadataCannotBeCloned() + public async Task ExecuteCopiesFrameMetadataEvenWhenMetadataCannotBeCloned() { var operation = new ResizeOperation("TestMetadataIssue2447.jpg", _directory, Settings()); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image( + await AssertEx.ImageAsync( _directory.File(), - image => Assert.IsNotNull(((BitmapMetadata)image.Frames[0].Metadata).CameraModel)); + async decoder => + { + var props = await decoder.BitmapProperties.GetPropertiesAsync(CameraModelPropertyQuery); + Assert.IsTrue(props.ContainsKey("System.Photo.CameraModel"), "Camera model metadata should be preserved"); + }); } [TestMethod] - public void ExecuteKeepsDateModified() + public async Task ExecuteKeepsDateModified() { var operation = new ResizeOperation("Test.png", _directory, Settings(s => s.KeepDateModified = true)); - operation.Execute(); + await operation.ExecuteAsync(); Assert.AreEqual(File.GetLastWriteTimeUtc("Test.png"), File.GetLastWriteTimeUtc(_directory.File())); } [TestMethod] - public void ExecuteKeepsDateModifiedWhenReplacingOriginals() + public async Task ExecuteKeepsDateModifiedWhenReplacingOriginals() { var path = Path.Combine(_directory, "Test.png"); File.Copy("Test.png", path); @@ -75,55 +89,59 @@ namespace ImageResizer.Models s.Replace = true; })); - operation.Execute(); + await operation.ExecuteAsync(); Assert.AreEqual(originalDateModified, File.GetLastWriteTimeUtc(_directory.File())); } [TestMethod] - public void ExecuteReplacesOriginals() + public async Task ExecuteReplacesOriginals() { var path = Path.Combine(_directory, "Test.png"); File.Copy("Test.png", path); var operation = new ResizeOperation(path, null, Settings(s => s.Replace = true)); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image(_directory.File(), image => Assert.AreEqual(96, image.Frames[0].PixelWidth)); + await AssertEx.ImageAsync(_directory.File(), decoder => Assert.AreEqual(96u, decoder.PixelWidth)); } [TestMethod] - public void ExecuteTransformsEachFrame() + public async Task ExecuteTransformsEachFrame() { var operation = new ResizeOperation("Test.gif", _directory, Settings()); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image( + await AssertEx.ImageAsync( _directory.File(), - image => + async decoder => { - Assert.AreEqual(2, image.Frames.Count); - AssertEx.All(image.Frames, frame => Assert.AreEqual(96, frame.PixelWidth)); + Assert.AreEqual(2u, decoder.FrameCount); + for (uint i = 0; i < decoder.FrameCount; i++) + { + var frame = await decoder.GetFrameAsync(i); + Assert.AreEqual(96u, frame.PixelWidth); + } }); } [TestMethod] - public void ExecuteUsesFallbackEncoder() + public async Task ExecuteUsesFallbackEncoder() { var operation = new ResizeOperation( "Test.ico", _directory, - Settings(s => s.FallbackEncoder = new PngBitmapEncoder().CodecInfo.ContainerFormat)); + Settings(s => s.FallbackEncoder = PngContainerFormatGuid)); - operation.Execute(); + await operation.ExecuteAsync(); CollectionAssert.Contains(_directory.FileNames.ToList(), "Test (Test).png"); } [TestMethod] - public void TransformIgnoresOrientationWhenLandscapeToPortrait() + public async Task TransformIgnoresOrientationWhenLandscapeToPortrait() { var operation = new ResizeOperation( "Test.png", @@ -136,19 +154,19 @@ namespace ImageResizer.Models x.SelectedSize.Height = 192; })); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image( + await AssertEx.ImageAsync( _directory.File(), - image => + decoder => { - Assert.AreEqual(192, image.Frames[0].PixelWidth); - Assert.AreEqual(96, image.Frames[0].PixelHeight); + Assert.AreEqual(192u, decoder.PixelWidth); + Assert.AreEqual(96u, decoder.PixelHeight); }); } [TestMethod] - public void TransformIgnoresOrientationWhenPortraitToLandscape() + public async Task TransformIgnoresOrientationWhenPortraitToLandscape() { var operation = new ResizeOperation( "TestPortrait.png", @@ -161,19 +179,19 @@ namespace ImageResizer.Models x.SelectedSize.Height = 96; })); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image( + await AssertEx.ImageAsync( _directory.File(), - image => + decoder => { - Assert.AreEqual(96, image.Frames[0].PixelWidth); - Assert.AreEqual(192, image.Frames[0].PixelHeight); + Assert.AreEqual(96u, decoder.PixelWidth); + Assert.AreEqual(192u, decoder.PixelHeight); }); } [TestMethod] - public void TransformIgnoresIgnoreOrientationWhenAuto() + public async Task TransformIgnoresIgnoreOrientationWhenAuto() { var operation = new ResizeOperation( "Test.png", @@ -186,19 +204,19 @@ namespace ImageResizer.Models x.SelectedSize.Height = 0; })); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image( + await AssertEx.ImageAsync( _directory.File(), - image => + decoder => { - Assert.AreEqual(96, image.Frames[0].PixelWidth); - Assert.AreEqual(48, image.Frames[0].PixelHeight); + Assert.AreEqual(96u, decoder.PixelWidth); + Assert.AreEqual(48u, decoder.PixelHeight); }); } [TestMethod] - public void TransformIgnoresIgnoreOrientationWhenPercent() + public async Task TransformIgnoresIgnoreOrientationWhenPercent() { var operation = new ResizeOperation( "Test.png", @@ -213,19 +231,19 @@ namespace ImageResizer.Models x.SelectedSize.Fit = ResizeFit.Stretch; })); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image( + await AssertEx.ImageAsync( _directory.File(), - image => + decoder => { - Assert.AreEqual(96, image.Frames[0].PixelWidth); - Assert.AreEqual(192, image.Frames[0].PixelHeight); + Assert.AreEqual(96u, decoder.PixelWidth); + Assert.AreEqual(192u, decoder.PixelHeight); }); } [TestMethod] - public void TransformHonorsShrinkOnly() + public async Task TransformHonorsShrinkOnly() { var operation = new ResizeOperation( "Test.png", @@ -238,19 +256,19 @@ namespace ImageResizer.Models x.SelectedSize.Height = 288; })); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image( + await AssertEx.ImageAsync( _directory.File(), - image => + decoder => { - Assert.AreEqual(192, image.Frames[0].PixelWidth); - Assert.AreEqual(96, image.Frames[0].PixelHeight); + Assert.AreEqual(192u, decoder.PixelWidth); + Assert.AreEqual(96u, decoder.PixelHeight); }); } [TestMethod] - public void TransformIgnoresShrinkOnlyWhenPercent() + public async Task TransformIgnoresShrinkOnlyWhenPercent() { var operation = new ResizeOperation( "Test.png", @@ -263,19 +281,19 @@ namespace ImageResizer.Models x.SelectedSize.Unit = ResizeUnit.Percent; })); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image( + await AssertEx.ImageAsync( _directory.File(), - image => + decoder => { - Assert.AreEqual(256, image.Frames[0].PixelWidth); - Assert.AreEqual(128, image.Frames[0].PixelHeight); + Assert.AreEqual(256u, decoder.PixelWidth); + Assert.AreEqual(128u, decoder.PixelHeight); }); } [TestMethod] - public void TransformHonorsShrinkOnlyWhenAutoHeight() + public async Task TransformHonorsShrinkOnlyWhenAutoHeight() { var operation = new ResizeOperation( "Test.png", @@ -288,15 +306,15 @@ namespace ImageResizer.Models x.SelectedSize.Height = 0; })); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image( + await AssertEx.ImageAsync( _directory.File(), - image => Assert.AreEqual(192, image.Frames[0].PixelWidth)); + decoder => Assert.AreEqual(192u, decoder.PixelWidth)); } [TestMethod] - public void TransformHonorsShrinkOnlyWhenAutoWidth() + public async Task TransformHonorsShrinkOnlyWhenAutoWidth() { var operation = new ResizeOperation( "Test.png", @@ -309,15 +327,15 @@ namespace ImageResizer.Models x.SelectedSize.Height = 288; })); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image( + await AssertEx.ImageAsync( _directory.File(), - image => Assert.AreEqual(96, image.Frames[0].PixelHeight)); + decoder => Assert.AreEqual(96u, decoder.PixelHeight)); } [TestMethod] - public void TransformHonorsUnit() + public async Task TransformHonorsUnit() { var operation = new ResizeOperation( "Test.png", @@ -330,82 +348,79 @@ namespace ImageResizer.Models x.SelectedSize.Unit = ResizeUnit.Inch; })); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image(_directory.File(), image => Assert.AreEqual(Math.Ceiling(image.Frames[0].DpiX), image.Frames[0].PixelWidth)); + await AssertEx.ImageAsync(_directory.File(), decoder => Assert.AreEqual((uint)Math.Ceiling(decoder.DpiX), decoder.PixelWidth)); } [TestMethod] - public void TransformHonorsFitWhenFit() + public async Task TransformHonorsFitWhenFit() { var operation = new ResizeOperation( "Test.png", _directory, Settings(x => x.SelectedSize.Fit = ResizeFit.Fit)); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image( + await AssertEx.ImageAsync( _directory.File(), - image => + decoder => { - Assert.AreEqual(96, image.Frames[0].PixelWidth); - Assert.AreEqual(48, image.Frames[0].PixelHeight); + Assert.AreEqual(96u, decoder.PixelWidth); + Assert.AreEqual(48u, decoder.PixelHeight); }); } [TestMethod] - public void TransformHonorsFitWhenFill() + public async Task TransformHonorsFitWhenFill() { var operation = new ResizeOperation( "Test.png", _directory, Settings(x => x.SelectedSize.Fit = ResizeFit.Fill)); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image( + await AssertEx.ImageAsync( _directory.File(), - image => + async decoder => { - Assert.AreEqual(Colors.White, image.Frames[0].GetFirstPixel()); - Assert.AreEqual(96, image.Frames[0].PixelWidth); - Assert.AreEqual(96, image.Frames[0].PixelHeight); + var pixel = await decoder.GetFirstPixelAsync(); + Assert.AreEqual((byte)255, pixel.R, "First pixel R should be 255 (white)"); + Assert.AreEqual((byte)255, pixel.G, "First pixel G should be 255 (white)"); + Assert.AreEqual((byte)255, pixel.B, "First pixel B should be 255 (white)"); + Assert.AreEqual(96u, decoder.PixelWidth); + Assert.AreEqual(96u, decoder.PixelHeight); }); } [TestMethod] - public void TransformHonorsFitWhenStretch() + public async Task TransformHonorsFitWhenStretch() { var operation = new ResizeOperation( "Test.png", _directory, Settings(x => x.SelectedSize.Fit = ResizeFit.Stretch)); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image( + await AssertEx.ImageAsync( _directory.File(), - image => + async decoder => { - Assert.AreEqual(Colors.Black, image.Frames[0].GetFirstPixel()); - Assert.AreEqual(96, image.Frames[0].PixelWidth); - Assert.AreEqual(96, image.Frames[0].PixelHeight); + var pixel = await decoder.GetFirstPixelAsync(); + Assert.AreEqual((byte)0, pixel.R, "First pixel R should be 0 (black)"); + Assert.AreEqual((byte)0, pixel.G, "First pixel G should be 0 (black)"); + Assert.AreEqual((byte)0, pixel.B, "First pixel B should be 0 (black)"); + Assert.AreEqual(96u, decoder.PixelWidth); + Assert.AreEqual(96u, decoder.PixelHeight); }); } [TestMethod] - public void TransformHonorsFillWithShrinkOnlyWhenCropRequired() + public async Task TransformHonorsFillWithShrinkOnlyWhenCropRequired() { - // Testing original 96x96 pixel Test.jpg cropped to 48x96 (Fill mode). - // - // ScaleX = 48/96 = 0.5 - // ScaleY = 96/96 = 1.0 - // Fill mode takes the max of these = 1.0. - // - // Previously, the transform logic saw the scale of 1.0 and returned the - // original dimensions. The corrected logic recognizes that a crop is - // required on one dimension and proceeds with the operation. var operation = new ResizeOperation( "Test.jpg", _directory, @@ -417,22 +432,20 @@ namespace ImageResizer.Models x.SelectedSize.Height = 96; })); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image( + await AssertEx.ImageAsync( _directory.File(), - image => + decoder => { - Assert.AreEqual(48, image.Frames[0].PixelWidth); - Assert.AreEqual(96, image.Frames[0].PixelHeight); + Assert.AreEqual(48u, decoder.PixelWidth); + Assert.AreEqual(96u, decoder.PixelHeight); }); } [TestMethod] - public void TransformHonorsFillWithShrinkOnlyWhenUpscaleAttempted() + public async Task TransformHonorsFillWithShrinkOnlyWhenUpscaleAttempted() { - // Confirm that attempting to upscale the original image will return the - // original dimensions when Shrink Only is enabled. var operation = new ResizeOperation( "Test.jpg", _directory, @@ -444,21 +457,20 @@ namespace ImageResizer.Models x.SelectedSize.Height = 192; })); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image( + await AssertEx.ImageAsync( _directory.File(), - image => + decoder => { - Assert.AreEqual(96, image.Frames[0].PixelWidth); - Assert.AreEqual(96, image.Frames[0].PixelHeight); + Assert.AreEqual(96u, decoder.PixelWidth); + Assert.AreEqual(96u, decoder.PixelHeight); }); } [TestMethod] - public void TransformHonorsFillWithShrinkOnlyWhenNoChangeRequired() + public async Task TransformHonorsFillWithShrinkOnlyWhenNoChangeRequired() { - // With a scale of 1.0 on both axes, the original should be returned. var operation = new ResizeOperation( "Test.jpg", _directory, @@ -470,70 +482,70 @@ namespace ImageResizer.Models x.SelectedSize.Height = 96; })); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image( + await AssertEx.ImageAsync( _directory.File(), - image => + decoder => { - Assert.AreEqual(96, image.Frames[0].PixelWidth); - Assert.AreEqual(96, image.Frames[0].PixelHeight); + Assert.AreEqual(96u, decoder.PixelWidth); + Assert.AreEqual(96u, decoder.PixelHeight); }); } [TestMethod] - public void GetDestinationPathUniquifiesOutputFilename() + public async Task GetDestinationPathUniquifiesOutputFilename() { File.WriteAllBytes(Path.Combine(_directory, "Test (Test).png"), Array.Empty()); var operation = new ResizeOperation("Test.png", _directory, Settings()); - operation.Execute(); + await operation.ExecuteAsync(); CollectionAssert.Contains(_directory.FileNames.ToList(), "Test (Test) (1).png"); } [TestMethod] - public void GetDestinationPathUniquifiesOutputFilenameAgain() + public async Task GetDestinationPathUniquifiesOutputFilenameAgain() { File.WriteAllBytes(Path.Combine(_directory, "Test (Test).png"), Array.Empty()); File.WriteAllBytes(Path.Combine(_directory, "Test (Test) (1).png"), Array.Empty()); var operation = new ResizeOperation("Test.png", _directory, Settings()); - operation.Execute(); + await operation.ExecuteAsync(); CollectionAssert.Contains(_directory.FileNames.ToList(), "Test (Test) (2).png"); } [TestMethod] - public void GetDestinationPathUsesFileNameFormat() + public async Task GetDestinationPathUsesFileNameFormat() { var operation = new ResizeOperation( "Test.png", _directory, Settings(s => s.FileName = "%1_%2_%3_%4_%5_%6")); - operation.Execute(); + await operation.ExecuteAsync(); CollectionAssert.Contains(_directory.FileNames.ToList(), "Test_Test_96_96_96_48.png"); } [TestMethod] - public void ExecuteHandlesDirectoriesInFileNameFormat() + public async Task ExecuteHandlesDirectoriesInFileNameFormat() { var operation = new ResizeOperation( "Test.png", _directory, Settings(s => s.FileName = @"Directory\%1 (%2)")); - operation.Execute(); + await operation.ExecuteAsync(); Assert.IsTrue(File.Exists(_directory + @"\Directory\Test (Test).png")); } [TestMethod] - public void StripMetadata() + public async Task StripMetadata() { var operation = new ResizeOperation( "TestMetadataIssue1928.jpg", @@ -544,18 +556,26 @@ namespace ImageResizer.Models x.RemoveMetadata = true; })); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image( + await AssertEx.ImageAsync( _directory.File(), - image => Assert.IsNull(((BitmapMetadata)image.Frames[0].Metadata).DateTaken)); - AssertEx.Image( - _directory.File(), - image => Assert.IsNotNull(((BitmapMetadata)image.Frames[0].Metadata).GetQuerySafe("System.Photo.Orientation"))); + async decoder => + { + try + { + var props = await decoder.BitmapProperties.GetPropertiesAsync(DateTakenPropertyQuery); + Assert.IsFalse(props.ContainsKey("System.Photo.DateTaken"), "DateTaken should be stripped"); + } + catch (Exception) + { + // If GetPropertiesAsync throws, metadata is not present — which is expected + } + }); } [TestMethod] - public void StripMetadataWhenNoMetadataPresent() + public async Task StripMetadataWhenNoMetadataPresent() { var operation = new ResizeOperation( "TestMetadataIssue1928_NoMetadata.jpg", @@ -566,18 +586,26 @@ namespace ImageResizer.Models x.RemoveMetadata = true; })); - operation.Execute(); + await operation.ExecuteAsync(); - AssertEx.Image( + await AssertEx.ImageAsync( _directory.File(), - image => Assert.IsNull(((BitmapMetadata)image.Frames[0].Metadata).DateTaken)); - AssertEx.Image( - _directory.File(), - image => Assert.IsNull(((BitmapMetadata)image.Frames[0].Metadata).GetQuerySafe("System.Photo.Orientation"))); + async decoder => + { + try + { + var props = await decoder.BitmapProperties.GetPropertiesAsync(DateTakenPropertyQuery); + Assert.IsFalse(props.ContainsKey("System.Photo.DateTaken"), "DateTaken should not exist"); + } + catch (Exception) + { + // Expected: no metadata block at all + } + }); } [TestMethod] - public void VerifyFileNameIsSanitized() + public async Task VerifyFileNameIsSanitized() { var operation = new ResizeOperation( "Test.png", @@ -589,13 +617,13 @@ namespace ImageResizer.Models s.SelectedSize.Name = "Test\\/"; })); - operation.Execute(); + await operation.ExecuteAsync(); Assert.IsTrue(File.Exists(_directory + @"\Directory\Test_______(Test__).png")); } [TestMethod] - public void VerifyNotRecommendedNameIsChanged() + public async Task VerifyNotRecommendedNameIsChanged() { var operation = new ResizeOperation( "Test.png", @@ -606,7 +634,7 @@ namespace ImageResizer.Models s.FileName = @"nul"; })); - operation.Execute(); + await operation.ExecuteAsync(); Assert.IsTrue(File.Exists(_directory + @"\nul_.png")); } diff --git a/src/modules/imageresizer/tests/Models/ResizeSizeTests.cs b/src/modules/imageresizer/tests/Models/ResizeSizeTests.cs index 13c99131ac..83e542d449 100644 --- a/src/modules/imageresizer/tests/Models/ResizeSizeTests.cs +++ b/src/modules/imageresizer/tests/Models/ResizeSizeTests.cs @@ -5,10 +5,10 @@ #pragma warning restore IDE0073 using System; -using System.Collections.Generic; using System.ComponentModel; +using System.Linq; -using ImageResizer.Properties; +using ImageResizer.Helpers; using ImageResizer.Test; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -17,6 +17,12 @@ namespace ImageResizer.Models [TestClass] public class ResizeSizeTests { + [ClassInitialize] + public static void ClassInit(TestContext context) + { + ResourceLoaderInstance.GetString = key => key; + } + [TestMethod] public void NameWorks() { @@ -34,22 +40,11 @@ namespace ImageResizer.Models [TestMethod] public void NameReplacesTokens() { - var args = new List<(string, string)> - { - ("$small$", Resources.Small), - ("$medium$", Resources.Medium), - ("$large$", Resources.Large), - ("$phone$", Resources.Phone), - }; - foreach (var (name, expected) in args) - { - var size = new ResizeSize - { - Name = name, - }; + var size = new ResizeSize(); - Assert.AreEqual(expected, size.Name); - } + size.Name = "$small$"; + + Assert.AreEqual("Small", size.Name); } [TestMethod] @@ -57,13 +52,15 @@ namespace ImageResizer.Models { var size = new ResizeSize(); - var e = AssertEx.Raises( + var events = AssertEx.RaisesAll( h => size.PropertyChanged += h, h => size.PropertyChanged -= h, () => size.Fit = ResizeFit.Stretch); Assert.AreEqual(ResizeFit.Stretch, size.Fit); - Assert.AreEqual(nameof(ResizeSize.Fit), e.Arguments.PropertyName); + Assert.IsTrue( + events.Any(e => e.Arguments.PropertyName == nameof(ResizeSize.Fit)), + "Expected PropertyChanged for Fit"); } [TestMethod] @@ -135,13 +132,15 @@ namespace ImageResizer.Models { var size = new ResizeSize(); - var e = AssertEx.Raises( + var events = AssertEx.RaisesAll( h => size.PropertyChanged += h, h => size.PropertyChanged -= h, () => size.Unit = ResizeUnit.Inch); Assert.AreEqual(ResizeUnit.Inch, size.Unit); - Assert.AreEqual(nameof(ResizeSize.Unit), e.Arguments.PropertyName); + Assert.IsTrue( + events.Any(e => e.Arguments.PropertyName == nameof(ResizeSize.Unit)), + "Expected PropertyChanged for Unit"); } [TestMethod] diff --git a/src/modules/imageresizer/tests/Properties/SettingsTests.cs b/src/modules/imageresizer/tests/Properties/SettingsTests.cs index 16cbceae6e..ed741bcbdc 100644 --- a/src/modules/imageresizer/tests/Properties/SettingsTests.cs +++ b/src/modules/imageresizer/tests/Properties/SettingsTests.cs @@ -11,6 +11,7 @@ using System.Linq; using System.Text; using System.Text.Json; +using ImageResizer.Helpers; using ImageResizer.Models; using ImageResizer.Test; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -25,10 +26,6 @@ namespace ImageResizer.Properties WriteIndented = true, }; - private static readonly CompositeFormat ValueMustBeBetween = System.Text.CompositeFormat.Parse(Properties.Resources.ValueMustBeBetween); - - private static App _imageResizerApp; - public SettingsTests() { // Change settings.json path to a temp file @@ -38,8 +35,7 @@ namespace ImageResizer.Properties [ClassInitialize] public static void ClassInitialize(TestContext context) { - // new App() needs to be created since Settings.Reload() uses App.Current to update properties on the UI thread. App() can be created only once otherwise it results in System.InvalidOperationException : Cannot create more than one System.Windows.Application instance in the same AppDomain. - _imageResizerApp = new App(); + ResourceLoaderInstance.GetString = key => key; } [TestMethod] @@ -193,9 +189,11 @@ namespace ImageResizer.Properties var result = ((IDataErrorInfo)settings)["JpegQualityLevel"]; - // Using InvariantCulture since this is used internally + // With test ResourceLoaderInstance, GetString returns the key itself ("ValueMustBeBetween") + // which becomes the CompositeFormat. Format it the same way the production code does. + var expectedFormat = CompositeFormat.Parse(ResourceLoaderInstance.GetString("ValueMustBeBetween")); Assert.AreEqual( - string.Format(CultureInfo.InvariantCulture, ValueMustBeBetween, 1, 100), + string.Format(CultureInfo.InvariantCulture, expectedFormat, 1, 100), result); } @@ -275,7 +273,11 @@ namespace ImageResizer.Properties { // Arrange var settings = new Settings(); - settings.Save(); // To create the settings file + settings.SelectedSizeIndex = 2; + settings.Save(); + + // Reset to default so Reload will trigger a real change + settings.SelectedSizeIndex = 0; var shrinkOnlyChanged = false; var replaceChanged = false; @@ -385,8 +387,7 @@ namespace ImageResizer.Properties [ClassCleanup] public static void ClassCleanup() { - _imageResizerApp.Dispose(); - _imageResizerApp = null; + // No App instance to dispose in WinUI3 test environment } [TestCleanup] diff --git a/src/modules/imageresizer/tests/Test/AssertEx.cs b/src/modules/imageresizer/tests/Test/AssertEx.cs index 4cb9a2067b..69c1978d26 100644 --- a/src/modules/imageresizer/tests/Test/AssertEx.cs +++ b/src/modules/imageresizer/tests/Test/AssertEx.cs @@ -8,11 +8,14 @@ using System; using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; +using System.IO; using System.IO.Abstractions; -using System.Windows.Media.Imaging; +using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Windows.Graphics.Imaging; + [module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1636:FileHeaderCopyrightTextMustMatch", Justification = "File created under PowerToys.")] namespace ImageResizer.Test @@ -29,17 +32,20 @@ namespace ImageResizer.Test } } - public static void Image(string path, Action action) + public static async Task ImageAsync(string path, Action action) { - using (var stream = _fileSystem.File.OpenRead(path)) - { - var image = BitmapDecoder.Create( - stream, - BitmapCreateOptions.PreservePixelFormat, - BitmapCacheOption.None); + using var stream = _fileSystem.File.OpenRead(path); + var winrtStream = stream.AsRandomAccessStream(); + var decoder = await BitmapDecoder.CreateAsync(winrtStream); + action(decoder); + } - action(image); - } + public static async Task ImageAsync(string path, Func action) + { + using var stream = _fileSystem.File.OpenRead(path); + var winrtStream = stream.AsRandomAccessStream(); + var decoder = await BitmapDecoder.CreateAsync(winrtStream); + await action(decoder); } public static RaisedEvent Raises( @@ -78,6 +84,24 @@ namespace ImageResizer.Test return raisedEvent; } + public static IList> RaisesAll( + Action attach, + Action detach, + Action testCode) + where T : PropertyChangedEventArgs + { + var events = new List>(); + PropertyChangedEventHandler handler = (sender, e) + => events.Add(new RaisedEvent(sender, e)); + attach(handler); + testCode(); + detach(handler); + + Assert.IsTrue(events.Count > 0, "Expected at least one PropertyChanged event."); + + return events; + } + public sealed class RaisedEvent { public RaisedEvent(object sender, TArgs args) diff --git a/src/modules/imageresizer/tests/Test/BitmapSourceExtensions.cs b/src/modules/imageresizer/tests/Test/BitmapSourceExtensions.cs index ed25e800f4..19d53dd113 100644 --- a/src/modules/imageresizer/tests/Test/BitmapSourceExtensions.cs +++ b/src/modules/imageresizer/tests/Test/BitmapSourceExtensions.cs @@ -1,28 +1,34 @@ -#pragma warning disable IDE0073 +#pragma warning disable IDE0073 // Copyright (c) Brice Lambson // The Brice Lambson licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ #pragma warning restore IDE0073 -using System.Windows; -using System.Windows.Media; -using System.Windows.Media.Imaging; +using System; +using System.Threading.Tasks; +using Windows.Graphics.Imaging; +using Windows.Storage.Streams; namespace ImageResizer.Test { internal static class BitmapSourceExtensions { - public static Color GetFirstPixel(this BitmapSource source) + public static async Task<(byte R, byte G, byte B, byte A)> GetFirstPixelAsync(this BitmapDecoder decoder) { - var pixel = new byte[4]; - new FormatConvertedBitmap( - new CroppedBitmap(source, new Int32Rect(0, 0, 1, 1)), - PixelFormats.Bgra32, - destinationPalette: null, - alphaThreshold: 0) - .CopyPixels(pixel, 4, 0); + using var softwareBitmap = await decoder.GetSoftwareBitmapAsync( + BitmapPixelFormat.Bgra8, + BitmapAlphaMode.Premultiplied); - return Color.FromArgb(pixel[3], pixel[2], pixel[1], pixel[0]); + var buffer = new Windows.Storage.Streams.Buffer((uint)(softwareBitmap.PixelWidth * softwareBitmap.PixelHeight * 4)); + softwareBitmap.CopyToBuffer(buffer); + + using var reader = DataReader.FromBuffer(buffer); + byte b = reader.ReadByte(); + byte g = reader.ReadByte(); + byte r = reader.ReadByte(); + byte a = reader.ReadByte(); + + return (r, g, b, a); } } } diff --git a/src/modules/imageresizer/tests/Views/TimeRemainingConverterTests.cs b/src/modules/imageresizer/tests/Views/TimeRemainingConverterTests.cs deleted file mode 100644 index 5ae1cf5f10..0000000000 --- a/src/modules/imageresizer/tests/Views/TimeRemainingConverterTests.cs +++ /dev/null @@ -1,50 +0,0 @@ -#pragma warning disable IDE0073 -// Copyright (c) Brice Lambson -// The Brice Lambson licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 - -using System; -using System.Globalization; - -using ImageResizer.Properties; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace ImageResizer.Views -{ - public class TimeRemainingConverterTests - { - [DataTestMethod] - [DataRow("HourMinute", 1, 1, 0)] - [DataRow("HourMinutes", 1, 2, 0)] - [DataRow("HoursMinute", 2, 1, 0)] - [DataRow("HoursMinutes", 2, 2, 0)] - [DataRow("MinuteSecond", 0, 1, 1)] - [DataRow("MinuteSeconds", 0, 1, 2)] - [DataRow("MinutesSecond", 0, 2, 1)] - [DataRow("MinutesSeconds", 0, 2, 2)] - [DataRow("Second", 0, 0, 1)] - [DataRow("Seconds", 0, 0, 2)] - public void ConvertWorks(string resource, int hours, int minutes, int seconds) - { - var timeRemaining = new TimeSpan(hours, minutes, seconds); - var converter = new TimeRemainingConverter(); - - // Using InvariantCulture since these are internal - var result = converter.Convert( - timeRemaining, - targetType: null, - parameter: null, - CultureInfo.InvariantCulture); - - Assert.AreEqual( - string.Format( - CultureInfo.InvariantCulture, - Resources.ResourceManager.GetString("Progress_TimeRemaining_" + resource, CultureInfo.InvariantCulture), - hours, - minutes, - seconds), - result); - } - } -} diff --git a/src/modules/imageresizer/ui/App.xaml b/src/modules/imageresizer/ui/App.xaml deleted file mode 100644 index be9316dbd8..0000000000 --- a/src/modules/imageresizer/ui/App.xaml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/modules/imageresizer/ui/App.xaml.cs b/src/modules/imageresizer/ui/App.xaml.cs deleted file mode 100644 index 9977a8a474..0000000000 --- a/src/modules/imageresizer/ui/App.xaml.cs +++ /dev/null @@ -1,243 +0,0 @@ -#pragma warning disable IDE0073 -// Copyright (c) Brice Lambson -// The Brice Lambson licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 - -using System; -using System.Globalization; -using System.Runtime.InteropServices; -using System.Text; -using System.Windows; -using ImageResizer.Models; -using ImageResizer.Properties; -using ImageResizer.Utilities; -using ImageResizer.ViewModels; -using ImageResizer.Views; -using ManagedCommon; - -namespace ImageResizer -{ - public partial class App : Application, IDisposable - { - private const string LogSubFolder = "\\Image Resizer\\Logs"; - - /// - /// Gets cached AI availability state, checked at app startup. - /// Can be updated after model download completes or background initialization. - /// - public static AiAvailabilityState AiAvailabilityState { get; internal set; } - - /// - /// Event fired when AI initialization completes in background. - /// Allows UI to refresh state when initialization finishes. - /// - public static event EventHandler AiInitializationCompleted; - - static App() - { - try - { - // Initialize logger early (mirroring PowerOCR pattern) - Logger.InitializeLogger(LogSubFolder); - } - catch - { - /* swallow logger init issues silently */ - } - - try - { - string appLanguage = LanguageHelper.LoadLanguage(); - if (!string.IsNullOrEmpty(appLanguage)) - { - System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(appLanguage); - } - } - catch (CultureNotFoundException ex) - { - Logger.LogError("CultureNotFoundException: " + ex.Message); - } - - Console.InputEncoding = Encoding.Unicode; - } - - protected override void OnStartup(StartupEventArgs e) - { - // Fix for .net 3.1.19 making Image Resizer not adapt to DPI changes. - NativeMethods.SetProcessDPIAware(); - - // TODO: Re-enable AI Super Resolution in next release by removing this #if block - // Temporarily disable AI Super Resolution feature (hide from UI but keep code) -#if true // Set to false to re-enable AI Super Resolution - AiAvailabilityState = AiAvailabilityState.NotSupported; - ResizeBatch.SetAiSuperResolutionService(Services.NoOpAiSuperResolutionService.Instance); - - // Skip AI detection mode as well - if (e?.Args?.Length > 0 && e.Args[0] == "--detect-ai") - { - Services.AiAvailabilityCacheService.SaveCache(AiAvailabilityState.NotSupported); - Environment.Exit(0); - return; - } -#else - // Check for AI detection mode (called by Runner in background) - if (e?.Args?.Length > 0 && e.Args[0] == "--detect-ai") - { - RunAiDetectionMode(); - return; - } -#endif - - if (PowerToys.GPOWrapperProjection.GPOWrapper.GetConfiguredImageResizerEnabledValue() == PowerToys.GPOWrapperProjection.GpoRuleConfigured.Disabled) - { - /* TODO: Add logs to ImageResizer. - * Logger.LogWarning("Tried to start with a GPO policy setting the utility to always be disabled. Please contact your systems administrator."); - */ - Logger.LogWarning("GPO policy disables ImageResizer. Exiting."); - Environment.Exit(0); // Current.Exit won't work until there's a window opened. - return; - } - - // AI Super Resolution is not supported on Windows 10 - skip cache check entirely - if (OSVersionHelper.IsWindows10()) - { - AiAvailabilityState = AiAvailabilityState.NotSupported; - ResizeBatch.SetAiSuperResolutionService(Services.NoOpAiSuperResolutionService.Instance); - Logger.LogInfo("AI Super Resolution not supported on Windows 10"); - } - else - { - // Load AI availability from cache (written by Runner's background detection) - var cachedState = Services.AiAvailabilityCacheService.LoadCache(); - - if (cachedState.HasValue) - { - AiAvailabilityState = cachedState.Value; - Logger.LogInfo($"AI state loaded from cache: {AiAvailabilityState}"); - } - else - { - // No valid cache - default to NotSupported (Runner will detect and cache for next startup) - AiAvailabilityState = AiAvailabilityState.NotSupported; - Logger.LogInfo("No AI cache found, defaulting to NotSupported"); - } - - // If AI is potentially available, start background initialization (non-blocking) - if (AiAvailabilityState == AiAvailabilityState.Ready) - { - _ = InitializeAiServiceAsync(); // Fire and forget - don't block UI - } - else - { - // AI not available - set NoOp service immediately - ResizeBatch.SetAiSuperResolutionService(Services.NoOpAiSuperResolutionService.Instance); - } - } - - var batch = ResizeBatch.FromCommandLine(Console.In, e?.Args); - - // TODO: Add command-line parameters that can be used in lieu of the input page (issue #14) - var mainWindow = new MainWindow(new MainViewModel(batch, Settings.Default)); - mainWindow.Show(); - - // Temporary workaround for issue #1273 - WindowHelpers.BringToForeground(new System.Windows.Interop.WindowInteropHelper(mainWindow).Handle); - } - - /// - /// AI detection mode: perform detection, write to cache, and exit. - /// Called by Runner in background to avoid blocking ImageResizer UI startup. - /// - private void RunAiDetectionMode() - { - try - { - Logger.LogInfo("Running AI detection mode..."); - - // AI Super Resolution is not supported on Windows 10 - if (OSVersionHelper.IsWindows10()) - { - Logger.LogInfo("AI detection skipped: Windows 10 does not support AI Super Resolution"); - Services.AiAvailabilityCacheService.SaveCache(AiAvailabilityState.NotSupported); - Environment.Exit(0); - return; - } - - // Perform detection (reuse existing logic) - var state = CheckAiAvailability(); - - // Write result to cache file - Services.AiAvailabilityCacheService.SaveCache(state); - - Logger.LogInfo($"AI detection complete: {state}"); - } - catch (Exception ex) - { - Logger.LogError($"AI detection failed: {ex.Message}"); - Services.AiAvailabilityCacheService.SaveCache(AiAvailabilityState.NotSupported); - } - - // Exit silently without showing UI - Environment.Exit(0); - } - - /// - /// Check AI Super Resolution availability on this system. - /// Performs architecture check and model availability check. - /// - private static AiAvailabilityState CheckAiAvailability() - { - // AI feature disabled - always return NotSupported - return AiAvailabilityState.NotSupported; - } - - /// - /// Initialize AI Super Resolution service asynchronously in background. - /// Runs without blocking UI startup - state change event notifies completion. - /// - private static async System.Threading.Tasks.Task InitializeAiServiceAsync() - { - AiAvailabilityState finalState; - - try - { - // Create and initialize AI service using async factory - var aiService = await Services.WinAiSuperResolutionService.CreateAsync(); - - if (aiService != null) - { - ResizeBatch.SetAiSuperResolutionService(aiService); - Logger.LogInfo("AI Super Resolution service initialized successfully."); - finalState = AiAvailabilityState.Ready; - } - else - { - // Initialization failed - use default NoOp service - ResizeBatch.SetAiSuperResolutionService(Services.NoOpAiSuperResolutionService.Instance); - Logger.LogWarning("AI Super Resolution service initialization failed. Using default service."); - finalState = AiAvailabilityState.NotSupported; - } - } - catch (Exception ex) - { - // Log error and use default NoOp service - ResizeBatch.SetAiSuperResolutionService(Services.NoOpAiSuperResolutionService.Instance); - Logger.LogError($"Exception during AI service initialization: {ex.Message}"); - finalState = AiAvailabilityState.NotSupported; - } - - // Update cached state and notify listeners - AiAvailabilityState = finalState; - AiInitializationCompleted?.Invoke(null, finalState); - } - - public void Dispose() - { - // Dispose AI Super Resolution service - ResizeBatch.DisposeAiSuperResolutionService(); - - GC.SuppressFinalize(this); - } - } -} diff --git a/src/modules/imageresizer/ui/Resources/ImageResizer.ico b/src/modules/imageresizer/ui/Assets/ImageResizer/ImageResizer.ico similarity index 100% rename from src/modules/imageresizer/ui/Resources/ImageResizer.ico rename to src/modules/imageresizer/ui/Assets/ImageResizer/ImageResizer.ico diff --git a/src/modules/imageresizer/ui/Resources/ImageResizer.png b/src/modules/imageresizer/ui/Assets/ImageResizer/ImageResizer.png similarity index 100% rename from src/modules/imageresizer/ui/Resources/ImageResizer.png rename to src/modules/imageresizer/ui/Assets/ImageResizer/ImageResizer.png diff --git a/src/modules/imageresizer/ui/Cli/ImageResizerCliExecutor.cs b/src/modules/imageresizer/ui/Cli/ImageResizerCliExecutor.cs index bd22da62da..851d3e7983 100644 --- a/src/modules/imageresizer/ui/Cli/ImageResizerCliExecutor.cs +++ b/src/modules/imageresizer/ui/Cli/ImageResizerCliExecutor.cs @@ -6,6 +6,7 @@ using System; using System.Globalization; using System.Linq; using System.Threading; +using System.Threading.Tasks; using ImageResizer.Models; using ImageResizer.Properties; @@ -58,10 +59,10 @@ namespace ImageResizer.Cli return 1; } - return RunSilentMode(cliOptions); + return RunSilentModeAsync(cliOptions).GetAwaiter().GetResult(); } - private int RunSilentMode(CliOptions cliOptions) + private async Task RunSilentModeAsync(CliOptions cliOptions) { var batch = ResizeBatch.FromCliOptions(Console.In, cliOptions); var settings = Settings.Default; @@ -73,7 +74,7 @@ namespace ImageResizer.Cli bool useLineBasedProgress = cliOptions.ProgressLines ?? false; int lastReportedMilestone = -1; - var errors = batch.Process( + var errors = await batch.ProcessAsync( (completed, total) => { var progress = (int)((completed / total) * 100); diff --git a/src/modules/imageresizer/ui/Converters/AutoDoubleConverter.cs b/src/modules/imageresizer/ui/Converters/AutoDoubleConverter.cs new file mode 100644 index 0000000000..3a7846bbe4 --- /dev/null +++ b/src/modules/imageresizer/ui/Converters/AutoDoubleConverter.cs @@ -0,0 +1,28 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using ImageResizer.Helpers; +using Microsoft.UI.Xaml.Data; + +namespace ImageResizer.Converters +{ + public partial class AutoDoubleConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, string language) + { + if (value is double d && (d == 0 || double.IsNaN(d))) + { + return ResourceLoaderInstance.GetString("Auto"); + } + + return value?.ToString(); + } + + public object ConvertBack(object value, Type targetType, object parameter, string language) + { + return value; + } + } +} diff --git a/src/modules/imageresizer/ui/Converters/EnumToIntConverter.cs b/src/modules/imageresizer/ui/Converters/EnumToIntConverter.cs new file mode 100644 index 0000000000..cb27efe095 --- /dev/null +++ b/src/modules/imageresizer/ui/Converters/EnumToIntConverter.cs @@ -0,0 +1,32 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using Microsoft.UI.Xaml.Data; + +namespace ImageResizer.Converters +{ + public partial class EnumToIntConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, string language) + { + if (value is Enum) + { + return System.Convert.ToInt32(value); + } + + return 0; + } + + public object ConvertBack(object value, Type targetType, object parameter, string language) + { + if (value is int intValue && targetType.IsEnum) + { + return Enum.ToObject(targetType, intValue); + } + + return value; + } + } +} diff --git a/src/modules/imageresizer/ui/Views/EnumValueConverter.cs b/src/modules/imageresizer/ui/Converters/EnumValueConverter.cs similarity index 63% rename from src/modules/imageresizer/ui/Views/EnumValueConverter.cs rename to src/modules/imageresizer/ui/Converters/EnumValueConverter.cs index 76ed5e2c23..6dd7531819 100644 --- a/src/modules/imageresizer/ui/Views/EnumValueConverter.cs +++ b/src/modules/imageresizer/ui/Converters/EnumValueConverter.cs @@ -1,26 +1,24 @@ -#pragma warning disable IDE0073 +#pragma warning disable IDE0073, SA1636 // Copyright (c) Brice Lambson // The Brice Lambson licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. // Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 +#pragma warning restore IDE0073, SA1636 using System; using System.Globalization; using System.Text; -using System.Windows.Data; +using ImageResizer.Helpers; +using Microsoft.UI.Xaml.Data; -using ImageResizer.Properties; - -namespace ImageResizer.Views +namespace ImageResizer.Converters { - [ValueConversion(typeof(Enum), typeof(string))] - public class EnumValueConverter : IValueConverter + public partial class EnumValueConverter : IValueConverter { - public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + public object Convert(object value, Type targetType, object parameter, string language) { var type = value?.GetType(); - if (!type.IsEnum) + if (type == null || !type.IsEnum) { return value; } @@ -44,20 +42,18 @@ namespace ImageResizer.Views .Append(parameter); } - // Fixes #16792 - Looks like culture defaults to en-US, so wrong resource is being fetched. -#pragma warning disable CA1304 // Specify CultureInfo - var targetValue = Resources.ResourceManager.GetString(builder.ToString()); -#pragma warning restore CA1304 // Specify CultureInfo + var targetValue = ResourceLoaderInstance.GetString(builder.ToString()); - if (toLower) + if (toLower && !string.IsNullOrEmpty(targetValue)) { + var culture = string.IsNullOrEmpty(language) ? CultureInfo.CurrentCulture : new CultureInfo(language); targetValue = targetValue.ToLower(culture); } return targetValue; } - public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + public object ConvertBack(object value, Type targetType, object parameter, string language) => value; } } diff --git a/src/modules/imageresizer/ui/Extensions/BitmapEncoderExtensions.cs b/src/modules/imageresizer/ui/Extensions/BitmapEncoderExtensions.cs deleted file mode 100644 index 963f6f15cc..0000000000 --- a/src/modules/imageresizer/ui/Extensions/BitmapEncoderExtensions.cs +++ /dev/null @@ -1,25 +0,0 @@ -#pragma warning disable IDE0073 -// Copyright (c) Brice Lambson -// The Brice Lambson licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 - -namespace System.Windows.Media.Imaging -{ - internal static class BitmapEncoderExtensions - { - public static bool CanEncode(this BitmapEncoder encoder) - { - try - { - var temp = encoder.CodecInfo; - } - catch (NotSupportedException) - { - return false; - } - - return true; - } - } -} diff --git a/src/modules/imageresizer/ui/Extensions/BitmapMetadataExtension.cs b/src/modules/imageresizer/ui/Extensions/BitmapMetadataExtension.cs deleted file mode 100644 index f5f7449834..0000000000 --- a/src/modules/imageresizer/ui/Extensions/BitmapMetadataExtension.cs +++ /dev/null @@ -1,258 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Windows.Media.Imaging; - -namespace ImageResizer.Extensions -{ - internal static class BitmapMetadataExtension - { - public static void CopyMetadataPropertyTo(this BitmapMetadata source, BitmapMetadata target, string query) - { - if (source == null || target == null || string.IsNullOrWhiteSpace(query)) - { - return; - } - - try - { - var value = source.GetQuerySafe(query); - - if (value == null) - { - return; - } - - target.SetQuery(query, value); - } - catch (InvalidOperationException) - { - // InvalidOperationException is thrown if metadata object is in readonly state. - return; - } - } - - public static object GetQuerySafe(this BitmapMetadata metadata, string query) - { - if (metadata == null || string.IsNullOrWhiteSpace(query)) - { - return null; - } - - try - { - if (metadata.ContainsQuery(query)) - { - return metadata.GetQuery(query); - } - else - { - return null; - } - } - catch (NotSupportedException) - { - // NotSupportedException is throw if the metadata entry is not preset on the target image (e.g. Orientation not set). - return null; - } - } - - public static void RemoveQuerySafe(this BitmapMetadata metadata, string query) - { - if (metadata == null || string.IsNullOrWhiteSpace(query)) - { - return; - } - - try - { - if (metadata.ContainsQuery(query)) - { - metadata.RemoveQuery(query); - } - } - catch (Exception ex) - { - Debug.WriteLine($"Exception while trying to remove metadata entry at position: {query}"); - Debug.WriteLine(ex); - } - } - - public static void SetQuerySafe(this BitmapMetadata metadata, string query, object value) - { - if (metadata == null || string.IsNullOrWhiteSpace(query) || value == null) - { - return; - } - - try - { - metadata.SetQuery(query, value); - } - catch (Exception ex) - { - Debug.WriteLine($"Exception while trying to set metadata {value} at position: {query}"); - Debug.WriteLine(ex); - } - } - - /// - /// Gets all metadata. - /// Iterates recursively through metadata and adds valid items to a list while skipping invalid data items. - /// - /// - /// Invalid data items are items which throw an exception when reading the data with metadata.GetQuery(...). - /// Sometimes Metadata collections are improper closed and cause an exception on IEnumerator.MoveNext(). In this case, we return all data items which were successfully collected so far. - /// - /// - /// metadata path and metadata value of all successfully read data items. - /// - public static List<(string MetadataPath, object Value)> GetListOfMetadata(this BitmapMetadata metadata) - { - var listOfAllMetadata = new List<(string MetadataPath, object Value)>(); - - try - { - GetMetadataRecursively(metadata, string.Empty); - } - catch (Exception ex) - { - Debug.WriteLine($"Exception while trying to iterate recursively over metadata. We were able to read {listOfAllMetadata.Count} metadata entries."); - Debug.WriteLine(ex); - } - - return listOfAllMetadata; - - void GetMetadataRecursively(BitmapMetadata metadata, string query) - { - foreach (string relativeQuery in metadata) - { - string absolutePath = query + relativeQuery; - - object metadataQueryReader = null; - - try - { - metadataQueryReader = GetQueryWithPreCheck(metadata, relativeQuery); - } - catch (Exception ex) - { - Debug.WriteLine($"Removing corrupt metadata property {absolutePath}. Skipping metadata entry | {ex.Message}"); - Debug.WriteLine(ex); - } - - if (metadataQueryReader != null) - { - listOfAllMetadata.Add((absolutePath, metadataQueryReader)); - } - else - { - Debug.WriteLine($"No metadata found for query {absolutePath}. Skipping empty null entry because its invalid."); - } - - if (metadataQueryReader is BitmapMetadata innerMetadata) - { - GetMetadataRecursively(innerMetadata, absolutePath); - } - } - } - - object GetQueryWithPreCheck(BitmapMetadata metadata, string query) - { - if (metadata == null || string.IsNullOrWhiteSpace(query)) - { - return null; - } - - if (metadata.ContainsQuery(query)) - { - return metadata.GetQuery(query); - } - else - { - return null; - } - } - } - - /// - /// Prints all metadata to debug console - /// - /// - /// Intended for debug only!!! - /// - public static void PrintsAllMetadataToDebugOutput(this BitmapMetadata metadata) - { - if (metadata == null) - { - Debug.WriteLine($"Metadata was null."); - } - - var listOfMetadata = metadata.GetListOfMetadataForDebug(); - foreach (var metadataItem in listOfMetadata) - { - // Debug.WriteLine($"modifiableMetadata.RemoveQuerySafe(\"{metadataItem.metadataPath}\");"); - Debug.WriteLine($"{metadataItem.MetadataPath} | {metadataItem.Value}"); - } - } - - /// - /// Gets all metadata - /// Iterates recursively through all metadata - /// - /// - /// Intended for debug only!!! - /// - public static List<(string MetadataPath, object Value)> GetListOfMetadataForDebug(this BitmapMetadata metadata) - { - var listOfAllMetadata = new List<(string MetadataPath, object Value)>(); - - try - { - GetMetadataRecursively(metadata, string.Empty); - } - catch (Exception ex) - { - Debug.WriteLine($"Exception while trying to iterate recursively over metadata. We were able to read {listOfAllMetadata.Count} metadata entries."); - Debug.WriteLine(ex); - } - - return listOfAllMetadata; - - void GetMetadataRecursively(BitmapMetadata metadata, string query) - { - if (metadata == null) - { - return; - } - - foreach (string relativeQuery in metadata) - { - string absolutePath = query + relativeQuery; - - object metadataQueryReader = null; - - try - { - metadataQueryReader = metadata.GetQuerySafe(relativeQuery); - listOfAllMetadata.Add((absolutePath, metadataQueryReader)); - } - catch (Exception ex) - { - listOfAllMetadata.Add((absolutePath, $"######## INVALID METADATA: {ex.Message}")); - Debug.WriteLine(ex); - } - - if (metadataQueryReader is BitmapMetadata innerMetadata) - { - GetMetadataRecursively(innerMetadata, absolutePath); - } - } - } - } - } -} diff --git a/src/modules/imageresizer/ui/Extensions/ICollectionExtensions.cs b/src/modules/imageresizer/ui/Extensions/ICollectionExtensions.cs index 11d11ddc22..6be73628bd 100644 --- a/src/modules/imageresizer/ui/Extensions/ICollectionExtensions.cs +++ b/src/modules/imageresizer/ui/Extensions/ICollectionExtensions.cs @@ -1,8 +1,8 @@ -#pragma warning disable IDE0073 +#pragma warning disable IDE0073, SA1636 // Copyright (c) Brice Lambson // The Brice Lambson licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 +#pragma warning restore IDE0073, SA1636 namespace System.Collections.Generic { diff --git a/src/modules/imageresizer/ui/Extensions/TimeSpanExtensions.cs b/src/modules/imageresizer/ui/Extensions/TimeSpanExtensions.cs deleted file mode 100644 index 252a67f0ff..0000000000 --- a/src/modules/imageresizer/ui/Extensions/TimeSpanExtensions.cs +++ /dev/null @@ -1,14 +0,0 @@ -#pragma warning disable IDE0073 -// Copyright (c) Brice Lambson -// The Brice Lambson licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 - -namespace System -{ - internal static class TimeSpanExtensions - { - public static TimeSpan Multiply(this TimeSpan timeSpan, double scalar) - => new TimeSpan((long)(timeSpan.Ticks * scalar)); - } -} diff --git a/src/modules/imageresizer/ui/Helpers/Observable.cs b/src/modules/imageresizer/ui/Helpers/Observable.cs deleted file mode 100644 index f631ca0ab3..0000000000 --- a/src/modules/imageresizer/ui/Helpers/Observable.cs +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.ComponentModel; -using System.Runtime.CompilerServices; - -namespace ImageResizer.Helpers -{ - public class Observable : INotifyPropertyChanged - { - public event PropertyChangedEventHandler PropertyChanged; - - protected void Set(ref T storage, T value, [CallerMemberName] string propertyName = null) - { - if (Equals(storage, value)) - { - return; - } - - storage = value; - OnPropertyChanged(propertyName); - } - - protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); - } -} diff --git a/src/modules/imageresizer/ui/Helpers/RelayCommand.cs b/src/modules/imageresizer/ui/Helpers/RelayCommand.cs deleted file mode 100644 index 39843ff837..0000000000 --- a/src/modules/imageresizer/ui/Helpers/RelayCommand.cs +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Windows.Input; - -namespace ImageResizer.Helpers -{ - public class RelayCommand : ICommand - { - private readonly Action _execute; - private readonly Func _canExecute; - - public event EventHandler CanExecuteChanged; - - public RelayCommand(Action execute) - : this(execute, null) - { - } - - public RelayCommand(Action execute, Func canExecute) - { - _execute = execute ?? throw new ArgumentNullException(nameof(execute)); - _canExecute = canExecute; - } - - public bool CanExecute(object parameter) => _canExecute == null || _canExecute(); - - public void Execute(object parameter) => _execute(); - - public void OnCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty); - } - - [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "abstract T and abstract")] - public class RelayCommand : ICommand - { - private readonly Action execute; - - private readonly Func canExecute; - - public event EventHandler CanExecuteChanged; - - public RelayCommand(Action execute) - : this(execute, null) - { - } - - public RelayCommand(Action execute, Func canExecute) - { - this.execute = execute ?? throw new ArgumentNullException(nameof(execute)); - this.canExecute = canExecute; - } - - public bool CanExecute(object parameter) => canExecute == null || canExecute((T)parameter); - - public void Execute(object parameter) => execute((T)parameter); - - public void OnCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty); - } -} diff --git a/src/modules/imageresizer/ui/Helpers/ResourceLoaderInstance.cs b/src/modules/imageresizer/ui/Helpers/ResourceLoaderInstance.cs new file mode 100644 index 0000000000..00f810d00d --- /dev/null +++ b/src/modules/imageresizer/ui/Helpers/ResourceLoaderInstance.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; + +using Microsoft.Windows.ApplicationModel.Resources; + +namespace ImageResizer.Helpers +{ + internal static class ResourceLoaderInstance + { + private static Func _getString; + + internal static Func GetString + { + get => _getString ??= CreateDefault(); + set => _getString = value; + } + + private static Func CreateDefault() + { + var loader = new ResourceLoader("PowerToys.ImageResizer.pri"); + return loader.GetString; + } + } +} diff --git a/src/modules/imageresizer/ui/ImageResizerUI.csproj b/src/modules/imageresizer/ui/ImageResizerUI.csproj index f2bec0c3b6..4152ccea86 100644 --- a/src/modules/imageresizer/ui/ImageResizerUI.csproj +++ b/src/modules/imageresizer/ui/ImageResizerUI.csproj @@ -2,81 +2,92 @@ - + PowerToys.ImageResizer - $(RepoRoot)$(Platform)\$(Configuration)\WinUI3Apps\ - false - false - true - true - true - - - - {2BE46397-4DFA-414C-9BD4-41E4BBF8CB34} + PowerToys Image Resizer WinExe + ..\..\..\..\$(Platform)\$(Configuration)\WinUI3Apps ImageResizer PowerToys.ImageResizer - {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + app.manifest + true + true + false + false + true + None + true + Assets\ImageResizer\ImageResizer.ico true - CA1863 + + PowerToys.ImageResizer.pri + + DISABLE_XAML_GENERATED_MAIN,TRACE - + + + + + + + + - Resources\ImageResizer.ico + 0436;SA1210;SA1516;CA1305;CA1863;CA1852 - + + + + + + + + + + + PowerToys.GPOWrapper + $(OutDir) + false - - ImageResizerUI.prod.manifest - --> - - - - PublicResXFileCodeGenerator - Resources.Designer.cs - Designer - - - - - - - - + + + - + + - + + + + + + + + + + + + + - - + + + + + + + - - - True - True - Resources.resx - - - - - - - - - - - - - \ No newline at end of file + diff --git a/src/modules/imageresizer/ui/ImageResizerUI.dev.manifest b/src/modules/imageresizer/ui/ImageResizerUI.dev.manifest deleted file mode 100644 index cb91bc2b66..0000000000 --- a/src/modules/imageresizer/ui/ImageResizerUI.dev.manifest +++ /dev/null @@ -1,8 +0,0 @@ - - - - - diff --git a/src/modules/imageresizer/ui/ImageResizerUI.prod.manifest b/src/modules/imageresizer/ui/ImageResizerUI.prod.manifest deleted file mode 100644 index bbb50a9ec5..0000000000 --- a/src/modules/imageresizer/ui/ImageResizerUI.prod.manifest +++ /dev/null @@ -1,8 +0,0 @@ - - - - - diff --git a/src/modules/imageresizer/ui/ImageResizerXAML/App.xaml b/src/modules/imageresizer/ui/ImageResizerXAML/App.xaml new file mode 100644 index 0000000000..ba2df46206 --- /dev/null +++ b/src/modules/imageresizer/ui/ImageResizerXAML/App.xaml @@ -0,0 +1,33 @@ + + + + + + + + + 76 + 12,12,12,0 + + + + + + + + + + + diff --git a/src/modules/imageresizer/ui/ImageResizerXAML/App.xaml.cs b/src/modules/imageresizer/ui/ImageResizerXAML/App.xaml.cs new file mode 100644 index 0000000000..a304403e9d --- /dev/null +++ b/src/modules/imageresizer/ui/ImageResizerXAML/App.xaml.cs @@ -0,0 +1,244 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ImageResizer.Models; +using ImageResizer.Properties; +using ImageResizer.Services; +using ImageResizer.ViewModels; +using ManagedCommon; +using Microsoft.UI.Xaml; + +namespace ImageResizer +{ + /// + /// Provides application-specific behavior to supplement the default Application class. + /// + public partial class App : Application, IDisposable + { + private const string LogSubFolder = "\\Image Resizer\\Logs"; + + /// + /// Gets cached AI availability state, checked at app startup. + /// Can be updated after model download completes or background initialization. + /// + public static AiAvailabilityState AiAvailabilityState { get; internal set; } + + /// + /// Event fired when AI initialization completes in background. + /// Allows UI to refresh state when initialization finishes. + /// + public static event EventHandler AiInitializationCompleted; + + private Window _window; + + /// + /// Initializes a new instance of the class. + /// + public App() + { + try + { + string appLanguage = LanguageHelper.LoadLanguage(); + if (!string.IsNullOrEmpty(appLanguage)) + { + Microsoft.Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = appLanguage; + } + } + catch (Exception ex) + { + Logger.LogError("Language initialization error: " + ex.Message); + } + + try + { + Logger.InitializeLogger(LogSubFolder); + } + catch + { + // Swallow logger init issues silently + } + + Console.InputEncoding = Encoding.Unicode; + + this.InitializeComponent(); + + UnhandledException += App_UnhandledException; + } + + /// + /// Invoked when the application is launched normally by the end user. + /// + /// Details about the launch request and process. + protected override void OnLaunched(LaunchActivatedEventArgs args) + { + // Initialize dispatcher for cross-thread property change notifications + Settings.InitializeDispatcher(); + + // Check GPO policy + if (PowerToys.GPOWrapper.GPOWrapper.GetConfiguredImageResizerEnabledValue() == PowerToys.GPOWrapper.GpoRuleConfigured.Disabled) + { + Logger.LogWarning("Tried to start with a GPO policy setting the utility to always be disabled. Please contact your systems administrator."); + Environment.Exit(0); + return; + } + + // Check for AI detection mode (called by Runner in background) + var commandLineArgs = Environment.GetCommandLineArgs(); + if (commandLineArgs?.Length > 1 && commandLineArgs[1] == "--detect-ai") + { + RunAiDetectionMode(); + return; + } + + // Initialize AI availability + InitializeAiAvailability(); + + // Create batch from command line + var batch = ResizeBatch.FromCommandLine(Console.In, commandLineArgs); + + // Create main window (not yet visible – HWND is available for the file picker) + var mainWindow = new MainWindow(new MainViewModel(batch, Settings.Default)); + _window = mainWindow; + + mainWindow.DispatcherQueue.TryEnqueue(async () => + { + if (batch.Files.Count == 0) + { + // Show file picker before the window is visible + var files = await mainWindow.OpenPictureFilesAsync(); + if (!files.Any()) + { + Environment.Exit(0); + return; + } + + foreach (var file in files) + { + batch.Files.Add(file); + } + } + + // Load ViewModel (sets page content). + // MainWindow will Activate itself after layout is settled to avoid flash. + await mainWindow.LoadViewModelAsync(); + }); + } + + private void InitializeAiAvailability() + { + // AI Super Resolution is currently disabled + AiAvailabilityState = AiAvailabilityState.NotSupported; + ResizeBatch.SetAiSuperResolutionService(NoOpAiSuperResolutionService.Instance); + + // If AI is enabled in the future, uncomment this section: + /* + // AI Super Resolution is not supported on Windows 10 + if (OSVersionHelper.IsWindows10()) + { + AiAvailabilityState = AiAvailabilityState.NotSupported; + ResizeBatch.SetAiSuperResolutionService(NoOpAiSuperResolutionService.Instance); + Logger.LogInfo("AI Super Resolution not supported on Windows 10"); + } + else + { + // Load AI availability from cache + var cachedState = AiAvailabilityCacheService.LoadCache(); + + if (cachedState.HasValue) + { + AiAvailabilityState = cachedState.Value; + Logger.LogInfo($"AI state loaded from cache: {AiAvailabilityState}"); + } + else + { + AiAvailabilityState = AiAvailabilityState.NotSupported; + Logger.LogInfo("No AI cache found, defaulting to NotSupported"); + } + + // If AI is potentially available, start background initialization + if (AiAvailabilityState == AiAvailabilityState.Ready) + { + _ = InitializeAiServiceAsync(); + } + else + { + ResizeBatch.SetAiSuperResolutionService(NoOpAiSuperResolutionService.Instance); + } + } + */ + } + + /// + /// AI detection mode: perform detection, write to cache, and exit. + /// + private void RunAiDetectionMode() + { + try + { + Logger.LogInfo("Running AI detection mode..."); + + // AI is currently disabled + AiAvailabilityCacheService.SaveCache(AiAvailabilityState.NotSupported); + Logger.LogInfo("AI detection complete: NotSupported (feature disabled)"); + } + catch (Exception ex) + { + Logger.LogError($"AI detection failed: {ex.Message}"); + AiAvailabilityCacheService.SaveCache(AiAvailabilityState.NotSupported); + } + + Environment.Exit(0); + } + + /// + /// Initialize AI Super Resolution service asynchronously in background. + /// + private static async Task InitializeAiServiceAsync() + { + AiAvailabilityState finalState; + + try + { + var aiService = await WinAiSuperResolutionService.CreateAsync(); + + if (aiService != null) + { + ResizeBatch.SetAiSuperResolutionService(aiService); + Logger.LogInfo("AI Super Resolution service initialized successfully."); + finalState = AiAvailabilityState.Ready; + } + else + { + ResizeBatch.SetAiSuperResolutionService(NoOpAiSuperResolutionService.Instance); + Logger.LogWarning("AI Super Resolution service initialization failed. Using default service."); + finalState = AiAvailabilityState.NotSupported; + } + } + catch (Exception ex) + { + ResizeBatch.SetAiSuperResolutionService(NoOpAiSuperResolutionService.Instance); + Logger.LogError($"Exception during AI service initialization: {ex.Message}"); + finalState = AiAvailabilityState.NotSupported; + } + + AiAvailabilityState = finalState; + AiInitializationCompleted?.Invoke(null, finalState); + } + + private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e) + { + Logger.LogError("Unhandled exception", e.Exception); + } + + public void Dispose() + { + ResizeBatch.DisposeAiSuperResolutionService(); + GC.SuppressFinalize(this); + } + } +} diff --git a/src/modules/imageresizer/ui/ImageResizerXAML/MainWindow.xaml b/src/modules/imageresizer/ui/ImageResizerXAML/MainWindow.xaml new file mode 100644 index 0000000000..8cc35c7137 --- /dev/null +++ b/src/modules/imageresizer/ui/ImageResizerXAML/MainWindow.xaml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + diff --git a/src/modules/imageresizer/ui/ImageResizerXAML/MainWindow.xaml.cs b/src/modules/imageresizer/ui/ImageResizerXAML/MainWindow.xaml.cs new file mode 100644 index 0000000000..fa7752806a --- /dev/null +++ b/src/modules/imageresizer/ui/ImageResizerXAML/MainWindow.xaml.cs @@ -0,0 +1,294 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +// Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Threading.Tasks; +using ImageResizer.ViewModels; +using ImageResizer.Views; +using ManagedCommon; +using Microsoft.UI.Dispatching; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Windows.Graphics; +using Windows.Storage.Pickers; +using WinUIEx; + +namespace ImageResizer +{ + public sealed partial class MainWindow : WindowEx, IMainView + { + private const int MinWindowWidth = 460; + private const int InitialWindowHeight = 1; + private const double MaxWindowHeightScreenFraction = 0.85; + + private bool _isFirstShow = true; + + public MainViewModel ViewModel { get; } + + private PropertyChangedEventHandler _selectedSizeChangedHandler; + private InputViewModel _currentInputViewModel; + + public MainWindow(MainViewModel viewModel) + { + ViewModel = viewModel; + + InitializeComponent(); + + ExtendsContentIntoTitleBar = true; + SetTitleBar(titleBar); + this.SetIcon("Assets/ImageResizer/ImageResizer.ico"); + + WindowHelpers.ForceTopBorder1PixelInsetOnWindows10(this.GetWindowHandle()); + + // Keep the window hidden until content is loaded and measured. + // A tiny provisional height avoids stretching the page during the first layout pass. + this.SetWindowSize(MinWindowWidth, InitialWindowHeight); + AppWindow.Hide(); + + // Listen to ViewModel property changes + ViewModel.PropertyChanged += ViewModel_PropertyChanged; + } + + public async Task LoadViewModelAsync() + { + await ViewModel.LoadAsync(this); + } + + private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (e.PropertyName == nameof(ViewModel.CurrentPage)) + { + UpdateCurrentPage(); + } + } + + private void UpdateCurrentPage() + { + var page = ViewModel.CurrentPage; + if (page == null) + { + contentPresenter.Content = null; + return; + } + + if (page is InputViewModel inputVM) + { + var inputPage = new InputPage { ViewModel = inputVM }; + contentPresenter.Content = inputPage; + + AdjustWindowForInputPage(inputVM, inputPage); + } + else if (page is ProgressViewModel progressVM) + { + var progressPage = new ProgressPage { ViewModel = progressVM }; + contentPresenter.Content = progressPage; + + SizeAndShowOnLoaded(progressPage); + } + else if (page is ResultsViewModel resultsVM) + { + var resultsPage = new ResultsPage { ViewModel = resultsVM }; + contentPresenter.Content = resultsPage; + + SizeAndShowOnLoaded(resultsPage); + } + } + + /// + /// After the element completes layout, size the window to fit and show it. + /// + private void SizeAndShowOnLoaded(FrameworkElement element) + { + void OnLoaded(object sender, RoutedEventArgs e) + { + element.Loaded -= OnLoaded; + SizeToContent(); + ShowWindow(); + } + + element.Loaded += OnLoaded; + } + + private void AdjustWindowForInputPage(InputViewModel inputVM, InputPage inputPage) + { + // Unsubscribe previous handler to prevent memory leak + if (_selectedSizeChangedHandler != null && _currentInputViewModel?.Settings != null) + { + _currentInputViewModel.Settings.PropertyChanged -= _selectedSizeChangedHandler; + } + + _currentInputViewModel = inputVM; + + // Create and store handler reference for future cleanup + _selectedSizeChangedHandler = (s, e) => + { + if (e.PropertyName == nameof(inputVM.Settings.SelectedSizeIndex)) + { + // Content visibility changes after the selected size option changes; + // listen for the next LayoutUpdated to re-measure once layout settles. + SizeToContentAfterLayout(inputPage); + } + }; + + inputVM.Settings.PropertyChanged += _selectedSizeChangedHandler; + + SizeAndShowOnLoaded(inputPage); + } + + /// + /// Activate and center the window on first show; subsequent calls are no-ops. + /// + private void ShowWindow() + { + if (_isFirstShow) + { + _isFirstShow = false; + this.CenterOnScreen(); + this.Show(); + Activate(); + + // Compact the visible window after the first shown layout pass. + // This trims any slack that remains after hidden-state sizing. + DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () => + { + CompactWindowToRenderedContent(); + this.CenterOnScreen(); + }); + } + } + + /// + /// Sizes the window after the next layout pass completes. + /// Used when content changes on an already-loaded element (e.g., visibility toggles). + /// LayoutUpdated fires once per layout pass, so we unsubscribe immediately. + /// + private void SizeToContentAfterLayout(FrameworkElement element) + { + void OnLayoutUpdated(object sender, object e) + { + element.LayoutUpdated -= OnLayoutUpdated; + SizeToContent(); + + DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, CompactWindowToRenderedContent); + } + + element.LayoutUpdated += OnLayoutUpdated; + } + + /// + /// WinUI3 has no built-in SizeToContent (unlike WPF). + /// Measure the title bar and current page content at the current client width with + /// unconstrained height, then resize the client area to match. + /// + /// Measuring the Page itself can over-report height because the Page may already be + /// stretched to the window's provisional size from the first layout pass. + /// + private void SizeToContent() + { + var pageContentRoot = GetCurrentPageContentRoot(); + if (pageContentRoot == null) + { + return; + } + + var scale = this.GetDpiForWindow() / 96.0; + var clientWidth = AppWindow.ClientSize.Width; + var availableWidth = clientWidth / scale; + + titleBar.Measure(new Windows.Foundation.Size(availableWidth, double.PositiveInfinity)); + pageContentRoot.Measure(new Windows.Foundation.Size(availableWidth, double.PositiveInfinity)); + + var desiredHeight = titleBar.DesiredSize.Height + pageContentRoot.DesiredSize.Height; + + if (desiredHeight <= 0) + { + return; + } + + ApplyWindowSizeForClientContent(desiredHeight); + } + + private FrameworkElement GetCurrentPageContentRoot() + { + if (contentPresenter.Content is Page page) + { + return page.Content as FrameworkElement ?? page; + } + + return contentPresenter.Content as FrameworkElement; + } + + private void CompactWindowToRenderedContent() + { + var pageContentRoot = GetCurrentPageContentRoot(); + var windowContentRoot = this.Content as FrameworkElement; + if (pageContentRoot == null || windowContentRoot == null) + { + return; + } + + var totalRenderedHeight = windowContentRoot.ActualHeight; + var occupiedHeight = titleBar.ActualHeight + pageContentRoot.ActualHeight; + var slackHeight = totalRenderedHeight - occupiedHeight; + + if (slackHeight <= 1) + { + return; + } + + var reducedHeight = totalRenderedHeight - slackHeight; + + if (reducedHeight <= 0 || reducedHeight >= totalRenderedHeight) + { + return; + } + + ApplyWindowSizeForClientContent(reducedHeight); + } + + private void ApplyWindowSizeForClientContent(double desiredClientHeight) + { + var scale = this.GetDpiForWindow() / 96.0; + var frameHeight = Math.Max(0, AppWindow.Size.Height - AppWindow.ClientSize.Height) / scale; + var outerHeight = desiredClientHeight + frameHeight; + + var displayArea = Microsoft.UI.Windowing.DisplayArea.GetFromWindowId(AppWindow.Id, Microsoft.UI.Windowing.DisplayAreaFallback.Nearest); + var maxScreenHeight = displayArea.WorkArea.Height / scale * MaxWindowHeightScreenFraction; + outerHeight = Math.Min(outerHeight, maxScreenHeight); + + this.SetWindowSize(MinWindowWidth, outerHeight); + } + + public async Task> OpenPictureFilesAsync() + { + var picker = this.CreateOpenFilePicker(); + picker.ViewMode = PickerViewMode.Thumbnail; + picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; + string[] imageExtensions = [".bmp", ".dib", ".exif", ".gif", ".jfif", ".jpe", + ".jpeg", ".jpg", ".jxr", ".png", ".rle", ".tif", ".tiff", ".wdp"]; + + foreach (var ext in imageExtensions) + { + picker.FileTypeFilter.Add(ext); + } + + var files = await picker.PickMultipleFilesAsync(); + if (files != null && files.Count > 0) + { + return files.Select(f => f.Path); + } + + return []; + } + + void IMainView.Close() + { + DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Normal, Close); + } + } +} diff --git a/src/modules/imageresizer/ui/ImageResizerXAML/Views/IMainView.cs b/src/modules/imageresizer/ui/ImageResizerXAML/Views/IMainView.cs new file mode 100644 index 0000000000..e3d5c5f5a1 --- /dev/null +++ b/src/modules/imageresizer/ui/ImageResizerXAML/Views/IMainView.cs @@ -0,0 +1,17 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +// Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace ImageResizer.Views +{ + public interface IMainView + { + Task> OpenPictureFilesAsync(); + + void Close(); + } +} diff --git a/src/modules/imageresizer/ui/ImageResizerXAML/Views/InputPage.xaml b/src/modules/imageresizer/ui/ImageResizerXAML/Views/InputPage.xaml new file mode 100644 index 0000000000..4c9bd3d948 --- /dev/null +++ b/src/modules/imageresizer/ui/ImageResizerXAML/Views/InputPage.xaml @@ -0,0 +1,346 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/modules/imageresizer/ui/ImageResizerXAML/Views/InputPage.xaml.cs b/src/modules/imageresizer/ui/ImageResizerXAML/Views/InputPage.xaml.cs new file mode 100644 index 0000000000..42671911ea --- /dev/null +++ b/src/modules/imageresizer/ui/ImageResizerXAML/Views/InputPage.xaml.cs @@ -0,0 +1,52 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +// Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ +using ImageResizer.ViewModels; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Input; + +using static ImageResizer.ViewModels.InputViewModel; + +namespace ImageResizer.Views +{ + public sealed partial class InputPage : Page + { + public InputViewModel ViewModel { get; set; } + + public InputPage() + { + InitializeComponent(); + } + + private void ResizeAccelerator_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args) + { + if (FocusManager.GetFocusedElement(XamlRoot) is NumberBox) + { + args.Handled = true; + } + } + + private void NumberBox_KeyDown(object sender, KeyRoutedEventArgs e) + { + if (e.Key == Windows.System.VirtualKey.Enter + && sender is NumberBox numberBox + && ViewModel is not null + && !double.IsNaN(numberBox.Value)) + { + KeyPressParams keyParams = numberBox.Name switch + { + "WidthNumberBox" => new KeyPressParams { Value = numberBox.Value, Dimension = Dimension.Width }, + "HeightNumberBox" => new KeyPressParams { Value = numberBox.Value, Dimension = Dimension.Height }, + _ => null, + }; + + if (keyParams is not null) + { + ViewModel.EnterKeyPressedCommand.Execute(keyParams); + } + } + } + } +} diff --git a/src/modules/imageresizer/ui/ImageResizerXAML/Views/ProgressPage.xaml b/src/modules/imageresizer/ui/ImageResizerXAML/Views/ProgressPage.xaml new file mode 100644 index 0000000000..8f25de8efe --- /dev/null +++ b/src/modules/imageresizer/ui/ImageResizerXAML/Views/ProgressPage.xaml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + diff --git a/src/modules/imageresizer/ui/ImageResizerXAML/Views/ProgressPage.xaml.cs b/src/modules/imageresizer/ui/ImageResizerXAML/Views/ProgressPage.xaml.cs new file mode 100644 index 0000000000..0ff41922c1 --- /dev/null +++ b/src/modules/imageresizer/ui/ImageResizerXAML/Views/ProgressPage.xaml.cs @@ -0,0 +1,26 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +// Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ +using ImageResizer.ViewModels; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; + +namespace ImageResizer.Views +{ + public sealed partial class ProgressPage : Page + { + public ProgressViewModel ViewModel { get; set; } + + public ProgressPage() + { + InitializeComponent(); + } + + private void Page_Loaded(object sender, RoutedEventArgs e) + { + ViewModel?.StartCommand.Execute(null); + } + } +} diff --git a/src/modules/imageresizer/ui/ImageResizerXAML/Views/ResultsPage.xaml b/src/modules/imageresizer/ui/ImageResizerXAML/Views/ResultsPage.xaml new file mode 100644 index 0000000000..852d57c30a --- /dev/null +++ b/src/modules/imageresizer/ui/ImageResizerXAML/Views/ResultsPage.xaml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/modules/imageresizer/ui/ImageResizerXAML/Views/ResultsPage.xaml.cs b/src/modules/imageresizer/ui/ImageResizerXAML/Views/ResultsPage.xaml.cs new file mode 100644 index 0000000000..dac9a4dda5 --- /dev/null +++ b/src/modules/imageresizer/ui/ImageResizerXAML/Views/ResultsPage.xaml.cs @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +// Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ +using ImageResizer.ViewModels; +using Microsoft.UI.Xaml.Controls; + +namespace ImageResizer.Views +{ + public sealed partial class ResultsPage : Page + { + public ResultsViewModel ViewModel { get; set; } + + public ResultsPage() + { + InitializeComponent(); + } + } +} diff --git a/src/modules/imageresizer/ui/ImageResizerXAML/Views/SizeDataTemplateSelector.cs b/src/modules/imageresizer/ui/ImageResizerXAML/Views/SizeDataTemplateSelector.cs new file mode 100644 index 0000000000..b8395e577e --- /dev/null +++ b/src/modules/imageresizer/ui/ImageResizerXAML/Views/SizeDataTemplateSelector.cs @@ -0,0 +1,37 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using ImageResizer.Models; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; + +namespace ImageResizer.Views +{ + public partial class SizeDataTemplateSelector : DataTemplateSelector + { + public DataTemplate ResizeSizeTemplate { get; set; } + public DataTemplate CustomSizeTemplate { get; set; } + public DataTemplate AiSizeTemplate { get; set; } + + protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) + { + if (item is AiSize) + { + return AiSizeTemplate; + } + + if (item is CustomSize) + { + return CustomSizeTemplate; + } + + if (item is ResizeSize) + { + return ResizeSizeTemplate; + } + + return base.SelectTemplateCore(item, container); + } + } +} diff --git a/src/modules/imageresizer/ui/Models/AiSize.cs b/src/modules/imageresizer/ui/Models/AiSize.cs index dcb9d521d8..78a5bf1a5a 100644 --- a/src/modules/imageresizer/ui/Models/AiSize.cs +++ b/src/modules/imageresizer/ui/Models/AiSize.cs @@ -1,32 +1,31 @@ -// Copyright (c) Microsoft Corporation +// Copyright (c) Microsoft Corporation // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.Globalization; using System.Text; using System.Text.Json.Serialization; - -using ImageResizer.Properties; +using CommunityToolkit.Mvvm.ComponentModel; +using ImageResizer.Helpers; namespace ImageResizer.Models { - public class AiSize : ResizeSize + public partial class AiSize : ResizeSize { - private static readonly CompositeFormat ScaleFormat = CompositeFormat.Parse(Resources.Input_AiScaleFormat); + private static CompositeFormat _scaleFormat; + + private static CompositeFormat ScaleFormat => + _scaleFormat ??= CompositeFormat.Parse(ResourceLoaderInstance.GetString("Input_AiScaleFormat")); + + [ObservableProperty] + [JsonPropertyName("scale")] private int _scale = 2; /// - /// Gets the formatted scale display string (e.g., "2×"). + /// Gets the formatted scale display string (e.g., "2x"). /// [JsonIgnore] - public string ScaleDisplay => string.Format(CultureInfo.CurrentCulture, ScaleFormat, _scale); - - [JsonPropertyName("scale")] - public int Scale - { - get => _scale; - set => Set(ref _scale, value); - } + public string ScaleDisplay => string.Format(CultureInfo.CurrentCulture, ScaleFormat, Scale); [JsonConstructor] public AiSize(int scale) diff --git a/src/modules/imageresizer/ui/Models/CliOptions.cs b/src/modules/imageresizer/ui/Models/CliOptions.cs index 2df23f532b..e188e77b3e 100644 --- a/src/modules/imageresizer/ui/Models/CliOptions.cs +++ b/src/modules/imageresizer/ui/Models/CliOptions.cs @@ -8,6 +8,7 @@ using System.Collections.ObjectModel; using System.CommandLine.Parsing; using System.Globalization; using ImageResizer.Cli.Commands; +using ImageResizer.Helpers; #pragma warning disable SA1649 // File name should match first type name #pragma warning disable SA1402 // File may only contain a single type @@ -19,117 +20,51 @@ namespace ImageResizer.Models /// public class CliOptions { - /// - /// Gets or sets a value indicating whether to show help information. - /// public bool ShowHelp { get; set; } - /// - /// Gets or sets a value indicating whether to show current configuration. - /// public bool ShowConfig { get; set; } - /// - /// Gets or sets the destination directory for resized images. - /// public string DestinationDirectory { get; set; } - /// - /// Gets or sets the width of the resized image. - /// public double? Width { get; set; } - /// - /// Gets or sets the height of the resized image. - /// public double? Height { get; set; } - /// - /// Gets or sets the resize unit (Pixel, Percent, Inch, Centimeter). - /// public ResizeUnit? Unit { get; set; } - /// - /// Gets or sets the resize fit mode (Fill, Fit, Stretch). - /// public ResizeFit? Fit { get; set; } - /// - /// Gets or sets the index of the preset size to use. - /// public int? SizeIndex { get; set; } - /// - /// Gets or sets a value indicating whether to only shrink images (not enlarge). - /// public bool? ShrinkOnly { get; set; } - /// - /// Gets or sets a value indicating whether to replace the original file. - /// public bool? Replace { get; set; } - /// - /// Gets or sets a value indicating whether to ignore orientation when resizing. - /// public bool? IgnoreOrientation { get; set; } - /// - /// Gets or sets a value indicating whether to remove metadata from the resized image. - /// public bool? RemoveMetadata { get; set; } - /// - /// Gets or sets the JPEG quality level (1-100). - /// public int? JpegQualityLevel { get; set; } - /// - /// Gets or sets a value indicating whether to keep the date modified. - /// public bool? KeepDateModified { get; set; } - /// - /// Gets or sets the output filename format. - /// public string FileName { get; set; } - /// - /// Gets or sets a value indicating whether to use line-based progress output for screen reader accessibility. - /// public bool? ProgressLines { get; set; } - /// - /// Gets the list of files to process. - /// public ICollection Files { get; } = new List(); - /// - /// Gets or sets the pipe name for receiving file list. - /// public string PipeName { get; set; } - /// - /// Gets parse/validation errors produced by System.CommandLine. - /// - public IReadOnlyList ParseErrors { get; private set; } = Array.Empty(); + public IReadOnlyList ParseErrors { get; private set; } = []; - /// - /// Converts a boolean value to nullable bool (true -> true, false -> null). - /// private static bool? ToBoolOrNull(bool value) => value ? true : null; - /// - /// Parses command-line arguments into CliOptions using System.CommandLine. - /// - /// The command-line arguments. - /// A CliOptions instance with parsed values. public static CliOptions Parse(string[] args) { var options = new CliOptions(); var cmd = new ImageResizerRootCommand(); - // Parse using System.CommandLine var parseResult = new Parser(cmd).Parse(args); if (parseResult.Errors.Count > 0) @@ -143,7 +78,6 @@ namespace ImageResizer.Models options.ParseErrors = new ReadOnlyCollection(errors); } - // Extract values from parse result using strongly typed options options.ShowHelp = parseResult.GetValueForOption(cmd.HelpOption); options.ShowConfig = parseResult.GetValueForOption(cmd.ShowConfigOption); options.DestinationDirectory = parseResult.GetValueForOption(cmd.DestinationOption); @@ -153,7 +87,6 @@ namespace ImageResizer.Models options.Fit = parseResult.GetValueForOption(cmd.FitOption); options.SizeIndex = parseResult.GetValueForOption(cmd.SizeOption); - // Convert bool to nullable bool (true -> true, false -> null) options.ShrinkOnly = ToBoolOrNull(parseResult.GetValueForOption(cmd.ShrinkOnlyOption)); options.Replace = ToBoolOrNull(parseResult.GetValueForOption(cmd.ReplaceOption)); options.IgnoreOrientation = ToBoolOrNull(parseResult.GetValueForOption(cmd.IgnoreOrientationOption)); @@ -165,14 +98,12 @@ namespace ImageResizer.Models options.FileName = parseResult.GetValueForOption(cmd.FileNameOption); - // Get files from arguments var files = parseResult.GetValueForArgument(cmd.FilesArgument); if (files != null) { const string pipeNamePrefix = "\\\\.\\pipe\\"; foreach (var file in files) { - // Check for pipe name (must be at the start of the path) if (file.StartsWith(pipeNamePrefix, StringComparison.OrdinalIgnoreCase)) { options.PipeName = file.Substring(pipeNamePrefix.Length); @@ -187,62 +118,55 @@ namespace ImageResizer.Models return options; } - /// - /// Prints current configuration to the console. - /// - /// The settings to display. public static void PrintConfig(ImageResizer.Properties.Settings settings) { + var getString = ResourceLoaderInstance.GetString; Console.OutputEncoding = System.Text.Encoding.UTF8; - Console.WriteLine(Properties.Resources.CLI_ConfigTitle); + Console.WriteLine(getString("CLI_ConfigTitle")); Console.WriteLine(); - Console.WriteLine(Properties.Resources.CLI_ConfigGeneralSettings); - Console.WriteLine(string.Format(CultureInfo.InvariantCulture, Properties.Resources.CLI_ConfigShrinkOnly, settings.ShrinkOnly)); - Console.WriteLine(string.Format(CultureInfo.InvariantCulture, Properties.Resources.CLI_ConfigReplaceOriginal, settings.Replace)); - Console.WriteLine(string.Format(CultureInfo.InvariantCulture, Properties.Resources.CLI_ConfigIgnoreOrientation, settings.IgnoreOrientation)); - Console.WriteLine(string.Format(CultureInfo.InvariantCulture, Properties.Resources.CLI_ConfigRemoveMetadata, settings.RemoveMetadata)); - Console.WriteLine(string.Format(CultureInfo.InvariantCulture, Properties.Resources.CLI_ConfigKeepDateModified, settings.KeepDateModified)); - Console.WriteLine(string.Format(CultureInfo.InvariantCulture, Properties.Resources.CLI_ConfigJpegQuality, settings.JpegQualityLevel)); - Console.WriteLine(string.Format(CultureInfo.InvariantCulture, Properties.Resources.CLI_ConfigPngInterlace, settings.PngInterlaceOption)); - Console.WriteLine(string.Format(CultureInfo.InvariantCulture, Properties.Resources.CLI_ConfigTiffCompress, settings.TiffCompressOption)); - Console.WriteLine(string.Format(CultureInfo.InvariantCulture, Properties.Resources.CLI_ConfigFilenameFormat, settings.FileName)); + Console.WriteLine(getString("CLI_ConfigGeneralSettings")); + Console.WriteLine(string.Format(CultureInfo.InvariantCulture, getString("CLI_ConfigShrinkOnly"), settings.ShrinkOnly)); + Console.WriteLine(string.Format(CultureInfo.InvariantCulture, getString("CLI_ConfigReplaceOriginal"), settings.Replace)); + Console.WriteLine(string.Format(CultureInfo.InvariantCulture, getString("CLI_ConfigIgnoreOrientation"), settings.IgnoreOrientation)); + Console.WriteLine(string.Format(CultureInfo.InvariantCulture, getString("CLI_ConfigRemoveMetadata"), settings.RemoveMetadata)); + Console.WriteLine(string.Format(CultureInfo.InvariantCulture, getString("CLI_ConfigKeepDateModified"), settings.KeepDateModified)); + Console.WriteLine(string.Format(CultureInfo.InvariantCulture, getString("CLI_ConfigJpegQuality"), settings.JpegQualityLevel)); + Console.WriteLine(string.Format(CultureInfo.InvariantCulture, getString("CLI_ConfigPngInterlace"), settings.PngInterlaceOption)); + Console.WriteLine(string.Format(CultureInfo.InvariantCulture, getString("CLI_ConfigTiffCompress"), settings.TiffCompressOption)); + Console.WriteLine(string.Format(CultureInfo.InvariantCulture, getString("CLI_ConfigFilenameFormat"), settings.FileName)); Console.WriteLine(); - Console.WriteLine(Properties.Resources.CLI_ConfigCustomSize); - Console.WriteLine(string.Format(CultureInfo.InvariantCulture, Properties.Resources.CLI_ConfigWidth, settings.CustomSize.Width, settings.CustomSize.Unit)); - Console.WriteLine(string.Format(CultureInfo.InvariantCulture, Properties.Resources.CLI_ConfigHeight, settings.CustomSize.Height, settings.CustomSize.Unit)); - Console.WriteLine(string.Format(CultureInfo.InvariantCulture, Properties.Resources.CLI_ConfigFitMode, settings.CustomSize.Fit)); + Console.WriteLine(getString("CLI_ConfigCustomSize")); + Console.WriteLine(string.Format(CultureInfo.InvariantCulture, getString("CLI_ConfigWidth"), settings.CustomSize.Width, settings.CustomSize.Unit)); + Console.WriteLine(string.Format(CultureInfo.InvariantCulture, getString("CLI_ConfigHeight"), settings.CustomSize.Height, settings.CustomSize.Unit)); + Console.WriteLine(string.Format(CultureInfo.InvariantCulture, getString("CLI_ConfigFitMode"), settings.CustomSize.Fit)); Console.WriteLine(); - Console.WriteLine(Properties.Resources.CLI_ConfigPresetSizes); + Console.WriteLine(getString("CLI_ConfigPresetSizes")); for (int i = 0; i < settings.Sizes.Count; i++) { var size = settings.Sizes[i]; var selected = i == settings.SelectedSizeIndex ? "*" : " "; - Console.WriteLine(string.Format(CultureInfo.InvariantCulture, Properties.Resources.CLI_ConfigPresetSizeFormat, i, selected, size.Name, size.Width, size.Height, size.Unit, size.Fit)); + Console.WriteLine(string.Format(CultureInfo.InvariantCulture, getString("CLI_ConfigPresetSizeFormat"), i, selected, size.Name, size.Width, size.Height, size.Unit, size.Fit)); } if (settings.SelectedSizeIndex >= settings.Sizes.Count) { - Console.WriteLine(string.Format(CultureInfo.InvariantCulture, Properties.Resources.CLI_ConfigCustomSelected, settings.CustomSize.Width, settings.CustomSize.Height, settings.CustomSize.Unit, settings.CustomSize.Fit)); + Console.WriteLine(string.Format(CultureInfo.InvariantCulture, getString("CLI_ConfigCustomSelected"), settings.CustomSize.Width, settings.CustomSize.Height, settings.CustomSize.Unit, settings.CustomSize.Fit)); } } - /// - /// Prints usage information to the console. - /// public static void PrintUsage() { + var getString = ResourceLoaderInstance.GetString; Console.OutputEncoding = System.Text.Encoding.UTF8; - Console.WriteLine(Properties.Resources.CLI_UsageTitle); + Console.WriteLine(getString("CLI_UsageTitle")); Console.WriteLine(); var cmd = new ImageResizerRootCommand(); - // Print usage line - Console.WriteLine(Properties.Resources.CLI_UsageLine); + Console.WriteLine(getString("CLI_UsageLine")); Console.WriteLine(); - // Print options from the command definition - Console.WriteLine(Properties.Resources.CLI_UsageOptions); + Console.WriteLine(getString("CLI_UsageOptions")); foreach (var option in cmd.Options) { var aliases = string.Join(", ", option.Aliases); @@ -251,11 +175,11 @@ namespace ImageResizer.Models } Console.WriteLine(); - Console.WriteLine(Properties.Resources.CLI_UsageExamples); - Console.WriteLine(Properties.Resources.CLI_UsageExampleHelp); - Console.WriteLine(Properties.Resources.CLI_UsageExampleDimensions); - Console.WriteLine(Properties.Resources.CLI_UsageExamplePercent); - Console.WriteLine(Properties.Resources.CLI_UsageExamplePreset); + Console.WriteLine(getString("CLI_UsageExamples")); + Console.WriteLine(getString("CLI_UsageExampleHelp")); + Console.WriteLine(getString("CLI_UsageExampleDimensions")); + Console.WriteLine(getString("CLI_UsageExamplePercent")); + Console.WriteLine(getString("CLI_UsageExamplePreset")); } } } diff --git a/src/modules/imageresizer/ui/Models/CustomSize.cs b/src/modules/imageresizer/ui/Models/CustomSize.cs index bc4e73557a..041781eb40 100644 --- a/src/modules/imageresizer/ui/Models/CustomSize.cs +++ b/src/modules/imageresizer/ui/Models/CustomSize.cs @@ -1,12 +1,11 @@ -#pragma warning disable IDE0073 +#pragma warning disable IDE0073, SA1636 // Copyright (c) Brice Lambson // The Brice Lambson licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 +#pragma warning restore IDE0073, SA1636 using System.Text.Json.Serialization; - -using ImageResizer.Properties; +using ImageResizer.Helpers; namespace ImageResizer.Models { @@ -15,7 +14,7 @@ namespace ImageResizer.Models [JsonIgnore] public override string Name { - get => Resources.Input_Custom; + get => ResourceLoaderInstance.GetString("Input_Custom"); set { /* no-op */ } } diff --git a/src/modules/imageresizer/ui/Models/ImagingEnums.cs b/src/modules/imageresizer/ui/Models/ImagingEnums.cs new file mode 100644 index 0000000000..9adf5f9d23 --- /dev/null +++ b/src/modules/imageresizer/ui/Models/ImagingEnums.cs @@ -0,0 +1,32 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace ImageResizer.Models +{ + /// + /// PNG interlace option for the encoder. + /// Integer values preserve backward compatibility with existing settings JSON. + /// + public enum PngInterlaceOption + { + Default = 0, + On = 1, + Off = 2, + } + + /// + /// TIFF compression option for the encoder. + /// Integer values preserve backward compatibility with existing settings JSON. + /// + public enum TiffCompressOption + { + Default = 0, + None = 1, + Ccitt3 = 2, + Ccitt4 = 3, + Lzw = 4, + Rle = 5, + Zip = 6, + } +} diff --git a/src/modules/imageresizer/ui/Models/ResizeBatch.cs b/src/modules/imageresizer/ui/Models/ResizeBatch.cs index 07df9cea75..34053db5ad 100644 --- a/src/modules/imageresizer/ui/Models/ResizeBatch.cs +++ b/src/modules/imageresizer/ui/Models/ResizeBatch.cs @@ -1,8 +1,8 @@ -#pragma warning disable IDE0073 +#pragma warning disable IDE0073, SA1636 // Copyright (c) Brice Lambson // The Brice Lambson licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 +#pragma warning restore IDE0073, SA1636 using System; using System.Collections.Concurrent; @@ -10,11 +10,9 @@ using System.Collections.Generic; using System.IO; using System.IO.Abstractions; using System.IO.Pipes; -using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; - using ImageResizer.Properties; using ImageResizer.Services; @@ -40,6 +38,12 @@ namespace ImageResizer.Models _aiSuperResolutionService = null; } + private static readonly HashSet ValidImageExtensions = new(StringComparer.OrdinalIgnoreCase) + { + ".bmp", ".dib", ".gif", ".jfif", ".jpe", ".jpeg", ".jpg", + ".jxr", ".png", ".rle", ".tif", ".tiff", ".wdp", + }; + /// /// Validates if a file path is a supported image format. /// @@ -57,14 +61,8 @@ namespace ImageResizer.Models return false; } - var ext = Path.GetExtension(path)?.ToLowerInvariant(); - var validExtensions = new[] - { - ".bmp", ".dib", ".gif", ".jfif", ".jpe", ".jpeg", ".jpg", - ".jxr", ".png", ".rle", ".tif", ".tiff", ".wdp", - }; - - return validExtensions.Contains(ext); + var ext = Path.GetExtension(path); + return ValidImageExtensions.Contains(ext); } /// @@ -120,7 +118,7 @@ namespace ImageResizer.Models { string file; - // Display the read text to the console + // Read file paths from the named pipe while ((file = sr.ReadLine()) != null) { if (IsValidImagePath(file)) @@ -141,37 +139,35 @@ namespace ImageResizer.Models return FromCliOptions(standardInput, options); } - public IEnumerable Process(Action reportProgress, CancellationToken cancellationToken) + public Task> ProcessAsync(Action reportProgress, CancellationToken cancellationToken) { // NOTE: Settings.Default is captured once before parallel processing. // Any changes to settings on disk during this batch will NOT be reflected until the next batch. // This improves performance and predictability by avoiding repeated mutex acquisition and behaviour change results in a batch. - return Process(reportProgress, Settings.Default, cancellationToken); + return ProcessAsync(reportProgress, Settings.Default, cancellationToken); } - public IEnumerable Process(Action reportProgress, Settings settings, CancellationToken cancellationToken) + public async Task> ProcessAsync(Action reportProgress, Settings settings, CancellationToken cancellationToken) { double total = Files.Count; int completed = 0; var errors = new ConcurrentBag(); - // TODO: If we ever switch to Windows.Graphics.Imaging, we can get a lot more throughput by using the async - // APIs and a custom SynchronizationContext - Parallel.ForEach( + await Parallel.ForEachAsync( Files, new ParallelOptions { CancellationToken = cancellationToken, }, - (file, state, i) => + async (file, ct) => { try { - Execute(file, settings); + await ExecuteAsync(file, settings); } catch (Exception ex) { - errors.Add(new ResizeError { File = _fileSystem.Path.GetFileName(file), Error = ex.Message }); + errors.Add(new ResizeError(_fileSystem.Path.GetFileName(file), ex.Message)); } Interlocked.Increment(ref completed); @@ -181,10 +177,10 @@ namespace ImageResizer.Models return errors; } - protected virtual void Execute(string file, Settings settings) + protected virtual async Task ExecuteAsync(string file, Settings settings) { var aiService = _aiSuperResolutionService ?? NoOpAiSuperResolutionService.Instance; - new ResizeOperation(file, DestinationDirectory, settings, aiService).Execute(); + await new ResizeOperation(file, DestinationDirectory, settings, aiService).ExecuteAsync(); } } } diff --git a/src/modules/imageresizer/ui/Models/ResizeError.cs b/src/modules/imageresizer/ui/Models/ResizeError.cs index 89d5550f03..8fe5e0000f 100644 --- a/src/modules/imageresizer/ui/Models/ResizeError.cs +++ b/src/modules/imageresizer/ui/Models/ResizeError.cs @@ -1,15 +1,10 @@ -#pragma warning disable IDE0073 +#pragma warning disable IDE0073, SA1636 // Copyright (c) Brice Lambson // The Brice Lambson licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 +#pragma warning restore IDE0073, SA1636 namespace ImageResizer.Models { - public class ResizeError - { - public string File { get; set; } - - public string Error { get; set; } - } + public record ResizeError(string File, string Error); } diff --git a/src/modules/imageresizer/ui/Models/ResizeFit.cs b/src/modules/imageresizer/ui/Models/ResizeFit.cs index 955d0b0ac3..a87baf7789 100644 --- a/src/modules/imageresizer/ui/Models/ResizeFit.cs +++ b/src/modules/imageresizer/ui/Models/ResizeFit.cs @@ -1,9 +1,9 @@ -#pragma warning disable IDE0073 +#pragma warning disable IDE0073, SA1636 // Copyright (c) Brice Lambson // The Brice Lambson licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. // Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 +#pragma warning restore IDE0073, SA1636 namespace ImageResizer.Models { diff --git a/src/modules/imageresizer/ui/Models/ResizeOperation.cs b/src/modules/imageresizer/ui/Models/ResizeOperation.cs index 4c3cb837a1..5c0af2b502 100644 --- a/src/modules/imageresizer/ui/Models/ResizeOperation.cs +++ b/src/modules/imageresizer/ui/Models/ResizeOperation.cs @@ -1,26 +1,24 @@ -#pragma warning disable IDE0073 +#pragma warning disable IDE0073, SA1636 // Copyright (c) Brice Lambson // The Brice Lambson licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 +#pragma warning restore IDE0073, SA1636 using System; -using System.Diagnostics; using System.Globalization; using System.IO; using System.IO.Abstractions; using System.Linq; using System.Text; -using System.Windows; -using System.Windows.Media; -using System.Windows.Media.Imaging; - -using ImageResizer.Extensions; +using System.Threading.Tasks; +using Windows.Foundation; +using Windows.Graphics.Imaging; +using Windows.Storage.Streams; +using ImageResizer.Helpers; using ImageResizer.Properties; using ImageResizer.Services; using ImageResizer.Utilities; using Microsoft.VisualBasic.FileIO; - using FileSystem = Microsoft.VisualBasic.FileIO.FileSystem; namespace ImageResizer.Models @@ -35,15 +33,24 @@ namespace ImageResizer.Models private readonly IAISuperResolutionService _aiSuperResolutionService; // Cache CompositeFormat for AI error message formatting (CA1863) - private static readonly CompositeFormat _aiErrorFormat = CompositeFormat.Parse(Resources.Error_AiProcessingFailed); + private static CompositeFormat _aiErrorFormat; + + private static CompositeFormat AiErrorFormat => + _aiErrorFormat ??= CompositeFormat.Parse(ResourceLoaderInstance.GetString("Error_AiProcessingFailed")); + + private static readonly string[] RenderingMetadataProperties = + [ + "System.Photo.Orientation", + "System.Image.ColorSpace", + ]; // Filenames to avoid according to https://learn.microsoft.com/windows/win32/fileio/naming-a-file#file-and-directory-names private static readonly string[] _avoidFilenames = - { + [ "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", - }; + ]; public ResizeOperation(string file, string destinationDirectory, Settings settings, IAISuperResolutionService aiSuperResolutionService = null) { @@ -53,78 +60,83 @@ namespace ImageResizer.Models _aiSuperResolutionService = aiSuperResolutionService ?? NoOpAiSuperResolutionService.Instance; } - public void Execute() + public async Task ExecuteAsync() { string path; + using (var inputStream = _fileSystem.File.OpenRead(_file)) { - var decoder = BitmapDecoder.Create( - inputStream, - BitmapCreateOptions.PreservePixelFormat, - BitmapCacheOption.None); + var winrtInputStream = inputStream.AsRandomAccessStream(); + var decoder = await BitmapDecoder.CreateAsync(winrtInputStream); - var containerFormat = decoder.CodecInfo.ContainerFormat; - - var encoder = CreateEncoder(containerFormat); - - if (decoder.Metadata != null) + // Determine encoder ID from decoder + var encoderId = CodecHelper.GetEncoderIdForDecoder(decoder); + if (encoderId == null || !CodecHelper.CanEncode(encoderId.Value)) { - try - { - encoder.Metadata = decoder.Metadata; - } - catch (InvalidOperationException) - { - } + encoderId = CodecHelper.GetEncoderIdFromLegacyGuid(_settings.FallbackEncoder); } - if (decoder.Palette != null) + var encoderGuid = encoderId.Value; + + if (_settings.SelectedSize is AiSize) { - encoder.Palette = decoder.Palette; + path = await ExecuteAiAsync(decoder, winrtInputStream, encoderGuid); } - - foreach (var originalFrame in decoder.Frames) + else { - var transformedBitmap = Transform(originalFrame); + var originalWidth = (int)decoder.PixelWidth; + var originalHeight = (int)decoder.PixelHeight; - // if the frame was not modified, we should not replace the metadata - if (transformedBitmap == originalFrame) + var (scaledWidth, scaledHeight, cropBounds, noTransformNeeded) = + CalculateDimensions(originalWidth, originalHeight, decoder.DpiX, decoder.DpiY); + + var (outputWidth, outputHeight) = noTransformNeeded + ? (originalWidth, originalHeight) + : cropBounds.HasValue + ? ((int)cropBounds.Value.Width, (int)cropBounds.Value.Height) + : ((int)scaledWidth, (int)scaledHeight); + + path = GetDestinationPath(encoderGuid, outputWidth, outputHeight); + _fileSystem.Directory.CreateDirectory(_fileSystem.Path.GetDirectoryName(path)); + + using (var outputStream = _fileSystem.File.Open(path, FileMode.CreateNew, FileAccess.ReadWrite)) { - encoder.Frames.Add(originalFrame); + var winrtOutputStream = outputStream.AsRandomAccessStream(); + await EncodeToStreamAsync( + decoder, + winrtInputStream, + winrtOutputStream, + encoderGuid, + async (encoder, isTranscode) => + { + if (isTranscode) + { + if (!noTransformNeeded) + { + encoder.BitmapTransform.ScaledWidth = scaledWidth; + encoder.BitmapTransform.ScaledHeight = scaledHeight; + encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant; + + if (cropBounds.HasValue) + { + encoder.BitmapTransform.Bounds = cropBounds.Value; + } + } + } + else + { + await EncodeFramesAsync( + encoder, + decoder, + scaledWidth, + scaledHeight, + cropBounds, + noTransformNeeded, + originalWidth, + originalHeight); + } + }); } - else - { - BitmapMetadata originalMetadata = (BitmapMetadata)originalFrame.Metadata; - -#if DEBUG - Debug.WriteLine($"### Processing metadata of file {_file}"); - originalMetadata.PrintsAllMetadataToDebugOutput(); -#endif - - var metadata = GetValidMetadata(originalMetadata, transformedBitmap, containerFormat); - - if (_settings.RemoveMetadata && metadata != null) - { - // strip any metadata that doesn't affect rendering - var newMetadata = new BitmapMetadata(metadata.Format); - - metadata.CopyMetadataPropertyTo(newMetadata, "System.Photo.Orientation"); - metadata.CopyMetadataPropertyTo(newMetadata, "System.Image.ColorSpace"); - - metadata = newMetadata; - } - - var frame = CreateBitmapFrame(transformedBitmap, metadata); - - encoder.Frames.Add(frame); - } - } - - path = GetDestinationPath(encoder); - _fileSystem.Directory.CreateDirectory(_fileSystem.Path.GetDirectoryName(path)); - using (var outputStream = _fileSystem.File.Open(path, FileMode.CreateNew, FileAccess.Write)) - { - encoder.Save(outputStream); } } @@ -141,54 +153,271 @@ namespace ImageResizer.Models } } - private BitmapEncoder CreateEncoder(Guid containerFormat) + private async Task ExecuteAiAsync(BitmapDecoder decoder, IRandomAccessStream winrtInputStream, Guid encoderGuid) { - var createdEncoder = BitmapEncoder.Create(containerFormat); - if (!createdEncoder.CanEncode()) + try { - createdEncoder = BitmapEncoder.Create(_settings.FallbackEncoder); - } + using var softwareBitmap = await decoder.GetSoftwareBitmapAsync( + BitmapPixelFormat.Bgra8, + BitmapAlphaMode.Premultiplied); - ConfigureEncoder(createdEncoder); + using var aiResult = _aiSuperResolutionService.ApplySuperResolution( + softwareBitmap, + _settings.AiSize.Scale, + _file); - return createdEncoder; - - void ConfigureEncoder(BitmapEncoder encoder) - { - switch (encoder) + if (aiResult == null) { - case JpegBitmapEncoder jpegEncoder: - jpegEncoder.QualityLevel = MathHelpers.Clamp(_settings.JpegQualityLevel, 1, 100); - break; - - case PngBitmapEncoder pngBitmapEncoder: - pngBitmapEncoder.Interlace = _settings.PngInterlaceOption; - break; - - case TiffBitmapEncoder tiffEncoder: - tiffEncoder.Compression = _settings.TiffCompressOption; - break; + throw new InvalidOperationException(ResourceLoaderInstance.GetString("Error_AiConversionFailed")); } + + var outputWidth = aiResult.PixelWidth; + var outputHeight = aiResult.PixelHeight; + + var path = GetDestinationPath(encoderGuid, outputWidth, outputHeight); + _fileSystem.Directory.CreateDirectory(_fileSystem.Path.GetDirectoryName(path)); + + using (var outputStream = _fileSystem.File.Open(path, FileMode.CreateNew, FileAccess.ReadWrite)) + { + var winrtOutputStream = outputStream.AsRandomAccessStream(); + await EncodeToStreamAsync( + decoder, + winrtInputStream, + winrtOutputStream, + encoderGuid, + (encoder, _) => + { + encoder.SetSoftwareBitmap(aiResult); + return Task.CompletedTask; + }); + } + + return path; + } + catch (Exception ex) when (ex is not InvalidOperationException) + { + var errorMessage = string.Format(CultureInfo.CurrentCulture, AiErrorFormat, ex.Message); + throw new InvalidOperationException(errorMessage, ex); } } - private BitmapSource Transform(BitmapSource source) + private async Task EncodeToStreamAsync( + BitmapDecoder decoder, + IRandomAccessStream inputStream, + IRandomAccessStream outputStream, + Guid encoderGuid, + Func writeContent) { - if (_settings.SelectedSize is AiSize) + var decoderEncoderId = CodecHelper.GetEncoderIdForDecoder(decoder); + bool canTranscode = !_settings.RemoveMetadata + && decoderEncoderId.HasValue + && decoderEncoderId.Value == encoderGuid; + + if (canTranscode) { - return TransformWithAi(source); + await TranscodeAsync(decoder, inputStream, outputStream, writeContent); + } + else + { + await FreshEncodeAsync(decoder, outputStream, encoderGuid, writeContent); + } + } + + /// + /// Transcode path: re-encodes pixels via BitmapTransform while preserving all metadata. + /// The callback receives isTranscode=true and should + /// configure properties only. + /// + private static async Task TranscodeAsync( + BitmapDecoder decoder, + IRandomAccessStream inputStream, + IRandomAccessStream outputStream, + Func writeContent) + { + inputStream.Seek(0); + var encoder = await BitmapEncoder.CreateForTranscodingAsync(outputStream, decoder); + await writeContent(encoder, true); + + // Safety net: some JPEG files with large/unusual metadata blocks (e.g. 54 KB + // embedded thumbnails) lose EXIF properties during transcode — the WPF equivalent + // threw InvalidOperationException on encoder.Metadata = decoder.Metadata for these. + // Re-set known critical properties to ensure they survive. + await CopyKnownMetadataAsync(decoder, encoder); + + await encoder.FlushAsync(); + } + + /// + /// Fresh encoder path: creates a blank encoder and manually writes pixel data. + /// Used when metadata must be stripped (RemoveMetadata) or format doesn't match (ICO→PNG). + /// The callback receives isTranscode=false and should + /// call or . + /// + private async Task FreshEncodeAsync( + BitmapDecoder decoder, + IRandomAccessStream outputStream, + Guid encoderGuid, + Func writeContent) + { + // Read rendering-critical metadata before encoding so we can restore it on + // the blank encoder. Only needed for RemoveMetadata; format-mismatch files + // (e.g. ICO) rarely carry meaningful EXIF data. + BitmapPropertySet renderingMetadata = null; + if (_settings.RemoveMetadata) + { + renderingMetadata = await ReadMetadataAsync(decoder, RenderingMetadataProperties); } - int originalWidth = source.PixelWidth; - int originalHeight = source.PixelHeight; + var encoder = await CreateFreshEncoderAsync(encoderGuid, outputStream); + await writeContent(encoder, false); + if (renderingMetadata != null) + { + await WriteMetadataAsync(encoder, renderingMetadata); + } + + await encoder.FlushAsync(); + } + + /// + /// Decodes each frame, applies the transform, and writes pixel data to the encoder. + /// Uses GetPixelDataAsync + SetPixelData for explicit pixel format control — the + /// SetSoftwareBitmap API can fail with ArgumentException for some decoder outputs. + /// + private static async Task EncodeFramesAsync( + BitmapEncoder encoder, + BitmapDecoder decoder, + uint scaledWidth, + uint scaledHeight, + BitmapBounds? cropBounds, + bool noTransformNeeded, + int originalWidth, + int originalHeight) + { + var transform = new BitmapTransform(); + if (!noTransformNeeded) + { + transform.ScaledWidth = scaledWidth; + transform.ScaledHeight = scaledHeight; + transform.InterpolationMode = BitmapInterpolationMode.Fant; + + if (cropBounds.HasValue) + { + transform.Bounds = cropBounds.Value; + } + } + else + { + transform.ScaledWidth = (uint)originalWidth; + transform.ScaledHeight = (uint)originalHeight; + } + + uint outWidth = cropBounds?.Width ?? (noTransformNeeded ? (uint)originalWidth : scaledWidth); + uint outHeight = cropBounds?.Height ?? (noTransformNeeded ? (uint)originalHeight : scaledHeight); + + for (uint i = 0; i < decoder.FrameCount; i++) + { + if (i > 0) + { + await encoder.GoToNextFrameAsync(); + } + + var frame = await decoder.GetFrameAsync(i); + var pixelData = await frame.GetPixelDataAsync( + BitmapPixelFormat.Bgra8, + BitmapAlphaMode.Premultiplied, + transform, + ExifOrientationMode.IgnoreExifOrientation, + ColorManagementMode.DoNotColorManage); + + encoder.SetPixelData( + BitmapPixelFormat.Bgra8, + BitmapAlphaMode.Premultiplied, + outWidth, + outHeight, + frame.DpiX, + frame.DpiY, + pixelData.DetachPixelData()); + } + } + + private static readonly string[] KnownMetadataProperties = + [ + "System.Photo.DateTaken", + "System.Photo.CameraModel", + "System.Photo.CameraManufacturer", + "System.Photo.Orientation", + "System.Image.ColorSpace", + "System.Comment", + ]; + + /// + /// Best-effort read of metadata properties from the decoder. + /// Returns null if the format doesn't support metadata (e.g. BMP). + /// + private static async Task ReadMetadataAsync(BitmapDecoder decoder, string[] propertyNames) + { + try + { + var props = await decoder.BitmapProperties.GetPropertiesAsync(propertyNames); + if (props.Count > 0) + { + var result = new BitmapPropertySet(); + foreach (var prop in props) + { + result[prop.Key] = prop.Value; + } + + return result; + } + } + catch + { + // Some formats (e.g. BMP) don't support property queries. + } + + return null; + } + + /// + /// Best-effort write of metadata properties to the encoder. + /// + private static async Task WriteMetadataAsync(BitmapEncoder encoder, BitmapPropertySet metadata) + { + if (metadata == null || metadata.Count == 0) + { + return; + } + + try + { + await encoder.BitmapProperties.SetPropertiesAsync(metadata); + } + catch + { + // Some encoders don't support these properties (e.g. BMP). + } + } + + /// + /// Safety net for the transcode path: re-sets known EXIF properties that + /// CreateForTranscodingAsync may silently drop for files with large or + /// unusual metadata blocks (see TestMetadataIssue2447.jpg). + /// + private static async Task CopyKnownMetadataAsync(BitmapDecoder decoder, BitmapEncoder encoder) + { + var metadata = await ReadMetadataAsync(decoder, KnownMetadataProperties); + await WriteMetadataAsync(encoder, metadata); + } + + private (uint ScaledWidth, uint ScaledHeight, BitmapBounds? CropBounds, bool NoTransformNeeded) CalculateDimensions( + int originalWidth, int originalHeight, double dpiX, double dpiY) + { // Convert from the chosen size unit to pixels, if necessary. - double width = _settings.SelectedSize.GetPixelWidth(originalWidth, source.DpiX); - double height = _settings.SelectedSize.GetPixelHeight(originalHeight, source.DpiY); + double width = _settings.SelectedSize.GetPixelWidth(originalWidth, dpiX); + double height = _settings.SelectedSize.GetPixelHeight(originalHeight, dpiY); // Swap target width/height dimensions if orientation correction is required. - // Ensures that we don't try to fit a landscape image into a portrait box by - // distorting it, unless specific Auto/Percent rules are applied. bool canSwapDimensions = _settings.IgnoreOrientation && !_settings.SelectedSize.HasAuto && _settings.SelectedSize.Unit != ResizeUnit.Percent; @@ -214,15 +443,11 @@ namespace ImageResizer.Models // Normalize scales based on the chosen Fit/Fill mode. if (_settings.SelectedSize.Fit == ResizeFit.Fit) { - // Fit: use the smaller scale to ensure the image fits within the target. scaleX = Math.Min(scaleX, scaleY); scaleY = scaleX; } else if (_settings.SelectedSize.Fit == ResizeFit.Fill) { - // Fill: use the larger scale to ensure the target area is fully covered. - // This often results in one dimension overflowing, which is handled by - // cropping later. scaleX = Math.Max(scaleX, scaleY); scaleY = scaleX; } @@ -230,177 +455,107 @@ namespace ImageResizer.Models // Handle Shrink Only mode. if (_settings.ShrinkOnly && _settings.SelectedSize.Unit != ResizeUnit.Percent) { - // Shrink Only mode should never return an image larger than the original. if (scaleX > 1 || scaleY > 1) { - return source; + return ((uint)originalWidth, (uint)originalHeight, null, true); } - // Allow for crop-only when in Fill mode. - // At this point, the scale is <= 1.0. In Fill mode, it is possible for - // the scale to be 1.0 (no resize needed) while the target dimensions are - // smaller than the originals, requiring a crop. bool isFillCropRequired = _settings.SelectedSize.Fit == ResizeFit.Fill && (originalWidth > width || originalHeight > height); - // If the scale is exactly 1.0 and a crop isn't required, we return the - // original image to prevent a re-encode. if (scaleX == 1 && scaleY == 1 && !isFillCropRequired) { - return source; + return ((uint)originalWidth, (uint)originalHeight, null, true); } } - // Apply the scaling. - var scaledBitmap = new TransformedBitmap(source, new ScaleTransform(scaleX, scaleY)); + // Calculate scaled dimensions + uint scaledWidth = (uint)Math.Max(1, (int)Math.Round(originalWidth * scaleX)); + uint scaledHeight = (uint)Math.Max(1, (int)Math.Round(originalHeight * scaleY)); - // Apply the centered crop for Fill mode, if necessary. Applies when Fill - // mode caused the scaled image to exceed the target dimensions. + // Apply the centered crop for Fill mode, if necessary. if (_settings.SelectedSize.Fit == ResizeFit.Fill - && (scaledBitmap.PixelWidth > width - || scaledBitmap.PixelHeight > height)) + && (scaledWidth > (uint)width || scaledHeight > (uint)height)) { - int x = (int)(((originalWidth * scaleX) - width) / 2); - int y = (int)(((originalHeight * scaleY) - height) / 2); + uint cropX = (uint)(((originalWidth * scaleX) - width) / 2); + uint cropY = (uint)(((originalHeight * scaleY) - height) / 2); - return new CroppedBitmap(scaledBitmap, new Int32Rect(x, y, (int)width, (int)height)); - } - - return scaledBitmap; - } - - private BitmapSource TransformWithAi(BitmapSource source) - { - try - { - var result = _aiSuperResolutionService.ApplySuperResolution( - source, - _settings.AiSize.Scale, - _file); - - if (result == null) + var cropBounds = new BitmapBounds { - throw new InvalidOperationException(Properties.Resources.Error_AiConversionFailed); - } + X = cropX, + Y = cropY, + Width = (uint)width, + Height = (uint)height, + }; - return result; - } - catch (Exception ex) - { - // Wrap the exception with a localized message - // This will be caught by ResizeBatch.Process() and displayed to the user - var errorMessage = string.Format(CultureInfo.CurrentCulture, _aiErrorFormat, ex.Message); - throw new InvalidOperationException(errorMessage, ex); + return (scaledWidth, scaledHeight, cropBounds, false); } + + return (scaledWidth, scaledHeight, null, false); } - /// - /// Checks original metadata by writing an image containing the given metadata into a memory stream. - /// In case of errors, we try to rebuild the metadata object and check again. - /// We return null if we were not able to get hold of valid metadata. - /// - private BitmapMetadata GetValidMetadata(BitmapMetadata originalMetadata, BitmapSource transformedBitmap, Guid containerFormat) + private async Task CreateFreshEncoderAsync(Guid encoderGuid, IRandomAccessStream outputStream) { - if (originalMetadata == null) + var propertySet = GetEncoderPropertySet(encoderGuid); + return propertySet != null + ? await BitmapEncoder.CreateAsync(encoderGuid, outputStream, propertySet) + : await BitmapEncoder.CreateAsync(encoderGuid, outputStream); + } + + private float GetJpegQualityFraction() + => (float)Math.Clamp(_settings.JpegQualityLevel, 1, 100) / 100f; + + private BitmapPropertySet GetEncoderPropertySet(Guid encoderGuid) + { + if (encoderGuid == BitmapEncoder.JpegEncoderId) { - return null; + return new BitmapPropertySet + { + { "ImageQuality", new BitmapTypedValue(GetJpegQualityFraction(), PropertyType.Single) }, + }; } - // Check if the original metadata is valid - var frameWithOriginalMetadata = CreateBitmapFrame(transformedBitmap, originalMetadata); - if (EnsureFrameIsValid(frameWithOriginalMetadata)) + if (encoderGuid == BitmapEncoder.TiffEncoderId) { - return originalMetadata; + var compressionMethod = MapTiffCompression(_settings.TiffCompressOption); + if (compressionMethod.HasValue) + { + return new BitmapPropertySet + { + { "TiffCompressionMethod", new BitmapTypedValue(compressionMethod.Value, PropertyType.UInt8) }, + }; + } } - // Original metadata was invalid. We try to rebuild the metadata object from the scratch and discard invalid metadata fields - var recreatedMetadata = BuildMetadataFromTheScratch(originalMetadata); - var frameWithRecreatedMetadata = CreateBitmapFrame(transformedBitmap, recreatedMetadata); - if (EnsureFrameIsValid(frameWithRecreatedMetadata)) - { - return recreatedMetadata; - } - - // Seems like we have an invalid metadata object. ImageResizer will fail when trying to write the image to disk. We discard all metadata to be able to save the image. return null; - - // The safest way to check if the metadata object is valid is to call Save() on the encoder. - // I tried other ways to check if metadata is valid (like calling Clone() on the metadata object) but this was not reliable resulting in a few github issues. - bool EnsureFrameIsValid(BitmapFrame frameToBeChecked) - { - try - { - var encoder = CreateEncoder(containerFormat); - encoder.Frames.Add(frameToBeChecked); - using (var testStream = new MemoryStream()) - { - encoder.Save(testStream); - } - - return true; - } - catch (Exception) - { - return false; - } - } } - /// - /// Read all metadata and build up metadata object from the scratch. Discard invalid (unreadable/unwritable) metadata. - /// - private static BitmapMetadata BuildMetadataFromTheScratch(BitmapMetadata originalMetadata) + private static byte? MapTiffCompression(TiffCompressOption option) { - try + return option switch { - var metadata = new BitmapMetadata(originalMetadata.Format); - var listOfMetadata = originalMetadata.GetListOfMetadata(); - foreach (var (metadataPath, value) in listOfMetadata) - { - if (value is BitmapMetadata bitmapMetadata) - { - var innerMetadata = new BitmapMetadata(bitmapMetadata.Format); - metadata.SetQuerySafe(metadataPath, innerMetadata); - } - else - { - metadata.SetQuerySafe(metadataPath, value); - } - } - - return metadata; - } - catch (ArgumentException ex) - { - Debug.WriteLine(ex); - - return null; - } + TiffCompressOption.None => 1, + TiffCompressOption.Ccitt3 => 2, + TiffCompressOption.Ccitt4 => 3, + TiffCompressOption.Lzw => 4, + TiffCompressOption.Rle => 5, + TiffCompressOption.Zip => 6, + _ => null, // Default: let the encoder decide + }; } - private static BitmapFrame CreateBitmapFrame(BitmapSource transformedBitmap, BitmapMetadata metadata) - { - return BitmapFrame.Create( - transformedBitmap, - thumbnail: null, /* should be null, see #15413 */ - metadata, - colorContexts: null /* should be null, see #14866 */ ); - } - - private string GetDestinationPath(BitmapEncoder encoder) + private string GetDestinationPath(Guid encoderGuid, int outputPixelWidth, int outputPixelHeight) { var directory = _destinationDirectory ?? _fileSystem.Path.GetDirectoryName(_file); var originalFileName = _fileSystem.Path.GetFileNameWithoutExtension(_file); - var supportedExtensions = encoder.CodecInfo.FileExtensions.Split(','); + var supportedExtensions = CodecHelper.GetSupportedExtensions(encoderGuid); var extension = _fileSystem.Path.GetExtension(_file); if (!supportedExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase)) { - extension = supportedExtensions.FirstOrDefault(); + extension = CodecHelper.GetDefaultExtension(encoderGuid); } - // Remove directory characters from the size's name. - // For AI Size, use the scale display (e.g., "2×") instead of the full name string sizeName = _settings.SelectedSize is AiSize aiSize ? aiSize.ScaleDisplay : _settings.SelectedSize.Name; @@ -408,9 +563,8 @@ namespace ImageResizer.Models .Replace('\\', '_') .Replace('/', '_'); - // Using CurrentCulture since this is user facing - var selectedWidth = _settings.SelectedSize is AiSize ? encoder.Frames[0].PixelWidth : _settings.SelectedSize.Width; - var selectedHeight = _settings.SelectedSize is AiSize ? encoder.Frames[0].PixelHeight : _settings.SelectedSize.Height; + var selectedWidth = _settings.SelectedSize is AiSize ? outputPixelWidth : _settings.SelectedSize.Width; + var selectedHeight = _settings.SelectedSize is AiSize ? outputPixelHeight : _settings.SelectedSize.Height; var fileName = string.Format( CultureInfo.CurrentCulture, _settings.FileNameFormat, @@ -418,10 +572,9 @@ namespace ImageResizer.Models sizeNameSanitized, selectedWidth, selectedHeight, - encoder.Frames[0].PixelWidth, - encoder.Frames[0].PixelHeight); + outputPixelWidth, + outputPixelHeight); - // Remove invalid characters from the final file name. fileName = fileName .Replace(':', '_') .Replace('*', '_') @@ -431,7 +584,6 @@ namespace ImageResizer.Models .Replace('>', '_') .Replace('|', '_'); - // Avoid creating not recommended filenames if (_avoidFilenames.Contains(fileName.ToUpperInvariant())) { fileName = fileName + "_"; diff --git a/src/modules/imageresizer/ui/Models/ResizeSize.cs b/src/modules/imageresizer/ui/Models/ResizeSize.cs index 49869f4bd2..6520133649 100644 --- a/src/modules/imageresizer/ui/Models/ResizeSize.cs +++ b/src/modules/imageresizer/ui/Models/ResizeSize.cs @@ -1,36 +1,52 @@ -#pragma warning disable IDE0073 +#pragma warning disable IDE0073, SA1636 // Copyright (c) Brice Lambson // The Brice Lambson licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. // Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 +#pragma warning restore IDE0073, SA1636 +using System; using System.Collections.Generic; using System.Diagnostics; using System.Text.Json.Serialization; - +using CommunityToolkit.Mvvm.ComponentModel; using ImageResizer.Helpers; -using ImageResizer.Properties; using ManagedCommon; namespace ImageResizer.Models { - public class ResizeSize : Observable, IHasId + public partial class ResizeSize : ObservableObject, IHasId { - private static readonly Dictionary _tokens = new Dictionary + private static readonly Dictionary _tokenKeys = new Dictionary { - ["$small$"] = Resources.Small, - ["$medium$"] = Resources.Medium, - ["$large$"] = Resources.Large, - ["$phone$"] = Resources.Phone, + ["$small$"] = "Small", + ["$medium$"] = "Medium", + ["$large$"] = "Large", + ["$phone$"] = "Phone", }; + [ObservableProperty] + [JsonPropertyName("Id")] private int _id; + private string _name; + + [ObservableProperty] + [JsonPropertyName("fit")] + [NotifyPropertyChangedFor(nameof(ShowHeight))] private ResizeFit _fit = ResizeFit.Fit; + + [ObservableProperty] + [JsonPropertyName("width")] private double _width; + + [ObservableProperty] + [JsonPropertyName("height")] private double _height; - private bool _showHeight = true; + + [ObservableProperty] + [JsonPropertyName("unit")] + [NotifyPropertyChangedFor(nameof(ShowHeight))] private ResizeUnit _unit = ResizeUnit.Pixel; public ResizeSize(int id, string name, ResizeFit fit, double width, double height, ResizeUnit unit) @@ -47,73 +63,18 @@ namespace ImageResizer.Models { } - [JsonPropertyName("Id")] - public int Id - { - get => _id; - set => Set(ref _id, value); - } - [JsonPropertyName("name")] public virtual string Name { get => _name; - set => Set(ref _name, ReplaceTokens(value)); + set => SetProperty(ref _name, ReplaceTokens(value)); } - [JsonPropertyName("fit")] - public ResizeFit Fit - { - get => _fit; - set - { - var previousFit = _fit; - Set(ref _fit, value); - if (!Equals(previousFit, value)) - { - UpdateShowHeight(); - } - } - } - - [JsonPropertyName("width")] - public double Width - { - get => _width; - set => Set(ref _width, value); - } - - [JsonPropertyName("height")] - public double Height - { - get => _height; - set => Set(ref _height, value); - } - - public bool ShowHeight - { - get => _showHeight; - set => Set(ref _showHeight, value); - } + public bool ShowHeight => Fit == ResizeFit.Stretch || Unit != ResizeUnit.Percent; public bool HasAuto => Width == 0 || Height == 0 || double.IsNaN(Width) || double.IsNaN(Height); - [JsonPropertyName("unit")] - public ResizeUnit Unit - { - get => _unit; - set - { - var previousUnit = _unit; - Set(ref _unit, value); - if (!Equals(previousUnit, value)) - { - UpdateShowHeight(); - } - } - } - public double GetPixelWidth(int originalWidth, double dpi) => ConvertToPixels(Width, Unit, originalWidth, dpi); @@ -127,15 +88,10 @@ namespace ImageResizer.Models dpi); private static string ReplaceTokens(string text) - => (text != null && _tokens.TryGetValue(text, out var result)) - ? result + => text != null && _tokenKeys.TryGetValue(text, out var key) + ? ResourceLoaderInstance.GetString(key) : text; - private void UpdateShowHeight() - { - ShowHeight = Fit == ResizeFit.Stretch || Unit != ResizeUnit.Percent; - } - private double ConvertToPixels(double value, ResizeUnit unit, int originalValue, double dpi) { if (value == 0 || double.IsNaN(value)) diff --git a/src/modules/imageresizer/ui/Models/ResizeUnit.cs b/src/modules/imageresizer/ui/Models/ResizeUnit.cs index 31851a7806..89c6f074cb 100644 --- a/src/modules/imageresizer/ui/Models/ResizeUnit.cs +++ b/src/modules/imageresizer/ui/Models/ResizeUnit.cs @@ -1,9 +1,9 @@ -#pragma warning disable IDE0073 +#pragma warning disable IDE0073, SA1636 // Copyright (c) Brice Lambson // The Brice Lambson licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. // Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 +#pragma warning restore IDE0073, SA1636 namespace ImageResizer.Models { diff --git a/src/modules/imageresizer/ui/Program.cs b/src/modules/imageresizer/ui/Program.cs new file mode 100644 index 0000000000..8b9379608e --- /dev/null +++ b/src/modules/imageresizer/ui/Program.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using Microsoft.UI.Dispatching; +using Microsoft.UI.Xaml; + +namespace ImageResizer +{ + public static class Program + { + [STAThread] + public static void Main(string[] args) + { + WinRT.ComWrappersSupport.InitializeComWrappers(); + Application.Start((p) => + { + var context = new DispatcherQueueSynchronizationContext( + DispatcherQueue.GetForCurrentThread()); + SynchronizationContext.SetSynchronizationContext(context); + _ = new App(); + }); + } + } +} diff --git a/src/modules/imageresizer/ui/Properties/InternalsVisibleTo.cs b/src/modules/imageresizer/ui/Properties/InternalsVisibleTo.cs deleted file mode 100644 index b2793599f5..0000000000 --- a/src/modules/imageresizer/ui/Properties/InternalsVisibleTo.cs +++ /dev/null @@ -1,9 +0,0 @@ -#pragma warning disable IDE0073 -// Copyright (c) Brice Lambson -// The Brice Lambson licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 - -using System.Runtime.CompilerServices; - -[assembly: InternalsVisibleTo("ImageResizer.Test")] diff --git a/src/modules/imageresizer/ui/Properties/Resources.Designer.cs b/src/modules/imageresizer/ui/Properties/Resources.Designer.cs deleted file mode 100644 index 916f38e890..0000000000 --- a/src/modules/imageresizer/ui/Properties/Resources.Designer.cs +++ /dev/null @@ -1,1152 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// Runtime Version:4.0.30319.42000 -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace ImageResizer.Properties { - using System; - - - /// - /// A strongly-typed resource class, for looking up localized strings, etc. - /// - // This class was auto-generated by the StronglyTypedResourceBuilder - // class via a tool like ResGen or Visual Studio. - // To add or remove a member, edit your .ResX file then rerun ResGen - // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "18.0.0.0")] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - public class Resources { - - private static global::System.Resources.ResourceManager resourceMan; - - private static global::System.Globalization.CultureInfo resourceCulture; - - [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal Resources() { - } - - /// - /// Returns the cached ResourceManager instance used by this class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - public static global::System.Resources.ResourceManager ResourceManager { - get { - if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ImageResizer.Properties.Resources", typeof(Resources).Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - /// - /// Overrides the current thread's CurrentUICulture property for all - /// resource lookups using this strongly typed resource class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - public static global::System.Globalization.CultureInfo Culture { - get { - return resourceCulture; - } - set { - resourceCulture = value; - } - } - - /// - /// Looks up a localized string similar to All Files. - /// - public static string AllFilesFilter { - get { - return ResourceManager.GetString("AllFilesFilter", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Cancel. - /// - public static string Cancel { - get { - return ResourceManager.GetString("Cancel", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Failed to convert image format for AI processing.. - /// - public static string Error_AiConversionFailed { - get { - return ResourceManager.GetString("Error_AiConversionFailed", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to AI super resolution processing failed: {0}. - /// - public static string Error_AiProcessingFailed { - get { - return ResourceManager.GetString("Error_AiProcessingFailed", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to AI scaling operation failed.. - /// - public static string Error_AiScalingFailed { - get { - return ResourceManager.GetString("Error_AiScalingFailed", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Height. - /// - public static string Height { - get { - return ResourceManager.GetString("Height", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Image sizes. - /// - public static string Image_Sizes { - get { - return ResourceManager.GetString("Image_Sizes", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Image Resizer. - /// - public static string ImageResizer { - get { - return ResourceManager.GetString("ImageResizer", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Current:. - /// - public static string Input_AiCurrentLabel { - get { - return ResourceManager.GetString("Input_AiCurrentLabel", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Checking AI model availability.... - /// - public static string Input_AiModelChecking { - get { - return ResourceManager.GetString("Input_AiModelChecking", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to AI feature is disabled by system settings.. - /// - public static string Input_AiModelDisabledByUser { - get { - return ResourceManager.GetString("Input_AiModelDisabledByUser", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Download. - /// - public static string Input_AiModelDownloadButton { - get { - return ResourceManager.GetString("Input_AiModelDownloadButton", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Failed to download AI model. Please try again.. - /// - public static string Input_AiModelDownloadFailed { - get { - return ResourceManager.GetString("Input_AiModelDownloadFailed", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Downloading AI model.... - /// - public static string Input_AiModelDownloading { - get { - return ResourceManager.GetString("Input_AiModelDownloading", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to AI model not downloaded. Click Download to get started.. - /// - public static string Input_AiModelNotAvailable { - get { - return ResourceManager.GetString("Input_AiModelNotAvailable", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to AI feature is not supported on this system.. - /// - public static string Input_AiModelNotSupported { - get { - return ResourceManager.GetString("Input_AiModelNotSupported", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to New:. - /// - public static string Input_AiNewLabel { - get { - return ResourceManager.GetString("Input_AiNewLabel", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to {0}×. - /// - public static string Input_AiScaleFormat { - get { - return ResourceManager.GetString("Input_AiScaleFormat", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Scale. - /// - public static string Input_AiScaleLabel { - get { - return ResourceManager.GetString("Input_AiScaleLabel", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Super resolution. - /// - public static string Input_AiSuperResolution { - get { - return ResourceManager.GetString("Input_AiSuperResolution", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Upscale images using on-device AI. - /// - public static string Input_AiSuperResolutionDescription { - get { - return ResourceManager.GetString("Input_AiSuperResolutionDescription", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Unavailable. - /// - public static string Input_AiUnknownSize { - get { - return ResourceManager.GetString("Input_AiUnknownSize", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to (auto). - /// - public static string Input_Auto { - get { - return ResourceManager.GetString("Input_Auto", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Select a size. - /// - public static string Input_Content { - get { - return ResourceManager.GetString("Input_Content", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Custom. - /// - public static string Input_Custom { - get { - return ResourceManager.GetString("Input_Custom", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Gif files with animations may not be correctly resized.. - /// - public static string Input_GifWarning { - get { - return ResourceManager.GetString("Input_GifWarning", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Ignore the _orientation of pictures. - /// - public static string Input_IgnoreOrientation { - get { - return ResourceManager.GetString("Input_IgnoreOrientation", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Remove meta_data that doesn't affect rendering. - /// - public static string Input_RemoveMetadata { - get { - return ResourceManager.GetString("Input_RemoveMetadata", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Ov_erwrite files. - /// - public static string Input_Replace { - get { - return ResourceManager.GetString("Input_Replace", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Resize. - /// - public static string Input_Resize { - get { - return ResourceManager.GetString("Input_Resize", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to _Make pictures smaller but not larger. - /// - public static string Input_ShrinkOnly { - get { - return ResourceManager.GetString("Input_ShrinkOnly", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to _Make pictures smaller but not larger. - /// - public static string Input_ShrinkOnly_Content { - get { - return ResourceManager.GetString("Input_ShrinkOnly.Content", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Large. - /// - public static string Large { - get { - return ResourceManager.GetString("Large", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Medium. - /// - public static string Medium { - get { - return ResourceManager.GetString("Medium", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to OK. - /// - public static string OK { - get { - return ResourceManager.GetString("OK", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Apply settings. - /// - public static string OK_Tooltip { - get { - return ResourceManager.GetString("OK_Tooltip", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Settings. - /// - public static string Open_settings { - get { - return ResourceManager.GetString("Open_settings", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Phone. - /// - public static string Phone { - get { - return ResourceManager.GetString("Phone", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to All Picture Files. - /// - public static string PictureFilter { - get { - return ResourceManager.GetString("PictureFilter", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to (Default). - /// - public static string PngInterlaceOption_Default { - get { - return ResourceManager.GetString("PngInterlaceOption_Default", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Off. - /// - public static string PngInterlaceOption_Off { - get { - return ResourceManager.GetString("PngInterlaceOption_Off", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to On. - /// - public static string PngInterlaceOption_On { - get { - return ResourceManager.GetString("PngInterlaceOption_On", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Resizing your pictures.... - /// - public static string Progress_MainInstruction { - get { - return ResourceManager.GetString("Progress_MainInstruction", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Stop. - /// - public static string Progress_Stop { - get { - return ResourceManager.GetString("Progress_Stop", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to About {0} hour, {1} minute remaining.. - /// - public static string Progress_TimeRemaining_HourMinute { - get { - return ResourceManager.GetString("Progress_TimeRemaining_HourMinute", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to About {0} hour, {1} minutes remaining.. - /// - public static string Progress_TimeRemaining_HourMinutes { - get { - return ResourceManager.GetString("Progress_TimeRemaining_HourMinutes", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to About {0} hours, {1} minute remaining.. - /// - public static string Progress_TimeRemaining_HoursMinute { - get { - return ResourceManager.GetString("Progress_TimeRemaining_HoursMinute", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to About {0} hours, {1} minutes remaining.. - /// - public static string Progress_TimeRemaining_HoursMinutes { - get { - return ResourceManager.GetString("Progress_TimeRemaining_HoursMinutes", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to About {1} minute, {2} second remaining.. - /// - public static string Progress_TimeRemaining_MinuteSecond { - get { - return ResourceManager.GetString("Progress_TimeRemaining_MinuteSecond", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to About {1} minute, {2} seconds remaining.. - /// - public static string Progress_TimeRemaining_MinuteSeconds { - get { - return ResourceManager.GetString("Progress_TimeRemaining_MinuteSeconds", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to About {1} minutes, {2} second remaining.. - /// - public static string Progress_TimeRemaining_MinutesSecond { - get { - return ResourceManager.GetString("Progress_TimeRemaining_MinutesSecond", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to About {1} minutes, {2} seconds remaining.. - /// - public static string Progress_TimeRemaining_MinutesSeconds { - get { - return ResourceManager.GetString("Progress_TimeRemaining_MinutesSeconds", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to About {2} second remaining.. - /// - public static string Progress_TimeRemaining_Second { - get { - return ResourceManager.GetString("Progress_TimeRemaining_Second", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to About {2} seconds remaining.. - /// - public static string Progress_TimeRemaining_Seconds { - get { - return ResourceManager.GetString("Progress_TimeRemaining_Seconds", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Resize pictures. - /// - public static string Resize_Tooltip { - get { - return ResourceManager.GetString("Resize_Tooltip", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Resize type. - /// - public static string Resize_Type { - get { - return ResourceManager.GetString("Resize_Type", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Fill. - /// - public static string ResizeFit_Fill { - get { - return ResourceManager.GetString("ResizeFit_Fill", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to fills. - /// - public static string ResizeFit_Fill_ThirdPersonSingular { - get { - return ResourceManager.GetString("ResizeFit_Fill_ThirdPersonSingular", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Fit. - /// - public static string ResizeFit_Fit { - get { - return ResourceManager.GetString("ResizeFit_Fit", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to fits within. - /// - public static string ResizeFit_Fit_ThirdPersonSingular { - get { - return ResourceManager.GetString("ResizeFit_Fit_ThirdPersonSingular", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Stretch. - /// - public static string ResizeFit_Stretch { - get { - return ResourceManager.GetString("ResizeFit_Stretch", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to stretches to. - /// - public static string ResizeFit_Stretch_ThirdPersonSingular { - get { - return ResourceManager.GetString("ResizeFit_Stretch_ThirdPersonSingular", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Centimeters. - /// - public static string ResizeUnit_Centimeter { - get { - return ResourceManager.GetString("ResizeUnit_Centimeter", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Inches. - /// - public static string ResizeUnit_Inch { - get { - return ResourceManager.GetString("ResizeUnit_Inch", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Percent. - /// - public static string ResizeUnit_Percent { - get { - return ResourceManager.GetString("ResizeUnit_Percent", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Pixels. - /// - public static string ResizeUnit_Pixel { - get { - return ResourceManager.GetString("ResizeUnit_Pixel", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Close. - /// - public static string Results_Close { - get { - return ResourceManager.GetString("Results_Close", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Can't resize the following pictures. - /// - public static string Results_MainInstruction { - get { - return ResourceManager.GetString("Results_MainInstruction", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Small. - /// - public static string Small { - get { - return ResourceManager.GetString("Small", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Unit. - /// - public static string Unit { - get { - return ResourceManager.GetString("Unit", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Value must be between '{0}' and '{1}'.. - /// - public static string ValueMustBeBetween { - get { - return ResourceManager.GetString("ValueMustBeBetween", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Version. - /// - public static string Version { - get { - return ResourceManager.GetString("Version", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Width. - /// - public static string Width { - get { - return ResourceManager.GetString("Width", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Processing {0} files.... - /// - public static string CLI_ProcessingFiles { - get { - return ResourceManager.GetString("CLI_ProcessingFiles", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to [{0}%] {1}/{2} completed. - /// - public static string CLI_ProgressFormat { - get { - return ResourceManager.GetString("CLI_ProgressFormat", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Completed with {0} error(s).. - /// - public static string CLI_CompletedWithErrors { - get { - return ResourceManager.GetString("CLI_CompletedWithErrors", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to All files processed successfully!. - /// - public static string CLI_AllFilesProcessed { - get { - return ResourceManager.GetString("CLI_AllFilesProcessed", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to No input files or pipe specified. Showing usage.. - /// - public static string CLI_NoInputFiles { - get { - return ResourceManager.GetString("CLI_NoInputFiles", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Warning: Size index {0} is invalid. Using custom size.. - /// - public static string CLI_WarningInvalidSizeIndex { - get { - return ResourceManager.GetString("CLI_WarningInvalidSizeIndex", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Current Configuration:. - /// - public static string CLI_ConfigTitle { - get { - return ResourceManager.GetString("CLI_ConfigTitle", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to General Settings:. - /// - public static string CLI_ConfigGeneralSettings { - get { - return ResourceManager.GetString("CLI_ConfigGeneralSettings", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Shrink only: {0}. - /// - public static string CLI_ConfigShrinkOnly { - get { - return ResourceManager.GetString("CLI_ConfigShrinkOnly", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Replace original: {0}. - /// - public static string CLI_ConfigReplaceOriginal { - get { - return ResourceManager.GetString("CLI_ConfigReplaceOriginal", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Ignore orientation: {0}. - /// - public static string CLI_ConfigIgnoreOrientation { - get { - return ResourceManager.GetString("CLI_ConfigIgnoreOrientation", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Remove metadata: {0}. - /// - public static string CLI_ConfigRemoveMetadata { - get { - return ResourceManager.GetString("CLI_ConfigRemoveMetadata", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Keep date modified: {0}. - /// - public static string CLI_ConfigKeepDateModified { - get { - return ResourceManager.GetString("CLI_ConfigKeepDateModified", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to JPEG quality: {0}. - /// - public static string CLI_ConfigJpegQuality { - get { - return ResourceManager.GetString("CLI_ConfigJpegQuality", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to PNG interlace: {0}. - /// - public static string CLI_ConfigPngInterlace { - get { - return ResourceManager.GetString("CLI_ConfigPngInterlace", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to TIFF compress: {0}. - /// - public static string CLI_ConfigTiffCompress { - get { - return ResourceManager.GetString("CLI_ConfigTiffCompress", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Filename format: {0}. - /// - public static string CLI_ConfigFilenameFormat { - get { - return ResourceManager.GetString("CLI_ConfigFilenameFormat", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Custom Size:. - /// - public static string CLI_ConfigCustomSize { - get { - return ResourceManager.GetString("CLI_ConfigCustomSize", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Width: {0}. - /// - public static string CLI_ConfigWidth { - get { - return ResourceManager.GetString("CLI_ConfigWidth", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Height: {0}. - /// - public static string CLI_ConfigHeight { - get { - return ResourceManager.GetString("CLI_ConfigHeight", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Fit mode: {0}. - /// - public static string CLI_ConfigFitMode { - get { - return ResourceManager.GetString("CLI_ConfigFitMode", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Preset Sizes: (* = currently selected). - /// - public static string CLI_ConfigPresetSizes { - get { - return ResourceManager.GetString("CLI_ConfigPresetSizes", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to {0}: {1} x {2} ({3}). - /// - public static string CLI_ConfigPresetSizeFormat { - get { - return ResourceManager.GetString("CLI_ConfigPresetSizeFormat", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to → Custom size selected. - /// - public static string CLI_ConfigCustomSelected { - get { - return ResourceManager.GetString("CLI_ConfigCustomSelected", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Image Resizer CLI. - /// - public static string CLI_UsageTitle { - get { - return ResourceManager.GetString("CLI_UsageTitle", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Usage: PowerToys.ImageResizer.exe [options] <files>. - /// - public static string CLI_UsageLine { - get { - return ResourceManager.GetString("CLI_UsageLine", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Options:. - /// - public static string CLI_UsageOptions { - get { - return ResourceManager.GetString("CLI_UsageOptions", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Examples:. - /// - public static string CLI_UsageExamples { - get { - return ResourceManager.GetString("CLI_UsageExamples", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to PowerToys.ImageResizer.exe --help. - /// - public static string CLI_UsageExampleHelp { - get { - return ResourceManager.GetString("CLI_UsageExampleHelp", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to PowerToys.ImageResizer.exe --width 800 --height 600 image.jpg. - /// - public static string CLI_UsageExampleDimensions { - get { - return ResourceManager.GetString("CLI_UsageExampleDimensions", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to PowerToys.ImageResizer.exe --size 50 --unit percent *.jpg. - /// - public static string CLI_UsageExamplePercent { - get { - return ResourceManager.GetString("CLI_UsageExamplePercent", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to PowerToys.ImageResizer.exe --size 2 image1.png image2.png. - /// - public static string CLI_UsageExamplePreset { - get { - return ResourceManager.GetString("CLI_UsageExamplePreset", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Destination directory for resized images. - /// - public static string CLI_Option_Destination { - get { - return ResourceManager.GetString("CLI_Option_Destination", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Output filename format (e.g., %1 (%2)). - /// - public static string CLI_Option_FileName { - get { - return ResourceManager.GetString("CLI_Option_FileName", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Image files to resize. - /// - public static string CLI_Option_Files { - get { - return ResourceManager.GetString("CLI_Option_Files", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to How to fit image: fill, fit, stretch. - /// - public static string CLI_Option_Fit { - get { - return ResourceManager.GetString("CLI_Option_Fit", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Height of the resized image in pixels. - /// - public static string CLI_Option_Height { - get { - return ResourceManager.GetString("CLI_Option_Height", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Display this help message. - /// - public static string CLI_Option_Help { - get { - return ResourceManager.GetString("CLI_Option_Help", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Ignore image orientation metadata. - /// - public static string CLI_Option_IgnoreOrientation { - get { - return ResourceManager.GetString("CLI_Option_IgnoreOrientation", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Preserve the original file modification date. - /// - public static string CLI_Option_KeepDateModified { - get { - return ResourceManager.GetString("CLI_Option_KeepDateModified", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Set JPEG quality level (1-100). - /// - public static string CLI_Option_Quality { - get { - return ResourceManager.GetString("CLI_Option_Quality", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Remove image metadata during resizing. - /// - public static string CLI_Option_RemoveMetadata { - get { - return ResourceManager.GetString("CLI_Option_RemoveMetadata", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Replace the original image file. - /// - public static string CLI_Option_Replace { - get { - return ResourceManager.GetString("CLI_Option_Replace", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Display current configuration. - /// - public static string CLI_Option_ShowConfig { - get { - return ResourceManager.GetString("CLI_Option_ShowConfig", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Only shrink images, do not enlarge. - /// - public static string CLI_Option_ShrinkOnly { - get { - return ResourceManager.GetString("CLI_Option_ShrinkOnly", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Use preset size by index (0-based). - /// - public static string CLI_Option_Size { - get { - return ResourceManager.GetString("CLI_Option_Size", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Unit of measurement: pixel, percent, cm, inch. - /// - public static string CLI_Option_Unit { - get { - return ResourceManager.GetString("CLI_Option_Unit", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Width of the resized image in pixels. - /// - public static string CLI_Option_Width { - get { - return ResourceManager.GetString("CLI_Option_Width", resourceCulture); - } - } - } -} diff --git a/src/modules/imageresizer/ui/Properties/Resources.cs b/src/modules/imageresizer/ui/Properties/Resources.cs new file mode 100644 index 0000000000..66641bc8ae --- /dev/null +++ b/src/modules/imageresizer/ui/Properties/Resources.cs @@ -0,0 +1,75 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using ImageResizer.Helpers; + +namespace ImageResizer.Properties +{ + /// + /// Resource accessor class for compatibility with CLI code and tests. + /// Wraps ResourceLoader for resource string access. + /// + internal static class Resources + { + // Size names (used by tests and ResizeSize token replacement) + public static string Small => ResourceLoaderInstance.GetString("Small"); + + public static string Medium => ResourceLoaderInstance.GetString("Medium"); + + public static string Large => ResourceLoaderInstance.GetString("Large"); + + public static string Phone => ResourceLoaderInstance.GetString("Phone"); + + // Input page resources + public static string Input_Custom => ResourceLoaderInstance.GetString("Input_Custom"); + + // Validation messages + public static string ValueMustBeBetween => ResourceLoaderInstance.GetString("ValueMustBeBetween"); + + // CLI options + public static string CLI_Option_Destination => ResourceLoaderInstance.GetString("CLI_Option_Destination"); + + public static string CLI_Option_FileName => ResourceLoaderInstance.GetString("CLI_Option_FileName"); + + public static string CLI_Option_Files => ResourceLoaderInstance.GetString("CLI_Option_Files"); + + public static string CLI_Option_Fit => ResourceLoaderInstance.GetString("CLI_Option_Fit"); + + public static string CLI_Option_Height => ResourceLoaderInstance.GetString("CLI_Option_Height"); + + public static string CLI_Option_Help => ResourceLoaderInstance.GetString("CLI_Option_Help"); + + public static string CLI_Option_IgnoreOrientation => ResourceLoaderInstance.GetString("CLI_Option_IgnoreOrientation"); + + public static string CLI_Option_KeepDateModified => ResourceLoaderInstance.GetString("CLI_Option_KeepDateModified"); + + public static string CLI_Option_Quality => ResourceLoaderInstance.GetString("CLI_Option_Quality"); + + public static string CLI_Option_Replace => ResourceLoaderInstance.GetString("CLI_Option_Replace"); + + public static string CLI_Option_ShowConfig => ResourceLoaderInstance.GetString("CLI_Option_ShowConfig"); + + public static string CLI_Option_ShrinkOnly => ResourceLoaderInstance.GetString("CLI_Option_ShrinkOnly"); + + public static string CLI_Option_RemoveMetadata => ResourceLoaderInstance.GetString("CLI_Option_RemoveMetadata"); + + public static string CLI_Option_Size => ResourceLoaderInstance.GetString("CLI_Option_Size"); + + public static string CLI_Option_Unit => ResourceLoaderInstance.GetString("CLI_Option_Unit"); + + public static string CLI_Option_Width => ResourceLoaderInstance.GetString("CLI_Option_Width"); + + public static string CLI_ProcessingFiles => ResourceLoaderInstance.GetString("CLI_ProcessingFiles"); + + public static string CLI_ProgressFormat => ResourceLoaderInstance.GetString("CLI_ProgressFormat"); + + public static string CLI_CompletedWithErrors => ResourceLoaderInstance.GetString("CLI_CompletedWithErrors"); + + public static string CLI_AllFilesProcessed => ResourceLoaderInstance.GetString("CLI_AllFilesProcessed"); + + public static string CLI_WarningInvalidSizeIndex => ResourceLoaderInstance.GetString("CLI_WarningInvalidSizeIndex"); + + public static string CLI_NoInputFiles => ResourceLoaderInstance.GetString("CLI_NoInputFiles"); + } +} diff --git a/src/modules/imageresizer/ui/Properties/Settings.cs b/src/modules/imageresizer/ui/Properties/Settings.cs index a6b535a782..9084eaa93f 100644 --- a/src/modules/imageresizer/ui/Properties/Settings.cs +++ b/src/modules/imageresizer/ui/Properties/Settings.cs @@ -1,8 +1,8 @@ -#pragma warning disable IDE0073 +#pragma warning disable IDE0073, SA1636 // Copyright (c) Brice Lambson // The Brice Lambson licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 +#pragma warning restore IDE0073, SA1636 using System; using System.Collections; @@ -16,13 +16,10 @@ using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using System.Text.Json.Serialization.Metadata; -using System.Threading; -using System.Windows.Media.Imaging; - +using ImageResizer.Helpers; using ImageResizer.Models; -using ImageResizer.Services; -using ImageResizer.ViewModels; using ManagedCommon; +using Microsoft.UI.Dispatching; namespace ImageResizer.Properties { @@ -42,14 +39,21 @@ namespace ImageResizer.Properties private static readonly JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions { NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals, + PropertyNameCaseInsensitive = true, WriteIndented = true, TypeInfoResolver = new DefaultJsonTypeInfoResolver(), }; - private static readonly CompositeFormat ValueMustBeBetween = System.Text.CompositeFormat.Parse(Properties.Resources.ValueMustBeBetween); + // Cached UI thread DispatcherQueue for cross-thread property change notifications + private static DispatcherQueue _uiDispatcherQueue; - // Used to synchronize access to the settings.json file - private static Mutex _jsonMutex = new Mutex(); + private static CompositeFormat _valueMustBeBetween; + + private static CompositeFormat ValueMustBeBetween => + _valueMustBeBetween ??= System.Text.CompositeFormat.Parse(ResourceLoaderInstance.GetString("ValueMustBeBetween")); + + // Used to synchronize access to the settings.json file (in-process only) + private static readonly System.Threading.Lock _jsonSyncLock = new(); private static string _settingsPath = _fileSystem.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), "Microsoft", "PowerToys", "Image Resizer", "settings.json"); private string _fileNameFormat; private bool _shrinkOnly; @@ -74,8 +78,8 @@ namespace ImageResizer.Properties IgnoreOrientation = true; RemoveMetadata = false; JpegQualityLevel = 90; - PngInterlaceOption = System.Windows.Media.Imaging.PngInterlaceOption.Default; - TiffCompressOption = System.Windows.Media.Imaging.TiffCompressOption.Default; + PngInterlaceOption = Models.PngInterlaceOption.Default; + TiffCompressOption = Models.TiffCompressOption.Default; FileName = "%1 (%2)"; Sizes = new ObservableCollection { @@ -87,32 +91,25 @@ namespace ImageResizer.Properties KeepDateModified = false; FallbackEncoder = new System.Guid("19e4a5aa-5662-4fc5-a0c0-1758028e1057"); CustomSize = new CustomSize(ResizeFit.Fit, 1024, 640, ResizeUnit.Pixel); - AiSize = new AiSize(2); // Initialize with default scale of 2 + AiSize = new AiSize(2); AllSizes = new AllSizesCollection(this); } - /// - /// Validates the SelectedSizeIndex to ensure it's within the valid range. - /// This handles cross-device migration where settings saved on ARM64 with AI selected - /// are loaded on non-ARM64 devices. - /// private void ValidateSelectedSizeIndex() { - // Index structure: 0 to Sizes.Count-1 (regular), Sizes.Count (CustomSize), Sizes.Count+1 (AiSize) var maxIndex = ImageResizer.App.AiAvailabilityState == AiAvailabilityState.NotSupported - ? Sizes.Count // CustomSize only - : Sizes.Count + 1; // CustomSize + AiSize + ? Sizes.Count + : Sizes.Count + 1; if (_selectedSizeIndex > maxIndex) { - _selectedSizeIndex = 0; // Reset to first size + _selectedSizeIndex = 0; } } [JsonIgnore] public IEnumerable AllSizes { get; set; } - // Using OrdinalIgnoreCase since this is internal and used for comparison with symbols public string FileNameFormat => _fileNameFormat ?? (_fileNameFormat = FileName @@ -144,7 +141,6 @@ namespace ImageResizer.Properties } else { - // Fallback to CustomSize when index is out of range or AI is not available return CustomSize; } } @@ -168,13 +164,7 @@ namespace ImageResizer.Properties } } - string IDataErrorInfo.Error - { - get - { - return string.Empty; - } - } + string IDataErrorInfo.Error => string.Empty; string IDataErrorInfo.this[string columnName] { @@ -187,7 +177,6 @@ namespace ImageResizer.Properties if (JpegQualityLevel < 1 || JpegQualityLevel > 100) { - // Using CurrentCulture since this is user facing return string.Format(CultureInfo.CurrentCulture, ValueMustBeBetween, 1, 100); } @@ -217,26 +206,20 @@ namespace ImageResizer.Properties if (e.PropertyName == nameof(Models.CustomSize)) { _customSize = settings.CustomSize; - OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } else if (e.PropertyName == nameof(Models.AiSize)) { _aiSize = settings.AiSize; - OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } else if (e.PropertyName == nameof(Sizes)) { var oldSizes = _sizes; - oldSizes.CollectionChanged -= HandleCollectionChanged; ((INotifyPropertyChanged)oldSizes).PropertyChanged -= HandlePropertyChanged; - _sizes = settings.Sizes; - OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); - _sizes.CollectionChanged += HandleCollectionChanged; ((INotifyPropertyChanged)_sizes).PropertyChanged += HandlePropertyChanged; } @@ -244,7 +227,6 @@ namespace ImageResizer.Properties } public event NotifyCollectionChangedEventHandler CollectionChanged; - public event PropertyChangedEventHandler PropertyChanged; public int Count @@ -291,7 +273,6 @@ namespace ImageResizer.Properties private class AllSizesEnumerator : IEnumerator { private readonly AllSizesCollection _list; - private int _index = -1; public AllSizesEnumerator(AllSizesCollection list) @@ -334,9 +315,13 @@ namespace ImageResizer.Properties get => _selectedSizeIndex; set { + if (_selectedSizeIndex == value) + { + return; + } + _selectedSizeIndex = value; NotifyPropertyChanged(); - NotifyPropertyChanged(nameof(SelectedSize)); } } @@ -376,15 +361,6 @@ namespace ImageResizer.Properties } } - /// - /// Gets or sets a value indicating whether resizing images removes any metadata that doesn't affect rendering. - /// Default is false. - /// - /// - /// Preserved Metadata: - /// System.Photo.Orientation, - /// System.Image.ColorSpace - /// [JsonConverter(typeof(WrappedJsonValueConverter))] [JsonPropertyName("imageresizer_removeMetadata")] public bool RemoveMetadata @@ -505,6 +481,15 @@ namespace ImageResizer.Properties public static string SettingsPath { get => _settingsPath; set => _settingsPath = value; } + /// + /// Initializes the UI DispatcherQueue for cross-thread property change notifications. + /// Must be called from the UI thread during app startup. + /// + public static void InitializeDispatcher() + { + _uiDispatcherQueue = DispatcherQueue.GetForCurrentThread(); + } + public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "") @@ -514,16 +499,23 @@ namespace ImageResizer.Properties public void Save() { - _jsonMutex.WaitOne(); + lock (_jsonSyncLock) + { + SaveCore(); + } + } + + /// + /// Writes current settings to disk. Must be called under . + /// + private void SaveCore() + { string jsonData = JsonSerializer.Serialize(new SettingsWrapper() { Properties = this }, _jsonSerializerOptions); - // Create directory if it doesn't exist IFileInfo file = _fileSystem.FileInfo.New(SettingsPath); file.Directory.Create(); - // write string to file _fileSystem.File.WriteAllText(SettingsPath, jsonData); - _jsonMutex.ReleaseMutex(); } public void Reload() @@ -536,35 +528,46 @@ namespace ImageResizer.Properties _fileSystem.Directory.Move(oldSettingsDir, settingsDir); } - _jsonMutex.WaitOne(); - if (!_fileSystem.File.Exists(SettingsPath)) + // Read and deserialize under lock; ReloadCore runs outside the lock + // because jsonSettings is an in-memory snapshot with no file I/O. + Settings jsonSettings; + lock (_jsonSyncLock) { - _jsonMutex.ReleaseMutex(); - Save(); - return; + if (!_fileSystem.File.Exists(SettingsPath)) + { + SaveCore(); + return; + } + + string jsonData = _fileSystem.File.ReadAllText(SettingsPath); + jsonSettings = new Settings(); + try + { + jsonSettings = JsonSerializer.Deserialize(jsonData, _jsonSerializerOptions)?.Properties; + } + catch (JsonException ex) + { + Logger.LogError($"Failed to parse settings JSON, using defaults: {ex.Message}"); + } } - string jsonData = _fileSystem.File.ReadAllText(SettingsPath); - var jsonSettings = new Settings(); - try + // Apply deserialized snapshot to live properties on the UI thread. + if (_uiDispatcherQueue != null) { - jsonSettings = JsonSerializer.Deserialize(jsonData, _jsonSerializerOptions)?.Properties; - } - catch (JsonException) - { - } - - if (App.Current?.Dispatcher != null) - { - // Needs to be called on the App UI thread as the properties are bound to the UI. - App.Current.Dispatcher.Invoke(() => ReloadCore(jsonSettings)); + if (_uiDispatcherQueue.HasThreadAccess) + { + ReloadCore(jsonSettings); + } + else + { + _uiDispatcherQueue.TryEnqueue(() => ReloadCore(jsonSettings)); + } } else { + // No UI context (unit tests or CLI mode) — call directly. ReloadCore(jsonSettings); } - - _jsonMutex.ReleaseMutex(); } private void ReloadCore(Settings jsonSettings) @@ -580,20 +583,16 @@ namespace ImageResizer.Properties KeepDateModified = jsonSettings.KeepDateModified; FallbackEncoder = jsonSettings.FallbackEncoder; CustomSize = jsonSettings.CustomSize; - AiSize = jsonSettings.AiSize ?? new AiSize(InputViewModel.DefaultAiScale); + AiSize = jsonSettings.AiSize ?? new AiSize(2); SelectedSizeIndex = jsonSettings.SelectedSizeIndex; if (jsonSettings.Sizes.Count > 0) { Sizes.Clear(); Sizes.AddRange(jsonSettings.Sizes); - - // Ensure Ids are unique and handle missing Ids IdRecoveryHelper.RecoverInvalidIds(Sizes); } - // Validate SelectedSizeIndex after Sizes collection has been updated - // This handles cross-device migration (e.g., ARM64 -> non-ARM64) ValidateSelectedSizeIndex(); } } diff --git a/src/modules/imageresizer/ui/Properties/SettingsWrapper.cs b/src/modules/imageresizer/ui/Properties/SettingsWrapper.cs index 638860e3fc..1d5a587eff 100644 --- a/src/modules/imageresizer/ui/Properties/SettingsWrapper.cs +++ b/src/modules/imageresizer/ui/Properties/SettingsWrapper.cs @@ -1,8 +1,8 @@ -#pragma warning disable IDE0073 +#pragma warning disable IDE0073, SA1636 // Copyright (c) Brice Lambson // The Brice Lambson licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 +#pragma warning restore IDE0073, SA1636 using System.Text.Json.Serialization; diff --git a/src/modules/imageresizer/ui/Properties/WrappedJsonConverter`1.cs b/src/modules/imageresizer/ui/Properties/WrappedJsonConverter`1.cs index e6ce293d1e..bb60033904 100644 --- a/src/modules/imageresizer/ui/Properties/WrappedJsonConverter`1.cs +++ b/src/modules/imageresizer/ui/Properties/WrappedJsonConverter`1.cs @@ -1,8 +1,8 @@ -#pragma warning disable IDE0073 +#pragma warning disable IDE0073, SA1636 // Copyright (c) Brice Lambson // The Brice Lambson licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 +#pragma warning restore IDE0073, SA1636 using System; using System.Text.Json; diff --git a/src/modules/imageresizer/ui/Properties/WrappedJsonValueConverter.cs b/src/modules/imageresizer/ui/Properties/WrappedJsonValueConverter.cs index c21af6e926..8b2fd98243 100644 --- a/src/modules/imageresizer/ui/Properties/WrappedJsonValueConverter.cs +++ b/src/modules/imageresizer/ui/Properties/WrappedJsonValueConverter.cs @@ -1,8 +1,8 @@ -#pragma warning disable IDE0073 +#pragma warning disable IDE0073, SA1636 // Copyright (c) Brice Lambson // The Brice Lambson licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 +#pragma warning restore IDE0073, SA1636 using System; using System.Text.Json; diff --git a/src/modules/imageresizer/ui/Services/IAISuperResolutionService.cs b/src/modules/imageresizer/ui/Services/IAISuperResolutionService.cs index 3db073c5e5..a85f3fcc97 100644 --- a/src/modules/imageresizer/ui/Services/IAISuperResolutionService.cs +++ b/src/modules/imageresizer/ui/Services/IAISuperResolutionService.cs @@ -3,12 +3,12 @@ // See the LICENSE file in the project root for more information. using System; -using System.Windows.Media.Imaging; +using Windows.Graphics.Imaging; namespace ImageResizer.Services { public interface IAISuperResolutionService : IDisposable { - BitmapSource ApplySuperResolution(BitmapSource source, int scale, string filePath); + SoftwareBitmap ApplySuperResolution(SoftwareBitmap source, int scale, string filePath); } } diff --git a/src/modules/imageresizer/ui/Services/NoOpAiSuperResolutionService.cs b/src/modules/imageresizer/ui/Services/NoOpAiSuperResolutionService.cs index e59b5033ac..56c75c1b09 100644 --- a/src/modules/imageresizer/ui/Services/NoOpAiSuperResolutionService.cs +++ b/src/modules/imageresizer/ui/Services/NoOpAiSuperResolutionService.cs @@ -2,7 +2,7 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Windows.Media.Imaging; +using Windows.Graphics.Imaging; namespace ImageResizer.Services { @@ -14,7 +14,7 @@ namespace ImageResizer.Services { } - public BitmapSource ApplySuperResolution(BitmapSource source, int scale, string filePath) + public SoftwareBitmap ApplySuperResolution(SoftwareBitmap source, int scale, string filePath) { return source; } diff --git a/src/modules/imageresizer/ui/Services/WinAiSuperResolutionService.cs b/src/modules/imageresizer/ui/Services/WinAiSuperResolutionService.cs index 4cd752184a..d931fc375d 100644 --- a/src/modules/imageresizer/ui/Services/WinAiSuperResolutionService.cs +++ b/src/modules/imageresizer/ui/Services/WinAiSuperResolutionService.cs @@ -3,13 +3,8 @@ // See the LICENSE file in the project root for more information. using System; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.WindowsRuntime; -using System.Threading; using System.Threading.Tasks; -using System.Windows; -using System.Windows.Media; -using System.Windows.Media.Imaging; +using ManagedCommon; using Microsoft.Windows.AI; using Microsoft.Windows.AI.Imaging; using Windows.Graphics.Imaging; @@ -47,8 +42,9 @@ namespace ImageResizer.Services return new WinAiSuperResolutionService(imageScaler); } - catch + catch (Exception ex) { + Logger.LogError($"Failed to create AI super resolution service: {ex.Message}"); return null; } } @@ -59,39 +55,28 @@ namespace ImageResizer.Services { return ImageScaler.GetReadyState(); } - catch (Exception) + catch (Exception ex) { // If we can't get the state, treat it as disabled by user - // The caller should check if it's Ready or NotReady + Logger.LogWarning($"Failed to get AI model ready state: {ex.Message}"); return AIFeatureReadyState.DisabledByUser; } } - public static async Task EnsureModelReadyAsync(IProgress progress = null) + public static async Task EnsureModelReadyAsync() { try { - var operation = ImageScaler.EnsureReadyAsync(); - - // Register progress handler if provided - if (progress != null) - { - operation.Progress = (asyncInfo, progressValue) => - { - // progressValue is a double representing completion percentage (0.0 to 1.0 or 0 to 100) - progress.Report(progressValue); - }; - } - - return await operation; + return await ImageScaler.EnsureReadyAsync(); } - catch (Exception) + catch (Exception ex) { + Logger.LogError($"Failed to ensure AI model ready: {ex.Message}"); return null; } } - public BitmapSource ApplySuperResolution(BitmapSource source, int scale, string filePath) + public SoftwareBitmap ApplySuperResolution(SoftwareBitmap source, int scale, string filePath) { if (source == null || _disposed) { @@ -102,19 +87,12 @@ namespace ImageResizer.Services // Currently not used by the ImageScaler API try { - // Convert WPF BitmapSource to WinRT SoftwareBitmap - var softwareBitmap = ConvertBitmapSourceToSoftwareBitmap(source); - if (softwareBitmap == null) - { - return source; - } - // Calculate target dimensions - var newWidth = softwareBitmap.PixelWidth * scale; - var newHeight = softwareBitmap.PixelHeight * scale; + var newWidth = source.PixelWidth * scale; + var newHeight = source.PixelHeight * scale; // Apply super resolution with thread-safe access - // _usageLock protects concurrent access from Parallel.ForEach threads + // _usageLock protects concurrent access from Parallel.ForEachAsync threads SoftwareBitmap scaledBitmap; lock (_usageLock) { @@ -123,120 +101,19 @@ namespace ImageResizer.Services return source; } - scaledBitmap = _imageScaler.ScaleSoftwareBitmap(softwareBitmap, newWidth, newHeight); + scaledBitmap = _imageScaler.ScaleSoftwareBitmap(source, newWidth, newHeight); } - if (scaledBitmap == null) - { - return source; - } - - // Convert back to WPF BitmapSource - return ConvertSoftwareBitmapToBitmapSource(scaledBitmap); + return scaledBitmap ?? source; } - catch (Exception) + catch (Exception ex) { // Any error, return original image gracefully + Logger.LogError($"AI super resolution failed for {filePath}: {ex.Message}"); return source; } } - private static SoftwareBitmap ConvertBitmapSourceToSoftwareBitmap(BitmapSource bitmapSource) - { - try - { - // Ensure the bitmap is in a compatible format - var convertedBitmap = new FormatConvertedBitmap(); - convertedBitmap.BeginInit(); - convertedBitmap.Source = bitmapSource; - convertedBitmap.DestinationFormat = PixelFormats.Bgra32; - convertedBitmap.EndInit(); - - int width = convertedBitmap.PixelWidth; - int height = convertedBitmap.PixelHeight; - int stride = width * 4; // 4 bytes per pixel for Bgra32 - byte[] pixels = new byte[height * stride]; - - convertedBitmap.CopyPixels(pixels, stride, 0); - - // Create SoftwareBitmap from pixel data - var softwareBitmap = new SoftwareBitmap( - BitmapPixelFormat.Bgra8, - width, - height, - BitmapAlphaMode.Premultiplied); - - using (var buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write)) - using (var reference = buffer.CreateReference()) - { - unsafe - { - ((IMemoryBufferByteAccess)reference).GetBuffer(out byte* dataInBytes, out uint capacity); - System.Runtime.InteropServices.Marshal.Copy(pixels, 0, (IntPtr)dataInBytes, pixels.Length); - } - } - - return softwareBitmap; - } - catch (Exception) - { - return null; - } - } - - private static BitmapSource ConvertSoftwareBitmapToBitmapSource(SoftwareBitmap softwareBitmap) - { - try - { - // Convert to Bgra8 format if needed - var convertedBitmap = SoftwareBitmap.Convert( - softwareBitmap, - BitmapPixelFormat.Bgra8, - BitmapAlphaMode.Premultiplied); - - int width = convertedBitmap.PixelWidth; - int height = convertedBitmap.PixelHeight; - int stride = width * 4; // 4 bytes per pixel for Bgra8 - byte[] pixels = new byte[height * stride]; - - using (var buffer = convertedBitmap.LockBuffer(BitmapBufferAccessMode.Read)) - using (var reference = buffer.CreateReference()) - { - unsafe - { - ((IMemoryBufferByteAccess)reference).GetBuffer(out byte* dataInBytes, out uint capacity); - System.Runtime.InteropServices.Marshal.Copy((IntPtr)dataInBytes, pixels, 0, pixels.Length); - } - } - - // Create WPF BitmapSource from pixel data - var wpfBitmap = BitmapSource.Create( - width, - height, - 96, // DPI X - 96, // DPI Y - PixelFormats.Bgra32, - null, - pixels, - stride); - - wpfBitmap.Freeze(); // Make it thread-safe - return wpfBitmap; - } - catch (Exception) - { - return null; - } - } - - [ComImport] - [Guid("5B0D3235-4DBA-4D44-865E-8F1D0E4FD04D")] - [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] - private interface IMemoryBufferByteAccess - { - unsafe void GetBuffer(out byte* buffer, out uint capacity); - } - public void Dispose() { if (_disposed) diff --git a/src/modules/imageresizer/ui/Properties/Resources.resx b/src/modules/imageresizer/ui/Strings/en-us/Resources.resw similarity index 80% rename from src/modules/imageresizer/ui/Properties/Resources.resx rename to src/modules/imageresizer/ui/Strings/en-us/Resources.resw index a939ab1808..70cd9b331b 100644 --- a/src/modules/imageresizer/ui/Properties/Resources.resx +++ b/src/modules/imageresizer/ui/Strings/en-us/Resources.resw @@ -1,4 +1,4 @@ - + Processing {0} file(s)... @@ -367,8 +311,6 @@ No input files or pipe specified. Showing usage. - - ImageResizer - Current Configuration @@ -423,8 +365,6 @@ [Custom]* {0}x{1} {2} ({3}) - - ImageResizer - PowerToys Image Resizer CLI @@ -449,8 +389,6 @@ PowerToys.ImageResizerCLI.exe --size 0 -d "C:\Output" photo.png - - Set destination directory @@ -499,4 +437,10 @@ Set width + + Value must be between '{0}' and '{1}'. + + + Image Resizer + \ No newline at end of file diff --git a/src/modules/imageresizer/ui/Utilities/CodecHelper.cs b/src/modules/imageresizer/ui/Utilities/CodecHelper.cs new file mode 100644 index 0000000000..52e33306e2 --- /dev/null +++ b/src/modules/imageresizer/ui/Utilities/CodecHelper.cs @@ -0,0 +1,91 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.Linq; +using Windows.Graphics.Imaging; + +namespace ImageResizer.Utilities +{ + /// + /// Maps between legacy container format GUIDs (used in settings JSON) and WinRT encoder/decoder IDs, + /// and provides file extension lookups. + /// + internal static class CodecHelper + { + // Legacy container format GUID (stored in settings JSON) -> WinRT Encoder ID + private static readonly Dictionary LegacyGuidToEncoderId = new() + { + [new Guid("19e4a5aa-5662-4fc5-a0c0-1758028e1057")] = BitmapEncoder.JpegEncoderId, + [new Guid("1b7cfaf4-713f-473c-bbcd-6137425faeaf")] = BitmapEncoder.PngEncoderId, + [new Guid("0af1d87e-fcfe-4188-bdeb-a7906471cbe3")] = BitmapEncoder.BmpEncoderId, + [new Guid("163bcc30-e2e9-4f0b-961d-a3e9fdb788a3")] = BitmapEncoder.TiffEncoderId, + [new Guid("1f8a5601-7d4d-4cbd-9c82-1bc8d4eeb9a5")] = BitmapEncoder.GifEncoderId, + }; + + // WinRT Decoder ID -> WinRT Encoder ID + private static readonly Dictionary DecoderIdToEncoderId = new() + { + [BitmapDecoder.JpegDecoderId] = BitmapEncoder.JpegEncoderId, + [BitmapDecoder.PngDecoderId] = BitmapEncoder.PngEncoderId, + [BitmapDecoder.BmpDecoderId] = BitmapEncoder.BmpEncoderId, + [BitmapDecoder.TiffDecoderId] = BitmapEncoder.TiffEncoderId, + [BitmapDecoder.GifDecoderId] = BitmapEncoder.GifEncoderId, + [BitmapDecoder.JpegXRDecoderId] = BitmapEncoder.JpegXREncoderId, + }; + + // Encoder ID -> supported file extensions + private static readonly Dictionary EncoderExtensions = new() + { + [BitmapEncoder.JpegEncoderId] = [".jpg", ".jpeg", ".jpe", ".jfif"], + [BitmapEncoder.PngEncoderId] = [".png"], + [BitmapEncoder.BmpEncoderId] = [".bmp", ".dib", ".rle"], + [BitmapEncoder.TiffEncoderId] = [".tiff", ".tif"], + [BitmapEncoder.GifEncoderId] = [".gif"], + [BitmapEncoder.JpegXREncoderId] = [".jxr", ".wdp"], + }; + + /// + /// Gets the WinRT encoder ID that corresponds to the given legacy container format GUID. + /// Falls back to PNG if the GUID is not recognized. + /// + public static Guid GetEncoderIdFromLegacyGuid(Guid containerFormatGuid) + => LegacyGuidToEncoderId.TryGetValue(containerFormatGuid, out var id) + ? id + : BitmapEncoder.PngEncoderId; + + /// + /// Gets the WinRT encoder ID that matches the given decoder's codec. + /// Returns null if no matching encoder exists (e.g., ICO decoder has no encoder). + /// + public static Guid? GetEncoderIdForDecoder(BitmapDecoder decoder) + { + var codecId = decoder.DecoderInformation?.CodecId ?? Guid.Empty; + return DecoderIdToEncoderId.TryGetValue(codecId, out var encoderId) + ? encoderId + : null; + } + + /// + /// Returns the supported file extensions for the given encoder ID. + /// + public static string[] GetSupportedExtensions(Guid encoderId) + => EncoderExtensions.TryGetValue(encoderId, out var extensions) + ? extensions + : []; + + /// + /// Returns the default (first) file extension for the given encoder ID. + /// + public static string GetDefaultExtension(Guid encoderId) + => GetSupportedExtensions(encoderId).FirstOrDefault() ?? ".png"; + + /// + /// Checks whether the given encoder ID is a known, supported encoder. + /// + public static bool CanEncode(Guid encoderId) + => EncoderExtensions.ContainsKey(encoderId); + } +} diff --git a/src/modules/imageresizer/ui/Utilities/MathHelpers.cs b/src/modules/imageresizer/ui/Utilities/MathHelpers.cs deleted file mode 100644 index 22ddc37aca..0000000000 --- a/src/modules/imageresizer/ui/Utilities/MathHelpers.cs +++ /dev/null @@ -1,16 +0,0 @@ -#pragma warning disable IDE0073 -// Copyright (c) Brice Lambson -// The Brice Lambson licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 - -using System; - -namespace ImageResizer.Utilities -{ - internal static class MathHelpers - { - public static int Clamp(int value, int min, int max) - => Math.Min(Math.Max(value, min), max); - } -} diff --git a/src/modules/imageresizer/ui/Utilities/NativeMethods.cs b/src/modules/imageresizer/ui/Utilities/NativeMethods.cs deleted file mode 100644 index 98f5b0bf1e..0000000000 --- a/src/modules/imageresizer/ui/Utilities/NativeMethods.cs +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Runtime.InteropServices; - -[module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1636:FileHeaderCopyrightTextMustMatch", Justification = "File created under PowerToys.")] - -namespace ImageResizer.Utilities -{ - // Win32 functions required for temporary workaround for issue #1273 - internal class NativeMethods - { - [DllImport("user32.dll")] - internal static extern bool SetForegroundWindow(IntPtr hWnd); - - [DllImport("user32.dll", SetLastError = true)] - internal static extern bool SetProcessDPIAware(); - } -} diff --git a/src/modules/imageresizer/ui/ViewModels/AdvancedViewModel.cs b/src/modules/imageresizer/ui/ViewModels/AdvancedViewModel.cs deleted file mode 100644 index a4792e529e..0000000000 --- a/src/modules/imageresizer/ui/ViewModels/AdvancedViewModel.cs +++ /dev/null @@ -1,89 +0,0 @@ -#pragma warning disable IDE0073 -// Copyright (c) Brice Lambson -// The Brice Lambson licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 - -using System; -using System.Collections.Generic; -using System.Reflection; -using System.Windows.Input; -using System.Windows.Media.Imaging; - -using ImageResizer.Helpers; -using ImageResizer.Models; -using ImageResizer.Properties; - -namespace ImageResizer.ViewModels -{ - public class AdvancedViewModel : Observable - { - private static Dictionary InitEncoderMap() - { - var bmpCodec = new BmpBitmapEncoder().CodecInfo; - var gifCodec = new GifBitmapEncoder().CodecInfo; - var jpegCodec = new JpegBitmapEncoder().CodecInfo; - var pngCodec = new PngBitmapEncoder().CodecInfo; - var tiffCodec = new TiffBitmapEncoder().CodecInfo; - var wmpCodec = new WmpBitmapEncoder().CodecInfo; - - return new Dictionary - { - [bmpCodec.ContainerFormat] = bmpCodec.FriendlyName, - [gifCodec.ContainerFormat] = gifCodec.FriendlyName, - [jpegCodec.ContainerFormat] = jpegCodec.FriendlyName, - [pngCodec.ContainerFormat] = pngCodec.FriendlyName, - [tiffCodec.ContainerFormat] = tiffCodec.FriendlyName, - [wmpCodec.ContainerFormat] = wmpCodec.FriendlyName, - }; - } - - public AdvancedViewModel(Settings settings) - { - RemoveSizeCommand = new RelayCommand(RemoveSize); - AddSizeCommand = new RelayCommand(AddSize); - Settings = settings; - } - - public static IDictionary EncoderMap { get; } = InitEncoderMap(); - - public Settings Settings { get; } - - public static string Version - => typeof(AdvancedViewModel).Assembly.GetCustomAttribute() - ?.InformationalVersion; - - public static IEnumerable Encoders => EncoderMap.Keys; - - public ICommand RemoveSizeCommand { get; } - - public ICommand AddSizeCommand { get; } - - public void RemoveSize(ResizeSize size) - => Settings.Sizes.Remove(size); - - public void AddSize() - => Settings.Sizes.Add(new ResizeSize()); - - public void Close(bool accepted) - { - if (accepted) - { - Settings.Save(); - - return; - } - - var selectedSizeIndex = Settings.SelectedSizeIndex; - var shrinkOnly = Settings.ShrinkOnly; - var replace = Settings.Replace; - var ignoreOrientation = Settings.IgnoreOrientation; - - Settings.Reload(); - Settings.SelectedSizeIndex = selectedSizeIndex; - Settings.ShrinkOnly = shrinkOnly; - Settings.Replace = replace; - Settings.IgnoreOrientation = ignoreOrientation; - } - } -} diff --git a/src/modules/imageresizer/ui/ViewModels/ITabViewModel.cs b/src/modules/imageresizer/ui/ViewModels/ITabViewModel.cs deleted file mode 100644 index 62ef93beab..0000000000 --- a/src/modules/imageresizer/ui/ViewModels/ITabViewModel.cs +++ /dev/null @@ -1,13 +0,0 @@ -#pragma warning disable IDE0073 -// Copyright (c) Brice Lambson -// The Brice Lambson licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 - -namespace ImageResizer.ViewModels -{ - public interface ITabViewModel - { - string Header { get; } - } -} diff --git a/src/modules/imageresizer/ui/ViewModels/InputViewModel.cs b/src/modules/imageresizer/ui/ViewModels/InputViewModel.cs index c241728276..09e021fb87 100644 --- a/src/modules/imageresizer/ui/ViewModels/InputViewModel.cs +++ b/src/modules/imageresizer/ui/ViewModels/InputViewModel.cs @@ -1,8 +1,8 @@ -#pragma warning disable IDE0073 +#pragma warning disable IDE0073, SA1636 // Copyright (c) Brice Lambson // The Brice Lambson licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 +#pragma warning restore IDE0073, SA1636 using System; using System.Collections.Generic; @@ -10,11 +10,12 @@ using System.ComponentModel; using System.Globalization; using System.IO; using System.Linq; -using System.Runtime.InteropServices; using System.Threading.Tasks; -using System.Windows.Input; -using System.Windows.Media.Imaging; +using Windows.Graphics.Imaging; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; using Common.UI; +using ManagedCommon; using ImageResizer.Helpers; using ImageResizer.Models; using ImageResizer.Properties; @@ -23,25 +24,12 @@ using ImageResizer.Views; namespace ImageResizer.ViewModels { - public class InputViewModel : Observable + public partial class InputViewModel : ObservableObject { public const int DefaultAiScale = 2; private const int MinAiScale = 1; private const int MaxAiScale = 8; - private readonly ResizeBatch _batch; - private readonly MainViewModel _mainViewModel; - private readonly IMainView _mainView; - private readonly bool _hasMultipleFiles; - private bool _originalDimensionsLoaded; - private int? _originalWidth; - private int? _originalHeight; - private string _currentResolutionDescription; - private string _newResolutionDescription; - private bool _isDownloadingModel; - private string _modelStatusMessage; - private double _modelDownloadProgress; - public enum Dimension { Width, @@ -55,6 +43,27 @@ namespace ImageResizer.ViewModels public Dimension Dimension { get; set; } } + private readonly ResizeBatch _batch; + private readonly MainViewModel _mainViewModel; + private readonly IMainView _mainView; + private readonly bool _hasGifFiles; + private readonly bool _hasMultipleFiles; + private bool _originalDimensionsLoaded; + private int? _originalWidth; + private int? _originalHeight; + + [ObservableProperty] + private string _currentResolutionDescription; + + [ObservableProperty] + private string _newResolutionDescription; + + [ObservableProperty] + private bool _isDownloadingModel; + + [ObservableProperty] + private string _modelStatusMessage; + public InputViewModel( Settings settings, MainViewModel mainViewModel, @@ -64,6 +73,7 @@ namespace ImageResizer.ViewModels _batch = batch; _mainViewModel = mainViewModel; _mainView = mainView; + _hasGifFiles = _batch?.Files.Any(filename => filename.EndsWith(".gif", StringComparison.OrdinalIgnoreCase)) == true; _hasMultipleFiles = _batch?.Files.Count > 1; Settings = settings; @@ -80,13 +90,6 @@ namespace ImageResizer.ViewModels settings.PropertyChanged += HandleSettingsPropertyChanged; } - ResizeCommand = new RelayCommand(Resize, () => CanResize); - CancelCommand = new RelayCommand(Cancel); - OpenSettingsCommand = new RelayCommand(OpenSettings); - EnterKeyPressedCommand = new RelayCommand(HandleEnterKeyPress); - DownloadModelCommand = new RelayCommand(async () => await DownloadModelAsync()); - - // Initialize AI UI state based on Settings availability InitializeAiState(); } @@ -111,94 +114,44 @@ namespace ImageResizer.ViewModels public string AiScaleDisplay => Settings?.AiSize?.ScaleDisplay ?? string.Empty; - public string CurrentResolutionDescription - { - get => _currentResolutionDescription; - private set => Set(ref _currentResolutionDescription, value); - } + public bool IsCustomSizeSelected => Settings?.SelectedSize is CustomSize; - public string NewResolutionDescription - { - get => _newResolutionDescription; - private set => Set(ref _newResolutionDescription, value); - } - - // ==================== UI State Properties ==================== - - // Show AI size descriptions only when AI size is selected and not multiple files public bool ShowAiSizeDescriptions => Settings?.SelectedSize is AiSize && !_hasMultipleFiles; - // Helper property: Is model currently being downloaded? - public bool IsModelDownloading => _isDownloadingModel; - - public string ModelStatusMessage - { - get => _modelStatusMessage; - private set => Set(ref _modelStatusMessage, value); - } - - public double ModelDownloadProgress - { - get => _modelDownloadProgress; - private set => Set(ref _modelDownloadProgress, value); - } - - // Show download prompt when: AI size is selected and model is not ready (including downloading) public bool ShowModelDownloadPrompt => Settings?.SelectedSize is AiSize && - (App.AiAvailabilityState == Properties.AiAvailabilityState.ModelNotReady || _isDownloadingModel); + (App.AiAvailabilityState == AiAvailabilityState.ModelNotReady || IsDownloadingModel); - // Show AI controls when: AI size is selected and AI is ready public bool ShowAiControls => Settings?.SelectedSize is AiSize && - App.AiAvailabilityState == Properties.AiAvailabilityState.Ready; + App.AiAvailabilityState == AiAvailabilityState.Ready; + + public bool ShowAiConfigurationSection => ShowModelDownloadPrompt || ShowAiControls; - /// - /// Gets a value indicating whether the resize operation can proceed. - /// For AI resize: only enabled when AI is fully ready. - /// For non-AI resize: always enabled. - /// public bool CanResize { get { - // If AI size is selected, only allow resize when AI is fully ready if (Settings?.SelectedSize is AiSize) { - return App.AiAvailabilityState == Properties.AiAvailabilityState.Ready; + return App.AiAvailabilityState == AiAvailabilityState.Ready; } - // Non-AI resize can always proceed return true; } } - public ICommand ResizeCommand { get; } - - public ICommand CancelCommand { get; } - - public ICommand OpenSettingsCommand { get; } - - public ICommand EnterKeyPressedCommand { get; private set; } - - public ICommand DownloadModelCommand { get; private set; } - - // Any of the files is a gif - public bool TryingToResizeGifFiles => - _batch?.Files.Any(filename => filename.EndsWith(".gif", System.StringComparison.InvariantCultureIgnoreCase)) == true; + public bool HasGifFiles => _hasGifFiles; + [RelayCommand(CanExecute = nameof(CanResize))] public void Resize() { Settings.Save(); _mainViewModel.CurrentPage = new ProgressViewModel(_batch, _mainViewModel, _mainView); } - public static void OpenSettings() - { - SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.ImageResizer); - } - - private void HandleEnterKeyPress(KeyPressParams parameters) + [RelayCommand] + private void EnterKeyPressed(KeyPressParams parameters) { switch (parameters.Dimension) { @@ -211,25 +164,68 @@ namespace ImageResizer.ViewModels } } + [RelayCommand] public void Cancel() => _mainView.Close(); + [RelayCommand] + public static void OpenSettings() + { + SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.ImageResizer); + } + + [RelayCommand] + public async Task DownloadModelAsync() + { + try + { + IsDownloadingModel = true; + ModelStatusMessage = ResourceLoaderInstance.GetString("Input_AiModelDownloading"); + NotifyAiStateChanged(); + + var result = await WinAiSuperResolutionService.EnsureModelReadyAsync(); + + if (result?.Status == Microsoft.Windows.AI.AIFeatureReadyResultState.Success) + { + App.AiAvailabilityState = AiAvailabilityState.Ready; + UpdateStatusMessage(); + + var aiService = await WinAiSuperResolutionService.CreateAsync(); + if (aiService != null) + { + ResizeBatch.SetAiSuperResolutionService(aiService); + } + else + { + ResizeBatch.SetAiSuperResolutionService(NoOpAiSuperResolutionService.Instance); + } + } + else + { + ModelStatusMessage = ResourceLoaderInstance.GetString("Input_AiModelDownloadFailed"); + } + } + catch (Exception ex) + { + Logger.LogError($"AI model download failed: {ex.Message}"); + ModelStatusMessage = ResourceLoaderInstance.GetString("Input_AiModelDownloadFailed"); + } + finally + { + IsDownloadingModel = false; + NotifyAiStateChanged(); + } + } + private void HandleSettingsPropertyChanged(object sender, PropertyChangedEventArgs e) { switch (e.PropertyName) { case nameof(Settings.SelectedSizeIndex): - case nameof(Settings.SelectedSize): - // Notify UI state properties that depend on SelectedSize + OnPropertyChanged(nameof(IsCustomSizeSelected)); NotifyAiStateChanged(); UpdateAiDetails(); - - // Trigger CanExecuteChanged for ResizeCommand - if (ResizeCommand is RelayCommand cmd) - { - cmd.OnCanExecuteChanged(); - } - + ResizeCommand.NotifyCanExecuteChanged(); break; } } @@ -238,51 +234,54 @@ namespace ImageResizer.ViewModels { if (Settings?.AiSize != null) { - Settings.AiSize.Scale = Math.Clamp( - Settings.AiSize.Scale, - MinAiScale, - MaxAiScale); + Settings.AiSize.Scale = Math.Clamp(Settings.AiSize.Scale, MinAiScale, MaxAiScale); } } - private void UpdateAiDetails() + private async void UpdateAiDetails() { - // Clear AI details if AI size not selected - if (Settings == null || Settings.SelectedSize is not AiSize) + try { - CurrentResolutionDescription = string.Empty; - NewResolutionDescription = string.Empty; - return; + if (Settings == null || Settings.SelectedSize is not AiSize) + { + CurrentResolutionDescription = string.Empty; + NewResolutionDescription = string.Empty; + return; + } + + EnsureAiScaleWithinRange(); + + if (_hasMultipleFiles) + { + CurrentResolutionDescription = string.Empty; + NewResolutionDescription = string.Empty; + return; + } + + await EnsureOriginalDimensionsLoadedAsync(); + + var hasConcreteSize = _originalWidth.HasValue && _originalHeight.HasValue; + CurrentResolutionDescription = hasConcreteSize + ? FormatDimensions(_originalWidth!.Value, _originalHeight!.Value) + : ResourceLoaderInstance.GetString("Input_AiUnknownSize"); + + var scale = Settings.AiSize.Scale; + NewResolutionDescription = hasConcreteSize + ? FormatDimensions((long)_originalWidth!.Value * scale, (long)_originalHeight!.Value * scale) + : ResourceLoaderInstance.GetString("Input_AiUnknownSize"); } - - EnsureAiScaleWithinRange(); - - if (_hasMultipleFiles) + catch (Exception ex) { - CurrentResolutionDescription = string.Empty; - NewResolutionDescription = string.Empty; - return; + Logger.LogError($"UpdateAiDetails failed: {ex.Message}"); } - - EnsureOriginalDimensionsLoaded(); - - var hasConcreteSize = _originalWidth.HasValue && _originalHeight.HasValue; - CurrentResolutionDescription = hasConcreteSize - ? FormatDimensions(_originalWidth!.Value, _originalHeight!.Value) - : Resources.Input_AiUnknownSize; - - var scale = Settings.AiSize.Scale; - NewResolutionDescription = hasConcreteSize - ? FormatDimensions((long)_originalWidth!.Value * scale, (long)_originalHeight!.Value * scale) - : Resources.Input_AiUnknownSize; } private static string FormatDimensions(long width, long height) { - return string.Format(CultureInfo.CurrentCulture, "{0} × {1}", width, height); + return string.Format(CultureInfo.CurrentCulture, "{0} x {1}", width, height); } - private void EnsureOriginalDimensionsLoaded() + private async Task EnsureOriginalDimensionsLoadedAsync() { if (_originalDimensionsLoaded) { @@ -298,18 +297,15 @@ namespace ImageResizer.ViewModels try { - using var stream = File.OpenRead(file); - var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None); - var frame = decoder.Frames.FirstOrDefault(); - if (frame != null) - { - _originalWidth = frame.PixelWidth; - _originalHeight = frame.PixelHeight; - } + using var fileStream = File.OpenRead(file); + using var stream = fileStream.AsRandomAccessStream(); + var decoder = await BitmapDecoder.CreateAsync(stream); + _originalWidth = (int)decoder.PixelWidth; + _originalHeight = (int)decoder.PixelHeight; } - catch (Exception) + catch (Exception ex) { - // Failed to load image dimensions - clear values + Logger.LogWarning($"Failed to read image dimensions for {file}: {ex.Message}"); _originalWidth = null; _originalHeight = null; } @@ -319,128 +315,44 @@ namespace ImageResizer.ViewModels } } - /// - /// Initializes AI UI state based on App's cached availability state. - /// Subscribe to state change event to update UI when background initialization completes. - /// private void InitializeAiState() { - // Subscribe to initialization completion event to refresh UI App.AiInitializationCompleted += OnAiInitializationCompleted; - - // Set initial status message based on current state UpdateStatusMessage(); } - /// - /// Handles AI initialization completion event from App. - /// Refreshes UI when background initialization finishes. - /// - private void OnAiInitializationCompleted(object sender, Properties.AiAvailabilityState finalState) + private void OnAiInitializationCompleted(object sender, AiAvailabilityState finalState) { UpdateStatusMessage(); NotifyAiStateChanged(); } - /// - /// Updates status message based on current App availability state. - /// private void UpdateStatusMessage() { ModelStatusMessage = App.AiAvailabilityState switch { - Properties.AiAvailabilityState.Ready => string.Empty, - Properties.AiAvailabilityState.ModelNotReady => Resources.Input_AiModelNotAvailable, - Properties.AiAvailabilityState.NotSupported => Resources.Input_AiModelNotSupported, + AiAvailabilityState.Ready => string.Empty, + AiAvailabilityState.ModelNotReady => ResourceLoaderInstance.GetString("Input_AiModelNotAvailable"), + AiAvailabilityState.NotSupported => ResourceLoaderInstance.GetString("Input_AiModelNotSupported"), _ => string.Empty, }; } - /// - /// Notifies UI when AI state changes (model availability, download status). - /// private void NotifyAiStateChanged() { - OnPropertyChanged(nameof(IsModelDownloading)); + OnPropertyChanged(nameof(IsDownloadingModel)); OnPropertyChanged(nameof(ShowModelDownloadPrompt)); OnPropertyChanged(nameof(ShowAiControls)); + OnPropertyChanged(nameof(ShowAiConfigurationSection)); OnPropertyChanged(nameof(ShowAiSizeDescriptions)); OnPropertyChanged(nameof(CanResize)); - - // Trigger CanExecuteChanged for ResizeCommand - if (ResizeCommand is RelayCommand resizeCommand) - { - resizeCommand.OnCanExecuteChanged(); - } } - /// - /// Notifies UI when AI scale changes (slider value). - /// private void NotifyAiScaleChanged() { OnPropertyChanged(nameof(AiSuperResolutionScale)); OnPropertyChanged(nameof(AiScaleDisplay)); UpdateAiDetails(); } - - private async Task DownloadModelAsync() - { - try - { - // Set downloading flag and show progress - _isDownloadingModel = true; - ModelStatusMessage = Resources.Input_AiModelDownloading; - ModelDownloadProgress = 0; - NotifyAiStateChanged(); - - // Create progress reporter to update UI - var progress = new Progress(value => - { - // progressValue could be 0-1 or 0-100, normalize to 0-100 - ModelDownloadProgress = value > 1 ? value : value * 100; - }); - - // Call EnsureReadyAsync to download and prepare the AI model - var result = await WinAiSuperResolutionService.EnsureModelReadyAsync(progress); - - if (result?.Status == Microsoft.Windows.AI.AIFeatureReadyResultState.Success) - { - // Model successfully downloaded and ready - ModelDownloadProgress = 100; - - // Update App's cached state - App.AiAvailabilityState = Properties.AiAvailabilityState.Ready; - UpdateStatusMessage(); - - // Initialize the AI service now that model is ready - var aiService = await WinAiSuperResolutionService.CreateAsync(); - ResizeBatch.SetAiSuperResolutionService(aiService ?? (Services.IAISuperResolutionService)NoOpAiSuperResolutionService.Instance); - } - else - { - // Download failed - ModelStatusMessage = Resources.Input_AiModelDownloadFailed; - } - } - catch (Exception) - { - // Exception during download - ModelStatusMessage = Resources.Input_AiModelDownloadFailed; - } - finally - { - // Clear downloading flag - _isDownloadingModel = false; - - // Reset progress if not successful - if (App.AiAvailabilityState != Properties.AiAvailabilityState.Ready) - { - ModelDownloadProgress = 0; - } - - NotifyAiStateChanged(); - } - } } } diff --git a/src/modules/imageresizer/ui/ViewModels/MainViewModel.cs b/src/modules/imageresizer/ui/ViewModels/MainViewModel.cs index d92753edba..9532c89cc9 100644 --- a/src/modules/imageresizer/ui/ViewModels/MainViewModel.cs +++ b/src/modules/imageresizer/ui/ViewModels/MainViewModel.cs @@ -1,53 +1,41 @@ -#pragma warning disable IDE0073 +#pragma warning disable IDE0073, SA1636 // Copyright (c) Brice Lambson // The Brice Lambson licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 +#pragma warning restore IDE0073, SA1636 -using System.Collections.Generic; -using System.Windows.Input; - -using ImageResizer.Helpers; +using System.Threading.Tasks; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; using ImageResizer.Models; using ImageResizer.Properties; using ImageResizer.Views; namespace ImageResizer.ViewModels { - public class MainViewModel : Observable + public partial class MainViewModel : ObservableObject { private readonly Settings _settings; private readonly ResizeBatch _batch; + [ObservableProperty] private object _currentPage; - private double _progress; public MainViewModel(ResizeBatch batch, Settings settings) { _batch = batch; _settings = settings; - LoadCommand = new RelayCommand(Load); } - public ICommand LoadCommand { get; } - - public object CurrentPage - { - get => _currentPage; - set => Set(ref _currentPage, value); - } - - public double Progress - { - get => _progress; - set => Set(ref _progress, value); - } - - public void Load(IMainView view) + [RelayCommand] + public async Task LoadAsync(IMainView view) { if (_batch.Files.Count == 0) { - _batch.Files.AddRange(view.OpenPictureFiles()); + foreach (var file in await view.OpenPictureFilesAsync()) + { + _batch.Files.Add(file); + } } CurrentPage = new InputViewModel(_settings, this, view, _batch); diff --git a/src/modules/imageresizer/ui/ViewModels/ProgressViewModel.cs b/src/modules/imageresizer/ui/ViewModels/ProgressViewModel.cs index 095c3a795e..fc95b76b5a 100644 --- a/src/modules/imageresizer/ui/ViewModels/ProgressViewModel.cs +++ b/src/modules/imageresizer/ui/ViewModels/ProgressViewModel.cs @@ -1,33 +1,59 @@ -#pragma warning disable IDE0073 +#pragma warning disable IDE0073, SA1636 // Copyright (c) Brice Lambson // The Brice Lambson licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 - +#pragma warning restore IDE0073, SA1636 using System; using System.Diagnostics; +using System.Globalization; using System.Linq; +using System.Text; using System.Threading; using System.Threading.Tasks; -using System.Windows.Input; - +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; using ImageResizer.Helpers; using ImageResizer.Models; using ImageResizer.Views; +using Microsoft.UI.Dispatching; namespace ImageResizer.ViewModels { - public class ProgressViewModel : Observable, IDisposable + public partial class ProgressViewModel : ObservableObject, IDisposable { private readonly MainViewModel _mainViewModel; private readonly ResizeBatch _batch; private readonly IMainView _mainView; - private readonly Stopwatch _stopwatch = new Stopwatch(); - private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); + private readonly Stopwatch _stopwatch = new(); + private readonly CancellationTokenSource _cancellationTokenSource = new(); + private readonly DispatcherQueue _dispatcherQueue; + private bool _disposedValue; + + [ObservableProperty] private double _progress; + + [ObservableProperty] + [NotifyPropertyChangedFor(nameof(TimeRemainingDisplay))] private TimeSpan _timeRemaining; - private bool disposedValue; + + private static CompositeFormat _progressTimeRemainingFormat; + + private static CompositeFormat ProgressTimeRemainingFormat => + _progressTimeRemainingFormat ??= CompositeFormat.Parse(ResourceLoaderInstance.GetString("Progress_TimeRemaining")); + + public string TimeRemainingDisplay + { + get + { + if (TimeRemaining == TimeSpan.MaxValue || TimeRemaining.TotalSeconds < 1) + { + return string.Empty; + } + + return string.Format(CultureInfo.CurrentCulture, ProgressTimeRemainingFormat, TimeRemaining); + } + } public ProgressViewModel( ResizeBatch batch, @@ -37,57 +63,49 @@ namespace ImageResizer.ViewModels _batch = batch; _mainViewModel = mainViewModel; _mainView = mainView; - - StartCommand = new RelayCommand(Start); - StopCommand = new RelayCommand(Stop); + _dispatcherQueue = DispatcherQueue.GetForCurrentThread(); } - public double Progress + [RelayCommand] + public async Task StartAsync() { - get => _progress; - set => Set(ref _progress, value); - } + try + { + _stopwatch.Restart(); + var errors = await _batch.ProcessAsync( + (completed, total) => + { + var progress = completed / total; + var timeRemaining = _stopwatch.Elapsed.Multiply((total - completed) / completed); - public TimeSpan TimeRemaining - { - get => _timeRemaining; - set => Set(ref _timeRemaining, value); - } + // Progress callback runs on thread-pool threads (from Parallel.ForEachAsync), + // so we must dispatch UI property updates to the UI thread. + _dispatcherQueue.TryEnqueue(() => + { + Progress = progress; + TimeRemaining = timeRemaining; + }); + }, + _cancellationTokenSource.Token); - public ICommand StartCommand { get; } - - public ICommand StopCommand { get; } - - public void Start() - { - _ = Task.Factory.StartNew(StartExecutingWork, _cancellationTokenSource.Token, TaskCreationOptions.None, TaskScheduler.Current); - } - - private void StartExecutingWork() - { - _stopwatch.Restart(); - var errors = _batch.Process( - (completed, total) => + // After await we are back on the UI thread (SynchronizationContext), + // so we can update UI directly without DispatcherQueue. + if (errors.Any()) { - var progress = completed / total; - Progress = progress; - _mainViewModel.Progress = progress; - - TimeRemaining = _stopwatch.Elapsed.Multiply((total - completed) / completed); - }, - _cancellationTokenSource.Token); - - if (errors.Any()) - { - _mainViewModel.Progress = 0; - _mainViewModel.CurrentPage = new ResultsViewModel(_mainView, errors); + _mainViewModel.CurrentPage = new ResultsViewModel(_mainView, errors); + } + else + { + _mainView.Close(); + } } - else + catch (OperationCanceledException) { - _mainView.Close(); + // User cancelled via Stop — window is already closing. } } + [RelayCommand] public void Stop() { _cancellationTokenSource.Cancel(); @@ -96,20 +114,19 @@ namespace ImageResizer.ViewModels protected virtual void Dispose(bool disposing) { - if (!disposedValue) + if (!_disposedValue) { if (disposing) { _cancellationTokenSource.Dispose(); } - disposedValue = true; + _disposedValue = true; } } public void Dispose() { - // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method Dispose(disposing: true); GC.SuppressFinalize(this); } diff --git a/src/modules/imageresizer/ui/ViewModels/ResultsViewModel.cs b/src/modules/imageresizer/ui/ViewModels/ResultsViewModel.cs index 854981479d..782b4f860f 100644 --- a/src/modules/imageresizer/ui/ViewModels/ResultsViewModel.cs +++ b/src/modules/imageresizer/ui/ViewModels/ResultsViewModel.cs @@ -1,19 +1,17 @@ -#pragma warning disable IDE0073 +#pragma warning disable IDE0073, SA1636 // Copyright (c) Brice Lambson // The Brice Lambson licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 - +#pragma warning restore IDE0073, SA1636 using System.Collections.Generic; -using System.Windows.Input; - -using ImageResizer.Helpers; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; using ImageResizer.Models; using ImageResizer.Views; namespace ImageResizer.ViewModels { - public class ResultsViewModel : Observable + public partial class ResultsViewModel : ObservableObject { private readonly IMainView _mainView; @@ -21,13 +19,11 @@ namespace ImageResizer.ViewModels { _mainView = mainView; Errors = errors; - CloseCommand = new RelayCommand(Close); } public IEnumerable Errors { get; } - public ICommand CloseCommand { get; } - + [RelayCommand] public void Close() => _mainView.Close(); } } diff --git a/src/modules/imageresizer/ui/Views/AccessTextToTextConverter.cs b/src/modules/imageresizer/ui/Views/AccessTextToTextConverter.cs deleted file mode 100644 index 36f7906b62..0000000000 --- a/src/modules/imageresizer/ui/Views/AccessTextToTextConverter.cs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Globalization; -using System.Windows.Data; - -namespace ImageResizer.Views -{ - public class AccessTextToTextConverter : IValueConverter - { - public object Convert(object value, Type targetType, object parameter, CultureInfo culture) - { - return ((string)value).Replace("_", string.Empty); - } - - public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) - { - throw new NotImplementedException(); - } - } -} diff --git a/src/modules/imageresizer/ui/Views/AutoDoubleConverter.cs b/src/modules/imageresizer/ui/Views/AutoDoubleConverter.cs deleted file mode 100644 index 631416a7c0..0000000000 --- a/src/modules/imageresizer/ui/Views/AutoDoubleConverter.cs +++ /dev/null @@ -1,64 +0,0 @@ -#pragma warning disable IDE0073 -// Copyright (c) Brice Lambson -// The Brice Lambson licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 - -using System; -using System.Globalization; -using System.Windows.Data; - -using ImageResizer.Properties; - -namespace ImageResizer.Views; - -/// -/// Converts between double and string for text-based controls bound to Width or Height fields. -/// Optionally returns localized "Auto" text when the underlying value is 0, letting the UI show, -/// for example "(auto) x 1024 pixels". -/// -[ValueConversion(typeof(double), typeof(string))] -internal class AutoDoubleConverter : IValueConverter -{ - /// - /// Converts a double to a string, optionally showing "Auto" for 0 values. NaN values are - /// converted to empty strings. - /// - /// The value to convert from to - /// . - /// The conversion target type. here. - /// Set to "Auto" to return the localized "Auto" string if the - /// value is 0. - /// The to use for the number formatting. - /// - /// The string representation of the passed-in value. - public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => - value switch - { - double d => d switch - { - double.NaN => "0", - 0 => (string)parameter == "Auto" ? Resources.Input_Auto : "0", - _ => d.ToString(culture), - }, - - _ => "0", - }; - - /// - /// Converts the string representation back to a double, returning 0 if the string is empty, - /// null or not a valid number in the specified culture. - /// - /// The string value to convert. - /// The conversion target type. here. - /// Converter parameter. Unused. - /// The to use for the text parsing. - /// The corresponding double value. - public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => - value switch - { - null or "" => 0, - string text when double.TryParse(text, NumberStyles.Any, culture, out double result) => result, - _ => 0, - }; -} diff --git a/src/modules/imageresizer/ui/Views/BoolValueConverter.cs b/src/modules/imageresizer/ui/Views/BoolValueConverter.cs deleted file mode 100644 index f4e83fab0d..0000000000 --- a/src/modules/imageresizer/ui/Views/BoolValueConverter.cs +++ /dev/null @@ -1,33 +0,0 @@ -#pragma warning disable IDE0073 -// Copyright (c) Brice Lambson -// The Brice Lambson licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 - -using System; -using System.Globalization; -using System.Windows; -using System.Windows.Data; - -namespace ImageResizer.Views -{ - [ValueConversion(typeof(bool), typeof(Visibility))] - internal class BoolValueConverter : IValueConverter - { - public object Convert(object value, Type targetType, object parameter, CultureInfo culture) - { - bool boolValue = (bool)value; - bool invert = parameter is string param && param.Equals("Inverted", StringComparison.OrdinalIgnoreCase); - - if (invert) - { - boolValue = !boolValue; - } - - return boolValue ? Visibility.Visible : Visibility.Collapsed; - } - - public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) - => (Visibility)value == Visibility.Visible; - } -} diff --git a/src/modules/imageresizer/ui/Views/EnumToIntConverter.cs b/src/modules/imageresizer/ui/Views/EnumToIntConverter.cs deleted file mode 100644 index a48030c2cc..0000000000 --- a/src/modules/imageresizer/ui/Views/EnumToIntConverter.cs +++ /dev/null @@ -1,23 +0,0 @@ -#pragma warning disable IDE0073 -// Copyright (c) Brice Lambson -// The Brice Lambson licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 - -using System; -using System.Globalization; -using System.Windows; -using System.Windows.Data; - -namespace ImageResizer.Views -{ - [ValueConversion(typeof(Enum), typeof(int))] - internal class EnumToIntConverter : IValueConverter - { - public object Convert(object value, Type targetType, object parameter, CultureInfo culture) - => (int)value; - - public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) - => targetType.GetEnumValues().GetValue((int)value); - } -} diff --git a/src/modules/imageresizer/ui/Views/IMainView.cs b/src/modules/imageresizer/ui/Views/IMainView.cs deleted file mode 100644 index cfbfd014ad..0000000000 --- a/src/modules/imageresizer/ui/Views/IMainView.cs +++ /dev/null @@ -1,17 +0,0 @@ -#pragma warning disable IDE0073 -// Copyright (c) Brice Lambson -// The Brice Lambson licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 - -using System.Collections.Generic; - -namespace ImageResizer.Views -{ - public interface IMainView - { - void Close(); - - IEnumerable OpenPictureFiles(); - } -} diff --git a/src/modules/imageresizer/ui/Views/InputPage.xaml b/src/modules/imageresizer/ui/Views/InputPage.xaml deleted file mode 100644 index b45b2a66bd..0000000000 --- a/src/modules/imageresizer/ui/Views/InputPage.xaml +++ /dev/null @@ -1,408 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/modules/imageresizer/ui/Views/InputPage.xaml.cs b/src/modules/imageresizer/ui/Views/InputPage.xaml.cs deleted file mode 100644 index b7dec222d5..0000000000 --- a/src/modules/imageresizer/ui/Views/InputPage.xaml.cs +++ /dev/null @@ -1,69 +0,0 @@ -#pragma warning disable IDE0073 -// Copyright (c) Brice Lambson -// The Brice Lambson licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. -// Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 - -using System.Windows.Controls; -using System.Windows.Input; - -using ImageResizer.ViewModels; -using Wpf.Ui.Controls; - -using static ImageResizer.ViewModels.InputViewModel; - -namespace ImageResizer.Views -{ - public partial class InputPage : UserControl - { - public InputPage() - => InitializeComponent(); - - /// - /// Pressing Enter key doesn't update value. PropertyChanged is only updated after losing focus to NumberBox. - /// We add this workaround the UI limitations and might need to be revisited or not needed anymore if we upgrade to WinUI3. - /// This function handles the KeyDown event for a NumberBox control. - /// It checks if the key pressed is 'Enter'. - /// According to the NumberBox name, it creates an instance of the KeyPressParams class with the appropriate dimension (Width or Height) and the parsed double value. - /// - private void Button_KeyDown(object sender, KeyEventArgs e) - { - // Check if the key pressed is the 'Enter' key - if (e.Key == Key.Enter) - { - var numberBox = sender as NumberBox; - var viewModel = (InputViewModel)DataContext; - KeyPressParams keyParams; - if (double.TryParse(((System.Windows.Controls.TextBox)e.OriginalSource).Text, out double number)) - { - // Determine which NumberBox triggered the event based on its name - switch (numberBox.Name) - { - case "WidthNumberBox": - keyParams = new KeyPressParams - { - Value = number, - Dimension = Dimension.Width, - }; - break; - - case "HeightNumberBox": - keyParams = new KeyPressParams - { - Value = number, - Dimension = Dimension.Height, - }; - break; - - default: - // Return without EnterKeyPressedCommand executed - return; - } - - viewModel.EnterKeyPressedCommand.Execute(keyParams); - } - } - } - } -} diff --git a/src/modules/imageresizer/ui/Views/MainWindow.xaml b/src/modules/imageresizer/ui/Views/MainWindow.xaml deleted file mode 100644 index 2ca6ca6b6c..0000000000 --- a/src/modules/imageresizer/ui/Views/MainWindow.xaml +++ /dev/null @@ -1,65 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/modules/imageresizer/ui/Views/MainWindow.xaml.cs b/src/modules/imageresizer/ui/Views/MainWindow.xaml.cs deleted file mode 100644 index 47092b602e..0000000000 --- a/src/modules/imageresizer/ui/Views/MainWindow.xaml.cs +++ /dev/null @@ -1,61 +0,0 @@ -#pragma warning disable IDE0073 -// Copyright (c) Brice Lambson -// The Brice Lambson licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/ -#pragma warning restore IDE0073 - -using System; -using System.Collections.Generic; -using System.Linq; -using ImageResizer.ViewModels; -using ManagedCommon; -using Microsoft.Win32; -using Wpf.Ui.Controls; - -using AppResources = ImageResizer.Properties.Resources; - -namespace ImageResizer.Views -{ - public partial class MainWindow : FluentWindow, IMainView - { - public MainWindow(MainViewModel viewModel) - { - DataContext = viewModel; - - InitializeComponent(); - - if (OSVersionHelper.IsWindows11()) - { - WindowBackdropType = WindowBackdropType.Mica; - } - else - { - WindowBackdropType = WindowBackdropType.None; - } - - Wpf.Ui.Appearance.SystemThemeWatcher.Watch(this, WindowBackdropType); - } - - public IEnumerable OpenPictureFiles() - { - var openFileDialog = new OpenFileDialog - { - Filter = AppResources.PictureFilter + - "|*.bmp;*.dib;*.exif;*.gif;*.jfif;*.jpe;*.jpeg;*.jpg;*.jxr;*.png;*.rle;*.tif;*.tiff;*.wdp|" + - AppResources.AllFilesFilter + "|*.*", - InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), - Multiselect = true, - }; - - if (openFileDialog.ShowDialog() != true) - { - return Enumerable.Empty(); - } - - return openFileDialog.FileNames; - } - - void IMainView.Close() - => Dispatcher.Invoke((Action)Close); - } -} diff --git a/src/modules/imageresizer/ui/Views/NumberBoxValueConverter.cs b/src/modules/imageresizer/ui/Views/NumberBoxValueConverter.cs deleted file mode 100644 index 44ebba6ff2..0000000000 --- a/src/modules/imageresizer/ui/Views/NumberBoxValueConverter.cs +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Globalization; -using System.Windows.Data; - -namespace ImageResizer.Views; - -public class NumberBoxValueConverter : IValueConverter -{ - /// - /// Converts the underlying double value to a display-friendly format. Ensures that NaN values - /// are not propagated to the UI. - /// - public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => - value is double d && double.IsNaN(d) ? 0 : value; - - /// - /// Converts the user input back to the underlying double value. If the input is not a valid - /// number, 0 is returned. - /// - public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => - value switch - { - null => 0, - double d when double.IsNaN(d) => 0, - string str when !double.TryParse(str, out _) => 0, - _ => value, - }; -} diff --git a/src/modules/imageresizer/ui/Views/ProgressPage.xaml b/src/modules/imageresizer/ui/Views/ProgressPage.xaml deleted file mode 100644 index 137dbadfa3..0000000000 --- a/src/modules/imageresizer/ui/Views/ProgressPage.xaml +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - - - - - - - - - - - - - -