diff --git a/.github/actions/spell-check/excludes.txt b/.github/actions/spell-check/excludes.txt index 3ffc4199f3..b7f4d46897 100644 --- a/.github/actions/spell-check/excludes.txt +++ b/.github/actions/spell-check/excludes.txt @@ -104,6 +104,8 @@ ^src/common/ManagedCommon/ColorFormatHelper\.cs$ ^src/common/notifications/BackgroundActivatorDLL/cpp\.hint$ ^src/common/sysinternals/Eula/ +^src/modules/cmdpal/Tests/Microsoft\.CommandPalette\.Extensions\.Toolkit\.UnitTests/FuzzyMatcherComparisonTests.cs$ +^src/modules/cmdpal/Tests/Microsoft\.CommandPalette\.Extensions\.Toolkit\.UnitTests/FuzzyMatcherDiacriticsTests.cs$ ^src/modules/cmdpal/doc/initial-sdk-spec/list-elements-mock-002\.pdn$ ^src/modules/cmdpal/ext/SamplePagesExtension/Pages/SampleMarkdownImagesPage\.cs$ ^src/modules/cmdpal/Microsoft\.CmdPal\.UI/Settings/InternalPage\.SampleData\.cs$ diff --git a/.github/actions/spell-check/expect.txt b/.github/actions/spell-check/expect.txt index 25c714a03f..d94db89953 100644 --- a/.github/actions/spell-check/expect.txt +++ b/.github/actions/spell-check/expect.txt @@ -635,6 +635,7 @@ GMEM GNumber googleai googlegemini +Gotchas gpedit gpo GPOCA @@ -892,9 +893,9 @@ Lclean Ldone Ldr LEFTALIGN +leftclick LEFTSCROLLBAR LEFTTEXT -leftclick LError LEVELID LExit @@ -1020,9 +1021,12 @@ MENUITEMINFO MENUITEMINFOW MERGECOPY MERGEPAINT +Metacharacter metadatamatters Metadatas +Metacharacter metafile +Metacharacter mfc Mgmt Microwaved @@ -1069,7 +1073,7 @@ mouseutils MOVESIZEEND MOVESIZESTART MRM -MRT +Mrt mru MSAL msc @@ -1487,7 +1491,9 @@ regfile REGISTERCLASSFAILED REGISTRYHEADER REGISTRYPREVIEWEXT +registryroot regkey +regroot regsvr REINSTALLMODE releaseblog @@ -1826,6 +1832,7 @@ TEXTBOXNEWLINE textextractor TEXTINCLUDE tfopen +tgamma tgz THEMECHANGED themeresources @@ -2167,4 +2174,4 @@ Zoneszonabletester Zoomin zoomit ZOOMITX -Zorder +Zorder \ No newline at end of file diff --git a/.pipelines/v2/release.yml b/.pipelines/v2/release.yml index a9799dc031..d3adc45f04 100644 --- a/.pipelines/v2/release.yml +++ b/.pipelines/v2/release.yml @@ -91,6 +91,7 @@ extends: official: true codeSign: true runTests: false + buildTests: false signingIdentity: serviceName: $(SigningServiceName) appId: $(SigningAppId) diff --git a/.pipelines/v2/templates/job-build-project.yml b/.pipelines/v2/templates/job-build-project.yml index fb321f6f2f..e41bfbc0ad 100644 --- a/.pipelines/v2/templates/job-build-project.yml +++ b/.pipelines/v2/templates/job-build-project.yml @@ -258,6 +258,7 @@ jobs: -restore -graph /p:RestorePackagesConfig=true /p:CIBuild=true + /p:BuildTests=${{ parameters.buildTests }} /bl:$(LogOutputDirectory)\build-0-main.binlog ${{ parameters.additionalBuildOptions }} $(MSBuildCacheParameters) diff --git a/.pipelines/v2/templates/pipeline-ci-build.yml b/.pipelines/v2/templates/pipeline-ci-build.yml index 0ef570d0c8..a56c575399 100644 --- a/.pipelines/v2/templates/pipeline-ci-build.yml +++ b/.pipelines/v2/templates/pipeline-ci-build.yml @@ -59,6 +59,7 @@ stages: enableMsBuildCaching: ${{ parameters.enableMsBuildCaching }} msBuildCacheIsReadOnly: ${{ parameters.msBuildCacheIsReadOnly }} runTests: ${{ parameters.runTests }} + buildTests: true useVSPreview: ${{ parameters.useVSPreview }} useLatestWinAppSDK: ${{ parameters.useLatestWinAppSDK }} ${{ if eq(parameters.useLatestWinAppSDK, true) }}: @@ -78,7 +79,9 @@ stages: ${{ else }}: name: SHINE-OSS-L ${{ if eq(parameters.useVSPreview, true) }}: - demands: ImageOverride -equals SHINE-VS17-Preview + demands: ImageOverride -equals SHINE-VS18-Preview + ${{ else }}: + demands: ImageOverride -equals SHINE-VS18-Latest buildConfigurations: [Release] official: false codeSign: false diff --git a/.pipelines/verifyNoticeMdAgainstNugetPackages.ps1 b/.pipelines/verifyNoticeMdAgainstNugetPackages.ps1 index af9ab8ff6f..a5cf73e6e9 100644 --- a/.pipelines/verifyNoticeMdAgainstNugetPackages.ps1 +++ b/.pipelines/verifyNoticeMdAgainstNugetPackages.ps1 @@ -90,9 +90,15 @@ if ($noticeMatch.Success) { $currentNoticePackageList = "" } +# Test-only packages that are allowed to be in NOTICE.md but not in the build +# (e.g., when BuildTests=false, these packages won't appear in the NuGet list) +$allowedExtraPackages = @( + "- Moq" +) + if (!$noticeFile.Trim().EndsWith($returnList.Trim())) { - Write-Host -ForegroundColor Red "Notice.md does not match NuGet list." + Write-Host -ForegroundColor Yellow "Notice.md does not exactly match NuGet list. Analyzing differences..." # Show detailed differences $generatedPackages = $returnList -split "`r`n|`n" | Where-Object { $_.Trim() -ne "" } | Sort-Object @@ -105,7 +111,7 @@ if (!$noticeFile.Trim().EndsWith($returnList.Trim())) # Find packages in proj file list but not in NOTICE.md $missingFromNotice = $generatedPackages | Where-Object { $noticePackages -notcontains $_ } if ($missingFromNotice.Count -gt 0) { - Write-Host -ForegroundColor Red "MissingFromNotice:" + Write-Host -ForegroundColor Red "MissingFromNotice (ERROR - these must be added to NOTICE.md):" foreach ($pkg in $missingFromNotice) { Write-Host -ForegroundColor Red " $pkg" } @@ -114,10 +120,23 @@ if (!$noticeFile.Trim().EndsWith($returnList.Trim())) # Find packages in NOTICE.md but not in proj file list $extraInNotice = $noticePackages | Where-Object { $generatedPackages -notcontains $_ } - if ($extraInNotice.Count -gt 0) { - Write-Host -ForegroundColor Yellow "ExtraInNotice:" - foreach ($pkg in $extraInNotice) { - Write-Host -ForegroundColor Yellow " $pkg" + + # Filter out allowed extra packages (test-only dependencies) + $unexpectedExtra = $extraInNotice | Where-Object { $allowedExtraPackages -notcontains $_ } + $allowedExtra = $extraInNotice | Where-Object { $allowedExtraPackages -contains $_ } + + if ($allowedExtra.Count -gt 0) { + Write-Host -ForegroundColor Green "ExtraInNotice (OK - allowed test-only packages):" + foreach ($pkg in $allowedExtra) { + Write-Host -ForegroundColor Green " $pkg" + } + Write-Host "" + } + + if ($unexpectedExtra.Count -gt 0) { + Write-Host -ForegroundColor Red "ExtraInNotice (ERROR - unexpected packages in NOTICE.md):" + foreach ($pkg in $unexpectedExtra) { + Write-Host -ForegroundColor Red " $pkg" } Write-Host "" } @@ -127,10 +146,17 @@ if (!$noticeFile.Trim().EndsWith($returnList.Trim())) Write-Host " Proj file list has $($generatedPackages.Count) packages" Write-Host " NOTICE.md has $($noticePackages.Count) packages" Write-Host " MissingFromNotice: $($missingFromNotice.Count) packages" - Write-Host " ExtraInNotice: $($extraInNotice.Count) packages" + Write-Host " ExtraInNotice (allowed): $($allowedExtra.Count) packages" + Write-Host " ExtraInNotice (unexpected): $($unexpectedExtra.Count) packages" Write-Host "" - exit 1 + # Fail if there are missing packages OR unexpected extra packages + if ($missingFromNotice.Count -gt 0 -or $unexpectedExtra.Count -gt 0) { + Write-Host -ForegroundColor Red "FAILED: NOTICE.md mismatch detected." + exit 1 + } else { + Write-Host -ForegroundColor Green "PASSED: NOTICE.md matches (with allowed test-only packages)." + } } exit 0 diff --git a/Cpp.Build.props b/Cpp.Build.props index 4b8a206306..5acfbdee1a 100644 --- a/Cpp.Build.props +++ b/Cpp.Build.props @@ -2,6 +2,12 @@ + + + false + false + + diff --git a/Directory.Build.props b/Directory.Build.props index e7b415cbca..99379ecefc 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -19,6 +19,39 @@ $(Platform) + + + <_ProjectName>$(MSBuildProjectName) + + <_IsSkippedTestProject Condition="$(_ProjectName.EndsWith('Test'))">true + <_IsSkippedTestProject Condition="$(_ProjectName.EndsWith('Tests'))">true + + <_IsSkippedTestProject Condition="$(_ProjectName.StartsWith('UnitTests-'))">true + <_IsSkippedTestProject Condition="$(_ProjectName.StartsWith('UITest-'))">true + + <_IsSkippedTestProject Condition="$(MSBuildProjectDirectory.Contains('\Tests\'))">true + + + + false + false + false + false + disable + + false + false + false + false + + $(Version).0 https://github.com/microsoft/PowerToys @@ -30,7 +63,9 @@ <_PropertySheetDisplayName>PowerToys.Root.Props $(MsbuildThisFileDirectory)\Cpp.Build.props - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Directory.Build.targets b/Directory.Build.targets index ab9bad297e..9efab5a9a5 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -28,4 +28,41 @@ $(NoWarn);CS8305;SA1500;CA1852 - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/PowerToys.slnx b/PowerToys.slnx index 8a166bb32e..1dc26be394 100644 --- a/PowerToys.slnx +++ b/PowerToys.slnx @@ -360,6 +360,10 @@ + + + + diff --git a/doc/devdocs/development/new-powertoy.md b/doc/devdocs/development/new-powertoy.md new file mode 100644 index 0000000000..1e0f7bccfa --- /dev/null +++ b/doc/devdocs/development/new-powertoy.md @@ -0,0 +1,311 @@ +# 🧭 Creating a new PowerToy: end-to-end developer guide + +First of all, thank you for wanting to contribute to PowerToys. The work we do would not be possible without the support of community supporters like you. + +This guide documents the process of building a new PowerToys utility from scratch, including architecture decisions, integration steps, and common pitfalls. + +--- + +## 1. Overview and prerequisites + +A PowerToy module is a self-contained utility integrated into the PowerToys ecosystem. It can be UI-based, service-based, or both. + +### Requirements + +- [Visual Studio 2026](https://visualstudio.microsoft.com/downloads/) and the following workloads/individual components: + - Desktop Development with C++ + - WinUI application development + - .NET desktop development + - Windows 10 SDK (10.0.22621.0) + - Windows 11 SDK (10.0.26100.3916) +- .NET 8 SDK +- Fork the [PowerToys repository](https://github.com/microsoft/PowerToys/tree/main) locally +- [Validate that you are able to build and run](https://github.com/microsoft/PowerToys/blob/main/doc/devdocs/development/debugging.md) `PowerToys.slnx`. + +Optional: +- [WiX v5 toolset](https://github.com/microsoft/PowerToys/tree/main) for the installer + +> [!NOTE] +> To ensure all the correct VS Workloads are installed, use [the WinGet configuration files](https://github.com/microsoft/PowerToys/tree/e13d6a78aafbcf32a4bb5f8581d041e1d057c3f1/.config) in the project repository. (Use the one that matches your VS distribution. ie: VS Community would use `configuration.winget`) + +### Folder structure + +``` +src/ + modules/ + your_module/ + YourModule.sln + YourModuleInterface/ + YourModuleUI/ (if needed) + YourModuleService/ (if needed) +``` + +--- +## 2. Design and planning + +### Decide the type of module + +Think about how your module works and which existing modules behave similarly. You are going to want to think about the UI needed for the application, the lifecycle, whether it is a service that is always running or event based. Below are some basic scenarios with some modules to explore. You can write your application in C++ or C#. +- **UI-only:** e.g., ColorPicker +- **Background service:** e.g., LightSwitch, Awake +- **Hybrid (UI + background logic):** e.g., ShortcutGuide +- **C++/C# interop:** e.g., PowerRename + +### Write your module interface + +Begin by setting up the [PowerToy module template project](https://github.com/microsoft/PowerToys/tree/main/tools/project_template). This will generate boilerplate for you to begin your new module. Below are the key headers in the Module Interface (`dllmain.cpp`) and an explanation of their purpose: +1. This is where module settings are defined. These can be anything from strings, bools, ints, and even custom Enums. +```c++ +struct ModuleSettings {}; +``` + +2. This is the header for the full class. It inherits the PowerToyModuleIface +```c++ +class ModuleInterface : public PowertoyModuleIface +{ + private: + // the private members of the class + // Can include the enabled variable, logic for event handlers, or hotkeys. + public: + // the public members of the class + // Will include the constructor and initialization logic. +} +``` + +> [!NOTE] +> Many of the class functions are boilerplate and need simple string replacements with your module name. The rest of the functions below will require bigger changes. + +3. GPO stands for "Group Policy Object" and allows for administrators to configure settings across a network of machines. It is required that your module is on this list of settings. You can right click the `powertoys_gpo` object to go to the definition and set up the `getConfiguredModuleEnabledValue` for your module. +```c++ +virtual powertoys_gpo::gpo_rule_configured_t gpo_policy_enabled_configuration() override +{ + return powertoys_gpo::getConfiguredModuleEnabledValue(); +} +``` + +4. `init_settings()` initializes the settings for the interface. Will either pull from existing settings.json or use defaults. +```c++ +void ModuleInterface::init_settings() +``` + +5. `get_config` retrieves the settings from the settings.json file. +```c++ +virtual bool get_config(wchar_t* buffer, int* buffer_size) override +``` + +6. `set_config` sets the new settings to the settings.json file. +```c++ +virtual void set_config(const wchar_t* config) override +``` + +7. `call_custom_action` allows custom actions to be called based on signals from the settings app. +```c++ +void call_custom_action(const wchar_t* action) override +``` + +8. Lifecycle events control whether the module is enabled or not, as well as the default status of the module. +```c++ +virtual void enable() // starts the module +virtual void disable() // terminates the module and performs any cleanup +virtual bool is_enabled() // returns if the module is currently enabled +virtual bool is_enabled_by_default() const override // allows the module to dictate whether it should be enabled by default in the PowerToys app. +``` + +9. Hotkey functions control the status of the hotkey. +```c++ +// takes the hotkey from settings into a format that the interface can understand +void parse_hotkey(PowerToysSettings::PowerToyValues& settings) + +// returns the hotkeys from settings +virtual size_t get_hotkeys(Hotkey* hotkeys, size_t buffer_size) override + +// performs logic when the hotkey event is fired +virtual bool on_hotkey(size_t hotkeyId) override +``` + +### Notes + +- Keep module logic isolated under `/modules/` +- Use shared utilities from [`common`](https://github.com/microsoft/PowerToys/tree/main/src/common) instead of cross-module dependencies +- init/set/get config use preset functions to access the settings. Check out the [`settings_objects.h`](https://github.com/microsoft/PowerToys/blob/main/src/common/SettingsAPI/settings_helpers.h) in `src\common\SettingsAPI` + +--- +## 3. Bootstrapping your module + +1. Use the [template](https://github.com/microsoft/PowerToys/tree/main/tools/project_template) to generate the module interface starter code. +2. Update all projects and namespaces with your module name. +3. Update GUIDs in `.vcxproj` and solution files. +4. Update the functions mentioned in the above section with your custom logic. +5. In order for your module to be detected by the runner you are required to add references to various lists. In order to register your module, add the corresponding module reference to the lists that can be found in the following files. (Hint: search other modules names to find the lists quicker) + - `src/runner/modules.h` + - `src/runner/modules.cpp` + - `src/runner/resource.h` + - `src/runner/settings_window.h` + - `src/runner/settings_window.cpp` + - `src/runner/main.cpp` + - `src/common/logger.h` (for logging) +6. ModuleInterface should build your `ModuleInterface.dll`. This will allow the runner to interact with your service. + +> [!TIP] +> Mismatched module IDs are one of the most common causes of load failures. Keep your ID consistent across manifest, registry, and service. + +--- +## 4. Write your service + +This is going to look different for every PowerToy. It may be easier to develop the application independently, and then link in the PowerToys settings logic later. But you have to write the service first, before connecting it to the runner. + +### Notes + +- This is a separate project from the Module Interface. +- You can develop this project using C# or C++. +- Set the service icon using the `.rc` file. +- Set the service name in the `.vcxproj` by setting the `` +``` + + ..\..\..\..\$(Platform)\$(Configuration)\$(MSBuildProjectName)\ + PowerToys.LightSwitchService + +``` +- To view the code of the `.vcxproj`, right click the item and select **Unload project** +- Use the following functions to interact with settings from your service +``` +ModuleSettings::instance().InitFileWatcher(); +ModuleSettings::instance().LoadSettings(); +auto& settings = ModuleSettings::instance().settings(); +``` +These come from the `ModuleSettings.h` file that lives with the Service. You can copy this from another module (e.g., Light Switch) and adjust to fit your needs. + +If your module has a user interface: +- Use the **WinUI Blank App** template when setting up your project +- Use [Windows design best practices](https://learn.microsoft.com/windows/apps/design/basics/) +- Use the [WinUI 3 Gallery](https://apps.microsoft.com/detail/9p3jfpwwdzrc) for help with your UI code, and additional guidance. + +## 5. Settings integration + +PowerToys settings are stored per-module as JSON under: + +``` +%LOCALAPPDATA%\Microsoft\PowerToys\\settings.json +``` + +### Implementation steps + +- In `src\settings-ui\Settings.UI.Library\` create `Properties.cs` and `Settings.cs` +- `Properties.cs` is where you will define your defaults. Every setting needs to be represented here. This should match what was set in the Module Interface. +- `Settings.cs`is where your settings.json will be built from. The structure should match the following +```cs +public ModuleSettings() +{ + Name = ModuleName; + Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); + Properties = new ModuleProperties(); // settings properties you set above. +} +``` + +- In `src\settings-ui\Settings.UI\ViewModels` create `ViewModel.cs` this is where the interaction happens between your settings page in the PowerToys app and the settings file that is stored on the device. Changes here will trigger the settings watcher via a `NotifyPropertyChanged` event. +- Create a `SettingsPage.xaml` at `src\settings-ui\Settings.UI\SettingsXAML\Views`. This will be the page where the user interacts with the settings of your module. +- Be sure to use resource strings for user facing strings so they can be localized. (`x:Uid` connects to Resources.resw) +```xaml +// LightSwitch.xaml + + +// Resources.resw + + Off + +``` +> [!IMPORTANT] +> In the above example we use `.Content` to target the content of the Combobox. This can change per UI element (e.g., `.Text`, `.Header`, etc.) + +> **Reminder:** Manual changes via external editors (VS Code, Notepad) do **not** trigger the settings watcher. Only changes written through PowerToys trigger reloads. + +--- + +### Gotchas: + +- Only use the WinUI 3 framework, _not_ UWP. +- Use [`DispatcherQueue`](https://learn.microsoft.com/windows/apps/develop/dispatcherqueue) when updating UI from non-UI threads. + +--- +## 6. Building and debugging + +### Debugging steps + +1. If this is your first time debugging PowerToys, be sure to follow [these steps first](https://github.com/microsoft/PowerToys/blob/main/doc/devdocs/development/debugging.md#pre-debugging-setup). +2. Set "runner" as the start up project and ensure your build configuration is set to match your system (ARM64/x64) +3. Select F5 or the **Local Windows Debugger** button to begin debugging. This should start the PowerToys runner. +4. To set breakpoints in your service, select Ctrl+Alt+P and search for your service to attach to the runner. +5. Use logs to document changes. The logs live at `%LOCALAPPDATA%\Microsoft\PowerToys\RunnerLogs` and `%LOCALAPPDATA%\Microsoft\PowerToys\Module\Service\` for the specific module. + +> [!TIP] +> PowerToys caches `.nuget` artifacts aggressively. Use `git clean -xfd` when builds behave unexpectedly. + +--- +## 7. Installer and packaging (WiX) + +### Add your module to installer + +1. Install [`WixToolset.Heat`](https://www.nuget.org/packages/WixToolset.Heat/) for Wix5 via nuget +2. Inside `installer\PowerToysInstallerVNext` add a new file for your module: `Module.wxs` +3. Inside of this file you will need copy the format from another module (ie: Light Switch) and replace the strings and GUID values. +4. The key part will be `` which is a placeholder for code that will be generated by `generateFileComponents.ps1`. +5. Inside `Product.wxs` add a line item in the `` section. It will look like a list of ` ` items. +6. Inside `generateFileComponents.ps1` you will need to add an entry to the bottom for your new module. It will follow the following format. `-fileListName Files` will match the string you set in `Module.wxs`, `` will match the name of your exe. +```bash +# Module Name +Generate-FileList -fileDepsJson "" -fileListName Files -wxsFilePath $PSScriptRoot\.wxs -depsPath "$PSScriptRoot..\..\..\$platform\Release\" +Generate-FileComponents -fileListName "Files" -wxsFilePath $PSScriptRoot\.wxs -regroot $registryroot +``` +--- +## 8. Testing and validation + +### UI tests + +- Place under `/modules//Tests` +- Create a new [WinUI Unit Test App](https://learn.microsoft.com/windows/apps/winui/winui3/testing/create-winui-unit-test-project) +- Write unit tests following the format from previous modules (ie: Light Switch). This can be to test your standalone UI (if you're a module like Color Picker) or to verify that the Settings UI in the PowerToys app is controlling your service. + +### Manual validation + +- Enable/disable in PowerToys Settings +- Check initialization in logs +- Confirm icons, tooltips, and OOBE page appear correctly + +### Pro tips + +1. Validate wake/sleep and elevation states. Background modules often fail silently after resume if event handles aren’t recreated. +2. Use Windows Sandbox to simulate clean install environments +3. To simulate a "new user" you can delete the PowerToys folder from `%LOCALAPPDATA%\Microsoft` + +### Shortcut conflict detection + +If your module has a shortcut, ensure that it is properly registered following [the steps listed in the documentation](https://github.com/microsoft/PowerToys/blob/main/doc/devdocs/core/settings/settings-implementation.md#shortcut-conflict-detection) for conflict detection. + +--- +## 9. The final touches + +### Out-of-Box experience (OOBE) page + +The OOBE page is a custom settings page that gives the user at a glance information about each module. This window opens before the Settings application for new users and after updates. Create `OOBE.xaml` at `src\settings-ui\Settings.UI\SettingsXAML\OOBE\Views`. You will also need to add your module name to the enum at `src\settings-ui\Settings.UI\OOBE\Enums\PowerToysModules.cs`. + +### Module assets + +Now that your PowerToy is _done_ you can start to think about the assets that will represent your module. +- Module Icon: This will be displayed in a number of places: OOBE page, in the README, on the home screen of PowerToys, on your individual module settings page, etc. +- Module Image: This is the image you see at the top of each individual settings page. +- OOBE Image: This is the header you see on the OOBE page for each module + +> [!NOTE] +> This step is something that the Design team will handle internally to ensure consistency throughout the application. If you have ideas or recommendations on what the icon or screenshots should be for your module feel free to leave it in the "Additional Comments" section of the PR and the team will take it into consideration. + +### Documentation + +There are two types of documentation that will be required when submitting a new PowerToy: +1. Developer documentation: This will live in the [PowerToys repo](https://github.com/microsoft/PowerToys/blob/main/doc/devdocs/modules) at `/doc/devdocs/modules/` and should tell a developer how to work on your app. It should outline the module architecture, key files, testing, and tips on debugging if necessary. +2. Microsoft Learn documentation: When your new Module is ready to be merged into the PowerToys repository, an internal team member will create Microsoft Learn documentation so that users will understand how to use your module. There is not much work on your end as the developer for this step, but keep an eye on your PR in case we need more information about your PowerToy for this step. + +--- +Thank you again for contributing! If you need help, feel free to [open an issue](https://github.com/microsoft/PowerToys/issues/new/choose) and use the `Needs-Team-Response` label so we know you need attention. diff --git a/doc/devdocs/modules/advancedpaste.md b/doc/devdocs/modules/advancedpaste.md index e23fde3a8a..b2ab244432 100644 --- a/doc/devdocs/modules/advancedpaste.md +++ b/doc/devdocs/modules/advancedpaste.md @@ -18,13 +18,28 @@ Advanced Paste is a PowerToys module that provides enhanced clipboard pasting wi TODO: Add implementation details +### Paste with AI Preview + +The "Show preview" setting (`ShowCustomPreview`) controls whether AI-generated results are displayed in a preview window before pasting. **The preview feature does not consume additional AI credits**—the preview displays the same AI response that was already generated, cached locally from a single API call. + +The implementation flow: +1. User initiates "Paste with AI" action +2. A single AI API call is made via `ExecutePasteFormatAsync` +3. The result is cached in `GeneratedResponses` +4. If preview is enabled, the cached result is displayed in the preview UI +5. User can paste the cached result without any additional API calls + +See the `ExecutePasteFormatAsync(PasteFormat, PasteActionSource)` method in `OptionsViewModel.cs` for the implementation. + ## Debugging TODO: Add debugging information ## Settings -TODO: Add settings documentation +| Setting | Description | +|---------|-------------| +| `ShowCustomPreview` | When enabled, shows AI-generated results in a preview window before pasting. Does not affect AI credit consumption. | ## Future Improvements diff --git a/installer/PowerToysSetupVNext/Resources.wxs b/installer/PowerToysSetupVNext/Resources.wxs index 7e62a34be9..a392004320 100644 --- a/installer/PowerToysSetupVNext/Resources.wxs +++ b/installer/PowerToysSetupVNext/Resources.wxs @@ -367,6 +367,12 @@ + + + + + + diff --git a/src/common/CalculatorEngineCommon/ExprtkEvaluator.cpp b/src/common/CalculatorEngineCommon/ExprtkEvaluator.cpp index dbc2120b24..eb04c18783 100644 --- a/src/common/CalculatorEngineCommon/ExprtkEvaluator.cpp +++ b/src/common/CalculatorEngineCommon/ExprtkEvaluator.cpp @@ -3,9 +3,27 @@ #include #include #include +#include +#include namespace ExprtkCalculator::internal { + static double factorial(const double n) + { + // Only allow non-negative integers + if (n < 0.0 || std::floor(n) != n) + { + return std::numeric_limits::quiet_NaN(); + } + return std::tgamma(n + 1.0); + } + + static double sign(const double n) + { + if (n > 0.0) return 1.0; + if (n < 0.0) return -1.0; + return 0.0; + } std::wstring ToWStringFullPrecision(double value) { @@ -25,6 +43,9 @@ namespace ExprtkCalculator::internal symbol_table.add_constant(name, value); } + symbol_table.add_function("factorial", factorial); + symbol_table.add_function("sign", sign); + exprtk::expression expression; expression.register_symbol_table(symbol_table); diff --git a/src/common/interop/shared_constants.h b/src/common/interop/shared_constants.h index 118683f24c..3dad776cf6 100644 --- a/src/common/interop/shared_constants.h +++ b/src/common/interop/shared_constants.h @@ -72,6 +72,10 @@ namespace CommonSharedConstants const wchar_t ALWAYS_ON_TOP_TERMINATE_EVENT[] = L"Local\\AlwaysOnTopTerminateEvent-cfdf1eae-791f-4953-8021-2f18f3837eae"; + const wchar_t ALWAYS_ON_TOP_INCREASE_OPACITY_EVENT[] = L"Local\\AlwaysOnTopIncreaseOpacityEvent-a1b2c3d4-e5f6-7890-abcd-ef1234567890"; + + const wchar_t ALWAYS_ON_TOP_DECREASE_OPACITY_EVENT[] = L"Local\\AlwaysOnTopDecreaseOpacityEvent-b2c3d4e5-f6a7-8901-bcde-f12345678901"; + // Path to the event used by PowerAccent const wchar_t POWERACCENT_EXIT_EVENT[] = L"Local\\PowerToysPowerAccentExitEvent-53e93389-d19a-4fbb-9b36-1981c8965e17"; diff --git a/src/modules/Workspaces/WorkspacesEditor/OverlayWindow.xaml.cs b/src/modules/Workspaces/WorkspacesEditor/OverlayWindow.xaml.cs index 54e892d9bd..d1646e7282 100644 --- a/src/modules/Workspaces/WorkspacesEditor/OverlayWindow.xaml.cs +++ b/src/modules/Workspaces/WorkspacesEditor/OverlayWindow.xaml.cs @@ -2,8 +2,11 @@ // 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; +using WorkspacesEditor.Utils; + namespace WorkspacesEditor { /// @@ -11,9 +14,40 @@ namespace WorkspacesEditor /// public partial class OverlayWindow : Window { + private int _targetX; + private int _targetY; + private int _targetWidth; + private int _targetHeight; + public OverlayWindow() { InitializeComponent(); + SourceInitialized += OnWindowSourceInitialized; + } + + /// + /// Sets the target bounds for the overlay window. + /// The window will be positioned using DPI-unaware context after initialization. + /// + public void SetTargetBounds(int x, int y, int width, int height) + { + _targetX = x; + _targetY = y; + _targetWidth = width; + _targetHeight = height; + + // Set initial WPF properties (will be corrected after HWND creation) + Left = x; + Top = y; + Width = width; + Height = height; + } + + private void OnWindowSourceInitialized(object sender, EventArgs e) + { + // Reposition window using DPI-unaware context to match the virtual coordinates. + // This fixes overlay positioning on mixed-DPI multi-monitor setups. + NativeMethods.SetWindowPositionDpiUnaware(this, _targetX, _targetY, _targetWidth, _targetHeight); } } } diff --git a/src/modules/Workspaces/WorkspacesEditor/Utils/NativeMethods.cs b/src/modules/Workspaces/WorkspacesEditor/Utils/NativeMethods.cs index 4105cbe959..9687aeac63 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Utils/NativeMethods.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Utils/NativeMethods.cs @@ -4,6 +4,8 @@ using System; using System.Runtime.InteropServices; +using System.Windows; +using System.Windows.Interop; namespace WorkspacesEditor.Utils { @@ -17,6 +19,39 @@ namespace WorkspacesEditor.Utils [return: MarshalAs(UnmanagedType.Bool)] public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); + [DllImport("user32.dll", SetLastError = true)] + private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); + + [DllImport("user32.dll")] + private static extern IntPtr SetThreadDpiAwarenessContext(IntPtr dpiContext); + + private const uint SWP_NOZORDER = 0x0004; + private const uint SWP_NOACTIVATE = 0x0010; + + private static readonly IntPtr DPI_AWARENESS_CONTEXT_UNAWARE = new IntPtr(-1); + + /// + /// Positions a WPF window using DPI-unaware context to match the virtual coordinates. + /// This fixes overlay positioning on mixed-DPI multi-monitor setups. + /// + public static void SetWindowPositionDpiUnaware(Window window, int x, int y, int width, int height) + { + var helper = new WindowInteropHelper(window).Handle; + if (helper != IntPtr.Zero) + { + // Temporarily switch to DPI-unaware context to position window. + IntPtr oldContext = SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_UNAWARE); + try + { + SetWindowPos(helper, IntPtr.Zero, x, y, width, height, SWP_NOZORDER | SWP_NOACTIVATE); + } + finally + { + SetThreadDpiAwarenessContext(oldContext); + } + } + } + [DllImport("USER32.DLL")] public static extern bool SetForegroundWindow(IntPtr hWnd); diff --git a/src/modules/Workspaces/WorkspacesEditor/ViewModels/MainViewModel.cs b/src/modules/Workspaces/WorkspacesEditor/ViewModels/MainViewModel.cs index 9c76c26fa0..5741fd65ab 100644 --- a/src/modules/Workspaces/WorkspacesEditor/ViewModels/MainViewModel.cs +++ b/src/modules/Workspaces/WorkspacesEditor/ViewModels/MainViewModel.cs @@ -495,10 +495,10 @@ namespace WorkspacesEditor.ViewModels { var bounds = screen.Bounds; OverlayWindow overlayWindow = new OverlayWindow(); - overlayWindow.Top = bounds.Top; - overlayWindow.Left = bounds.Left; - overlayWindow.Width = bounds.Width; - overlayWindow.Height = bounds.Height; + + // Use DPI-unaware positioning to fix overlay on mixed-DPI multi-monitor setups + overlayWindow.SetTargetBounds(bounds.Left, bounds.Top, bounds.Width, bounds.Height); + overlayWindow.ShowActivated = true; overlayWindow.Topmost = true; overlayWindow.Show(); diff --git a/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.cpp b/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.cpp index 8287ee1cce..f09cc2997f 100644 --- a/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.cpp +++ b/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.cpp @@ -153,9 +153,21 @@ LRESULT AlwaysOnTop::WndProc(HWND window, UINT message, WPARAM wparam, LPARAM lp { if (message == WM_HOTKEY) { + int hotkeyId = static_cast(wparam); if (HWND fw{ GetForegroundWindow() }) { - ProcessCommand(fw); + if (hotkeyId == static_cast(HotkeyId::Pin)) + { + ProcessCommand(fw); + } + else if (hotkeyId == static_cast(HotkeyId::IncreaseOpacity)) + { + StepWindowTransparency(fw, Settings::transparencyStep); + } + else if (hotkeyId == static_cast(HotkeyId::DecreaseOpacity)) + { + StepWindowTransparency(fw, -Settings::transparencyStep); + } } } else if (message == WM_PRIV_SETTINGS_CHANGED) @@ -191,6 +203,10 @@ void AlwaysOnTop::ProcessCommand(HWND window) m_topmostWindows.erase(iter); } + // Restore transparency when unpinning + RestoreWindowAlpha(window); + m_windowOriginalLayeredState.erase(window); + Trace::AlwaysOnTop::UnpinWindow(); } } @@ -200,6 +216,7 @@ void AlwaysOnTop::ProcessCommand(HWND window) { soundType = Sound::Type::On; AssignBorder(window); + Trace::AlwaysOnTop::PinWindow(); } } @@ -269,11 +286,22 @@ void AlwaysOnTop::RegisterHotkey() const { if (m_useCentralizedLLKH) { + // All hotkeys are handled by centralized LLKH return; } + // Register hotkeys only when not using centralized LLKH UnregisterHotKey(m_window, static_cast(HotkeyId::Pin)); + UnregisterHotKey(m_window, static_cast(HotkeyId::IncreaseOpacity)); + UnregisterHotKey(m_window, static_cast(HotkeyId::DecreaseOpacity)); + + // Register pin hotkey RegisterHotKey(m_window, static_cast(HotkeyId::Pin), AlwaysOnTopSettings::settings().hotkey.get_modifiers(), AlwaysOnTopSettings::settings().hotkey.get_code()); + + // Register transparency hotkeys using the same modifiers as the pin hotkey + UINT modifiers = AlwaysOnTopSettings::settings().hotkey.get_modifiers(); + RegisterHotKey(m_window, static_cast(HotkeyId::IncreaseOpacity), modifiers, VK_OEM_PLUS); + RegisterHotKey(m_window, static_cast(HotkeyId::DecreaseOpacity), modifiers, VK_OEM_MINUS); } void AlwaysOnTop::RegisterLLKH() @@ -285,6 +313,8 @@ void AlwaysOnTop::RegisterLLKH() m_hPinEvent = CreateEventW(nullptr, false, false, CommonSharedConstants::ALWAYS_ON_TOP_PIN_EVENT); m_hTerminateEvent = CreateEventW(nullptr, false, false, CommonSharedConstants::ALWAYS_ON_TOP_TERMINATE_EVENT); + m_hIncreaseOpacityEvent = CreateEventW(nullptr, false, false, CommonSharedConstants::ALWAYS_ON_TOP_INCREASE_OPACITY_EVENT); + m_hDecreaseOpacityEvent = CreateEventW(nullptr, false, false, CommonSharedConstants::ALWAYS_ON_TOP_DECREASE_OPACITY_EVENT); if (!m_hPinEvent) { @@ -298,30 +328,54 @@ void AlwaysOnTop::RegisterLLKH() return; } - HANDLE handles[2] = { m_hPinEvent, - m_hTerminateEvent }; + if (!m_hIncreaseOpacityEvent) + { + Logger::warn(L"Failed to create increaseOpacityEvent. {}", get_last_error_or_default(GetLastError())); + } + + if (!m_hDecreaseOpacityEvent) + { + Logger::warn(L"Failed to create decreaseOpacityEvent. {}", get_last_error_or_default(GetLastError())); + } + + HANDLE handles[4] = { m_hPinEvent, + m_hTerminateEvent, + m_hIncreaseOpacityEvent, + m_hDecreaseOpacityEvent }; m_thread = std::thread([this, handles]() { MSG msg; while (m_running) { - DWORD dwEvt = MsgWaitForMultipleObjects(2, handles, false, INFINITE, QS_ALLINPUT); + DWORD dwEvt = MsgWaitForMultipleObjects(4, handles, false, INFINITE, QS_ALLINPUT); if (!m_running) { break; } switch (dwEvt) { - case WAIT_OBJECT_0: + case WAIT_OBJECT_0: // Pin event if (HWND fw{ GetForegroundWindow() }) { ProcessCommand(fw); } break; - case WAIT_OBJECT_0 + 1: + case WAIT_OBJECT_0 + 1: // Terminate event PostThreadMessage(m_mainThreadId, WM_QUIT, 0, 0); break; - case WAIT_OBJECT_0 + 2: + case WAIT_OBJECT_0 + 2: // Increase opacity event + if (HWND fw{ GetForegroundWindow() }) + { + StepWindowTransparency(fw, Settings::transparencyStep); + } + break; + case WAIT_OBJECT_0 + 3: // Decrease opacity event + if (HWND fw{ GetForegroundWindow() }) + { + StepWindowTransparency(fw, -Settings::transparencyStep); + } + break; + case WAIT_OBJECT_0 + 4: // Message queue if (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); @@ -370,9 +424,12 @@ void AlwaysOnTop::UnpinAll() { Logger::error(L"Unpinning topmost window failed"); } + // Restore transparency when unpinning all + RestoreWindowAlpha(topWindow); } m_topmostWindows.clear(); + m_windowOriginalLayeredState.clear(); } void AlwaysOnTop::CleanUp() @@ -456,6 +513,7 @@ void AlwaysOnTop::HandleWinHookEvent(WinHookEvent* data) noexcept for (const auto window : toErase) { m_topmostWindows.erase(window); + m_windowOriginalLayeredState.erase(window); } switch (data->event) @@ -556,4 +614,166 @@ void AlwaysOnTop::RefreshBorders() } } } +} + +HWND AlwaysOnTop::ResolveTransparencyTargetWindow(HWND window) +{ + if (!window || !IsWindow(window)) + { + return nullptr; + } + + // Only allow transparency changes on pinned windows + if (!IsPinned(window)) + { + return nullptr; + } + + return window; +} + + +void AlwaysOnTop::StepWindowTransparency(HWND window, int delta) +{ + HWND targetWindow = ResolveTransparencyTargetWindow(window); + if (!targetWindow) + { + return; + } + + int currentTransparency = Settings::maxTransparencyPercentage; + LONG exStyle = GetWindowLong(targetWindow, GWL_EXSTYLE); + if (exStyle & WS_EX_LAYERED) + { + BYTE alpha = 255; + if (GetLayeredWindowAttributes(targetWindow, nullptr, &alpha, nullptr)) + { + currentTransparency = (alpha * 100) / 255; + } + } + + int newTransparency = (std::max)(Settings::minTransparencyPercentage, + (std::min)(Settings::maxTransparencyPercentage, currentTransparency + delta)); + + if (newTransparency != currentTransparency) + { + ApplyWindowAlpha(targetWindow, newTransparency); + + if (AlwaysOnTopSettings::settings().enableSound) + { + m_sound.Play(delta > 0 ? Sound::Type::IncreaseOpacity : Sound::Type::DecreaseOpacity); + } + + Logger::debug(L"Transparency adjusted to {}%", newTransparency); + } +} + +void AlwaysOnTop::ApplyWindowAlpha(HWND window, int percentage) +{ + if (!window || !IsWindow(window)) + { + return; + } + + percentage = (std::max)(Settings::minTransparencyPercentage, + (std::min)(Settings::maxTransparencyPercentage, percentage)); + + LONG exStyle = GetWindowLong(window, GWL_EXSTYLE); + bool isCurrentlyLayered = (exStyle & WS_EX_LAYERED) != 0; + + // Cache original state on first transparency application + if (m_windowOriginalLayeredState.find(window) == m_windowOriginalLayeredState.end()) + { + WindowLayeredState state; + state.hadLayeredStyle = isCurrentlyLayered; + + if (isCurrentlyLayered) + { + BYTE alpha = 255; + COLORREF colorKey = 0; + DWORD flags = 0; + if (GetLayeredWindowAttributes(window, &colorKey, &alpha, &flags)) + { + state.originalAlpha = alpha; + state.usedColorKey = (flags & LWA_COLORKEY) != 0; + state.colorKey = colorKey; + } + else + { + Logger::warn(L"GetLayeredWindowAttributes failed for layered window, skipping"); + return; + } + } + m_windowOriginalLayeredState[window] = state; + } + + // Clear WS_EX_LAYERED first to ensure SetLayeredWindowAttributes works + if (isCurrentlyLayered) + { + SetWindowLong(window, GWL_EXSTYLE, exStyle & ~WS_EX_LAYERED); + SetWindowPos(window, nullptr, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); + exStyle = GetWindowLong(window, GWL_EXSTYLE); + } + + BYTE alphaValue = static_cast((255 * percentage) / 100); + SetWindowLong(window, GWL_EXSTYLE, exStyle | WS_EX_LAYERED); + SetLayeredWindowAttributes(window, 0, alphaValue, LWA_ALPHA); +} + +void AlwaysOnTop::RestoreWindowAlpha(HWND window) +{ + if (!window || !IsWindow(window)) + { + return; + } + + LONG exStyle = GetWindowLong(window, GWL_EXSTYLE); + auto it = m_windowOriginalLayeredState.find(window); + + if (it != m_windowOriginalLayeredState.end()) + { + const auto& originalState = it->second; + + if (originalState.hadLayeredStyle) + { + // Window originally had WS_EX_LAYERED - restore original attributes + // Clear and re-add to ensure clean state + if (exStyle & WS_EX_LAYERED) + { + SetWindowLong(window, GWL_EXSTYLE, exStyle & ~WS_EX_LAYERED); + exStyle = GetWindowLong(window, GWL_EXSTYLE); + } + SetWindowLong(window, GWL_EXSTYLE, exStyle | WS_EX_LAYERED); + + // Restore original alpha and/or color key + DWORD flags = LWA_ALPHA; + if (originalState.usedColorKey) + { + flags |= LWA_COLORKEY; + } + SetLayeredWindowAttributes(window, originalState.colorKey, originalState.originalAlpha, flags); + SetWindowPos(window, nullptr, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); + } + else + { + // Window originally didn't have WS_EX_LAYERED - remove it completely + if (exStyle & WS_EX_LAYERED) + { + SetLayeredWindowAttributes(window, 0, 255, LWA_ALPHA); + SetWindowLong(window, GWL_EXSTYLE, exStyle & ~WS_EX_LAYERED); + SetWindowPos(window, nullptr, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); + } + } + + m_windowOriginalLayeredState.erase(it); + } + else + { + // Fallback: no cached state, just remove layered style + if (exStyle & WS_EX_LAYERED) + { + SetLayeredWindowAttributes(window, 0, 255, LWA_ALPHA); + SetWindowLong(window, GWL_EXSTYLE, exStyle & ~WS_EX_LAYERED); + } + } } \ No newline at end of file diff --git a/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.h b/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.h index 0505c837a2..438eaa64c4 100644 --- a/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.h +++ b/src/modules/alwaysontop/AlwaysOnTop/AlwaysOnTop.h @@ -10,6 +10,7 @@ #include #include +#include class AlwaysOnTop : public SettingsObserver { @@ -38,6 +39,8 @@ private: enum class HotkeyId : int { Pin = 1, + IncreaseOpacity = 2, + DecreaseOpacity = 3, }; static inline AlwaysOnTop* s_instance = nullptr; @@ -48,8 +51,20 @@ private: HWND m_window{ nullptr }; HINSTANCE m_hinstance; std::map> m_topmostWindows{}; + + // Store original window layered state for proper restoration + struct WindowLayeredState { + bool hadLayeredStyle = false; + BYTE originalAlpha = 255; + bool usedColorKey = false; + COLORREF colorKey = 0; + }; + std::map m_windowOriginalLayeredState{}; + HANDLE m_hPinEvent; HANDLE m_hTerminateEvent; + HANDLE m_hIncreaseOpacityEvent; + HANDLE m_hDecreaseOpacityEvent; DWORD m_mainThreadId; std::thread m_thread; const bool m_useCentralizedLLKH; @@ -78,6 +93,12 @@ private: bool AssignBorder(HWND window); void RefreshBorders(); + // Transparency methods + HWND ResolveTransparencyTargetWindow(HWND window); + void StepWindowTransparency(HWND window, int delta); + void ApplyWindowAlpha(HWND window, int percentage); + void RestoreWindowAlpha(HWND window); + virtual void SettingsUpdate(SettingId type) override; static void CALLBACK WinHookProc(HWINEVENTHOOK winEventHook, diff --git a/src/modules/alwaysontop/AlwaysOnTop/Settings.h b/src/modules/alwaysontop/AlwaysOnTop/Settings.h index 7b674c5cf0..9c0624298e 100644 --- a/src/modules/alwaysontop/AlwaysOnTop/Settings.h +++ b/src/modules/alwaysontop/AlwaysOnTop/Settings.h @@ -15,6 +15,9 @@ class SettingsObserver; struct Settings { PowerToysSettings::HotkeyObject hotkey = PowerToysSettings::HotkeyObject::from_settings(true, true, false, false, 84); // win + ctrl + T + static constexpr int minTransparencyPercentage = 20; // minimum transparency (can't go below 20%) + static constexpr int maxTransparencyPercentage = 100; // maximum (fully opaque) + static constexpr int transparencyStep = 10; // step size for +/- adjustment bool enableFrame = true; bool enableSound = true; bool roundCornersEnabled = true; diff --git a/src/modules/alwaysontop/AlwaysOnTop/Sound.h b/src/modules/alwaysontop/AlwaysOnTop/Sound.h index e8f8dd5de4..3bb868b179 100644 --- a/src/modules/alwaysontop/AlwaysOnTop/Sound.h +++ b/src/modules/alwaysontop/AlwaysOnTop/Sound.h @@ -2,7 +2,6 @@ #include "pch.h" -#include #include // sound class Sound @@ -12,12 +11,10 @@ public: { On, Off, + IncreaseOpacity, + DecreaseOpacity, }; - Sound() - : isPlaying(false) - {} - void Play(Type type) { BOOL success = false; @@ -29,6 +26,12 @@ public: case Type::Off: success = PlaySound(TEXT("Media\\Speech Sleep.wav"), NULL, SND_FILENAME | SND_ASYNC); break; + case Type::IncreaseOpacity: + success = PlaySound(TEXT("Media\\Windows Hardware Insert.wav"), NULL, SND_FILENAME | SND_ASYNC); + break; + case Type::DecreaseOpacity: + success = PlaySound(TEXT("Media\\Windows Hardware Remove.wav"), NULL, SND_FILENAME | SND_ASYNC); + break; default: break; } @@ -38,7 +41,4 @@ public: Logger::error(L"Sound playing error"); } } - -private: - std::atomic isPlaying; }; \ No newline at end of file diff --git a/src/modules/alwaysontop/AlwaysOnTopModuleInterface/dllmain.cpp b/src/modules/alwaysontop/AlwaysOnTopModuleInterface/dllmain.cpp index 1ed96e79bd..bc52137ed2 100644 --- a/src/modules/alwaysontop/AlwaysOnTopModuleInterface/dllmain.cpp +++ b/src/modules/alwaysontop/AlwaysOnTopModuleInterface/dllmain.cpp @@ -105,17 +105,28 @@ public: } } - virtual bool on_hotkey(size_t /*hotkeyId*/) override + virtual bool on_hotkey(size_t hotkeyId) override { if (m_enabled) { - Logger::trace(L"AlwaysOnTop hotkey pressed"); + Logger::trace(L"AlwaysOnTop hotkey pressed, id={}", hotkeyId); if (!is_process_running()) { Enable(); } - SetEvent(m_hPinEvent); + if (hotkeyId == 0) + { + SetEvent(m_hPinEvent); + } + else if (hotkeyId == 1) + { + SetEvent(m_hIncreaseOpacityEvent); + } + else if (hotkeyId == 2) + { + SetEvent(m_hDecreaseOpacityEvent); + } return true; } @@ -125,19 +136,48 @@ public: virtual size_t get_hotkeys(Hotkey* hotkeys, size_t buffer_size) override { + size_t count = 0; + + // Hotkey 0: Pin/Unpin (e.g., Win+Ctrl+T) if (m_hotkey.key) { - if (hotkeys && buffer_size >= 1) + if (hotkeys && buffer_size > count) { - hotkeys[0] = m_hotkey; + hotkeys[count] = m_hotkey; + Logger::trace(L"AlwaysOnTop hotkey[0]: win={}, ctrl={}, shift={}, alt={}, key={}", + m_hotkey.win, m_hotkey.ctrl, m_hotkey.shift, m_hotkey.alt, m_hotkey.key); } + count++; + } - return 1; - } - else + // Hotkey 1: Increase opacity (same modifiers + VK_OEM_PLUS '=') + if (m_hotkey.key) { - return 0; + if (hotkeys && buffer_size > count) + { + hotkeys[count] = m_hotkey; + hotkeys[count].key = VK_OEM_PLUS; // '=' key + Logger::trace(L"AlwaysOnTop hotkey[1] (increase opacity): win={}, ctrl={}, shift={}, alt={}, key={}", + hotkeys[count].win, hotkeys[count].ctrl, hotkeys[count].shift, hotkeys[count].alt, hotkeys[count].key); + } + count++; } + + // Hotkey 2: Decrease opacity (same modifiers + VK_OEM_MINUS '-') + if (m_hotkey.key) + { + if (hotkeys && buffer_size > count) + { + hotkeys[count] = m_hotkey; + hotkeys[count].key = VK_OEM_MINUS; // '-' key + Logger::trace(L"AlwaysOnTop hotkey[2] (decrease opacity): win={}, ctrl={}, shift={}, alt={}, key={}", + hotkeys[count].win, hotkeys[count].ctrl, hotkeys[count].shift, hotkeys[count].alt, hotkeys[count].key); + } + count++; + } + + Logger::trace(L"AlwaysOnTop get_hotkeys returning count={}", count); + return count; } // Enable the powertoy @@ -175,6 +215,8 @@ public: app_key = NonLocalizable::ModuleKey; m_hPinEvent = CreateDefaultEvent(CommonSharedConstants::ALWAYS_ON_TOP_PIN_EVENT); m_hTerminateEvent = CreateDefaultEvent(CommonSharedConstants::ALWAYS_ON_TOP_TERMINATE_EVENT); + m_hIncreaseOpacityEvent = CreateDefaultEvent(CommonSharedConstants::ALWAYS_ON_TOP_INCREASE_OPACITY_EVENT); + m_hDecreaseOpacityEvent = CreateDefaultEvent(CommonSharedConstants::ALWAYS_ON_TOP_DECREASE_OPACITY_EVENT); init_settings(); } @@ -292,6 +334,8 @@ private: // Handle to event used to pin/unpin windows HANDLE m_hPinEvent; HANDLE m_hTerminateEvent; + HANDLE m_hIncreaseOpacityEvent; + HANDLE m_hDecreaseOpacityEvent; }; extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create() diff --git a/src/modules/cmdpal/CommandPalette.slnf b/src/modules/cmdpal/CommandPalette.slnf index 6575a60790..c6ccbb7338 100644 --- a/src/modules/cmdpal/CommandPalette.slnf +++ b/src/modules/cmdpal/CommandPalette.slnf @@ -30,6 +30,7 @@ "src\\modules\\cmdpal\\Tests\\Microsoft.CmdPal.Ext.WindowWalker.UnitTests\\Microsoft.CmdPal.Ext.WindowWalker.UnitTests.csproj", "src\\modules\\cmdpal\\Tests\\Microsoft.CmdPal.UI.ViewModels.UnitTests\\Microsoft.CmdPal.UI.ViewModels.UnitTests.csproj", "src\\modules\\cmdpal\\Tests\\Microsoft.CmdPal.UITests\\Microsoft.CmdPal.UITests.csproj", + "src\\modules\\cmdpal\\Tests\\Microsoft.CommandPalette.Extensions.Toolkit.UnitTests\\Microsoft.CommandPalette.Extensions.Toolkit.UnitTests.csproj", "src\\modules\\cmdpal\\ext\\Microsoft.CmdPal.Ext.Apps\\Microsoft.CmdPal.Ext.Apps.csproj", "src\\modules\\cmdpal\\ext\\Microsoft.CmdPal.Ext.Bookmark\\Microsoft.CmdPal.Ext.Bookmarks.csproj", "src\\modules\\cmdpal\\ext\\Microsoft.CmdPal.Ext.Calc\\Microsoft.CmdPal.Ext.Calc.csproj", diff --git a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/BatchUpdateManager.cs b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/BatchUpdateManager.cs new file mode 100644 index 0000000000..52d6091bd8 --- /dev/null +++ b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/BatchUpdateManager.cs @@ -0,0 +1,136 @@ +// 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.Collections.Concurrent; +using Microsoft.CmdPal.Core.Common; +using Microsoft.CmdPal.Core.Common.Helpers; + +namespace Microsoft.CmdPal.Core.ViewModels; + +internal static class BatchUpdateManager +{ + private const int ExpectedBatchSize = 32; + + // 30 ms chosen empirically to balance responsiveness and batching: + // - Keeps perceived latency low (< ~50 ms) for user-visible updates. + // - Still allows multiple COM/background events to be coalesced into a single batch. + private static readonly TimeSpan BatchDelay = TimeSpan.FromMilliseconds(30); + private static readonly ConcurrentQueue DirtyQueue = []; + private static readonly Timer Timer = new(static _ => Flush(), null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); + + private static InterlockedBoolean _isFlushScheduled; + + /// + /// Enqueue a target for batched processing. Safe to call from any thread (including COM callbacks). + /// + public static void Queue(IBatchUpdateTarget target) + { + if (!target.TryMarkBatchQueued()) + { + return; // already queued in current batch window + } + + DirtyQueue.Enqueue(target); + TryScheduleFlush(); + } + + private static void TryScheduleFlush() + { + if (!_isFlushScheduled.Set()) + { + return; + } + + if (DirtyQueue.IsEmpty) + { + _isFlushScheduled.Clear(); + + if (DirtyQueue.IsEmpty) + { + return; + } + + if (!_isFlushScheduled.Set()) + { + return; + } + } + + try + { + Timer.Change(BatchDelay, Timeout.InfiniteTimeSpan); + } + catch (Exception ex) + { + _isFlushScheduled.Clear(); + CoreLogger.LogError("Failed to arm batch timer.", ex); + } + } + + private static void Flush() + { + try + { + var drained = new List(ExpectedBatchSize); + while (DirtyQueue.TryDequeue(out var item)) + { + drained.Add(item); + } + + if (drained.Count == 0) + { + return; + } + + // LOAD BEARING: + // ApplyPendingUpdates must run on a background thread. + // The VM itself is responsible for marshaling UI notifications to its _uiScheduler. + ApplyBatch(drained); + } + catch (Exception ex) + { + // Don't kill the timer thread. + CoreLogger.LogError("Batch flush failed.", ex); + } + finally + { + _isFlushScheduled.Clear(); + TryScheduleFlush(); + } + } + + private static void ApplyBatch(List items) + { + // Runs on the Timer callback thread (ThreadPool). That's fine: background work only. + foreach (var item in items) + { + // Allow re-queueing immediately if more COM events arrive during apply. + item.ClearBatchQueued(); + + try + { + item.ApplyPendingUpdates(); + } + catch (Exception ex) + { + CoreLogger.LogError("Failed to apply pending updates for a batched target.", ex); + } + } + } +} + +internal interface IBatchUpdateTarget +{ + /// UI scheduler (used by targets internally for UI marshaling). Kept here for diagnostics / consistency. + TaskScheduler UIScheduler { get; } + + /// Apply any coalesced updates. Must be safe to call on a background thread. + void ApplyPendingUpdates(); + + /// De-dupe gate: returns true only for the first enqueue until cleared. + bool TryMarkBatchQueued(); + + /// Clear the de-dupe gate so the item can be queued again. + void ClearBatchQueued(); +} diff --git a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/ExtensionObjectViewModel.cs b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/ExtensionObjectViewModel.cs index e7bed46db6..53af586431 100644 --- a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/ExtensionObjectViewModel.cs +++ b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/ExtensionObjectViewModel.cs @@ -2,36 +2,99 @@ // 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.Buffers; +using System.Collections.Concurrent; +using System.ComponentModel; +using System.Runtime.CompilerServices; using CommunityToolkit.Mvvm.ComponentModel; using Microsoft.CmdPal.Core.Common; +using Microsoft.CmdPal.Core.Common.Helpers; namespace Microsoft.CmdPal.Core.ViewModels; -public abstract partial class ExtensionObjectViewModel : ObservableObject +public abstract partial class ExtensionObjectViewModel : ObservableObject, IBatchUpdateTarget, IBackgroundPropertyChangedNotification { - public WeakReference PageContext { get; set; } + private const int InitialPropertyBatchingBufferSize = 16; - internal ExtensionObjectViewModel(IPageContext? context) - { - var realContext = context ?? (this is IPageContext c ? c : throw new ArgumentException("You need to pass in an IErrorContext")); - PageContext = new(realContext); - } + // Raised on the background thread before UI notifications. It's raised on the background thread to prevent + // blocking the COM proxy. + public event PropertyChangedEventHandler? PropertyChangedBackground; - internal ExtensionObjectViewModel(WeakReference context) - { - PageContext = context; - } + private readonly ConcurrentQueue _pendingProps = []; - public async virtual Task InitializePropertiesAsync() + private readonly TaskScheduler _uiScheduler; + + private InterlockedBoolean _batchQueued; + + public WeakReference PageContext { get; private set; } = null!; + + TaskScheduler IBatchUpdateTarget.UIScheduler => _uiScheduler; + + void IBatchUpdateTarget.ApplyPendingUpdates() => ApplyPendingUpdates(); + + bool IBatchUpdateTarget.TryMarkBatchQueued() => _batchQueued.Set(); + + void IBatchUpdateTarget.ClearBatchQueued() => _batchQueued.Clear(); + + private protected ExtensionObjectViewModel(TaskScheduler scheduler) { - var t = new Task(() => + if (this is not IPageContext) { - SafeInitializePropertiesSynchronous(); - }); - t.Start(); - await t; + throw new InvalidOperationException($"Constructor overload without IPageContext can only be used when the derived class implements IPageContext. Type: {GetType().FullName}"); + } + + _uiScheduler = scheduler ?? throw new ArgumentNullException(nameof(scheduler)); + + // Defer PageContext assignment - derived constructor MUST call InitializePageContext() + // or we set it lazily on first access } + private protected ExtensionObjectViewModel(IPageContext context) + { + ArgumentNullException.ThrowIfNull(context); + + PageContext = new WeakReference(context); + _uiScheduler = context.Scheduler; + + LogIfDefaultScheduler(); + } + + private protected ExtensionObjectViewModel(WeakReference contextRef) + { + ArgumentNullException.ThrowIfNull(contextRef); + + if (!contextRef.TryGetTarget(out var context)) + { + throw new ArgumentException("IPageContext must be alive when creating view models.", nameof(contextRef)); + } + + PageContext = contextRef; + _uiScheduler = context.Scheduler; + + LogIfDefaultScheduler(); + } + + protected void InitializeSelfAsPageContext() + { + if (this is not IPageContext self) + { + throw new InvalidOperationException("This method can only be called when the class implements IPageContext."); + } + + PageContext = new WeakReference(self); + } + + private void LogIfDefaultScheduler() + { + if (_uiScheduler == TaskScheduler.Default) + { + CoreLogger.LogDebug($"ExtensionObjectViewModel created with TaskScheduler.Default. Type: {GetType().FullName}"); + } + } + + public virtual Task InitializePropertiesAsync() + => Task.Run(SafeInitializePropertiesSynchronous); + public void SafeInitializePropertiesSynchronous() { try @@ -46,49 +109,151 @@ public abstract partial class ExtensionObjectViewModel : ObservableObject public abstract void InitializeProperties(); - protected void UpdateProperty(string propertyName) - { - DoOnUiThread(() => OnPropertyChanged(propertyName)); - } + protected void UpdateProperty(string propertyName) => MarkPropertyDirty(propertyName); protected void UpdateProperty(string propertyName1, string propertyName2) { - DoOnUiThread(() => - { - OnPropertyChanged(propertyName1); - OnPropertyChanged(propertyName2); - }); - } - - protected void UpdateProperty(string propertyName1, string propertyName2, string propertyName3) - { - DoOnUiThread(() => - { - OnPropertyChanged(propertyName1); - OnPropertyChanged(propertyName2); - OnPropertyChanged(propertyName3); - }); + MarkPropertyDirty(propertyName1); + MarkPropertyDirty(propertyName2); } protected void UpdateProperty(params string[] propertyNames) { - DoOnUiThread(() => + foreach (var p in propertyNames) { - foreach (var propertyName in propertyNames) - { - OnPropertyChanged(propertyName); - } - }); + MarkPropertyDirty(p); + } } + internal void MarkPropertyDirty(string? propertyName) + { + if (string.IsNullOrEmpty(propertyName)) + { + return; + } + + // We should re-consider if this worth deduping + _pendingProps.Enqueue(propertyName); + BatchUpdateManager.Queue(this); + } + + public void ApplyPendingUpdates() + { + ((IBatchUpdateTarget)this).ClearBatchQueued(); + + var buffer = ArrayPool.Shared.Rent(InitialPropertyBatchingBufferSize); + var count = 0; + var transferred = false; + + try + { + while (_pendingProps.TryDequeue(out var name)) + { + if (count == buffer.Length) + { + var bigger = ArrayPool.Shared.Rent(buffer.Length * 2); + Array.Copy(buffer, bigger, buffer.Length); + ArrayPool.Shared.Return(buffer, clearArray: true); + buffer = bigger; + } + + buffer[count++] = name; + } + + if (count == 0) + { + return; + } + + // 1) Background subscribers (must be raised before UI notifications). + var propertyChangedEventHandler = PropertyChangedBackground; + if (propertyChangedEventHandler is not null) + { + RaiseBackground(propertyChangedEventHandler, this, buffer, count); + } + + // 2) UI-facing PropertyChanged: ALWAYS marshal to UI scheduler. + // Hand-off pooled buffer to UI task (UI task returns it). + // + // It would be lovely to do nothing if no one is actually listening on PropertyChanged, + // but ObservableObject doesn't expose that information. + _ = Task.Factory.StartNew( + static state => + { + var p = (UiBatch)state!; + try + { + p.Owner.RaiseUi(p.Names, p.Count); + } + catch (Exception ex) + { + CoreLogger.LogError("Failed to raise property change notifications on UI thread.", ex); + } + finally + { + ArrayPool.Shared.Return(p.Names, clearArray: true); + } + }, + new UiBatch(this, buffer, count), + CancellationToken.None, + TaskCreationOptions.DenyChildAttach, + _uiScheduler); + + transferred = true; + } + catch (Exception ex) + { + CoreLogger.LogError("Failed to apply pending property updates.", ex); + } + finally + { + if (!transferred) + { + ArrayPool.Shared.Return(buffer, clearArray: true); + } + } + } + + private void RaiseUi(string[] names, int count) + { + for (var i = 0; i < count; i++) + { + OnPropertyChanged(Args(names[i])); + } + } + + private static void RaiseBackground(PropertyChangedEventHandler handlers, object sender, string[] names, int count) + { + try + { + for (var i = 0; i < count; i++) + { + handlers(sender, Args(names[i])); + } + } + catch (Exception ex) + { + CoreLogger.LogError("Failed to raise PropertyChangedBackground notifications.", ex); + } + } + + private sealed record UiBatch(ExtensionObjectViewModel Owner, string[] Names, int Count); + protected void ShowException(Exception ex, string? extensionHint = null) { if (PageContext.TryGetTarget(out var pageContext)) { pageContext.ShowException(ex, extensionHint); } + else + { + CoreLogger.LogError("Failed to show exception because PageContext is no longer available.", ex); + } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static PropertyChangedEventArgs Args(string name) => new(name); + protected void DoOnUiThread(Action action) { if (PageContext.TryGetTarget(out var pageContext)) diff --git a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/IBackgroundPropertyChangedNotification.cs b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/IBackgroundPropertyChangedNotification.cs new file mode 100644 index 0000000000..4db157f46d --- /dev/null +++ b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/IBackgroundPropertyChangedNotification.cs @@ -0,0 +1,19 @@ +// 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; + +namespace Microsoft.CmdPal.Core.ViewModels; + +/// +/// Provides a notification mechanism for property changes that fires +/// synchronously on the calling thread. +/// +public interface IBackgroundPropertyChangedNotification +{ + /// + /// Occurs when the value of a property changes. + /// + event PropertyChangedEventHandler? PropertyChangedBackground; +} diff --git a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Microsoft.CmdPal.Core.ViewModels.csproj b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Microsoft.CmdPal.Core.ViewModels.csproj index 4ace6c5783..6e1b224ecd 100644 --- a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Microsoft.CmdPal.Core.ViewModels.csproj +++ b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Microsoft.CmdPal.Core.ViewModels.csproj @@ -1,5 +1,9 @@  + + + false + diff --git a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/PageViewModel.cs b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/PageViewModel.cs index 2a82f80a02..3b08b9266b 100644 --- a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/PageViewModel.cs +++ b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/PageViewModel.cs @@ -77,11 +77,11 @@ public partial class PageViewModel : ExtensionObjectViewModel, IPageContext public IconInfoViewModel Icon { get; protected set; } public PageViewModel(IPage? model, TaskScheduler scheduler, AppExtensionHost extensionHost) - : base((IPageContext?)null) + : base(scheduler) { + InitializeSelfAsPageContext(); _pageModel = new(model); Scheduler = scheduler; - PageContext = new(this); ExtensionHost = extensionHost; Icon = new(null); diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/TopLevelViewModel.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/TopLevelViewModel.cs index 6d7f830658..cc863fe362 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/TopLevelViewModel.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/TopLevelViewModel.cs @@ -199,7 +199,7 @@ public sealed partial class TopLevelViewModel : ObservableObject, IListItem, IEx _fallbackId = fallback.Id; } - item.PropertyChanged += Item_PropertyChanged; + item.PropertyChangedBackground += Item_PropertyChanged; // UpdateAlias(); // UpdateHotkey(); diff --git a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/CloseOnEnterTests.cs b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/CloseOnEnterTests.cs index 5c4cf39783..04771fc621 100644 --- a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/CloseOnEnterTests.cs +++ b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/CloseOnEnterTests.cs @@ -19,6 +19,7 @@ public class CloseOnEnterTests { var settings = new Settings(closeOnEnter: true); TypedEventHandler handleSave = (s, e) => { }; + TypedEventHandler handleReplace = (s, e) => { }; var item = ResultHelper.CreateResult( 4m, @@ -26,7 +27,8 @@ public class CloseOnEnterTests CultureInfo.CurrentCulture, "2+2", settings, - handleSave); + handleSave, + handleReplace); Assert.IsNotNull(item); Assert.IsInstanceOfType(item.Command, typeof(CopyTextCommand)); @@ -41,6 +43,7 @@ public class CloseOnEnterTests { var settings = new Settings(closeOnEnter: false); TypedEventHandler handleSave = (s, e) => { }; + TypedEventHandler handleReplace = (s, e) => { }; var item = ResultHelper.CreateResult( 4m, @@ -48,7 +51,8 @@ public class CloseOnEnterTests CultureInfo.CurrentCulture, "2+2", settings, - handleSave); + handleSave, + handleReplace); Assert.IsNotNull(item); Assert.IsInstanceOfType(item.Command, typeof(SaveCommand)); diff --git a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/ExtendedCalculatorParserTests.cs b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/ExtendedCalculatorParserTests.cs index b631f59be7..85b712fe20 100644 --- a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/ExtendedCalculatorParserTests.cs +++ b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/ExtendedCalculatorParserTests.cs @@ -65,6 +65,9 @@ public class ExtendedCalculatorParserTests : CommandPaletteUnitTestBase ["log10(3)", 0.47712125471966M], ["ln(e)", 1M], ["cosh(0)", 1M], + ["1*10^(-5)", 0.00001M], + ["1*10^(-15)", 0.0000000000000001M], + ["1*10^(-16)", 0M], ]; [DataTestMethod] @@ -192,9 +195,11 @@ public class ExtendedCalculatorParserTests : CommandPaletteUnitTestBase private static IEnumerable Interpret_MustReturnExpectedResult_WhenCalled_Data => [ - // ["factorial(5)", 120M], ToDo: this don't support now - // ["sign(-2)", -1M], - // ["sign(2)", +1M], + ["factorial(5)", 120M], + ["5!", 120M], + ["(2+3)!", 120M], + ["sign(-2)", -1M], + ["sign(2)", +1M], ["abs(-2)", 2M], ["abs(2)", 2M], ["0+(1*2)/(0+1)", 2M], // Validate that division by "(0+1)" is not interpret as division by zero. @@ -221,6 +226,9 @@ public class ExtendedCalculatorParserTests : CommandPaletteUnitTestBase [ ["0.2E1", "en-US", 2M], ["0,2E1", "pt-PT", 2M], + ["3.5e3 + 2.5E2", "en-US", 3750M], + ["3,5e3 + 2,5E2", "fr-FR", 3750M], + ["1E3-1E3/1.5", "en-US", 333.333333333333371M], ]; [DataTestMethod] @@ -389,4 +397,17 @@ public class ExtendedCalculatorParserTests : CommandPaletteUnitTestBase Assert.IsNotNull(result); Assert.AreEqual(expectedResult, result); } + + [DataTestMethod] + [DataRow("171!")] + [DataRow("1000!")] + public void Interpret_ReturnsError_WhenValueOverflowsDecimal(string input) + { + var settings = new Settings(); + + CalculateEngine.Interpret(settings, input, CultureInfo.InvariantCulture, out var error); + + Assert.IsFalse(string.IsNullOrEmpty(error)); + Assert.AreNotEqual(null, error); + } } diff --git a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/IncompleteQueryTests.cs b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/IncompleteQueryTests.cs new file mode 100644 index 0000000000..894660ade0 --- /dev/null +++ b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/IncompleteQueryTests.cs @@ -0,0 +1,38 @@ +// 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 Microsoft.CmdPal.Ext.Calc.Helper; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Microsoft.CmdPal.Ext.Calc.UnitTests; + +[TestClass] +public class IncompleteQueryTests +{ + [DataTestMethod] + [DataRow("2+2+", "2+2")] + [DataRow("2+2*", "2+2")] + [DataRow("sin(30", "sin(30)")] + [DataRow("((1+2)", "((1+2))")] + [DataRow("2*(3+4", "2*(3+4)")] + [DataRow("(1+2", "(1+2)")] + [DataRow("2*(", "2")] + [DataRow("2*(((", "2")] + public void TestTryGetIncompleteQuerySuccess(string input, string expected) + { + var result = QueryHelper.TryGetIncompleteQuery(input, out var newQuery); + Assert.IsTrue(result); + Assert.AreEqual(expected, newQuery); + } + + [DataTestMethod] + [DataRow("")] + [DataRow(" ")] + public void TestTryGetIncompleteQueryFail(string input) + { + var result = QueryHelper.TryGetIncompleteQuery(input, out var newQuery); + Assert.IsFalse(result); + Assert.AreEqual(input, newQuery); + } +} diff --git a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/QueryHelperTests.cs b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/QueryHelperTests.cs new file mode 100644 index 0000000000..c152dd1f45 --- /dev/null +++ b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/QueryHelperTests.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 Microsoft.CmdPal.Ext.Calc.Helper; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Microsoft.CmdPal.Ext.Calc.UnitTests; + +[TestClass] +public class QueryHelperTests +{ + [DataTestMethod] + [DataRow("2²", "4")] + [DataRow("2³", "8")] + [DataRow("2!", "2")] + [DataRow("2\u00A0*\u00A02", "4")] // Non-breaking space + [DataRow("20:10", "2")] // Colon as division + public void Interpret_HandlesNormalizedInputs(string input, string expected) + { + var settings = new Settings(); + var result = QueryHelper.Query(input, settings, false, out _, (_, _) => { }); + + Assert.IsNotNull(result); + Assert.AreEqual(expected, result.Title); + } +} diff --git a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/QueryTests.cs b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/QueryTests.cs index 73927849a1..fa8a441d43 100644 --- a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/QueryTests.cs +++ b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/QueryTests.cs @@ -6,7 +6,6 @@ using System.Linq; using Microsoft.CmdPal.Ext.Calc.Helper; using Microsoft.CmdPal.Ext.Calc.Pages; using Microsoft.CmdPal.Ext.UnitTestBase; -using Microsoft.CommandPalette.Extensions.Toolkit; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Microsoft.CmdPal.Ext.Calc.UnitTests; @@ -72,7 +71,7 @@ public class QueryTests : CommandPaletteUnitTestBase [DataRow("sin(60)", "0.809016", CalculateEngine.TrigMode.Gradians)] public void TrigModeSettingsTest(string input, string expected, CalculateEngine.TrigMode trigMode) { - var settings = new Settings(trigUnit: trigMode); + var settings = new Settings(trigUnit: trigMode, outputUseEnglishFormat: true); var page = new CalculatorListPage(settings); diff --git a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/Settings.cs b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/Settings.cs index ccd231767d..767b040fe4 100644 --- a/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/Settings.cs +++ b/src/modules/cmdpal/Tests/Microsoft.CmdPal.Ext.Calc.UnitTests/Settings.cs @@ -12,17 +12,26 @@ public class Settings : ISettingsInterface private readonly bool inputUseEnglishFormat; private readonly bool outputUseEnglishFormat; private readonly bool closeOnEnter; + private readonly bool copyResultToSearchBarIfQueryEndsWithEqualSign; + private readonly bool autoFixQuery; + private readonly bool inputNormalization; public Settings( CalculateEngine.TrigMode trigUnit = CalculateEngine.TrigMode.Radians, bool inputUseEnglishFormat = false, bool outputUseEnglishFormat = false, - bool closeOnEnter = true) + bool closeOnEnter = true, + bool copyResultToSearchBarIfQueryEndsWithEqualSign = true, + bool autoFixQuery = true, + bool inputNormalization = true) { this.trigUnit = trigUnit; this.inputUseEnglishFormat = inputUseEnglishFormat; this.outputUseEnglishFormat = outputUseEnglishFormat; this.closeOnEnter = closeOnEnter; + this.copyResultToSearchBarIfQueryEndsWithEqualSign = copyResultToSearchBarIfQueryEndsWithEqualSign; + this.autoFixQuery = autoFixQuery; + this.inputNormalization = inputNormalization; } public CalculateEngine.TrigMode TrigUnit => trigUnit; @@ -32,4 +41,10 @@ public class Settings : ISettingsInterface public bool OutputUseEnglishFormat => outputUseEnglishFormat; public bool CloseOnEnter => closeOnEnter; + + public bool CopyResultToSearchBarIfQueryEndsWithEqualSign => copyResultToSearchBarIfQueryEndsWithEqualSign; + + public bool AutoFixQuery => autoFixQuery; + + public bool InputNormalization => inputNormalization; } diff --git a/src/modules/cmdpal/Tests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests/FuzzyMatcherComparisonTests.cs b/src/modules/cmdpal/Tests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests/FuzzyMatcherComparisonTests.cs new file mode 100644 index 0000000000..11c3113dac --- /dev/null +++ b/src/modules/cmdpal/Tests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests/FuzzyMatcherComparisonTests.cs @@ -0,0 +1,235 @@ +// 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.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit.UnitTests.Legacy; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Microsoft.CommandPalette.Extensions.Toolkit.UnitTests; + +[TestClass] +public class FuzzyMatcherComparisonTests +{ + public static IEnumerable TestData => + [ + ["a", "a"], + ["a", "A"], + ["A", "a"], + ["abc", "abc"], + ["abc", "axbycz"], + ["abc", "abxcyz"], + ["sln", "solution.sln"], + ["vs", "visualstudio"], + ["test", "Test"], + ["pt", "PowerToys"], + ["p/t", "power\\toys"], + ["p\\t", "power/toys"], + ["c/w", "c:\\windows"], + ["foo", "bar"], + ["verylongstringthatdoesnotmatch", "short"], + [string.Empty, "anything"], + ["something", string.Empty], + ["git", "git"], + ["em", "Emmy"], + ["my", "Emmy"], + ["word", "word"], + ["wd", "word"], + ["w d", "word"], + ["a", "ba"], + ["a", "ab"], + ["a", "bab"], + ["z", "abcdefg"], + ["CC", "CamelCase"], + ["cc", "camelCase"], + ["cC", "camelCase"], + ["some", "awesome"], + ["some", "somewhere"], + ["1", "1"], + ["1", "2"], + [".", "."], + ["f.t", "file.txt"], + ["excel", "Excel"], + ["Excel", "excel"], + ["PowerPoint", "Power Point"], + ["power point", "PowerPoint"], + ["visual studio code", "Visual Studio Code"], + ["vsc", "Visual Studio Code"], + ["code", "Visual Studio Code"], + ["vs code", "Visual Studio Code"], + ["word", "Microsoft Word"], + ["ms word", "Microsoft Word"], + ["browser", "Internet Explorer"], + ["chrome", "Google Chrome"], + ["edge", "Microsoft Edge"], + ["term", "Windows Terminal"], + ["cmd", "Command Prompt"], + ["calc", "Calculator"], + ["snipping", "Snipping Tool"], + ["note", "Notepad"], + ["file expl", "File Explorer"], + ["settings", "Settings"], + ["p t", "PowerToys"], + ["p t", "PowerToys"], + [" v ", " Visual Studio "], + [" a b ", " a b c d "], + [string.Empty, string.Empty], + [" ", " "], + [" ", " "], + [" ", "abc"], + ["abc", " "], + [" ", " "], + [" ", " a b "], + ["sh", "ShangHai"], + ["bj", "BeiJing"], + ["bj", "北京"], + ["sh", "上海"], + ["nh", "你好"], + ["bj", "Beijing"], + ["hello", "你好"], + ["nihao", "你好"], + ["rmb", "人民币"], + ["zwr", "中文"], + ["zw", "中文"], + ["fbr", "foobar"], + ["w11", "windows 11"], + ["pwr", "powershell"], + ["vm", "void main"], + ["ps", "PowerShell"], + ["az", "Azure"], + ["od", "onedrive"], + ["gc", "google chrome"], + ["ff", "firefox"], + ["fs", "file_system"], + ["pt", "power-toys"], + ["jt", "json.test"], + ["ps", "power shell"], + ["ps", "power'shell"], + ["ps", "power\"shell"], + ["hw", "hello:world"], + ["abc", "a_b_c"], + ["abc", "a-b-c"], + ["abc", "a.b.c"], + ["abc", "a b c"], + ["abc", "a'b'c"], + ["abc", "a\"b\"c"], + ["abc", "a:b:c"], + ["_a", "_a"], + ["a_", "a_"], + ["-a", "-a"], + ["a-", "a-"] + ]; + + [TestMethod] + [DynamicData(nameof(TestData))] + public void CompareScores(string needle, string haystack) + { + var legacyScore = LegacyFuzzyStringMatcher.ScoreFuzzy(needle, haystack); + var newScore = FuzzyStringMatcher.ScoreFuzzy(needle, haystack); + + Assert.AreEqual(legacyScore, newScore, $"Score mismatch for needle='{needle}', haystack='{haystack}'"); + } + + [TestMethod] + [DynamicData(nameof(TestData))] + public void ComparePositions(string needle, string haystack) + { + var (legacyScore, legacyPos) = LegacyFuzzyStringMatcher.ScoreFuzzyWithPositions(needle, haystack, true); + var (newScore, newPos) = FuzzyStringMatcher.ScoreFuzzyWithPositions(needle, haystack, true); + + Assert.AreEqual(legacyScore, newScore, $"Score mismatch (with pos) for needle='{needle}', haystack='{haystack}'"); + + // Ensure lists are not null + legacyPos ??= []; + newPos ??= []; + + // Compare list contents + var legacyPosStr = string.Join(',', legacyPos); + var newPosStr = string.Join(',', newPos); + + Assert.AreEqual(legacyPos.Count, newPos.Count, $"Position count mismatch: Legacy=[{legacyPosStr}], New=[{newPosStr}]"); + + for (var i = 0; i < legacyPos.Count; i++) + { + Assert.AreEqual(legacyPos[i], newPos[i], $"Position mismatch at index {i}: Legacy=[{legacyPosStr}], New=[{newPosStr}]"); + } + } + + [TestMethod] + [DynamicData(nameof(TestData))] + public void CompareScores_ContiguousOnly(string needle, string haystack) + { + var legacyScore = LegacyFuzzyStringMatcher.ScoreFuzzy(needle, haystack, allowNonContiguousMatches: false); + var newScore = FuzzyStringMatcher.ScoreFuzzy(needle, haystack, allowNonContiguousMatches: false); + + Assert.AreEqual(legacyScore, newScore, $"Score mismatch (contiguous only) for needle='{needle}', haystack='{haystack}'"); + } + + [TestMethod] + [DynamicData(nameof(TestData))] + public void CompareScores_PinyinEnabled(string needle, string haystack) + { + var originalNew = FuzzyStringMatcher.ChinesePinYinSupport; + var originalLegacy = LegacyFuzzyStringMatcher.ChinesePinYinSupport; + try + { + FuzzyStringMatcher.ChinesePinYinSupport = true; + LegacyFuzzyStringMatcher.ChinesePinYinSupport = true; + + var legacyScore = LegacyFuzzyStringMatcher.ScoreFuzzy(needle, haystack); + var newScore = FuzzyStringMatcher.ScoreFuzzy(needle, haystack); + + Assert.AreEqual(legacyScore, newScore, $"Score mismatch (Pinyin enabled) for needle='{needle}', haystack='{haystack}'"); + } + finally + { + FuzzyStringMatcher.ChinesePinYinSupport = originalNew; + LegacyFuzzyStringMatcher.ChinesePinYinSupport = originalLegacy; + } + } + + [TestMethod] + [DynamicData(nameof(TestData))] + public void ComparePositions_PinyinEnabled(string needle, string haystack) + { + var originalNew = FuzzyStringMatcher.ChinesePinYinSupport; + var originalLegacy = LegacyFuzzyStringMatcher.ChinesePinYinSupport; + try + { + FuzzyStringMatcher.ChinesePinYinSupport = true; + LegacyFuzzyStringMatcher.ChinesePinYinSupport = true; + + var (legacyScore, legacyPos) = LegacyFuzzyStringMatcher.ScoreFuzzyWithPositions(needle, haystack, true); + var (newScore, newPos) = FuzzyStringMatcher.ScoreFuzzyWithPositions(needle, haystack, true); + + Assert.AreEqual(legacyScore, newScore, $"Score mismatch (with pos, Pinyin enabled) for needle='{needle}', haystack='{haystack}'"); + + // Ensure lists are not null + legacyPos ??= []; + newPos ??= []; + + // If newPos is empty but newScore > 0, it means it's a secondary match (like Pinyin) + // which we don't return positions for in the new matcher. + if (newScore > 0 && newPos.Count == 0 && legacyPos.Count > 0) + { + return; + } + + // Compare list contents + var legacyPosStr = string.Join(',', legacyPos); + var newPosStr = string.Join(',', newPos); + + Assert.AreEqual(legacyPos.Count, newPos.Count, $"Position count mismatch: Legacy=[{legacyPosStr}], New=[{newPosStr}]"); + + for (var i = 0; i < legacyPos.Count; i++) + { + Assert.AreEqual(legacyPos[i], newPos[i], $"Position mismatch at index {i}: Legacy=[{legacyPosStr}], New=[{newPosStr}]"); + } + } + finally + { + FuzzyStringMatcher.ChinesePinYinSupport = originalNew; + LegacyFuzzyStringMatcher.ChinesePinYinSupport = originalLegacy; + } + } +} diff --git a/src/modules/cmdpal/Tests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests/FuzzyMatcherDiacriticsTests.cs b/src/modules/cmdpal/Tests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests/FuzzyMatcherDiacriticsTests.cs new file mode 100644 index 0000000000..d4b6b8614f --- /dev/null +++ b/src/modules/cmdpal/Tests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests/FuzzyMatcherDiacriticsTests.cs @@ -0,0 +1,85 @@ +// 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 Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Microsoft.CommandPalette.Extensions.Toolkit.UnitTests; + +[TestClass] +public class FuzzyMatcherDiacriticsTests +{ + [TestMethod] + public void ScoreFuzzy_WithDiacriticsRemoval_MatchesWithDiacritics() + { + // "eco" should match "école" when diacritics are removed (é -> E) + var score = FuzzyStringMatcher.ScoreFuzzy("eco", "école", allowNonContiguousMatches: true, removeDiacritics: true); + Assert.IsTrue(score > 0, "Should match 'école' with 'eco' when diacritics are removed"); + + // "uber" should match "über" + score = FuzzyStringMatcher.ScoreFuzzy("uber", "über", allowNonContiguousMatches: true, removeDiacritics: true); + Assert.IsTrue(score > 0, "Should match 'über' with 'uber' when diacritics are removed"); + } + + [TestMethod] + public void ScoreFuzzy_WithoutDiacriticsRemoval_DoesNotMatchWhenCharactersDiffer() + { + // "eco" should NOT match "école" if 'é' is treated as distinct from 'e' and order is strict + // 'é' (index 0) != 'e'. 'e' (index 4) is after 'c' (index 1) and 'o' (index 2). + // Since needle is "e-c-o", to match "école": + // 'e' matches 'e' at 4. + // 'c' must show up after. No. + // So no match. + var score = FuzzyStringMatcher.ScoreFuzzy("eco", "école", allowNonContiguousMatches: true, removeDiacritics: false); + Assert.AreEqual(0, score, "Should not match 'école' with 'eco' when diacritics are NOT removed"); + + // "uber" vs "über" + // u != ü. + // b (index 1) match b (index 2). e (2) match e (3). r (3) match r (4). + // but 'u' has no match. + score = FuzzyStringMatcher.ScoreFuzzy("uber", "über", allowNonContiguousMatches: true, removeDiacritics: false); + Assert.AreEqual(0, score, "Should not match 'über' with 'uber' when diacritics are NOT removed"); + } + + [TestMethod] + public void ScoreFuzzy_DefaultRemovesDiacritics() + { + // Now default is true, so "eco" vs "école" should match + var score = FuzzyStringMatcher.ScoreFuzzy("eco", "école"); + Assert.IsTrue(score > 0, "Default should remove diacritics and match 'école'"); + } + + [DataTestMethod] + [DataRow("a", "à", true)] + [DataRow("e", "é", true)] + [DataRow("i", "ï", true)] + [DataRow("o", "ô", true)] + [DataRow("u", "ü", true)] + [DataRow("c", "ç", true)] + [DataRow("n", "ñ", true)] + [DataRow("s", "ß", false)] // ß doesn't strip to s via simple invalid-uppercasing + public void VerifySpecificCharacters(string needle, string haystack, bool expectingMatch) + { + var score = FuzzyStringMatcher.ScoreFuzzy(needle, haystack, allowNonContiguousMatches: true, removeDiacritics: true); + if (expectingMatch) + { + Assert.IsTrue(score > 0, $"Expected match for '{needle}' in '{haystack}' with diacritics removal"); + } + else + { + Assert.AreEqual(0, score, $"Expected NO match for '{needle}' in '{haystack}' even with diacritics removal"); + } + } + + [TestMethod] + public void VerifyBothPathsWorkSameForASCII() + { + var needle = "test"; + var haystack = "TestString"; + + var score1 = FuzzyStringMatcher.ScoreFuzzy(needle, haystack, allowNonContiguousMatches: true, removeDiacritics: true); + var score2 = FuzzyStringMatcher.ScoreFuzzy(needle, haystack, allowNonContiguousMatches: true, removeDiacritics: false); + + Assert.AreEqual(score1, score2, "Scores should be identical for ASCII strings regardless of diacritics setting"); + } +} diff --git a/src/modules/cmdpal/Tests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests/FuzzyMatcherPinyinLogicTests.cs b/src/modules/cmdpal/Tests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests/FuzzyMatcherPinyinLogicTests.cs new file mode 100644 index 0000000000..8898fe5035 --- /dev/null +++ b/src/modules/cmdpal/Tests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests/FuzzyMatcherPinyinLogicTests.cs @@ -0,0 +1,46 @@ +// 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 Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Microsoft.CommandPalette.Extensions.Toolkit.UnitTests; + +[TestClass] +public class FuzzyMatcherPinyinLogicTests +{ + [TestInitialize] + public void Setup() + { + FuzzyStringMatcher.ChinesePinYinSupport = true; + FuzzyStringMatcher.ClearCache(); + } + + [TestCleanup] + public void Cleanup() + { + FuzzyStringMatcher.ChinesePinYinSupport = false; // Reset to default state + FuzzyStringMatcher.ClearCache(); + } + + [DataTestMethod] + [DataRow("bj", "北京")] + [DataRow("sh", "上海")] + [DataRow("nihao", "你好")] + [DataRow("北京", "北京")] + [DataRow("北京", "Beijing")] + [DataRow("北", "北京")] + [DataRow("你好", "nihao")] + public void PinyinMatch_DataDriven(string needle, string haystack) + { + Assert.IsTrue(FuzzyStringMatcher.ScoreFuzzy(needle, haystack) > 0, $"Expected match for '{needle}' in '{haystack}'"); + } + + [TestMethod] + public void PinyinPositions_ShouldBeEmpty() + { + var (score, positions) = FuzzyStringMatcher.ScoreFuzzyWithPositions("bj", "北京", true); + Assert.IsTrue(score > 0); + Assert.AreEqual(0, positions.Count); + } +} diff --git a/src/modules/cmdpal/Tests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests/FuzzyMatcherValidationTests.cs b/src/modules/cmdpal/Tests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests/FuzzyMatcherValidationTests.cs new file mode 100644 index 0000000000..a03c2ccbb6 --- /dev/null +++ b/src/modules/cmdpal/Tests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests/FuzzyMatcherValidationTests.cs @@ -0,0 +1,43 @@ +// 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 Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Microsoft.CommandPalette.Extensions.Toolkit.UnitTests; + +[TestClass] +public class FuzzyMatcherValidationTests +{ + [DataTestMethod] + [DataRow(null, "haystack")] + [DataRow("", "haystack")] + [DataRow("needle", null)] + [DataRow("needle", "")] + [DataRow(null, null)] + public void ScoreFuzzy_HandlesIncorrectInputs(string needle, string haystack) + { + Assert.AreEqual(0, FuzzyStringMatcher.ScoreFuzzy(needle!, haystack!)); + Assert.AreEqual(0, FuzzyStringMatcher.ScoreFuzzy(needle!, haystack!, allowNonContiguousMatches: true, removeDiacritics: true)); + Assert.AreEqual(0, FuzzyStringMatcher.ScoreFuzzy(needle!, haystack!, allowNonContiguousMatches: false, removeDiacritics: false)); + } + + [DataTestMethod] + [DataRow(null, "haystack")] + [DataRow("", "haystack")] + [DataRow("needle", null)] + [DataRow("needle", "")] + [DataRow(null, null)] + public void ScoreFuzzyWithPositions_HandlesIncorrectInputs(string needle, string haystack) + { + var (score1, pos1) = FuzzyStringMatcher.ScoreFuzzyWithPositions(needle!, haystack!, true); + Assert.AreEqual(0, score1); + Assert.IsNotNull(pos1); + Assert.AreEqual(0, pos1.Count); + + var (score2, pos2) = FuzzyStringMatcher.ScoreFuzzyWithPositions(needle!, haystack!, allowNonContiguousMatches: true, removeDiacritics: true); + Assert.AreEqual(0, score2); + Assert.IsNotNull(pos2); + Assert.AreEqual(0, pos2.Count); + } +} diff --git a/src/modules/cmdpal/Tests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests/Legacy/LegacyFuzzyStringMatcher.cs b/src/modules/cmdpal/Tests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests/Legacy/LegacyFuzzyStringMatcher.cs new file mode 100644 index 0000000000..9cb2f4556d --- /dev/null +++ b/src/modules/cmdpal/Tests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests/Legacy/LegacyFuzzyStringMatcher.cs @@ -0,0 +1,225 @@ +// 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.Globalization; +using System.Linq; +using ToolGood.Words.Pinyin; + +namespace Microsoft.CommandPalette.Extensions.Toolkit.UnitTests.Legacy; + +// Inspired by the fuzzy.rs from edit.exe +public static class LegacyFuzzyStringMatcher +{ + private const int NOMATCH = 0; + + /// + /// Gets or sets a value indicating whether to support Chinese PinYin. + /// Automatically enabled when the system UI culture is Simplified Chinese. + /// + public static bool ChinesePinYinSupport { get; set; } = IsSimplifiedChinese(); + + private static bool IsSimplifiedChinese() + { + var culture = CultureInfo.CurrentUICulture; + + // Detect Simplified Chinese: zh-CN, zh-Hans, zh-Hans-* + return culture.Name.StartsWith("zh-CN", StringComparison.OrdinalIgnoreCase) + || culture.Name.StartsWith("zh-Hans", StringComparison.OrdinalIgnoreCase); + } + + public static int ScoreFuzzy(string needle, string haystack, bool allowNonContiguousMatches = true) + { + var (s, _) = ScoreFuzzyWithPositions(needle, haystack, allowNonContiguousMatches); + return s; + } + + public static (int Score, List Positions) ScoreFuzzyWithPositions(string needle, string haystack, bool allowNonContiguousMatches) + => ScoreAllFuzzyWithPositions(needle, haystack, allowNonContiguousMatches).MaxBy(i => i.Score); + + public static IEnumerable<(int Score, List Positions)> ScoreAllFuzzyWithPositions(string needle, string haystack, bool allowNonContiguousMatches) + { + List needles = [needle]; + List haystacks = [haystack]; + + if (ChinesePinYinSupport) + { + // Remove IME composition split characters. + var input = needle.Replace("'", string.Empty); + needles.Add(WordsHelper.GetPinyin(input)); + if (WordsHelper.HasChinese(haystack)) + { + haystacks.Add(WordsHelper.GetPinyin(haystack)); + } + } + + return needles.SelectMany(i => haystacks.Select(j => ScoreFuzzyWithPositionsInternal(i, j, allowNonContiguousMatches))); + } + + private static (int Score, List Positions) ScoreFuzzyWithPositionsInternal(string needle, string haystack, bool allowNonContiguousMatches) + { + if (string.IsNullOrEmpty(haystack) || string.IsNullOrEmpty(needle)) + { + return (NOMATCH, new List()); + } + + var target = haystack.ToCharArray(); + var query = needle.ToCharArray(); + + if (target.Length < query.Length) + { + return (NOMATCH, new List()); + } + + var targetUpper = FoldCase(haystack); + var queryUpper = FoldCase(needle); + var targetUpperChars = targetUpper.ToCharArray(); + var queryUpperChars = queryUpper.ToCharArray(); + + var area = query.Length * target.Length; + var scores = new int[area]; + var matches = new int[area]; + + for (var qi = 0; qi < query.Length; qi++) + { + var qiOffset = qi * target.Length; + var qiPrevOffset = qi > 0 ? (qi - 1) * target.Length : 0; + + for (var ti = 0; ti < target.Length; ti++) + { + var currentIndex = qiOffset + ti; + var diagIndex = (qi > 0 && ti > 0) ? qiPrevOffset + ti - 1 : 0; + var leftScore = ti > 0 ? scores[currentIndex - 1] : 0; + var diagScore = (qi > 0 && ti > 0) ? scores[diagIndex] : 0; + var matchSeqLen = (qi > 0 && ti > 0) ? matches[diagIndex] : 0; + + var score = (diagScore == 0 && qi != 0) ? 0 : + ComputeCharScore( + query[qi], + queryUpperChars[qi], + ti != 0 ? target[ti - 1] : null, + target[ti], + targetUpperChars[ti], + matchSeqLen); + + var isValidScore = score != 0 && diagScore + score >= leftScore && + (allowNonContiguousMatches || qi > 0 || + targetUpperChars.Skip(ti).Take(queryUpperChars.Length).SequenceEqual(queryUpperChars)); + + if (isValidScore) + { + matches[currentIndex] = matchSeqLen + 1; + scores[currentIndex] = diagScore + score; + } + else + { + matches[currentIndex] = NOMATCH; + scores[currentIndex] = leftScore; + } + } + } + + var positions = new List(); + if (query.Length > 0 && target.Length > 0) + { + var qi = query.Length - 1; + var ti = target.Length - 1; + + while (true) + { + var index = (qi * target.Length) + ti; + if (matches[index] == NOMATCH) + { + if (ti == 0) + { + break; + } + + ti--; + } + else + { + positions.Add(ti); + if (qi == 0 || ti == 0) + { + break; + } + + qi--; + ti--; + } + } + + positions.Reverse(); + } + + return (scores[area - 1], positions); + } + + private static string FoldCase(string input) + { + return input.ToUpperInvariant(); + } + + private static int ComputeCharScore( + char query, + char queryLower, + char? targetPrev, + char targetCurr, + char targetLower, + int matchSeqLen) + { + if (!ConsiderAsEqual(queryLower, targetLower)) + { + return 0; + } + + var score = 1; // Character match bonus + + if (matchSeqLen > 0) + { + score += matchSeqLen * 5; // Consecutive match bonus + } + + if (query == targetCurr) + { + score += 1; // Same case bonus + } + + if (targetPrev.HasValue) + { + var sepBonus = ScoreSeparator(targetPrev.Value); + if (sepBonus > 0) + { + score += sepBonus; + } + else if (char.IsUpper(targetCurr) && matchSeqLen == 0) + { + score += 2; // CamelCase bonus + } + } + else + { + score += 8; // Start of word bonus + } + + return score; + } + + private static bool ConsiderAsEqual(char a, char b) + { + return a == b || (a == '/' && b == '\\') || (a == '\\' && b == '/'); + } + + private static int ScoreSeparator(char ch) + { + return ch switch + { + '/' or '\\' => 5, + '_' or '-' or '.' or ' ' or '\'' or '"' or ':' => 4, + _ => 0, + }; + } +} diff --git a/src/modules/cmdpal/Tests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests.csproj b/src/modules/cmdpal/Tests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests.csproj new file mode 100644 index 0000000000..91d423031a --- /dev/null +++ b/src/modules/cmdpal/Tests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests/Microsoft.CommandPalette.Extensions.Toolkit.UnitTests.csproj @@ -0,0 +1,30 @@ + + + + + + $(MSBuildThisFileDirectory)..\..\..\..\..\ + false + true + Microsoft.CommandPalette.Extensions.Toolkit.UnitTests + $(SolutionDir)$(Platform)\$(Configuration)\WinUI3Apps\CmdPal\tests\ + false + false + enable + + + + + + + + + + + + + true + true + $(RepoRoot).pipelines\272MSSharedLibSN2048.snk + + \ No newline at end of file diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/BracketHelper.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/BracketHelper.cs index 67d8940ed4..e3e6a3a3fb 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/BracketHelper.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/BracketHelper.cs @@ -53,6 +53,56 @@ public static class BracketHelper return trailTest.Count == 0; } + public static string BalanceBrackets(string query) + { + if (string.IsNullOrWhiteSpace(query)) + { + return query ?? string.Empty; + } + + var openBrackets = new Stack(); + + for (var i = 0; i < query.Length; i++) + { + var (direction, type) = BracketTrail(query[i]); + + if (direction == TrailDirection.None) + { + continue; + } + + if (direction == TrailDirection.Open) + { + openBrackets.Push(type); + } + else if (direction == TrailDirection.Close) + { + // Only pop if we have a matching open bracket + if (openBrackets.Count > 0 && openBrackets.Peek() == type) + { + openBrackets.Pop(); + } + } + } + + if (openBrackets.Count == 0) + { + return query; + } + + // Build closing brackets in LIFO order + var closingBrackets = new char[openBrackets.Count]; + var index = 0; + + while (openBrackets.Count > 0) + { + var type = openBrackets.Pop(); + closingBrackets[index++] = type == TrailType.Round ? ')' : ']'; + } + + return query + new string(closingBrackets); + } + private static (TrailDirection Direction, TrailType Type) BracketTrail(char @char) { switch (@char) diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/CalculateEngine.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/CalculateEngine.cs index a927e07499..fea869a497 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/CalculateEngine.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/CalculateEngine.cs @@ -1,9 +1,8 @@ -// 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; -using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using CalculatorEngineCommon; @@ -16,6 +15,7 @@ public static class CalculateEngine private static readonly PropertySet _constants = new() { { "pi", Math.PI }, + { "π", Math.PI }, { "e", Math.E }, }; @@ -59,6 +59,8 @@ public static class CalculateEngine input = CalculateHelper.FixHumanMultiplicationExpressions(input); + input = CalculateHelper.UpdateFactorialFunctions(input); + // Get the user selected trigonometry unit TrigMode trigMode = settings.TrigUnit; @@ -77,6 +79,13 @@ public static class CalculateEngine return default; } + // If we're out of bounds + if (result is "inf" or "-inf") + { + error = Properties.Resources.calculator_not_covert_to_decimal; + return default; + } + if (string.IsNullOrEmpty(result)) { return default; @@ -110,15 +119,19 @@ public static class CalculateEngine /// public static decimal FormatMax15Digits(decimal value, CultureInfo cultureInfo) { + const int maxDisplayDigits = 15; + + if (value == 0m) + { + return 0m; + } + var absValue = Math.Abs(value); var integerDigits = absValue >= 1 ? (int)Math.Floor(Math.Log10((double)absValue)) + 1 : 1; - var maxDecimalDigits = Math.Max(0, 15 - integerDigits); + var maxDecimalDigits = Math.Max(0, maxDisplayDigits - integerDigits); var rounded = Math.Round(value, maxDecimalDigits, MidpointRounding.AwayFromZero); - - var formatted = rounded.ToString("G29", cultureInfo); - - return Convert.ToDecimal(formatted, cultureInfo); + return rounded / 1.000000000000000000000000000000000m; } } diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/CalculateHelper.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/CalculateHelper.cs index ed13acb7b3..0ad44bedd7 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/CalculateHelper.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/CalculateHelper.cs @@ -3,11 +3,12 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections.Generic; using System.Text.RegularExpressions; namespace Microsoft.CmdPal.Ext.Calc.Helper; -public static class CalculateHelper +public static partial class CalculateHelper { private static readonly Regex RegValidExpressChar = new Regex( @"^(" + @@ -19,7 +20,7 @@ public static class CalculateHelper @"rad\s*\(|deg\s*\(|grad\s*\(|" + /* trigonometry unit conversion macros */ @"pi|" + @"==|~=|&&|\|\||" + - @"((-?(\d+(\.\d*)?)|-?(\.\d+))[Ee](-?\d+))|" + /* expression from CheckScientificNotation between parenthesis */ + @"((\d+(?:\.\d*)?|\.\d+)[eE](-?\d+))|" + /* expression from CheckScientificNotation between parenthesis */ @"e|[0-9]|0[xX][0-9a-fA-F]+|0[bB][01]+|0[oO][0-7]+|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" + @")+$", RegexOptions.Compiled); @@ -31,6 +32,94 @@ public static class CalculateHelper private const string RadToDeg = "(180 / pi) * "; private const string RadToGrad = "(200 / pi) * "; + // replacements from the user input to displayed query + private static readonly Dictionary QueryReplacements = new() + { + { "%", "%" }, { "﹪", "%" }, + { "−", "-" }, { "–", "-" }, { "—", "-" }, + { "!", "!" }, + { "*", "×" }, { "∗", "×" }, { "·", "×" }, { "⊗", "×" }, { "⋅", "×" }, { "✕", "×" }, { "✖", "×" }, { "\u2062", "×" }, + { "/", "÷" }, { "∕", "÷" }, { "➗", "÷" }, { ":", "÷" }, + }; + + // replacements from a query to engine input + private static readonly Dictionary EngineReplacements = new() + { + { "×", "*" }, + { "÷", "/" }, + }; + + private static readonly Dictionary SuperscriptReplacements = new() + { + { "²", "^2" }, { "³", "^3" }, + }; + + private static readonly HashSet StandardOperators = [ + + // binary operators; doesn't make sense for them to be at the end of a query + '+', '-', '*', '/', '%', '^', '=', '&', '|', '\\', + + // parentheses + '(', '[', + ]; + + private static readonly HashSet SuffixOperators = [ + + // unary operators; can appear at the end of a query + ')', ']', '!', + ]; + + private static readonly Regex ReplaceScientificNotationRegex = CreateReplaceScientificNotationRegex(); + + public static char[] GetQueryOperators() + { + var ops = new HashSet(StandardOperators); + ops.ExceptWith(SuffixOperators); + return [.. ops]; + } + + /// + /// Normalizes the query for display + /// This replaces standard operators with more visually appealing ones (e.g., '*' -> '×') if enabled. + /// Always applies safe normalizations (standardizing variants like minus, percent, etc.). + /// + /// The query string to normalize. + public static string NormalizeCharsForDisplayQuery(string input) + { + // 1. Safe/Trivial replacements (Variant -> Standard) + // These are always applied to ensure consistent behavior for non-math symbols (spaces) and + // operator variants like minus, percent, and exclamation mark. + foreach (var (key, value) in QueryReplacements) + { + input = input.Replace(key, value); + } + + return input; + } + + /// + /// Normalizes the query for the calculation engine. + /// This replaces all supported operator variants (visual or standard) with the specific + /// ASCII operators required by the engine (e.g., '×' -> '*'). + /// It duplicates and expands upon replacements in NormalizeQuery to ensure the engine + /// receives valid input regardless of whether NormalizeQuery was executed. + /// + public static string NormalizeCharsToEngine(string input) + { + foreach (var (key, value) in EngineReplacements) + { + input = input.Replace(key, value); + } + + // Replace superscript characters with their engine equivalents (e.g., '²' -> '^2') + foreach (var (key, value) in SuperscriptReplacements) + { + input = input.Replace(key, value); + } + + return input; + } + public static bool InputValid(string input) { if (string.IsNullOrWhiteSpace(input)) @@ -50,7 +139,7 @@ public static class CalculateHelper // If the input ends with a binary operator then it is not a valid input to mages and the Interpret function would throw an exception. Because we expect here that the user has not finished typing we block those inputs. var trimmedInput = input.TrimEnd(); - if (trimmedInput.EndsWith('+') || trimmedInput.EndsWith('-') || trimmedInput.EndsWith('*') || trimmedInput.EndsWith('|') || trimmedInput.EndsWith('\\') || trimmedInput.EndsWith('^') || trimmedInput.EndsWith('=') || trimmedInput.EndsWith('&') || trimmedInput.EndsWith('/') || trimmedInput.EndsWith('%')) + if (EndsWithBinaryOperator(trimmedInput)) { return false; } @@ -58,6 +147,18 @@ public static class CalculateHelper return true; } + private static bool EndsWithBinaryOperator(string input) + { + var operators = GetQueryOperators(); + if (string.IsNullOrEmpty(input)) + { + return false; + } + + var lastChar = input[^1]; + return Array.Exists(operators, op => op == lastChar); + } + public static string FixHumanMultiplicationExpressions(string input) { var output = CheckScientificNotation(input); @@ -72,18 +173,7 @@ public static class CalculateHelper private static string CheckScientificNotation(string input) { - /** - * NOTE: By the time that the expression gets to us, it's already in English format. - * - * Regex explanation: - * (-?(\d+({0}\d*)?)|-?({0}\d+)): Used to capture one of two types: - * -?(\d+({0}\d*)?): Captures a decimal number starting with a number (e.g. "-1.23") - * -?({0}\d+): Captures a decimal number without leading number (e.g. ".23") - * e: Captures 'e' or 'E' - * (-?\d+): Captures an integer number (e.g. "-1" or "23") - */ - var p = @"(-?(\d+(\.\d*)?)|-?(\.\d+))e(-?\d+)"; - return Regex.Replace(input, p, "($1 * 10^($5))", RegexOptions.IgnoreCase); + return ReplaceScientificNotationRegex.Replace(input, "($1 * 10^($2))"); } /* @@ -292,6 +382,86 @@ public static class CalculateHelper return modifiedInput; } + public static string UpdateFactorialFunctions(string input) + { + // Handle n! -> factorial(n) + int startSearch = 0; + while (true) + { + var index = input.IndexOf('!', startSearch); + if (index == -1) + { + break; + } + + // Ignore != + if (index + 1 < input.Length && input[index + 1] == '=') + { + startSearch = index + 2; + continue; + } + + if (index == 0) + { + startSearch = index + 1; + continue; + } + + // Scan backwards + var endArg = index - 1; + while (endArg >= 0 && char.IsWhiteSpace(input[endArg])) + { + endArg--; + } + + if (endArg < 0) + { + startSearch = index + 1; + continue; + } + + var startArg = endArg; + if (input[endArg] == ')') + { + // Find matching '(' + startArg = FindOpeningBracketIndexInFrontOfIndex(input, endArg); + if (startArg == -1) + { + startSearch = index + 1; + continue; + } + } + else + { + // Scan back for number or word + while (startArg >= 0 && (char.IsLetterOrDigit(input[startArg]) || input[startArg] == '.')) + { + startArg--; + } + + startArg++; // Move back to first valid char + } + + if (startArg > endArg) + { + // No argument found + startSearch = index + 1; + continue; + } + + // Extract argument + var arg = input.Substring(startArg, endArg - startArg + 1); + + // Replace ! with factorial() + input = input.Remove(startArg, index - startArg + 1); + input = input.Insert(startArg, $"factorial({arg})"); + + startSearch = 0; // Reset search because string changed + } + + return input; + } + private static string ModifyMathFunction(string input, string function, string modification) { // Create the pattern to match the function, opening bracket, and any spaces in between @@ -325,4 +495,43 @@ public static class CalculateHelper return modifiedInput; } + + private static int FindOpeningBracketIndexInFrontOfIndex(string input, int end) + { + var bracketCount = 0; + for (var i = end; i >= 0; i--) + { + switch (input[i]) + { + case ')': + bracketCount++; + break; + case '(': + { + bracketCount--; + if (bracketCount == 0) + { + return i; + } + + break; + } + } + } + + return -1; + } + + /* + * NOTE: By the time that the expression gets to us, it's already in English format. + * + * Regex explanation: + * (-?(\d+({0}\d*)?)|-?({0}\d+)): Used to capture one of two types: + * -?(\d+({0}\d*)?): Captures a decimal number starting with a number (e.g. "-1.23") + * -?({0}\d+): Captures a decimal number without leading number (e.g. ".23") + * e: Captures 'e' or 'E' + * (?\d+): Captures an integer number (e.g. "-1" or "23") + */ + [GeneratedRegex(@"(\d+(?:\.\d*)?|\.\d+)e(-?\d+)", RegexOptions.IgnoreCase, "en-US")] + private static partial Regex CreateReplaceScientificNotationRegex(); } diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/ISettingsInterface.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/ISettingsInterface.cs index f4b7a50644..f0639aaa29 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/ISettingsInterface.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/ISettingsInterface.cs @@ -2,8 +2,6 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using Microsoft.CmdPal.Ext.Calc.Helper; - namespace Microsoft.CmdPal.Ext.Calc.Helper; public interface ISettingsInterface @@ -15,4 +13,8 @@ public interface ISettingsInterface public bool OutputUseEnglishFormat { get; } public bool CloseOnEnter { get; } + + public bool CopyResultToSearchBarIfQueryEndsWithEqualSign { get; } + + public bool AutoFixQuery { get; } } diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/QueryHelper.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/QueryHelper.cs index 99f782d714..2dc6ff9b3f 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/QueryHelper.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/QueryHelper.cs @@ -12,7 +12,13 @@ namespace Microsoft.CmdPal.Ext.Calc.Helper; public static partial class QueryHelper { - public static ListItem Query(string query, ISettingsInterface settings, bool isFallbackSearch, TypedEventHandler handleSave = null) + public static ListItem Query( + string query, + ISettingsInterface settings, + bool isFallbackSearch, + out string displayQuery, + TypedEventHandler handleSave = null, + TypedEventHandler handleReplace = null) { ArgumentNullException.ThrowIfNull(query); if (!isFallbackSearch) @@ -20,26 +26,50 @@ public static partial class QueryHelper ArgumentNullException.ThrowIfNull(handleSave); } - CultureInfo inputCulture = settings.InputUseEnglishFormat ? new CultureInfo("en-us") : CultureInfo.CurrentCulture; - CultureInfo outputCulture = settings.OutputUseEnglishFormat ? new CultureInfo("en-us") : CultureInfo.CurrentCulture; + CultureInfo inputCulture = + settings.InputUseEnglishFormat ? new CultureInfo("en-us") : CultureInfo.CurrentCulture; + CultureInfo outputCulture = + settings.OutputUseEnglishFormat ? new CultureInfo("en-us") : CultureInfo.CurrentCulture; // In case the user pastes a query with a leading = - query = query.TrimStart('='); + query = query.TrimStart('=').TrimStart(); + + // Enables better looking characters for multiplication and division (e.g., '×' and '÷') + displayQuery = CalculateHelper.NormalizeCharsForDisplayQuery(query); // Happens if the user has only typed the action key so far - if (string.IsNullOrEmpty(query)) + if (string.IsNullOrEmpty(displayQuery)) { return null; } - NumberTranslator translator = NumberTranslator.Create(inputCulture, new CultureInfo("en-US")); - var input = translator.Translate(query.Normalize(NormalizationForm.FormKC)); + // Normalize query to engine format (e.g., replace '×' with '*', converts superscripts to functions) + // This must be done before any further normalization to avoid losing information + var engineQuery = CalculateHelper.NormalizeCharsToEngine(displayQuery); + + // Cleanup rest of the Unicode characters, whitespace + var queryForEngine2 = engineQuery.Normalize(NormalizationForm.FormKC); + + // Translate numbers from input culture to en-US culture for the calculation engine + var translator = NumberTranslator.Create(inputCulture, new CultureInfo("en-US")); + + // Translate the input query + var input = translator.Translate(queryForEngine2); if (string.IsNullOrWhiteSpace(input)) { return ErrorHandler.OnError(isFallbackSearch, query, Properties.Resources.calculator_expression_empty); } + // normalize again to engine chars after translation + input = CalculateHelper.NormalizeCharsToEngine(input); + + // Auto fix incomplete queries (if enabled) + if (settings.AutoFixQuery && TryGetIncompleteQuery(input, out var newInput)) + { + input = newInput; + } + if (!CalculateHelper.InputValid(input)) { return null; @@ -60,10 +90,10 @@ public static partial class QueryHelper if (isFallbackSearch) { // Fallback search - return ResultHelper.CreateResult(result.RoundedResult, inputCulture, outputCulture, query); + return ResultHelper.CreateResult(result.RoundedResult, inputCulture, outputCulture, displayQuery); } - return ResultHelper.CreateResult(result.RoundedResult, inputCulture, outputCulture, query, settings, handleSave); + return ResultHelper.CreateResult(result.RoundedResult, inputCulture, outputCulture, displayQuery, settings, handleSave, handleReplace); } catch (OverflowException) { @@ -77,4 +107,32 @@ public static partial class QueryHelper return ErrorHandler.OnError(isFallbackSearch, query, default, e); } } + + public static bool TryGetIncompleteQuery(string query, out string newQuery) + { + newQuery = query; + + var trimmed = query.TrimEnd(); + if (string.IsNullOrEmpty(trimmed)) + { + return false; + } + + // 1. Trim trailing operators + var operators = CalculateHelper.GetQueryOperators(); + while (trimmed.Length > 0 && Array.IndexOf(operators, trimmed[^1]) > -1) + { + trimmed = trimmed[..^1].TrimEnd(); + } + + if (trimmed.Length == 0) + { + return false; + } + + // 2. Fix brackets + newQuery = BracketHelper.BalanceBrackets(trimmed); + + return true; + } } diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/ReplaceQueryCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/ReplaceQueryCommand.cs new file mode 100644 index 0000000000..2dfb17bd16 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/ReplaceQueryCommand.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. + +using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; +using Windows.Foundation; + +namespace Microsoft.CmdPal.Ext.Calc.Helper; + +public sealed partial class ReplaceQueryCommand : InvokableCommand +{ + public event TypedEventHandler ReplaceRequested; + + public ReplaceQueryCommand() + { + Name = "Replace query"; + Icon = new IconInfo("\uE70F"); // Edit icon + } + + public override ICommandResult Invoke() + { + ReplaceRequested?.Invoke(this, null); + return CommandResult.KeepOpen(); + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/ResultHelper.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/ResultHelper.cs index cd2b811567..0147f73c07 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/ResultHelper.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/ResultHelper.cs @@ -6,6 +6,7 @@ using System; using System.Collections.Generic; using System.Globalization; using ManagedCommon; +using Microsoft.CommandPalette.Extensions; using Microsoft.CommandPalette.Extensions.Toolkit; using Windows.Foundation; @@ -13,7 +14,14 @@ namespace Microsoft.CmdPal.Ext.Calc.Helper; public static class ResultHelper { - public static ListItem CreateResult(decimal? roundedResult, CultureInfo inputCulture, CultureInfo outputCulture, string query, ISettingsInterface settings, TypedEventHandler handleSave) + public static ListItem CreateResult( + decimal? roundedResult, + CultureInfo inputCulture, + CultureInfo outputCulture, + string query, + ISettingsInterface settings, + TypedEventHandler handleSave, + TypedEventHandler handleReplace) { // Return null when the expression is not a valid calculator query. if (roundedResult is null) @@ -28,6 +36,9 @@ public static class ResultHelper var saveCommand = new SaveCommand(result); saveCommand.SaveRequested += handleSave; + var replaceCommand = new ReplaceQueryCommand(); + replaceCommand.ReplaceRequested += handleReplace; + var copyCommandItem = CreateResult(roundedResult, inputCulture, outputCulture, query); // No TextToSuggest on the main save command item. We don't want to keep suggesting what the result is, @@ -40,6 +51,7 @@ public static class ResultHelper Subtitle = query, MoreCommands = [ new CommandContextItem(settings.CloseOnEnter ? saveCommand : copyCommandItem.Command), + new CommandContextItem(replaceCommand) { RequestedShortcut = KeyChords.CopyResultToSearchBox, }, ..copyCommandItem.MoreCommands, ], }; @@ -55,11 +67,15 @@ public static class ResultHelper var decimalResult = roundedResult?.ToString(outputCulture); - List context = []; + List context = []; if (decimal.IsInteger((decimal)roundedResult)) { + context.Add(new Separator()); + var i = decimal.ToInt64((decimal)roundedResult); + + // hexadecimal try { var hexResult = "0x" + i.ToString("X", outputCulture); @@ -70,9 +86,10 @@ public static class ResultHelper } catch (Exception ex) { - Logger.LogError("Error parsing hex format", ex); + Logger.LogError("Error converting to hex format", ex); } + // binary try { var binaryResult = "0b" + i.ToString("B", outputCulture); @@ -83,7 +100,21 @@ public static class ResultHelper } catch (Exception ex) { - Logger.LogError("Error parsing binary format", ex); + Logger.LogError("Error converting to binary format", ex); + } + + // octal + try + { + var octalResult = "0o" + Convert.ToString(i, 8); + context.Add(new CommandContextItem(new CopyTextCommand(octalResult) { Name = Properties.Resources.calculator_copy_octal }) + { + Title = octalResult, + }); + } + catch (Exception ex) + { + Logger.LogError("Error converting to octal format", ex); } } diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/SettingsManager.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/SettingsManager.cs index cea59e170f..245af25da7 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/SettingsManager.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Helper/SettingsManager.cs @@ -45,6 +45,18 @@ public class SettingsManager : JsonSettingsManager, ISettingsInterface Properties.Resources.calculator_settings_close_on_enter_description, true); + private readonly ToggleSetting _copyResultToSearchBarIfQueryEndsWithEqualSign = new( + Namespaced(nameof(CopyResultToSearchBarIfQueryEndsWithEqualSign)), + Properties.Resources.calculator_settings_copy_result_to_search_bar, + Properties.Resources.calculator_settings_copy_result_to_search_bar_description, + false); + + private readonly ToggleSetting _autoFixQuery = new( + Namespaced(nameof(AutoFixQuery)), + Properties.Resources.calculator_settings_auto_fix_query, + Properties.Resources.calculator_settings_auto_fix_query_description, + true); + public CalculateEngine.TrigMode TrigUnit { get @@ -81,6 +93,10 @@ public class SettingsManager : JsonSettingsManager, ISettingsInterface public bool CloseOnEnter => _closeOnEnter.Value; + public bool CopyResultToSearchBarIfQueryEndsWithEqualSign => _copyResultToSearchBarIfQueryEndsWithEqualSign.Value; + + public bool AutoFixQuery => _autoFixQuery.Value; + internal static string SettingsJsonPath() { var directory = Utilities.BaseSettingsPath("Microsoft.CmdPal"); @@ -98,6 +114,8 @@ public class SettingsManager : JsonSettingsManager, ISettingsInterface Settings.Add(_inputUseEnNumberFormat); Settings.Add(_outputUseEnNumberFormat); Settings.Add(_closeOnEnter); + Settings.Add(_copyResultToSearchBarIfQueryEndsWithEqualSign); + Settings.Add(_autoFixQuery); // Load settings from file upon initialization LoadSettings(); diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/KeyChords.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/KeyChords.cs new file mode 100644 index 0000000000..32bf117d90 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/KeyChords.cs @@ -0,0 +1,13 @@ +// 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 Microsoft.CommandPalette.Extensions; +using Windows.System; + +namespace Microsoft.CmdPal.Ext.Calc; + +internal static class KeyChords +{ + internal static KeyChord CopyResultToSearchBox { get; } = new(VirtualKeyModifiers.Control | VirtualKeyModifiers.Shift, (int)VirtualKey.Enter, 0); +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/CalculatorListPage.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/CalculatorListPage.cs index 72e5f3db30..70023794e9 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/CalculatorListPage.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/CalculatorListPage.cs @@ -25,12 +25,12 @@ public sealed partial class CalculatorListPage : DynamicListPage private readonly Lock _resultsLock = new(); private readonly ISettingsInterface _settingsManager; private readonly List _items = []; - private readonly List history = []; + private readonly List _history = []; private readonly ListItem _emptyItem; // This is the text that saved when the user click the result. // We need to avoid the double calculation. This may cause some wierd behaviors. - private string skipQuerySearchText = string.Empty; + private string _skipQuerySearchText = string.Empty; public CalculatorListPage(ISettingsInterface settings) { @@ -54,6 +54,17 @@ public sealed partial class CalculatorListPage : DynamicListPage UpdateSearchText(string.Empty, string.Empty); } + private void HandleReplaceQuery(object sender, object args) + { + var lastResult = _items[0].Title; + if (!string.IsNullOrEmpty(lastResult)) + { + _skipQuerySearchText = lastResult; + SearchText = lastResult; + OnPropertyChanged(nameof(SearchText)); + } + } + public override void UpdateSearchText(string oldSearch, string newSearch) { if (oldSearch == newSearch) @@ -61,19 +72,37 @@ public sealed partial class CalculatorListPage : DynamicListPage return; } - if (!string.IsNullOrEmpty(skipQuerySearchText) && newSearch == skipQuerySearchText) + if (!string.IsNullOrEmpty(_skipQuerySearchText) && newSearch == _skipQuerySearchText) { // only skip once. - skipQuerySearchText = string.Empty; + _skipQuerySearchText = string.Empty; return; } - skipQuerySearchText = string.Empty; + var copyResultToSearchText = false; + if (_settingsManager.CopyResultToSearchBarIfQueryEndsWithEqualSign && newSearch.EndsWith('=')) + { + newSearch = newSearch.TrimEnd('=').TrimEnd(); + copyResultToSearchText = true; + } + + _skipQuerySearchText = string.Empty; _emptyItem.Subtitle = newSearch; - var result = QueryHelper.Query(newSearch, _settingsManager, false, HandleSave); + var result = QueryHelper.Query(newSearch, _settingsManager, isFallbackSearch: false, out var displayQuery, HandleSave, HandleReplaceQuery); + UpdateResult(result); + + if (copyResultToSearchText && result is not null) + { + _skipQuerySearchText = result.Title; + SearchText = result.Title; + + // LOAD BEARING: The SearchText setter does not raise a PropertyChanged notification, + // so we must raise it explicitly to ensure the UI updates correctly. + OnPropertyChanged(nameof(SearchText)); + } } private void UpdateResult(ListItem result) @@ -91,7 +120,7 @@ public sealed partial class CalculatorListPage : DynamicListPage _items.Add(_emptyItem); } - this._items.AddRange(history); + this._items.AddRange(_history); } RaiseItemsChanged(this._items.Count); @@ -109,7 +138,7 @@ public sealed partial class CalculatorListPage : DynamicListPage TextToSuggest = lastResult, }; - history.Insert(0, li); + _history.Insert(0, li); _items.Insert(1, li); // Why we need to clean the query record? Removed, but if necessary, please move it back. @@ -117,9 +146,14 @@ public sealed partial class CalculatorListPage : DynamicListPage // this change will call the UpdateSearchText again. // We need to avoid it. - skipQuerySearchText = lastResult; + _skipQuerySearchText = lastResult; SearchText = lastResult; - this.RaiseItemsChanged(this._items.Count); + + // LOAD BEARING: The SearchText setter does not raise a PropertyChanged notification, + // so we must raise it explicitly to ensure the UI updates correctly. + OnPropertyChanged(nameof(SearchText)); + + RaiseItemsChanged(this._items.Count); } } diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/FallbackCalculatorItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/FallbackCalculatorItem.cs index ebf33094f2..935c338a94 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/FallbackCalculatorItem.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/FallbackCalculatorItem.cs @@ -27,7 +27,7 @@ public sealed partial class FallbackCalculatorItem : FallbackCommandItem public override void UpdateQuery(string query) { - var result = QueryHelper.Query(query, _settings, true, null); + var result = QueryHelper.Query(query, _settings, true, out _); if (result is null) { diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.Designer.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.Designer.cs index 328b57ea96..6dedfe2169 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.Designer.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.Designer.cs @@ -19,7 +19,7 @@ namespace Microsoft.CmdPal.Ext.Calc.Properties { // 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", "17.0.0.0")] + [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 { @@ -96,6 +96,15 @@ namespace Microsoft.CmdPal.Ext.Calc.Properties { } } + /// + /// Looks up a localized string similar to Copy octal. + /// + public static string calculator_copy_octal { + get { + return ResourceManager.GetString("calculator_copy_octal", resourceCulture); + } + } + /// /// Looks up a localized string similar to Calculator. /// @@ -186,6 +195,24 @@ namespace Microsoft.CmdPal.Ext.Calc.Properties { } } + /// + /// Looks up a localized string similar to Fix incomplete calculations automatically. + /// + public static string calculator_settings_auto_fix_query { + get { + return ResourceManager.GetString("calculator_settings_auto_fix_query", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Attempt to evaluate incomplete calculations by ignoring extra operators or symbols. + /// + public static string calculator_settings_auto_fix_query_description { + get { + return ResourceManager.GetString("calculator_settings_auto_fix_query_description", resourceCulture); + } + } + /// /// Looks up a localized string similar to Close on Enter. /// @@ -204,6 +231,24 @@ namespace Microsoft.CmdPal.Ext.Calc.Properties { } } + /// + /// Looks up a localized string similar to Replace query with result on equals. + /// + public static string calculator_settings_copy_result_to_search_bar { + get { + return ResourceManager.GetString("calculator_settings_copy_result_to_search_bar", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Updates the query to the result when (=) is entered. + /// + public static string calculator_settings_copy_result_to_search_bar_description { + get { + return ResourceManager.GetString("calculator_settings_copy_result_to_search_bar_description", resourceCulture); + } + } + /// /// Looks up a localized string similar to Use English (United States) number format for input. /// @@ -222,6 +267,24 @@ namespace Microsoft.CmdPal.Ext.Calc.Properties { } } + /// + /// Looks up a localized string similar to Handle extra operators and symbols. + /// + public static string calculator_settings_input_normalization { + get { + return ResourceManager.GetString("calculator_settings_input_normalization", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Enable advanced input normalization and extra symbols (e.g. ÷, ×, π). + /// + public static string calculator_settings_input_normalization_description { + get { + return ResourceManager.GetString("calculator_settings_input_normalization_description", resourceCulture); + } + } + /// /// Looks up a localized string similar to Use English (United States) number format for output. /// diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.resx b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.resx index 1da0e6f61e..72e1cc84a0 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.resx +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Properties/Resources.resx @@ -208,4 +208,25 @@ Please enter an expression + + Replace query with result on equals + + + Updates the query to the result when (=) is entered + + + Fix incomplete calculations automatically + + + Attempt to evaluate incomplete calculations by ignoring extra operators or symbols + + + Handle extra operators and symbols + + + Enable advanced input normalization and extra symbols (e.g. ÷, ×, π) + + + Copy octal + \ No newline at end of file diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Components/FuzzyMatching.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Components/FuzzyMatching.cs deleted file mode 100644 index d640058c98..0000000000 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Components/FuzzyMatching.cs +++ /dev/null @@ -1,134 +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. - -// Code forked from Betsegaw Tadele's https://github.com/betsegaw/windowwalker/ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; - -namespace Microsoft.CmdPal.Ext.WindowWalker.Components; - -/// -/// Class housing fuzzy matching methods -/// -internal static class FuzzyMatching -{ - /// - /// Finds the best match (the one with the most - /// number of letters adjacent to each other) and - /// returns the index location of each of the letters - /// of the matches - /// - /// The text to search inside of - /// the text to search for - /// returns the index location of each of the letters of the matches - internal static List FindBestFuzzyMatch(string text, string searchText) - { - ArgumentNullException.ThrowIfNull(searchText); - - ArgumentNullException.ThrowIfNull(text); - - // Using CurrentCulture since this is user facing - searchText = searchText.ToLower(CultureInfo.CurrentCulture); - text = text.ToLower(CultureInfo.CurrentCulture); - - // Create a grid to march matches like - // e.g. - // a b c a d e c f g - // a x x - // c x x - var matches = new bool[text.Length, searchText.Length]; - for (var firstIndex = 0; firstIndex < text.Length; firstIndex++) - { - for (var secondIndex = 0; secondIndex < searchText.Length; secondIndex++) - { - matches[firstIndex, secondIndex] = - searchText[secondIndex] == text[firstIndex] ? - true : - false; - } - } - - // use this table to get all the possible matches - List> allMatches = GetAllMatchIndexes(matches); - - // return the score that is the max - var maxScore = allMatches.Count > 0 ? CalculateScoreForMatches(allMatches[0]) : 0; - List bestMatch = allMatches.Count > 0 ? allMatches[0] : new List(); - - foreach (var match in allMatches) - { - var score = CalculateScoreForMatches(match); - if (score > maxScore) - { - bestMatch = match; - maxScore = score; - } - } - - return bestMatch; - } - - /// - /// Gets all the possible matches to the search string with in the text - /// - /// a table showing the matches as generated by - /// a two dimensional array with the first dimension the text and the second - /// one the search string and each cell marked as an intersection between the two - /// a list of the possible combinations that match the search text - internal static List> GetAllMatchIndexes(bool[,] matches) - { - ArgumentNullException.ThrowIfNull(matches); - - List> results = new List>(); - - for (var secondIndex = 0; secondIndex < matches.GetLength(1); secondIndex++) - { - for (var firstIndex = 0; firstIndex < matches.GetLength(0); firstIndex++) - { - if (secondIndex == 0 && matches[firstIndex, secondIndex]) - { - results.Add(new List { firstIndex }); - } - else if (matches[firstIndex, secondIndex]) - { - var tempList = results.Where(x => x.Count == secondIndex && x[x.Count - 1] < firstIndex).Select(x => x.ToList()).ToList(); - - foreach (var pathSofar in tempList) - { - pathSofar.Add(firstIndex); - } - - results.AddRange(tempList); - } - } - - results = results.Where(x => x.Count == secondIndex + 1).ToList(); - } - - return results.Where(x => x.Count == matches.GetLength(1)).ToList(); - } - - /// - /// Calculates the score for a string - /// - /// the index of the matches - /// an integer representing the score - internal static int CalculateScoreForMatches(List matches) - { - ArgumentNullException.ThrowIfNull(matches); - - var score = 0; - - for (var currentIndex = 1; currentIndex < matches.Count; currentIndex++) - { - var previousIndex = currentIndex - 1; - - score -= matches[currentIndex] - matches[previousIndex]; - } - - return score == 0 ? -10000 : score; - } -} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Components/ResultHelper.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Components/ResultHelper.cs index 8739d88a2f..f2428fd6c4 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Components/ResultHelper.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Components/ResultHelper.cs @@ -3,7 +3,7 @@ // See the LICENSE file in the project root for more information. using System; using System.Collections.Generic; -using System.Linq; +using System.Threading.Tasks; using Microsoft.CmdPal.Ext.WindowWalker.Commands; using Microsoft.CmdPal.Ext.WindowWalker.Helpers; using Microsoft.CmdPal.Ext.WindowWalker.Properties; @@ -19,33 +19,58 @@ internal static class ResultHelper /// /// Returns a list of all results for the query. /// - /// List with all search controller matches + /// List with all search controller matches /// List of results - internal static List GetResultList(List searchControllerResults, bool isKeywordSearch) + internal static WindowWalkerListItem[] GetResultList(ICollection>? scoredWindows) { - if (searchControllerResults is null || searchControllerResults.Count == 0) + if (scoredWindows is null || scoredWindows.Count == 0) { return []; } - var resultsList = new List(searchControllerResults.Count); - var addExplorerInfo = searchControllerResults.Any(x => - string.Equals(x.Result.Process.Name, "explorer.exe", StringComparison.OrdinalIgnoreCase) && - x.Result.Process.IsShellProcess); + var list = scoredWindows as IList> ?? new List>(scoredWindows); - // Process each SearchResult to convert it into a Result. - // Using parallel processing if the operation is CPU-bound and the list is large. - resultsList = searchControllerResults - .AsParallel() - .Select(x => CreateResultFromSearchResult(x)) - .ToList(); + var addExplorerInfo = false; + for (var i = 0; i < list.Count; i++) + { + var window = list[i].Item; + if (window?.Process is null) + { + continue; + } + + if (string.Equals(window.Process.Name, "explorer.exe", StringComparison.OrdinalIgnoreCase) && window.Process.IsShellProcess) + { + addExplorerInfo = true; + break; + } + } + + var projected = new WindowWalkerListItem[list.Count]; + if (list.Count >= 32) + { + Parallel.For(0, list.Count, i => + { + projected[i] = CreateResultFromSearchResult(list[i]); + }); + } + else + { + for (var i = 0; i < list.Count; i++) + { + projected[i] = CreateResultFromSearchResult(list[i]); + } + } if (addExplorerInfo && !SettingsManager.Instance.HideExplorerSettingInfo) { - resultsList.Insert(0, GetExplorerInfoResult()); + var withInfo = new WindowWalkerListItem[projected.Length + 1]; + withInfo[0] = GetExplorerInfoResult(); + Array.Copy(projected, 0, withInfo, 1, projected.Length); + return withInfo; } - return resultsList; + return projected; } /// @@ -53,16 +78,15 @@ internal static class ResultHelper /// /// The SearchResult object to convert. /// A Result object populated with data from the SearchResult. - private static WindowWalkerListItem CreateResultFromSearchResult(SearchResult searchResult) + private static WindowWalkerListItem CreateResultFromSearchResult(Scored searchResult) { - var item = new WindowWalkerListItem(searchResult.Result) + var item = new WindowWalkerListItem(searchResult.Item) { - Title = searchResult.Result.Title, - Subtitle = GetSubtitle(searchResult.Result), - Tags = GetTags(searchResult.Result), + Title = searchResult.Item.Title, + Subtitle = GetSubtitle(searchResult.Item), + Tags = GetTags(searchResult.Item), }; item.MoreCommands = ContextMenuHelper.GetContextMenuResults(item).ToArray(); - return item; } diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Components/SearchController.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Components/SearchController.cs deleted file mode 100644 index 2e5345bdfd..0000000000 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Components/SearchController.cs +++ /dev/null @@ -1,150 +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. - -// Code forked from Betsegaw Tadele's https://github.com/betsegaw/windowwalker/ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using Microsoft.CmdPal.Ext.WindowWalker.Helpers; - -namespace Microsoft.CmdPal.Ext.WindowWalker.Components; - -/// -/// Responsible for searching and finding matches for the strings provided. -/// Essentially the UI independent model of the application -/// -internal sealed class SearchController -{ - /// - /// the current search text - /// - private string searchText; - - /// - /// Open window search results - /// - private List? searchMatches; - - /// - /// Singleton pattern - /// - private static SearchController? instance; - - /// - /// Gets or sets the current search text - /// - internal string SearchText - { - get => searchText; - - set => - searchText = value.ToLower(CultureInfo.CurrentCulture).Trim(); - } - - /// - /// Gets the open window search results - /// - internal List SearchMatches => new List(searchMatches ?? []).OrderByDescending(x => x.Score).ToList(); - - /// - /// Gets singleton Pattern - /// - internal static SearchController Instance - { - get - { - instance ??= new SearchController(); - - return instance; - } - } - - /// - /// Initializes a new instance of the class. - /// Initializes the search controller object - /// - private SearchController() - { - searchText = string.Empty; - } - - /// - /// Event handler for when the search text has been updated - /// - internal void UpdateSearchText(string searchText) - { - SearchText = searchText; - SyncOpenWindowsWithModel(); - } - - /// - /// Syncs the open windows with the OpenWindows Model - /// - internal void SyncOpenWindowsWithModel() - { - System.Diagnostics.Debug.Print("Syncing WindowSearch result with OpenWindows Model"); - - var snapshotOfOpenWindows = OpenWindows.Instance.Windows; - - searchMatches = string.IsNullOrWhiteSpace(SearchText) ? AllOpenWindows(snapshotOfOpenWindows) : FuzzySearchOpenWindows(snapshotOfOpenWindows); - } - - /// - /// Search method that matches the title of windows with the user search text - /// - /// what windows are open - /// Returns search results - private List FuzzySearchOpenWindows(List openWindows) - { - List result = []; - var searchStrings = new SearchString(searchText, SearchResult.SearchType.Fuzzy); - - foreach (var window in openWindows) - { - var titleMatch = FuzzyMatching.FindBestFuzzyMatch(window.Title, searchStrings.SearchText); - var processMatch = FuzzyMatching.FindBestFuzzyMatch(window.Process.Name ?? string.Empty, searchStrings.SearchText); - - if ((titleMatch.Count != 0 || processMatch.Count != 0) && window.Title.Length != 0) - { - result.Add(new SearchResult(window, titleMatch, processMatch, searchStrings.SearchType)); - } - } - - System.Diagnostics.Debug.Print("Found " + result.Count + " windows that match the search text"); - - return result; - } - - /// - /// Search method that matches all the windows with a title - /// - /// what windows are open - /// Returns search results - private List AllOpenWindows(List openWindows) - { - List result = []; - - foreach (var window in openWindows) - { - if (window.Title.Length != 0) - { - result.Add(new SearchResult(window)); - } - } - - return SettingsManager.Instance.InMruOrder - ? result.ToList() - : result - .OrderBy(w => w.Result.Title) - .ToList(); - } - - /// - /// Event args for a window list update event - /// - internal sealed class SearchResultUpdateEventArgs : EventArgs - { - } -} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Components/SearchResult.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Components/SearchResult.cs deleted file mode 100644 index bfe51344ce..0000000000 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Components/SearchResult.cs +++ /dev/null @@ -1,147 +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. - -// Code forked from Betsegaw Tadele's https://github.com/betsegaw/windowwalker/ -using System.Collections.Generic; - -namespace Microsoft.CmdPal.Ext.WindowWalker.Components; - -/// -/// Contains search result windows with each window including the reason why the result was included -/// -internal sealed class SearchResult -{ - /// - /// Gets the actual window reference for the search result - /// - internal Window Result - { - get; - private set; - } - - /// - /// Gets the list of indexes of the matching characters for the search in the title window - /// - internal List SearchMatchesInTitle - { - get; - private set; - } - - /// - /// Gets the list of indexes of the matching characters for the search in the - /// name of the process - /// - internal List SearchMatchesInProcessName - { - get; - private set; - } - - /// - /// Gets the type of match (shortcut, fuzzy or nothing) - /// - internal SearchType SearchResultMatchType - { - get; - private set; - } - - /// - /// Gets a score indicating how well this matches what we are looking for - /// - internal int Score - { - get; - private set; - } - - /// - /// Gets the source of where the best score was found - /// - internal TextType BestScoreSource - { - get; - private set; - } - - /// - /// Initializes a new instance of the class. - /// Constructor - /// - internal SearchResult(Window window, List matchesInTitle, List matchesInProcessName, SearchType matchType) - { - Result = window; - SearchMatchesInTitle = matchesInTitle; - SearchMatchesInProcessName = matchesInProcessName; - SearchResultMatchType = matchType; - CalculateScore(); - } - - /// - /// Initializes a new instance of the class. - /// - internal SearchResult(Window window) - { - Result = window; - SearchMatchesInTitle = new List(); - SearchMatchesInProcessName = new List(); - SearchResultMatchType = SearchType.Empty; - CalculateScore(); - } - - /// - /// Calculates the score for how closely this window matches the search string - /// - /// - /// Higher Score is better - /// - private void CalculateScore() - { - if (FuzzyMatching.CalculateScoreForMatches(SearchMatchesInProcessName) > - FuzzyMatching.CalculateScoreForMatches(SearchMatchesInTitle)) - { - Score = FuzzyMatching.CalculateScoreForMatches(SearchMatchesInProcessName); - BestScoreSource = TextType.ProcessName; - } - else - { - Score = FuzzyMatching.CalculateScoreForMatches(SearchMatchesInTitle); - BestScoreSource = TextType.WindowTitle; - } - } - - /// - /// The type of text that a string represents - /// - internal enum TextType - { - ProcessName, - WindowTitle, - } - - /// - /// The type of search - /// - internal enum SearchType - { - /// - /// the search string is empty, which means all open windows are - /// going to be returned - /// - Empty, - - /// - /// Regular fuzzy match search - /// - Fuzzy, - - /// - /// The user has entered text that has been matched to a shortcut - /// and the shortcut is now being searched - /// - Shortcut, - } -} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Components/SearchString.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Components/SearchString.cs deleted file mode 100644 index c61d193637..0000000000 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Components/SearchString.cs +++ /dev/null @@ -1,45 +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. - -// Code forked from Betsegaw Tadele's https://github.com/betsegaw/windowwalker/ -namespace Microsoft.CmdPal.Ext.WindowWalker.Components; - -/// -/// A class to represent a search string -/// -/// Class was added in order to be able to attach various context data to -/// a search string -internal sealed class SearchString -{ - /// - /// Gets where is the search string coming from (is it a shortcut - /// or direct string, etc...) - /// - internal SearchResult.SearchType SearchType - { - get; - private set; - } - - /// - /// Gets the actual text we are searching for - /// - internal string SearchText - { - get; - private set; - } - - /// - /// Initializes a new instance of the class. - /// Constructor - /// - /// text from search - /// type of search - internal SearchString(string searchText, SearchResult.SearchType searchType) - { - SearchText = searchText; - SearchType = searchType; - } -} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Pages/WindowWalkerListPage.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Pages/WindowWalkerListPage.cs index f0cbc01995..b9531163f9 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Pages/WindowWalkerListPage.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowWalker/Pages/WindowWalkerListPage.cs @@ -3,9 +3,8 @@ // See the LICENSE file in the project root for more information. using System; -using System.Collections.Generic; -using System.Globalization; using Microsoft.CmdPal.Ext.WindowWalker.Components; +using Microsoft.CmdPal.Ext.WindowWalker.Helpers; using Microsoft.CmdPal.Ext.WindowWalker.Properties; using Microsoft.CommandPalette.Extensions; using Microsoft.CommandPalette.Extensions.Toolkit; @@ -33,10 +32,12 @@ internal sealed partial class WindowWalkerListPage : DynamicListPage, IDisposabl }; } - public override void UpdateSearchText(string oldSearch, string newSearch) => + public override void UpdateSearchText(string oldSearch, string newSearch) + { RaiseItemsChanged(0); + } - public List Query(string query) + private WindowWalkerListItem[] Query(string query) { ArgumentNullException.ThrowIfNull(query); @@ -46,13 +47,37 @@ internal sealed partial class WindowWalkerListPage : DynamicListPage, IDisposabl WindowWalkerCommandsProvider.VirtualDesktopHelperInstance.UpdateDesktopList(); OpenWindows.Instance.UpdateOpenWindowsList(_cancellationTokenSource.Token); - SearchController.Instance.UpdateSearchText(query); - var searchControllerResults = SearchController.Instance.SearchMatches; - return ResultHelper.GetResultList(searchControllerResults, !string.IsNullOrEmpty(query)); + var windows = OpenWindows.Instance.Windows; + + if (string.IsNullOrWhiteSpace(query)) + { + if (!SettingsManager.Instance.InMruOrder) + { + windows.Sort(static (a, b) => string.Compare(a?.Title, b?.Title, StringComparison.OrdinalIgnoreCase)); + } + + var results = new Scored[windows.Count]; + for (var i = 0; i < windows.Count; i++) + { + results[i] = new Scored { Item = windows[i], Score = 100 }; + } + + return ResultHelper.GetResultList(results); + } + + var scored = ListHelpers.FilterListWithScores(windows, query, ScoreFunction); + return ResultHelper.GetResultList([.. scored]); } - public override IListItem[] GetItems() => Query(SearchText).ToArray(); + private static int ScoreFunction(string q, Window window) + { + var titleScore = FuzzyStringMatcher.ScoreFuzzy(q, window.Title); + var processNameScore = FuzzyStringMatcher.ScoreFuzzy(q, window.Process?.Name ?? string.Empty); + return Math.Max(titleScore, processNameScore); + } + + public override IListItem[] GetItems() => Query(SearchText); public void Dispose() { diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/FuzzyStringMatcher.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/FuzzyStringMatcher.cs index 7ecfc74222..40491970b3 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/FuzzyStringMatcher.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/FuzzyStringMatcher.cs @@ -2,8 +2,10 @@ // 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.Buffers; using System.Globalization; - +using System.Runtime.CompilerServices; +using System.Text; using ToolGood.Words.Pinyin; namespace Microsoft.CommandPalette.Extensions.Toolkit; @@ -11,213 +13,1075 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit; // Inspired by the fuzzy.rs from edit.exe public static class FuzzyStringMatcher { - private const int NOMATCH = 0; + private const int NoMatchScore = 0; + private const int StackAllocThreshold = 512; /// /// Gets a value indicating whether to support Chinese PinYin. /// Automatically enabled when the system UI culture is Simplified Chinese. /// - public static bool ChinesePinYinSupport { get; } = IsSimplifiedChinese(); + public static bool ChinesePinYinSupport { get; internal set; } = IsSimplifiedChinese(); private static bool IsSimplifiedChinese() { var culture = CultureInfo.CurrentUICulture; - - // Detect Simplified Chinese: zh-CN, zh-Hans, zh-Hans-* return culture.Name.StartsWith("zh-CN", StringComparison.OrdinalIgnoreCase) || culture.Name.StartsWith("zh-Hans", StringComparison.OrdinalIgnoreCase); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static PreparedFuzzyQuery GetOrPrepareThreadCached(string needle, bool removeDiacritics) + { + return PreparedFuzzyQueryThreadCache.GetOrPrepare(needle, removeDiacritics); + } + + /// + /// Prepare a query for repeated scoring against many targets. + /// + private static PreparedFuzzyQuery PrepareQuery(string input, bool mayNeedDiacriticsRemoval = false) + => new(input, precomputeNoDiacritics: mayNeedDiacriticsRemoval); + + // ============================================================ + // Public API + // ============================================================ public static int ScoreFuzzy(string needle, string haystack, bool allowNonContiguousMatches = true) { - var (s, _) = ScoreFuzzyWithPositions(needle, haystack, allowNonContiguousMatches); - return s; + return ScoreFuzzy(needle, haystack, allowNonContiguousMatches, removeDiacritics: true); + } + + public static int ScoreFuzzy(string needle, string haystack, bool allowNonContiguousMatches, bool removeDiacritics) + { + var query = GetOrPrepareThreadCached(needle, removeDiacritics); + return ScoreBestVariant(in query, haystack, allowNonContiguousMatches, removeDiacritics); } public static (int Score, List Positions) ScoreFuzzyWithPositions(string needle, string haystack, bool allowNonContiguousMatches) - => ScoreAllFuzzyWithPositions(needle, haystack, allowNonContiguousMatches).MaxBy(i => i.Score); - - public static IEnumerable<(int Score, List Positions)> ScoreAllFuzzyWithPositions(string needle, string haystack, bool allowNonContiguousMatches) { - List needles = [needle]; - List haystacks = [haystack]; + return ScoreFuzzyWithPositions(needle, haystack, allowNonContiguousMatches, removeDiacritics: true); + } - if (ChinesePinYinSupport) + public static (int Score, List Positions) ScoreFuzzyWithPositions( + string needle, string haystack, bool allowNonContiguousMatches, bool removeDiacritics) + { + var query = GetOrPrepareThreadCached(needle, removeDiacritics); + return ScoreBestVariantWithPositions(in query, haystack, allowNonContiguousMatches, removeDiacritics); + } + + internal static void ClearCache() + { + PreparedFuzzyQueryThreadCache.Clear(); + } + + // ============================================================ + // Best-variant selection + // ============================================================ + [SkipLocalsInit] + private static int ScoreBestVariant( + in PreparedFuzzyQuery query, + string haystack, + bool allowNonContiguousMatches, + bool removeDiacritics) + { + if (string.IsNullOrEmpty(haystack)) { - // Remove IME composition split characters. - var input = needle.Replace("'", string.Empty); - needles.Add(WordsHelper.GetPinyin(input)); - if (WordsHelper.HasChinese(haystack)) + return NoMatchScore; + } + + var tLen = haystack.Length; + + // Fold haystack ONCE + using var tFoldBuffer = new RentedSpan(tLen, stackalloc char[Math.Min(tLen, StackAllocThreshold)]); + Folding.FoldInto(haystack, removeDiacritics, tFoldBuffer.Span); + ReadOnlySpan tFold = tFoldBuffer.Span; + + var qFold = query.GetPrimaryFolded(removeDiacritics); + var best = ScoreCore(query.PrimaryRaw, qFold, haystack, tFold, allowNonContiguousMatches); + + if (!ChinesePinYinSupport || !query.HasSecondary) + { + return best; + } + + var qRawSecondary = query.SecondaryRaw ?? string.Empty; + var qFoldSecondary = query.GetSecondaryFolded(removeDiacritics); + + best = Math.Max(best, ScoreCore(qRawSecondary, qFoldSecondary, haystack, tFold, allowNonContiguousMatches)); + + if (!WordsHelper.HasChinese(haystack)) + { + return best; + } + + // Fold PinYin target ONCE + var tPinYin = WordsHelper.GetPinyin(haystack) ?? string.Empty; + var tPinYinLen = tPinYin.Length; + + using var tPinYinFoldBuffer = new RentedSpan(tPinYinLen, stackalloc char[Math.Min(tPinYinLen, StackAllocThreshold)]); + Folding.FoldInto(tPinYin, removeDiacritics, tPinYinFoldBuffer.Span); + ReadOnlySpan tPinYinFold = tPinYinFoldBuffer.Span; + + best = Math.Max(best, ScoreCore(query.PrimaryRaw, qFold, tPinYin, tPinYinFold, allowNonContiguousMatches)); + best = Math.Max(best, ScoreCore(qRawSecondary, qFoldSecondary, tPinYin, tPinYinFold, allowNonContiguousMatches)); + + return best; + } + + private static (int Score, List Positions) ScoreBestVariantWithPositions( + in PreparedFuzzyQuery query, + string haystack, + bool allowNonContiguousMatches, + bool removeDiacritics) + { + if (string.IsNullOrEmpty(haystack)) + { + return (NoMatchScore, []); + } + + var tLen = haystack.Length; + + // Fold haystack ONCE + using var tFoldBuffer = new RentedSpan(tLen, stackalloc char[Math.Min(tLen, StackAllocThreshold)]); + Folding.FoldInto(haystack, removeDiacritics, tFoldBuffer.Span); + ReadOnlySpan tFold = tFoldBuffer.Span; + + var needsPinYin = ChinesePinYinSupport && query.HasSecondary && WordsHelper.HasChinese(haystack); + var tPinYin = needsPinYin ? (WordsHelper.GetPinyin(haystack) ?? string.Empty) : string.Empty; + var tPinYinLen = tPinYin.Length; + + // Fold PinYin target if needed + using var tPinYinFoldBuffer = new RentedSpan( + needsPinYin ? tPinYinLen : 0, + needsPinYin ? stackalloc char[Math.Min(tPinYinLen, StackAllocThreshold)] : Span.Empty); + + if (needsPinYin) + { + Folding.FoldInto(tPinYin, removeDiacritics, tPinYinFoldBuffer.Span); + } + + ReadOnlySpan tPinYinFold = tPinYinFoldBuffer.Span; + + var qFoldPrimary = query.GetPrimaryFoldedString(removeDiacritics); + + // (primary query, original haystack) - get score AND positions + var (bestScore, bestPositions) = ScoreWithPositionsCore(query.PrimaryRaw, qFoldPrimary, haystack, tFold, allowNonContiguousMatches); + + // (primary query, pinyin target) - score only. + // We only return positions for matches against the original haystack. + // For Pinyin variants, we typically don't show highlights in the UI since there's + // no 1:1 mapping back to the original characters' positions. + if (needsPinYin) + { + var score = ScoreCore(query.PrimaryRaw, qFoldPrimary, tPinYin, tPinYinFold, allowNonContiguousMatches); + if (score > bestScore) { - haystacks.Add(WordsHelper.GetPinyin(haystack)); + bestScore = score; } } - return needles.SelectMany(i => haystacks.Select(j => ScoreFuzzyWithPositionsInternal(i, j, allowNonContiguousMatches))); + if (ChinesePinYinSupport && query.HasSecondary) + { + var qRawSecondary = query.SecondaryRaw ?? string.Empty; + var qFoldSecondary = query.GetSecondaryFoldedString(removeDiacritics) ?? string.Empty; + + // (secondary query, original haystack) - get score AND positions + var (scoreSecondary, positionsSecondary) = ScoreWithPositionsCore( + qRawSecondary, qFoldSecondary, haystack, tFold, allowNonContiguousMatches); + + if (scoreSecondary > bestScore) + { + bestScore = scoreSecondary; + bestPositions = positionsSecondary; + } + + // (secondary query, pinyin target) - score only. + // Highlight positions are not returned for Pinyin variants. + if (needsPinYin) + { + var score = ScoreCore(qRawSecondary, qFoldSecondary, tPinYin, tPinYinFold, allowNonContiguousMatches); + if (score > bestScore) + { + bestScore = score; + } + } + } + + return (bestScore, bestPositions); } - private static (int Score, List Positions) ScoreFuzzyWithPositionsInternal(string needle, string haystack, bool allowNonContiguousMatches) + // ============================================================ + // Core scoring + // ============================================================ + private static int ScoreCore( + ReadOnlySpan qRaw, + ReadOnlySpan qFold, + ReadOnlySpan tRaw, + ReadOnlySpan tFold, + bool allowNonContiguousMatches) { - if (string.IsNullOrEmpty(haystack) || string.IsNullOrEmpty(needle)) + var qLen = qRaw.Length; + var tLen = tRaw.Length; + + if (qLen == 0 || tLen < qLen || qFold.Length != qLen) { - return (NOMATCH, new List()); + return NoMatchScore; } - var target = haystack.ToCharArray(); - var query = needle.ToCharArray(); + return allowNonContiguousMatches + ? ScoreNonContiguous(qRaw, qFold, tRaw, tFold, qLen, tLen) + : ScoreContiguous(qRaw, qFold, tRaw, tFold).Score; + } - if (target.Length < query.Length) + private static (int Score, List Positions) ScoreWithPositionsCore( + ReadOnlySpan qRaw, + ReadOnlySpan qFold, + ReadOnlySpan tRaw, + ReadOnlySpan tFold, + bool allowNonContiguousMatches) + { + var qLen = qRaw.Length; + var tLen = tRaw.Length; + + if (qLen == 0 || tLen < qLen || qFold.Length != qLen) { - return (NOMATCH, new List()); + return (NoMatchScore, []); } - var targetUpper = FoldCase(haystack); - var queryUpper = FoldCase(needle); - var targetUpperChars = targetUpper.ToCharArray(); - var queryUpperChars = queryUpper.ToCharArray(); + return allowNonContiguousMatches + ? ScoreNonContiguousWithPositions(qRaw, qFold, tRaw, tFold, qLen, tLen) + : ScoreContiguousWithPositions(qRaw, qFold, tRaw, tFold); + } - var area = query.Length * target.Length; - var scores = new int[area]; - var matches = new int[area]; - - for (var qi = 0; qi < query.Length; qi++) + // ============================================================ + // Non-contiguous matching (score only) + // ============================================================ + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + [SkipLocalsInit] + private static int ScoreNonContiguous( + ReadOnlySpan qRaw, + ReadOnlySpan qFold, + ReadOnlySpan tRaw, + ReadOnlySpan tFold, + int qLen, + int tLen) + { + if (!Scoring.CanMatchSubsequence(qFold, tFold)) { - var qiOffset = qi * target.Length; - var qiPrevOffset = qi > 0 ? (qi - 1) * target.Length : 0; + return NoMatchScore; + } - for (var ti = 0; ti < target.Length; ti++) + using var dpBuffer = new RentedSpan(tLen * 2, stackalloc int[Math.Min(tLen * 2, StackAllocThreshold)]); + var scores = dpBuffer.Span[..tLen]; + var seqLens = dpBuffer.Span.Slice(tLen, tLen); + scores.Clear(); + seqLens.Clear(); + + for (var qi = 0; qi < qLen; qi++) + { + var qChar = qRaw[qi]; + var qCharFold = qFold[qi]; + + var leftScore = 0; + var diagScore = 0; + var diagSeqLen = 0; + + var isFirstRow = qi == 0; + var tiMax = tLen - qLen + qi; + + for (var ti = 0; ti <= tiMax; ti++) { - var currentIndex = qiOffset + ti; - var diagIndex = (qi > 0 && ti > 0) ? qiPrevOffset + ti - 1 : 0; - var leftScore = ti > 0 ? scores[currentIndex - 1] : 0; - var diagScore = (qi > 0 && ti > 0) ? scores[diagIndex] : 0; - var matchSeqLen = (qi > 0 && ti > 0) ? matches[diagIndex] : 0; + var upScore = scores[ti]; + var upSeqLen = seqLens[ti]; - var score = (diagScore == 0 && qi != 0) ? 0 : - ComputeCharScore( - query[qi], - queryUpperChars[qi], - ti != 0 ? target[ti - 1] : null, - target[ti], - targetUpperChars[ti], - matchSeqLen); - - var isValidScore = score != 0 && diagScore + score >= leftScore && - (allowNonContiguousMatches || qi > 0 || - targetUpperChars.Skip(ti).Take(queryUpperChars.Length).SequenceEqual(queryUpperChars)); - - if (isValidScore) + var charScore = 0; + if (diagScore != 0 || isFirstRow) { - matches[currentIndex] = matchSeqLen + 1; - scores[currentIndex] = diagScore + score; + var tCharFold = tFold[ti]; + if (qCharFold == tCharFold) + { + charScore = Scoring.ComputeCharScore( + qRawChar: qChar, + tHasPrev: ti != 0, + tRawCharPrev: ti != 0 ? tRaw[ti - 1] : '\0', + tRawCharCurr: tRaw[ti], + matchSeqLen: diagSeqLen); + } + } + + var candidateScore = diagScore + charScore; + + if (charScore != 0 && candidateScore >= leftScore) + { + scores[ti] = candidateScore; + seqLens[ti] = diagSeqLen + 1; } else { - matches[currentIndex] = NOMATCH; - scores[currentIndex] = leftScore; + scores[ti] = leftScore; + seqLens[ti] = 0; } + + leftScore = scores[ti]; + diagScore = upScore; + diagSeqLen = upSeqLen; + } + + if (leftScore == 0) + { + return NoMatchScore; + } + + if (qi == qLen - 1) + { + return leftScore; } } - var positions = new List(); - if (query.Length > 0 && target.Length > 0) + return scores[tLen - 1]; + } + + // ============================================================ + // Non-contiguous matching (with positions) + // ============================================================ + private static (int Score, List Positions) ScoreNonContiguousWithPositions( + ReadOnlySpan qRaw, + ReadOnlySpan qFold, + ReadOnlySpan tRaw, + ReadOnlySpan tFold, + int qLen, + int tLen) + { + if (!Scoring.CanMatchSubsequence(qFold, tFold)) { - var qi = query.Length - 1; - var ti = target.Length - 1; + return (NoMatchScore, []); + } - while (true) + var areaLong = (long)qLen * tLen; + if (areaLong is <= 0 or > int.MaxValue) + { + return (NoMatchScore, []); + } + + var area = (int)areaLong; + var bitCount = (area + 63) >> 6; + + using var bitsBuffer = new RentedSpan(bitCount, stackalloc ulong[Math.Min(bitCount, StackAllocThreshold / 8)]); + bitsBuffer.Span.Clear(); + + using var dpBuffer = new RentedSpan(tLen * 2, stackalloc int[Math.Min(tLen * 2, StackAllocThreshold)]); + var scores = dpBuffer.Span[..tLen]; + var seqLens = dpBuffer.Span.Slice(tLen, tLen); + scores.Clear(); + seqLens.Clear(); + + for (var qi = 0; qi < qLen; qi++) + { + var qChar = qRaw[qi]; + var qCharFold = qFold[qi]; + + var leftScore = 0; + var diagScore = 0; + var diagSeqLen = 0; + + var isFirstRow = qi == 0; + var rowBase = qi * tLen; + + for (var ti = 0; ti < tLen; ti++) { - var index = (qi * target.Length) + ti; - if (matches[index] == NOMATCH) - { - if (ti == 0) - { - break; - } + var upScore = scores[ti]; + var upSeqLen = seqLens[ti]; - ti--; + var charScore = 0; + if (diagScore != 0 || isFirstRow) + { + var tCharFold = tFold[ti]; + if (qCharFold == tCharFold) + { + charScore = Scoring.ComputeCharScore( + qRawChar: qChar, + tHasPrev: ti != 0, + tRawCharPrev: ti != 0 ? tRaw[ti - 1] : '\0', + tRawCharCurr: tRaw[ti], + matchSeqLen: diagSeqLen); + } + } + + var candidateScore = diagScore + charScore; + + if (charScore != 0 && candidateScore >= leftScore) + { + scores[ti] = candidateScore; + seqLens[ti] = diagSeqLen + 1; + SetBit(bitsBuffer.Span, rowBase + ti); } else { - positions.Add(ti); - if (qi == 0 || ti == 0) - { - break; - } + scores[ti] = leftScore; + seqLens[ti] = 0; + } - qi--; - ti--; + leftScore = scores[ti]; + diagScore = upScore; + diagSeqLen = upSeqLen; + } + + if (leftScore == 0) + { + return (NoMatchScore, []); + } + } + + var finalScore = scores[tLen - 1]; + if (finalScore == 0) + { + return (NoMatchScore, []); + } + + // Backtrack to find positions + var positions = new List(qLen); + var q = qLen - 1; + var t = tLen - 1; + + while (true) + { + var bitIdx = (q * tLen) + t; + + if (!GetBit(bitsBuffer.Span, bitIdx)) + { + if (t == 0) + { + break; + } + + t--; + } + else + { + positions.Add(t); + if (q == 0 || t == 0) + { + break; + } + + q--; + t--; + } + } + + positions.Reverse(); + return (finalScore, positions); + } + + // ============================================================ + // Contiguous matching + // ============================================================ + private static (int Score, int Start) ScoreContiguous( + ReadOnlySpan qRaw, + ReadOnlySpan qFold, + ReadOnlySpan tRaw, + ReadOnlySpan tFold) + { + var qLen = qRaw.Length; + var tLen = tRaw.Length; + + if (qLen == 0 || tLen == 0 || tLen < qLen) + { + return (NoMatchScore, -1); + } + + var bestScore = NoMatchScore; + var bestStart = -1; + var searchStart = 0; + + while (searchStart <= tLen - qLen) + { + var relativeIdx = tFold.Slice(searchStart).IndexOf(qFold); + if (relativeIdx < 0) + { + break; + } + + var matchStart = searchStart + relativeIdx; + var score = 0; + + for (var i = 0; i < qLen; i++) + { + var ti = matchStart + i; + score += Scoring.ComputeCharScore( + qRawChar: qRaw[i], + tHasPrev: ti != 0, + tRawCharPrev: ti != 0 ? tRaw[ti - 1] : '\0', + tRawCharCurr: tRaw[ti], + matchSeqLen: i); + } + + if (score >= bestScore) + { + bestScore = score; + bestStart = matchStart; + } + + searchStart = matchStart + 1; + } + + return (bestScore, bestStart); + } + + private static (int Score, List Positions) ScoreContiguousWithPositions( + ReadOnlySpan qRaw, + ReadOnlySpan qFold, + ReadOnlySpan tRaw, + ReadOnlySpan tFold) + { + var (score, bestStart) = ScoreContiguous(qRaw, qFold, tRaw, tFold); + + if (bestStart < 0 || score == NoMatchScore) + { + return (NoMatchScore, []); + } + + var qLen = qRaw.Length; + var positions = new List(qLen); + for (var i = 0; i < qLen; i++) + { + positions.Add(bestStart + i); + } + + return (score, positions); + } + + // ============================================================ + // Bit manipulation helpers + // ============================================================ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void SetBit(Span words, int idx) + { + words[idx >> 6] |= 1UL << (idx & 63); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static bool GetBit(ReadOnlySpan words, int idx) + { + return ((words[idx >> 6] >> (idx & 63)) & 1UL) != 0; + } + + // ============================================================ + // Scoring helpers + // ============================================================ + private static class Scoring + { + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + internal static bool CanMatchSubsequence(ReadOnlySpan qFold, ReadOnlySpan tFold) + { + var qi = 0; + var qLen = qFold.Length; + + foreach (var tChar in tFold) + { + if (qi < qLen && qFold[qi] == tChar) + { + qi++; } } - positions.Reverse(); + return qi == qLen; } - return (scores[area - 1], positions); - } - - private static string FoldCase(string input) - { - return input.ToUpperInvariant(); - } - - private static int ComputeCharScore( - char query, - char queryLower, - char? targetPrev, - char targetCurr, - char targetLower, - int matchSeqLen) - { - if (!ConsiderAsEqual(queryLower, targetLower)) + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + internal static int ComputeCharScore( + char qRawChar, + bool tHasPrev, + char tRawCharPrev, + char tRawCharCurr, + int matchSeqLen) { - return 0; - } + var score = Bonus.CharacterMatch; - var score = 1; // Character match bonus - - if (matchSeqLen > 0) - { - score += matchSeqLen * 5; // Consecutive match bonus - } - - if (query == targetCurr) - { - score += 1; // Same case bonus - } - - if (targetPrev.HasValue) - { - var sepBonus = ScoreSeparator(targetPrev.Value); - if (sepBonus > 0) + if (matchSeqLen > 0) { - score += sepBonus; + score += matchSeqLen * Bonus.ConsecutiveMultiplier; } - else if (char.IsUpper(targetCurr) && matchSeqLen == 0) + + var tCharCurrIsUpper = char.IsUpper(tRawCharCurr); + if (qRawChar == tRawCharCurr) { - score += 2; // CamelCase bonus + score += Bonus.ExactCase; + } + + if (!tHasPrev) + { + return score + Bonus.StringStart; + } + + var separatorBonus = GetSeparatorBonus(tRawCharPrev); + if (separatorBonus != 0) + { + return score + separatorBonus; + } + + if (matchSeqLen == 0 && tCharCurrIsUpper) + { + return score + Bonus.CamelCase; + } + + return score; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + private static int GetSeparatorBonus(char ch) + { + return ch switch + { + '/' or '\\' => Bonus.PathSeparator, + '_' or '-' or '.' or ' ' or '\'' or '"' or ':' => Bonus.WordSeparator, + _ => 0, + }; + } + } + + // ============================================================ + // Text folding + // ============================================================ + + // Folding: slash normalization + upper case + optional diacritics stripping + private static class Folding + { + // Cache maps an upper case char to its diacritics-stripped upper case char. + // '\0' means "not cached yet". + private static readonly char[] StripCacheUpper = new char[char.MaxValue + 1]; + + /// + /// Folds into : + /// - Normalizes slashes: '\' -> '/' + /// - Upper case with char.ToUpperInvariant (length-preserving) + /// - Optionally strips diacritics (length-preserving) + /// + public static void FoldInto(ReadOnlySpan input, bool removeDiacritics, Span dest) + { + // Assumes dest.Length >= input.Length. + if (!removeDiacritics) + { + for (var i = 0; i < input.Length; i++) + { + var c = input[i]; + dest[i] = c == '\\' ? '/' : char.ToUpperInvariant(c); + } + + return; + } + + // ASCII cannot have diacritics (and ToUpperInvariant is cheap), but we STILL normalize slashes. + if (Ascii.IsValid(input)) + { + for (var i = 0; i < input.Length; i++) + { + var c = input[i]; + dest[i] = c == '\\' ? '/' : char.ToUpperInvariant(c); + } + + return; + } + + // Non-ASCII + removeDiacritics + for (var i = 0; i < input.Length; i++) + { + var c = input[i]; + var upper = c == '\\' ? '/' : char.ToUpperInvariant(c); + dest[i] = StripDiacriticsFromUpper(upper); } } - else + + /// + /// Creates a folded string for fast equality comparisons: + /// - ALWAYS normalizes slashes: '\' -> '/' + /// - Upper case with char.ToUpperInvariant (length-preserving) + /// - Optionally strips diacritics (length-preserving) + /// + /// Returns the original when it is already in the desired form. + /// + public static string FoldForComparison(string input, bool removeDiacritics) { - score += 8; // Start of word bonus + if (string.IsNullOrEmpty(input)) + { + return string.Empty; + } + + // If already fully normalized (slashes + casing), return input without allocating. + // Note: when removeDiacritics==true we still must run diacritics stripping on non-ASCII, + // so the "no-op" path is only safe if removeDiacritics==false OR input is ASCII. + if (!removeDiacritics) + { + if (IsAlreadyFoldedAndSlashNormalized(input)) + { + return input; + } + + return string.Create(input.Length, input, static (dst, src) => + { + for (var i = 0; i < src.Length; i++) + { + var c = src[i]; + dst[i] = c == '\\' ? '/' : char.ToUpperInvariant(c); + } + }); + } + + // removeDiacritics == true + if (Ascii.IsValid(input)) + { + // IMPORTANT: still normalize slashes for ASCII so caller can do simple equality checks. + if (IsAlreadyFoldedAndSlashNormalized(input)) + { + return input; + } + + return string.Create(input.Length, input, static (dst, src) => + { + for (var i = 0; i < src.Length; i++) + { + var c = src[i]; + dst[i] = c == '\\' ? '/' : char.ToUpperInvariant(c); + } + }); + } + + // Non-ASCII + removeDiacritics: must fold + strip (and still normalize slashes). + return string.Create(input.Length, input, static (dst, src) => + { + for (var i = 0; i < src.Length; i++) + { + var c = src[i]; + var upper = c == '\\' ? '/' : char.ToUpperInvariant(c); + dst[i] = StripDiacriticsFromUpper(upper); + } + }); } - return score; - } - - private static bool ConsiderAsEqual(char a, char b) - { - return a == b || (a == '/' && b == '\\') || (a == '\\' && b == '/'); - } - - private static int ScoreSeparator(char ch) - { - return ch switch + // ============================================================ + // "No-op" detector (fast, avoids ToUpperInvariant per char for CJK) + // ============================================================ + private static bool IsAlreadyFoldedAndSlashNormalized(string input) { - '/' or '\\' => 5, - '_' or '-' or '.' or ' ' or '\'' or '"' or ':' => 4, - _ => 0, - }; + var sawNonAscii = false; + + // Tier 1: cheap ASCII checks. + for (var i = 0; i < input.Length; i++) + { + var c = input[i]; + + if (c == '\\') + { + return false; + } + + // ASCII lowercase present => would change. + if ((uint)(c - 'a') <= ('z' - 'a')) + { + return false; + } + + if (c > 0x7F) + { + sawNonAscii = true; + } + } + + // Tier 2: only when non-ASCII exists; avoid char.ToUpperInvariant for scripts without case. + if (sawNonAscii) + { + for (var i = 0; i < input.Length; i++) + { + var c = input[i]; + if (c <= 0x7F) + { + continue; + } + + var cat = CharUnicodeInfo.GetUnicodeCategory(c); + + // Lowercase/Titlecase letters will change under ToUpperInvariant. + if (cat is UnicodeCategory.LowercaseLetter or UnicodeCategory.TitlecaseLetter) + { + return false; + } + } + } + + return true; + } + + // ============================================================ + // Diacritics stripping (cached; input is expected to be uppercase already) + // ============================================================ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static char StripDiacriticsFromUpper(char upper) + { + if (upper <= 0x7F) + { + return upper; + } + + var cached = StripCacheUpper[upper]; + if (cached != '\0') + { + return cached; + } + + var mapped = StripDiacriticsSlow(upper); + StripCacheUpper[upper] = mapped; + return mapped; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static char StripDiacriticsSlow(char upper) + { + var baseChar = FirstNonMark(upper, NormalizationForm.FormD); + if (baseChar == '\0' || baseChar == upper) + { + var kd = FirstNonMark(upper, NormalizationForm.FormKD); + if (kd != '\0') + { + baseChar = kd; + } + } + + // Ensure result remains uppercase invariant. + return char.ToUpperInvariant(baseChar == '\0' ? upper : baseChar); + + static char FirstNonMark(char c, NormalizationForm form) + { + var normalized = c.ToString().Normalize(form); + + foreach (var ch in normalized) + { + var cat = CharUnicodeInfo.GetUnicodeCategory(ch); + if (cat is not (UnicodeCategory.NonSpacingMark + or UnicodeCategory.SpacingCombiningMark + or UnicodeCategory.EnclosingMark)) + { + return ch; + } + } + + return '\0'; + } + } + } + + // ============================================================ + // Text utilities + // ============================================================ + private static class Text + { + internal static string RemoveApostrophes(ReadOnlySpan input) + { + var firstIdx = input.IndexOf('\''); + if (firstIdx < 0) + { + return input.ToString(); + } + + var count = 1; + for (var i = firstIdx + 1; i < input.Length; i++) + { + if (input[i] == '\'') + { + count++; + } + } + + return string.Create(input.Length - count, input.ToString(), static (dest, src) => + { + var destIdx = 0; + foreach (var c in src) + { + if (c != '\'') + { + dest[destIdx++] = c; + } + } + }); + } + } + + // ============================================================ + // Scoring bonuses + // ============================================================ + private static class Bonus + { + public const int CharacterMatch = 1; + public const int ConsecutiveMultiplier = 5; + public const int ExactCase = 1; + public const int StringStart = 8; + public const int PathSeparator = 5; + public const int WordSeparator = 4; + public const int CamelCase = 2; + } + + // ============================================================ + // Memory management + // ============================================================ + private ref struct RentedSpan + { + private readonly Span _span; + private T[]? _poolArray; + + public readonly Span Span => _span; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public RentedSpan(int length, Span stackBuffer) + { + if (length <= stackBuffer.Length) + { + _poolArray = null; + _span = stackBuffer[..length]; + } + else + { + _poolArray = ArrayPool.Shared.Rent(length); + _span = new Span(_poolArray, 0, length); + } + } + + public static implicit operator Span(RentedSpan rented) => rented._span; + + public static implicit operator ReadOnlySpan(RentedSpan rented) => rented._span; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Dispose() + { + var toReturn = _poolArray; + if (toReturn != null) + { + _poolArray = null; + ArrayPool.Shared.Return(toReturn, clearArray: RuntimeHelpers.IsReferenceOrContainsReferences()); + } + } + } + + // ============================================================ + // Prepared query + // ============================================================ + private readonly struct PreparedFuzzyQuery + { + public readonly string PrimaryRaw; + internal readonly string? SecondaryRaw; + + internal readonly string PrimaryFolded; + internal readonly string? PrimaryFoldedNoDiacritics; + + internal readonly string? SecondaryFolded; + internal readonly string? SecondaryFoldedNoDiacritics; + + internal PreparedFuzzyQuery(string primaryRaw, bool precomputeNoDiacritics) + { + PrimaryRaw = primaryRaw ?? string.Empty; + + PrimaryFolded = Folding.FoldForComparison(PrimaryRaw, removeDiacritics: false); + PrimaryFoldedNoDiacritics = precomputeNoDiacritics + ? Folding.FoldForComparison(PrimaryRaw, removeDiacritics: true) + : null; + + if (ChinesePinYinSupport) + { + var input = Text.RemoveApostrophes(PrimaryRaw); + SecondaryRaw = WordsHelper.GetPinyin(input) ?? string.Empty; + + SecondaryFolded = Folding.FoldForComparison(SecondaryRaw, removeDiacritics: false); + SecondaryFoldedNoDiacritics = precomputeNoDiacritics + ? Folding.FoldForComparison(SecondaryRaw, removeDiacritics: true) + : null; + } + else + { + SecondaryRaw = null; + SecondaryFolded = null; + SecondaryFoldedNoDiacritics = null; + } + } + + internal bool HasSecondary => SecondaryFolded is not null; + + internal string GetPrimaryFoldedString(bool removeDiacritics) + { + return !removeDiacritics + ? PrimaryFolded + : (PrimaryFoldedNoDiacritics ?? Folding.FoldForComparison(PrimaryRaw, removeDiacritics: true)); + } + + internal string? GetSecondaryFoldedString(bool removeDiacritics) + { + if (SecondaryFolded is null) + { + return null; + } + + return !removeDiacritics + ? SecondaryFolded + : (SecondaryFoldedNoDiacritics ?? Folding.FoldForComparison(SecondaryRaw ?? string.Empty, removeDiacritics: true)); + } + + internal ReadOnlySpan GetPrimaryFolded(bool removeDiacritics) + { + return GetPrimaryFoldedString(removeDiacritics).AsSpan(); + } + + internal ReadOnlySpan GetSecondaryFolded(bool removeDiacritics) + { + return GetSecondaryFoldedString(removeDiacritics).AsSpan(); + } + } + + // ============================================================ + // Thread-local query cache + // ============================================================ + private static class PreparedFuzzyQueryThreadCache + { + [ThreadStatic] + private static Cache? _cache; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static Cache GetCache() + { + return _cache ??= new Cache(); + } + + public static void Clear() + { + _cache = null; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static PreparedFuzzyQuery GetOrPrepare(string? needle, bool removeDiacritics) + { + needle ??= string.Empty; + + var cache = GetCache(); + + if (string.Equals(cache.Needle, needle, StringComparison.Ordinal)) + { + if (!removeDiacritics || cache.HasDiacriticsVersion) + { + return cache.Query; + } + + cache.Query = PrepareQuery(needle, true); + cache.HasDiacriticsVersion = true; + return cache.Query; + } + + cache.Needle = needle; + cache.Query = PrepareQuery(needle, removeDiacritics); + cache.HasDiacriticsVersion = removeDiacritics; + return cache.Query; + } + + private sealed class Cache + { + public string? Needle { get; set; } + + public PreparedFuzzyQuery Query { get; set; } + + public bool HasDiacriticsVersion { get; set; } + } } } diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Microsoft.CommandPalette.Extensions.Toolkit.csproj b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Microsoft.CommandPalette.Extensions.Toolkit.csproj index 24f2c50c5a..0e72a8a51a 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Microsoft.CommandPalette.Extensions.Toolkit.csproj +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Microsoft.CommandPalette.Extensions.Toolkit.csproj @@ -56,7 +56,7 @@ PreserveNewest - + @@ -83,4 +83,14 @@ IL2081;$(WarningsNotAsErrors) + + + + + + + + + + diff --git a/src/settings-ui/Settings.UI/SettingsXAML/OOBE/Views/OobeColorPicker.xaml b/src/settings-ui/Settings.UI/SettingsXAML/OOBE/Views/OobeColorPicker.xaml index 115bdd417a..1700a3b05f 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/OOBE/Views/OobeColorPicker.xaml +++ b/src/settings-ui/Settings.UI/SettingsXAML/OOBE/Views/OobeColorPicker.xaml @@ -21,6 +21,7 @@