From adc5a43fd42e010f91ce5ffa39875605a0240136 Mon Sep 17 00:00:00 2001 From: Yu Leng Date: Fri, 28 Nov 2025 17:06:45 +0800 Subject: [PATCH] Refactor AI handling and improve code organization - Removed ARM64 architecture check in `CheckAiAvailability` to simplify AI availability logic. - Centralized JSON serialization settings in `AiAvailabilityCacheService.cs` using `SerializerOptions`. - Refactored JSON serialization to use the new options for consistency and maintainability. - Moved `AiCapabilityCache` to its own file, `AiCapabilityCache.cs`, with a copyright header for better organization. - Updated `ImageResizer.exe` path in `main.cpp` to reflect its new location in the `WinUI3Apps` folder. - Added `ai_detection.h` to `runner.vcxproj` and its filter file to support new AI detection functionality. --- src/modules/imageresizer/ui/App.xaml.cs | 6 ----- .../ui/Services/AiAvailabilityCacheService.cs | 26 +++++-------------- .../ui/Services/AiCapabilityCache.cs | 22 ++++++++++++++++ src/runner/main.cpp | 4 +-- src/runner/runner.vcxproj | 1 + src/runner/runner.vcxproj.filters | 3 +++ 6 files changed, 34 insertions(+), 28 deletions(-) create mode 100644 src/modules/imageresizer/ui/Services/AiCapabilityCache.cs diff --git a/src/modules/imageresizer/ui/App.xaml.cs b/src/modules/imageresizer/ui/App.xaml.cs index 17104e55de..74e918c60c 100644 --- a/src/modules/imageresizer/ui/App.xaml.cs +++ b/src/modules/imageresizer/ui/App.xaml.cs @@ -156,12 +156,6 @@ namespace ImageResizer { try { - // First check if the platform is ARM64 - AI Super Resolution only works on ARM64 - if (RuntimeInformation.ProcessArchitecture != Architecture.Arm64) - { - return AiAvailabilityState.NotSupported; - } - // Check Windows AI service model ready state // it's so slow, why? var readyState = Services.WinAiSuperResolutionService.GetModelReadyState(); diff --git a/src/modules/imageresizer/ui/Services/AiAvailabilityCacheService.cs b/src/modules/imageresizer/ui/Services/AiAvailabilityCacheService.cs index 77bc64cb37..d1a235297e 100644 --- a/src/modules/imageresizer/ui/Services/AiAvailabilityCacheService.cs +++ b/src/modules/imageresizer/ui/Services/AiAvailabilityCacheService.cs @@ -22,6 +22,11 @@ namespace ImageResizer.Services private const string CacheFileName = "ai_capabilities.json"; private const int CacheVersion = 1; + private static readonly JsonSerializerOptions SerializerOptions = new JsonSerializerOptions + { + WriteIndented = true, + }; + private static string CachePath => Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", @@ -82,10 +87,7 @@ namespace ImageResizer.Services Directory.CreateDirectory(dir); } - var json = JsonSerializer.Serialize(cache, new JsonSerializerOptions - { - WriteIndented = true, - }); + var json = JsonSerializer.Serialize(cache, SerializerOptions); File.WriteAllText(CachePath, json); Logger.LogInfo($"AI cache saved: {state}"); @@ -120,20 +122,4 @@ namespace ImageResizer.Services return true; } } - - /// - /// Data model for AI capability cache file. - /// - internal sealed class AiCapabilityCache - { - public int Version { get; set; } - - public int State { get; set; } - - public string WindowsBuild { get; set; } - - public string Architecture { get; set; } - - public string Timestamp { get; set; } - } } diff --git a/src/modules/imageresizer/ui/Services/AiCapabilityCache.cs b/src/modules/imageresizer/ui/Services/AiCapabilityCache.cs new file mode 100644 index 0000000000..f787f13569 --- /dev/null +++ b/src/modules/imageresizer/ui/Services/AiCapabilityCache.cs @@ -0,0 +1,22 @@ +// 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.Services +{ + /// + /// Data model for AI capability cache file. + /// + internal sealed class AiCapabilityCache + { + public int Version { get; set; } + + public int State { get; set; } + + public string WindowsBuild { get; set; } + + public string Architecture { get; set; } + + public string Timestamp { get; set; } + } +} diff --git a/src/runner/main.cpp b/src/runner/main.cpp index 547f6f6be0..27cea414f8 100644 --- a/src/runner/main.cpp +++ b/src/runner/main.cpp @@ -105,9 +105,9 @@ void DetectAiCapabilitiesAsync(bool skipSettingsCheck) } } - // Get ImageResizer.exe path + // Get ImageResizer.exe path (located in WinUI3Apps folder) std::wstring imageResizerPath = get_module_folderpath(); - imageResizerPath += L"\\modules\\ImageResizer\\ImageResizer.exe"; + imageResizerPath += L"\\WinUI3Apps\\PowerToys.ImageResizer.exe"; if (!std::filesystem::exists(imageResizerPath)) { diff --git a/src/runner/runner.vcxproj b/src/runner/runner.vcxproj index afff599d8e..aa4b91506c 100644 --- a/src/runner/runner.vcxproj +++ b/src/runner/runner.vcxproj @@ -77,6 +77,7 @@ + diff --git a/src/runner/runner.vcxproj.filters b/src/runner/runner.vcxproj.filters index 812d7857a2..d55e53048e 100644 --- a/src/runner/runner.vcxproj.filters +++ b/src/runner/runner.vcxproj.filters @@ -81,6 +81,9 @@ Utils + + Utils + Utils