mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-30 00:46:20 +01:00
Compare commits
8 Commits
leilzh/fuz
...
user/yeela
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a0cafb99c6 | ||
|
|
d314fa075e | ||
|
|
90723d5b12 | ||
|
|
f2a5505601 | ||
|
|
311ab88ec3 | ||
|
|
c04400e7df | ||
|
|
05218e8af6 | ||
|
|
6cf73ce839 |
9
.github/actions/spell-check/expect.txt
vendored
9
.github/actions/spell-check/expect.txt
vendored
@@ -273,6 +273,7 @@ CURSORINFO
|
||||
cursorpos
|
||||
customaction
|
||||
CUSTOMACTIONTEST
|
||||
CUSTOMFORMATPLACEHOLDER
|
||||
CVal
|
||||
cvd
|
||||
CVirtual
|
||||
@@ -517,7 +518,6 @@ FRAMECHANGED
|
||||
frm
|
||||
Froml
|
||||
FROMTOUCH
|
||||
fsanitize
|
||||
fsmgmt
|
||||
FZE
|
||||
gacutil
|
||||
@@ -629,7 +629,6 @@ HOTKEYF
|
||||
hotkeys
|
||||
hotlight
|
||||
hotspot
|
||||
Hostx
|
||||
HPAINTBUFFER
|
||||
HRAWINPUT
|
||||
HREDRAW
|
||||
@@ -1007,6 +1006,7 @@ netsh
|
||||
newcolor
|
||||
NEWDIALOGSTYLE
|
||||
NEWFILE
|
||||
NEWFILEHEADER
|
||||
newitem
|
||||
newpath
|
||||
newplus
|
||||
@@ -1077,7 +1077,6 @@ NOTRACK
|
||||
NOTSRCCOPY
|
||||
NOTSRCERASE
|
||||
NOTXORPEN
|
||||
notwindows
|
||||
NOZORDER
|
||||
NPH
|
||||
npmjs
|
||||
@@ -1302,6 +1301,7 @@ QUNS
|
||||
QXZ
|
||||
RAII
|
||||
RAlt
|
||||
Rappl
|
||||
randi
|
||||
Rasterization
|
||||
Rasterize
|
||||
@@ -1398,7 +1398,6 @@ sacl
|
||||
safeprojectname
|
||||
SAMEKEYPREVIOUSLYMAPPED
|
||||
SAMESHORTCUTPREVIOUSLYMAPPED
|
||||
sancov
|
||||
SAVEFAILED
|
||||
scanled
|
||||
schedtasks
|
||||
@@ -1651,6 +1650,7 @@ telephon
|
||||
templatenamespace
|
||||
testprocess
|
||||
TEXCOORD
|
||||
TEXTBOXNEWLINE
|
||||
TEXTEXTRACTOR
|
||||
TEXTINCLUDE
|
||||
tfopen
|
||||
@@ -1932,7 +1932,6 @@ WUX
|
||||
Wwanpp
|
||||
XAxis
|
||||
xclip
|
||||
xcopy
|
||||
XDocument
|
||||
XElement
|
||||
xfd
|
||||
|
||||
@@ -220,6 +220,7 @@
|
||||
"WinUI3Apps\\PowerToys.Settings.exe",
|
||||
|
||||
"PowerToys.CmdPalModuleInterface.dll",
|
||||
"CmdPalKeyboardService.dll",
|
||||
"*Microsoft.CmdPal.UI_*.msix"
|
||||
],
|
||||
"SigningInfo": {
|
||||
|
||||
34
NOTICE.md
34
NOTICE.md
@@ -75,6 +75,40 @@ OTHER DEALINGS IN THE SOFTWARE.
|
||||
For more information, please refer to <http://unlicense.org/>
|
||||
```
|
||||
|
||||
## Utility: Command Palette Built-in Extensions
|
||||
|
||||
### Calculator
|
||||
|
||||
#### Mages
|
||||
|
||||
We use the Mages NuGet package for calculating the result of expression.
|
||||
|
||||
**Source**: [https://github.com/FlorianRappl/Mages](https://github.com/FlorianRappl/Mages)
|
||||
|
||||
```
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 - 2025 Florian Rappl
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
```
|
||||
|
||||
## Utility: File Explorer Add-ins
|
||||
|
||||
### Monaco Editor
|
||||
|
||||
@@ -708,8 +708,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.CmdPal.Ext.System
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CmdPalKeyboardService", "src\modules\cmdpal\CmdPalKeyboardService\CmdPalKeyboardService.vcxproj", "{5F63C743-F6CE-4DBA-A200-2B3F8A14E8C2}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PowerRename.FuzzingTest", "src\modules\powerrename\PowerRename.FuzzingTest\PowerRename.FuzzingTest.vcxproj", "{2694E2FB-DCD5-4BFF-A418-B6C3C7CE3B8E}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|ARM64 = Debug|ARM64
|
||||
@@ -2590,12 +2588,6 @@ Global
|
||||
{5F63C743-F6CE-4DBA-A200-2B3F8A14E8C2}.Release|ARM64.Build.0 = Release|ARM64
|
||||
{5F63C743-F6CE-4DBA-A200-2B3F8A14E8C2}.Release|x64.ActiveCfg = Release|x64
|
||||
{5F63C743-F6CE-4DBA-A200-2B3F8A14E8C2}.Release|x64.Build.0 = Release|x64
|
||||
{2694E2FB-DCD5-4BFF-A418-B6C3C7CE3B8E}.Debug|ARM64.ActiveCfg = Debug|ARM64
|
||||
{2694E2FB-DCD5-4BFF-A418-B6C3C7CE3B8E}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{2694E2FB-DCD5-4BFF-A418-B6C3C7CE3B8E}.Debug|x64.Build.0 = Debug|x64
|
||||
{2694E2FB-DCD5-4BFF-A418-B6C3C7CE3B8E}.Release|ARM64.ActiveCfg = Release|ARM64
|
||||
{2694E2FB-DCD5-4BFF-A418-B6C3C7CE3B8E}.Release|x64.ActiveCfg = Release|x64
|
||||
{2694E2FB-DCD5-4BFF-A418-B6C3C7CE3B8E}.Release|x64.Build.0 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
@@ -2867,7 +2859,6 @@ Global
|
||||
{5702B3CC-8575-48D5-83D8-15BB42269CD3} = {929C1324-22E8-4412-A9A8-80E85F3985A5}
|
||||
{64B88F02-CD88-4ED8-9624-989A800230F9} = {ECB8E0D1-7603-4E5C-AB10-D1E545E6F8E2}
|
||||
{5F63C743-F6CE-4DBA-A200-2B3F8A14E8C2} = {3846508C-77EB-4034-A702-F8BB263C4F79}
|
||||
{2694E2FB-DCD5-4BFF-A418-B6C3C7CE3B8E} = {89E20BCE-EB9C-46C8-8B50-E01A82E6FDC3}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {C3A2F9D1-7930-4EF4-A6FC-7EE0A99821D0}
|
||||
|
||||
@@ -1,117 +0,0 @@
|
||||
# 🧪 C++ Project Fuzzing Test Guide
|
||||
|
||||
This guide walks you through setting up a **fuzzing test** project for a C++ module using [libFuzzer](https://llvm.org/docs/LibFuzzer.html).
|
||||
.
|
||||
|
||||
---
|
||||
|
||||
## 🏗️ Step-by-Step Setup
|
||||
|
||||
### 1. Create a New C++ Project
|
||||
|
||||
- Use **Empty Project** template.
|
||||
- Name it `<ModuleName>.FuzzingTest`.
|
||||
|
||||
---
|
||||
|
||||
### 2. Update Build Configuration
|
||||
|
||||
- In **Configuration Manager**, Uncheck Build for both Release|ARM64, Debug|ARM64 and Debug|x64 configurations.
|
||||
- Note: ARM64 is not supported in this case, so leave ARM64 configurations build disabled.
|
||||
---
|
||||
|
||||
### 3. Enable ASan and libFuzzer in `.vcxproj`
|
||||
|
||||
Edit the project file to enable fuzzing:
|
||||
|
||||
```xml
|
||||
<PropertyGroup>
|
||||
<EnableASAN>true</EnableASAN>
|
||||
<EnableFuzzer>true</EnableFuzzer>
|
||||
</PropertyGroup>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 4. Add Fuzzing Compiler Flags
|
||||
|
||||
Add this to `AdditionalOptions` under the `Fuzzing` configuration:
|
||||
|
||||
```xml
|
||||
/fsanitize=address
|
||||
/fsanitize-coverage=inline-8bit-counters
|
||||
/fsanitize-coverage=edge
|
||||
/fsanitize-coverage=trace-cmp
|
||||
/fsanitize-coverage=trace-div
|
||||
%(AdditionalOptions)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 5. Link the Sanitizer Coverage Runtime
|
||||
|
||||
In `Linker → Input → Additional Dependencies`, add:
|
||||
|
||||
```text
|
||||
$(VCToolsInstallDir)lib\$(Platform)\libsancov.lib
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 6. Copy Required Runtime DLL
|
||||
|
||||
Add a `PostBuildEvent` to copy the ASAN DLL:
|
||||
|
||||
```xml
|
||||
<Command>
|
||||
xcopy /y "$(VCToolsInstallDir)bin\Hostx64\x64\clang_rt.asan_dynamic-x86_64.dll" "$(OutDir)"
|
||||
</Command>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 7. Add Preprocessor Definitions
|
||||
|
||||
To avoid annotation issues, add these to the `Preprocessor Definitions`:
|
||||
|
||||
```text
|
||||
_DISABLE_VECTOR_ANNOTATION;_DISABLE_STRING_ANNOTATION
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🧬 Required Code
|
||||
|
||||
### `LLVMFuzzerTestOneInput` Entry Point
|
||||
|
||||
Every fuzzing project must expose this function:
|
||||
|
||||
```cpp
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
|
||||
{
|
||||
std::string input(reinterpret_cast<const char*>(data), size);
|
||||
|
||||
try
|
||||
{
|
||||
// Call your module with the input here.
|
||||
}
|
||||
catch (...) {}
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ⚙️ [Test run in the cloud](https://eng.ms/docs/cloud-ai-platform/azure-edge-platform-aep/aep-security/epsf-edge-and-platform-security-fundamentals/the-onefuzz-service/onefuzz/faq/notwindows/walkthrough)
|
||||
|
||||
To submit a job to the cloud you can run with this command:
|
||||
|
||||
```
|
||||
oip submit --config .\OneFuzzConfig.json --drop-path <your_submission_directory> --platform windows --do-not-file-bugs --duration 1
|
||||
```
|
||||
You want to run with --do-not-file-bugs because if there is an issue with running the parser in the cloud (which is very possible), you don't want bugs to be created if there is an issue. The --duration task is the number of hours you want the task to run. I recommend just running for 1 hour to make sure things work initially. If you don't specify this parameter, it will default to 48 hours. You can find more about submitting a test job here.
|
||||
|
||||
OneFuzz will send you an email when the job has started.
|
||||
|
||||
---
|
||||
@@ -7,11 +7,15 @@ using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Microsoft.CmdPal.UI.ViewModels.Messages;
|
||||
using Microsoft.CommandPalette.Extensions;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
using Windows.System;
|
||||
|
||||
namespace Microsoft.CmdPal.UI.ViewModels;
|
||||
|
||||
public partial class CommandBarViewModel : ObservableObject,
|
||||
IRecipient<UpdateCommandBarMessage>
|
||||
IRecipient<UpdateCommandBarMessage>,
|
||||
IRecipient<UpdateItemKeybindingsMessage>
|
||||
{
|
||||
public ICommandBarContext? SelectedItem
|
||||
{
|
||||
@@ -49,13 +53,18 @@ public partial class CommandBarViewModel : ObservableObject,
|
||||
[ObservableProperty]
|
||||
public partial ObservableCollection<CommandContextItemViewModel> ContextCommands { get; set; } = [];
|
||||
|
||||
private Dictionary<KeyChord, CommandContextItemViewModel>? _contextKeybindings;
|
||||
|
||||
public CommandBarViewModel()
|
||||
{
|
||||
WeakReferenceMessenger.Default.Register<UpdateCommandBarMessage>(this);
|
||||
WeakReferenceMessenger.Default.Register<UpdateItemKeybindingsMessage>(this);
|
||||
}
|
||||
|
||||
public void Receive(UpdateCommandBarMessage message) => SelectedItem = message.ViewModel;
|
||||
|
||||
public void Receive(UpdateItemKeybindingsMessage message) => _contextKeybindings = message.Keys;
|
||||
|
||||
private void SetSelectedItem(ICommandBarContext? value)
|
||||
{
|
||||
if (value != null)
|
||||
@@ -131,4 +140,22 @@ public partial class CommandBarViewModel : ObservableObject,
|
||||
WeakReferenceMessenger.Default.Send<PerformCommandMessage>(new(SecondaryCommand.Command.Model, SecondaryCommand.Model));
|
||||
}
|
||||
}
|
||||
|
||||
public bool CheckKeybinding(bool ctrl, bool alt, bool shift, bool win, VirtualKey key)
|
||||
{
|
||||
if (_contextKeybindings != null)
|
||||
{
|
||||
// Does the pressed key match any of the keybindings?
|
||||
var pressedKeyChord = KeyChordHelpers.FromModifiers(ctrl, alt, shift, win, key, 0);
|
||||
if (_contextKeybindings.TryGetValue(pressedKeyChord, out var item))
|
||||
{
|
||||
// TODO GH #245: This is a bit of a hack, but we need to make sure that the keybindings are updated before we send the message
|
||||
// so that the correct item is activated.
|
||||
WeakReferenceMessenger.Default.Send<PerformCommandMessage>(new(item));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,12 +9,16 @@ namespace Microsoft.CmdPal.UI.ViewModels;
|
||||
|
||||
public partial class CommandContextItemViewModel(ICommandContextItem contextItem, WeakReference<IPageContext> context) : CommandItemViewModel(new(contextItem), context)
|
||||
{
|
||||
private readonly KeyChord nullKeyChord = new(0, 0, 0);
|
||||
|
||||
public new ExtensionObject<ICommandContextItem> Model { get; } = new(contextItem);
|
||||
|
||||
public bool IsCritical { get; private set; }
|
||||
|
||||
public KeyChord? RequestedShortcut { get; private set; }
|
||||
|
||||
public bool HasRequestedShortcut => RequestedShortcut != null && (RequestedShortcut.Value != nullKeyChord);
|
||||
|
||||
public override void InitializeProperties()
|
||||
{
|
||||
if (IsInitialized)
|
||||
@@ -31,6 +35,9 @@ public partial class CommandContextItemViewModel(ICommandContextItem contextItem
|
||||
}
|
||||
|
||||
IsCritical = contextItem.IsCritical;
|
||||
|
||||
// I actually don't think this will ever actually be null, because
|
||||
// KeyChord is a struct, which isn't nullable in WinRT
|
||||
if (contextItem.RequestedShortcut != null)
|
||||
{
|
||||
RequestedShortcut = new(
|
||||
|
||||
@@ -398,6 +398,23 @@ public partial class CommandItemViewModel : ExtensionObjectViewModel, ICommandBa
|
||||
base.SafeCleanup();
|
||||
Initialized |= InitializedState.CleanedUp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a mapping of key -> command item for this particular item's
|
||||
/// MoreCommands. (This won't include the primary Command, but it will
|
||||
/// include the secondary one). This map can be used to quickly check if a
|
||||
/// shortcut key was pressed
|
||||
/// </summary>
|
||||
/// <returns>a dictionary of KeyChord -> Context commands, for all commands
|
||||
/// that have a shortcut key set.</returns>
|
||||
internal Dictionary<KeyChord, CommandContextItemViewModel> Keybindings()
|
||||
{
|
||||
return MoreCommands
|
||||
.Where(c => c.HasRequestedShortcut)
|
||||
.ToDictionary(
|
||||
c => c.RequestedShortcut ?? new KeyChord(0, 0, 0),
|
||||
c => c);
|
||||
}
|
||||
}
|
||||
|
||||
[Flags]
|
||||
|
||||
@@ -344,6 +344,8 @@ public partial class ListViewModel : PageViewModel, IDisposable
|
||||
{
|
||||
WeakReferenceMessenger.Default.Send<UpdateCommandBarMessage>(new(item));
|
||||
|
||||
WeakReferenceMessenger.Default.Send<UpdateItemKeybindingsMessage>(new(item.Keybindings()));
|
||||
|
||||
if (ShowDetails && item.HasDetails)
|
||||
{
|
||||
WeakReferenceMessenger.Default.Send<ShowDetailsMessage>(new(item.Details));
|
||||
|
||||
@@ -51,6 +51,12 @@ public record PerformCommandMessage
|
||||
Context = context.Unsafe;
|
||||
}
|
||||
|
||||
public PerformCommandMessage(CommandContextItemViewModel contextCommand)
|
||||
{
|
||||
Command = contextCommand.Command.Model;
|
||||
Context = contextCommand.Model.Unsafe;
|
||||
}
|
||||
|
||||
public PerformCommandMessage(ConfirmResultViewModel vm)
|
||||
{
|
||||
Command = vm.PrimaryCommand.Model;
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
// 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;
|
||||
|
||||
namespace Microsoft.CmdPal.UI.ViewModels.Messages;
|
||||
|
||||
public record UpdateItemKeybindingsMessage(Dictionary<KeyChord, CommandContextItemViewModel>? Keys);
|
||||
@@ -36,7 +36,7 @@
|
||||
|
||||
<!-- Template for actions in the mode actions dropdown button -->
|
||||
<DataTemplate x:Key="ContextMenuViewModelTemplate" x:DataType="viewmodels:CommandContextItemViewModel">
|
||||
<Grid>
|
||||
<Grid AutomationProperties.Name="{x:Bind Title, Mode=OneWay}">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="32" />
|
||||
<ColumnDefinition Width="*" />
|
||||
@@ -53,37 +53,16 @@
|
||||
Grid.Column="1"
|
||||
VerticalAlignment="Center"
|
||||
Text="{x:Bind Title, Mode=OneWay}" />
|
||||
<!--<TextBlock
|
||||
<TextBlock
|
||||
Grid.Column="2"
|
||||
Margin="16,0,0,0"
|
||||
HorizontalAlignment="Right"
|
||||
VerticalAlignment="Center"
|
||||
Foreground="{ThemeResource MenuFlyoutItemKeyboardAcceleratorTextForeground}"
|
||||
Style="{StaticResource CaptionTextBlockStyle}"
|
||||
Text="{x:Bind RequestedShortcut, Mode=OneWay, Converter={StaticResource KeyChordToStringConverter}}" />-->
|
||||
Text="{x:Bind RequestedShortcut, Mode=OneWay, Converter={StaticResource KeyChordToStringConverter}}" />
|
||||
</Grid>
|
||||
</DataTemplate>
|
||||
|
||||
<animations:ImplicitAnimationSet x:Name="ShowAnimations">
|
||||
<animations:OpacityAnimation
|
||||
From="0"
|
||||
To="1.0"
|
||||
Duration="0:0:0.340" />
|
||||
<animations:ScaleAnimation
|
||||
From="0.85"
|
||||
To="1"
|
||||
Duration="0:0:0.350" />
|
||||
</animations:ImplicitAnimationSet>
|
||||
<animations:ImplicitAnimationSet x:Name="HideAnimations">
|
||||
<animations:OpacityAnimation
|
||||
From="1.0"
|
||||
To="0"
|
||||
Duration="0:0:0.240" />
|
||||
<animations:ScaleAnimation
|
||||
From="1"
|
||||
To="0.85"
|
||||
Duration="0:0:0.350" />
|
||||
</animations:ImplicitAnimationSet>
|
||||
</ResourceDictionary>
|
||||
</UserControl.Resources>
|
||||
|
||||
@@ -135,9 +114,6 @@
|
||||
<Button
|
||||
x:Name="SettingsIconButton"
|
||||
x:Uid="SettingsButton"
|
||||
animations:Implicit.HideAnimations="{StaticResource HideAnimations}"
|
||||
animations:Implicit.ShowAnimations="{StaticResource ShowAnimations}"
|
||||
ui:VisualExtensions.NormalizedCenterPoint="0.5,0.5"
|
||||
Style="{StaticResource SubtleButtonStyle}"
|
||||
Tapped="SettingsIcon_Tapped"
|
||||
Visibility="{x:Bind CurrentPageViewModel.IsNested, Mode=OneWay, Converter={StaticResource BoolToInvertedVisibilityConverter}}">
|
||||
@@ -167,10 +143,8 @@
|
||||
<Button
|
||||
x:Name="PrimaryButton"
|
||||
Padding="6,4,4,4"
|
||||
animations:Implicit.HideAnimations="{StaticResource HideAnimations}"
|
||||
animations:Implicit.ShowAnimations="{StaticResource ShowAnimations}"
|
||||
ui:VisualExtensions.NormalizedCenterPoint="0.5,0.5"
|
||||
x:Load="{x:Bind IsLoaded, Mode=OneWay}"
|
||||
AutomationProperties.Name="{x:Bind ViewModel.PrimaryCommand.Name, Mode=OneWay}"
|
||||
Background="Transparent"
|
||||
Style="{StaticResource SubtleButtonStyle}"
|
||||
Tapped="PrimaryButton_Tapped"
|
||||
@@ -198,10 +172,8 @@
|
||||
<Button
|
||||
x:Name="SecondaryButton"
|
||||
Padding="6,4,4,4"
|
||||
animations:Implicit.HideAnimations="{StaticResource HideAnimations}"
|
||||
animations:Implicit.ShowAnimations="{StaticResource ShowAnimations}"
|
||||
ui:VisualExtensions.NormalizedCenterPoint="0.5,0.5"
|
||||
x:Load="{x:Bind IsLoaded, Mode=OneWay}"
|
||||
AutomationProperties.Name="{x:Bind ViewModel.SecondaryCommand.Name, Mode=OneWay}"
|
||||
Style="{StaticResource SubtleButtonStyle}"
|
||||
Tapped="SecondaryButton_Tapped"
|
||||
Visibility="{x:Bind ViewModel.HasSecondaryCommand, Mode=OneWay}">
|
||||
@@ -245,13 +217,10 @@
|
||||
x:Name="MoreCommandsButton"
|
||||
x:Uid="MoreCommandsButton"
|
||||
Padding="4"
|
||||
animations:Implicit.HideAnimations="{StaticResource HideAnimations}"
|
||||
animations:Implicit.ShowAnimations="{StaticResource ShowAnimations}"
|
||||
ui:VisualExtensions.NormalizedCenterPoint="0.5,0.5"
|
||||
Content="{ui:FontIcon Glyph=,
|
||||
FontSize=16}"
|
||||
Style="{StaticResource SubtleButtonStyle}"
|
||||
ToolTipService.ToolTip="Ctrl+k"
|
||||
ToolTipService.ToolTip="Ctrl+K"
|
||||
Visibility="{x:Bind ViewModel.ShouldShowContextMenu, Mode=OneWay}">
|
||||
<Button.Flyout>
|
||||
<Flyout Placement="TopEdgeAlignedRight">
|
||||
@@ -263,6 +232,7 @@
|
||||
ItemClick="CommandsDropdown_ItemClick"
|
||||
ItemTemplate="{StaticResource ContextMenuViewModelTemplate}"
|
||||
ItemsSource="{x:Bind ViewModel.ContextCommands, Mode=OneWay}"
|
||||
KeyDown="CommandsDropdown_KeyDown"
|
||||
SelectionMode="None">
|
||||
<ListView.ItemContainerStyle>
|
||||
<Style BasedOn="{StaticResource DefaultListViewItemStyle}" TargetType="ListViewItem">
|
||||
|
||||
@@ -6,10 +6,13 @@ using CommunityToolkit.Mvvm.Messaging;
|
||||
using Microsoft.CmdPal.UI.ViewModels;
|
||||
using Microsoft.CmdPal.UI.ViewModels.Messages;
|
||||
using Microsoft.CmdPal.UI.Views;
|
||||
using Microsoft.UI.Input;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Controls.Primitives;
|
||||
using Microsoft.UI.Xaml.Input;
|
||||
using Windows.System;
|
||||
using Windows.UI.Core;
|
||||
|
||||
namespace Microsoft.CmdPal.UI.Controls;
|
||||
|
||||
@@ -89,4 +92,23 @@ public sealed partial class CommandBar : UserControl,
|
||||
MoreCommandsButton.Flyout.Hide();
|
||||
}
|
||||
}
|
||||
|
||||
private void CommandsDropdown_KeyDown(object sender, KeyRoutedEventArgs e)
|
||||
{
|
||||
if (e.Handled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var ctrlPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down);
|
||||
var altPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Menu).HasFlag(CoreVirtualKeyStates.Down);
|
||||
var shiftPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down);
|
||||
var winPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.LeftWindows).HasFlag(CoreVirtualKeyStates.Down) ||
|
||||
InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.RightWindows).HasFlag(CoreVirtualKeyStates.Down);
|
||||
|
||||
if (ViewModel?.CheckKeybinding(ctrlPressed, altPressed, shiftPressed, winPressed, e.Key) ?? false)
|
||||
{
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ using CommunityToolkit.WinUI;
|
||||
using Microsoft.CmdPal.UI.ViewModels;
|
||||
using Microsoft.CmdPal.UI.ViewModels.Messages;
|
||||
using Microsoft.CmdPal.UI.Views;
|
||||
using Microsoft.CommandPalette.Extensions;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
using Microsoft.UI.Dispatching;
|
||||
using Microsoft.UI.Input;
|
||||
using Microsoft.UI.Xaml;
|
||||
@@ -21,6 +23,7 @@ namespace Microsoft.CmdPal.UI.Controls;
|
||||
public sealed partial class SearchBar : UserControl,
|
||||
IRecipient<GoHomeMessage>,
|
||||
IRecipient<FocusSearchBoxMessage>,
|
||||
IRecipient<UpdateItemKeybindingsMessage>,
|
||||
ICurrentPageAware
|
||||
{
|
||||
private readonly DispatcherQueue _queue = DispatcherQueue.GetForCurrentThread();
|
||||
@@ -31,6 +34,8 @@ public sealed partial class SearchBar : UserControl,
|
||||
private readonly DispatcherQueueTimer _debounceTimer = DispatcherQueue.GetForCurrentThread().CreateTimer();
|
||||
private bool _isBackspaceHeld;
|
||||
|
||||
private Dictionary<KeyChord, CommandContextItemViewModel>? _keyBindings;
|
||||
|
||||
public PageViewModel? CurrentPageViewModel
|
||||
{
|
||||
get => (PageViewModel?)GetValue(CurrentPageViewModelProperty);
|
||||
@@ -69,6 +74,7 @@ public sealed partial class SearchBar : UserControl,
|
||||
this.InitializeComponent();
|
||||
WeakReferenceMessenger.Default.Register<GoHomeMessage>(this);
|
||||
WeakReferenceMessenger.Default.Register<FocusSearchBoxMessage>(this);
|
||||
WeakReferenceMessenger.Default.Register<UpdateItemKeybindingsMessage>(this);
|
||||
}
|
||||
|
||||
public void ClearSearch()
|
||||
@@ -105,7 +111,9 @@ public sealed partial class SearchBar : UserControl,
|
||||
|
||||
var ctrlPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down);
|
||||
var altPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Menu).HasFlag(CoreVirtualKeyStates.Down);
|
||||
|
||||
var shiftPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down);
|
||||
var winPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.LeftWindows).HasFlag(CoreVirtualKeyStates.Down) ||
|
||||
InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.RightWindows).HasFlag(CoreVirtualKeyStates.Down);
|
||||
if (ctrlPressed && e.Key == VirtualKey.Enter)
|
||||
{
|
||||
// ctrl+enter
|
||||
@@ -164,6 +172,19 @@ public sealed partial class SearchBar : UserControl,
|
||||
{
|
||||
WeakReferenceMessenger.Default.Send<NavigateBackMessage>(new());
|
||||
}
|
||||
|
||||
if (_keyBindings != null)
|
||||
{
|
||||
// Does the pressed key match any of the keybindings?
|
||||
var pressedKeyChord = KeyChordHelpers.FromModifiers(ctrlPressed, altPressed, shiftPressed, winPressed, (int)e.Key, 0);
|
||||
if (_keyBindings.TryGetValue(pressedKeyChord, out var item))
|
||||
{
|
||||
// TODO GH #245: This is a bit of a hack, but we need to make sure that the keybindings are updated before we send the message
|
||||
// so that the correct item is activated.
|
||||
WeakReferenceMessenger.Default.Send<PerformCommandMessage>(new(item));
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void FilterBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
|
||||
@@ -282,4 +303,9 @@ public sealed partial class SearchBar : UserControl,
|
||||
public void Receive(GoHomeMessage message) => ClearSearch();
|
||||
|
||||
public void Receive(FocusSearchBoxMessage message) => this.Focus(Microsoft.UI.Xaml.FocusState.Programmatic);
|
||||
|
||||
public void Receive(UpdateItemKeybindingsMessage message)
|
||||
{
|
||||
_keyBindings = message.Keys;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
<Setter Property="IsTabStop" Value="False" />
|
||||
<Setter Property="HorizontalAlignment" Value="Center" />
|
||||
<Setter Property="VerticalAlignment" Value="Center" />
|
||||
<Setter Property="AutomationProperties.AutomationControlType" Value="Custom" />
|
||||
<Setter Property="BackgroundSizing" Value="InnerBorderEdge" />
|
||||
<Setter Property="Padding" Value="{ThemeResource TagPadding}" />
|
||||
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
|
||||
@@ -56,6 +57,7 @@
|
||||
Padding="{TemplateBinding Padding}"
|
||||
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
|
||||
VerticalAlignment="{TemplateBinding VerticalAlignment}"
|
||||
AutomationProperties.Name="{TemplateBinding Text}"
|
||||
Background="{TemplateBinding Background}"
|
||||
BackgroundSizing="{TemplateBinding BackgroundSizing}"
|
||||
BorderBrush="{TemplateBinding BorderBrush}"
|
||||
|
||||
@@ -28,21 +28,22 @@
|
||||
NotEmptyValue="Visible" />
|
||||
|
||||
<DataTemplate x:Key="TagTemplate" x:DataType="viewmodels:TagViewModel">
|
||||
<ItemContainer>
|
||||
<cpcontrols:Tag
|
||||
BackgroundColor="{x:Bind Background, Mode=OneWay}"
|
||||
FontSize="12"
|
||||
ForegroundColor="{x:Bind Foreground, Mode=OneWay}"
|
||||
Icon="{x:Bind Icon, Mode=OneWay}"
|
||||
Text="{x:Bind Text, Mode=OneWay}"
|
||||
ToolTipService.ToolTip="{x:Bind ToolTip, Mode=OneWay}" />
|
||||
</ItemContainer>
|
||||
<cpcontrols:Tag
|
||||
AutomationProperties.Name="{x:Bind Text, Mode=OneWay}"
|
||||
BackgroundColor="{x:Bind Background, Mode=OneWay}"
|
||||
FontSize="12"
|
||||
ForegroundColor="{x:Bind Foreground, Mode=OneWay}"
|
||||
Icon="{x:Bind Icon, Mode=OneWay}"
|
||||
Text="{x:Bind Text, Mode=OneWay}"
|
||||
ToolTipService.ToolTip="{x:Bind ToolTip, Mode=OneWay}" />
|
||||
</DataTemplate>
|
||||
|
||||
<!-- https://learn.microsoft.com/windows/apps/design/controls/itemsview#specify-the-look-of-the-items -->
|
||||
<DataTemplate x:Key="ListItemViewModelTemplate" x:DataType="viewmodels:ListItemViewModel">
|
||||
|
||||
<Grid Padding="0,12,0,12" ColumnSpacing="12">
|
||||
<Grid
|
||||
Padding="0,12,0,12"
|
||||
AutomationProperties.Name="{x:Bind Title, Mode=OneWay}"
|
||||
ColumnSpacing="12">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="28" />
|
||||
<ColumnDefinition Width="*" />
|
||||
@@ -55,6 +56,7 @@
|
||||
Width="20"
|
||||
Height="20"
|
||||
Margin="4,0,4,0"
|
||||
AutomationProperties.AccessibilityView="Raw"
|
||||
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
|
||||
SourceKey="{x:Bind Icon, Mode=OneWay}"
|
||||
SourceRequested="{x:Bind help:IconCacheProvider.SourceRequested}" />
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
<DataTemplate x:Key="TagTemplate" x:DataType="viewModels:TagViewModel">
|
||||
<cpcontrols:Tag
|
||||
HorizontalAlignment="Left"
|
||||
AutomationProperties.Name="{x:Bind Text, Mode=OneWay}"
|
||||
BackgroundColor="{x:Bind Background, Mode=OneWay}"
|
||||
FontSize="12"
|
||||
ForegroundColor="{x:Bind Foreground, Mode=OneWay}"
|
||||
@@ -147,27 +148,36 @@
|
||||
Visibility="{x:Bind ViewModel.CurrentPage.IsNested, Mode=OneWay, Converter={StaticResource BoolToInvertedVisibilityConverter}}">
|
||||
<animations:Implicit.ShowAnimations>
|
||||
<animations:OpacityAnimation
|
||||
EasingMode="EaseIn"
|
||||
EasingType="Cubic"
|
||||
From="0"
|
||||
To="1.0"
|
||||
Duration="0:0:0.340" />
|
||||
Duration="0:0:0.187" />
|
||||
<animations:ScaleAnimation
|
||||
EasingMode="EaseIn"
|
||||
EasingType="Cubic"
|
||||
From="0.5"
|
||||
To="1"
|
||||
Duration="0:0:0.350" />
|
||||
Duration="0:0:0.187" />
|
||||
</animations:Implicit.ShowAnimations>
|
||||
<animations:Implicit.HideAnimations>
|
||||
<animations:OpacityAnimation
|
||||
EasingMode="EaseOut"
|
||||
EasingType="Cubic"
|
||||
From="1.0"
|
||||
To="0"
|
||||
Duration="0:0:0.240" />
|
||||
Duration="0:0:0.187" />
|
||||
<animations:ScaleAnimation
|
||||
EasingMode="EaseOut"
|
||||
EasingType="Cubic"
|
||||
From="1"
|
||||
To="0.5"
|
||||
Duration="0:0:0.350" />
|
||||
Duration="0:0:0.187" />
|
||||
</animations:Implicit.HideAnimations>
|
||||
</Image>
|
||||
<Button
|
||||
x:Name="BackButton"
|
||||
x:Uid="BackButton"
|
||||
Margin="4,0,4,0"
|
||||
Padding="4"
|
||||
HorizontalAlignment="Center"
|
||||
@@ -178,35 +188,42 @@
|
||||
FontSize="16"
|
||||
Style="{StaticResource SubtleButtonStyle}"
|
||||
Tapped="BackButton_Tapped"
|
||||
ToolTipService.ToolTip="Back"
|
||||
Visibility="{x:Bind ViewModel.CurrentPage.IsNested, Mode=OneWay}">
|
||||
<animations:Implicit.ShowAnimations>
|
||||
<animations:OpacityAnimation
|
||||
EasingMode="EaseIn"
|
||||
EasingType="Cubic"
|
||||
From="0"
|
||||
To="1.0"
|
||||
Duration="0:0:0.340" />
|
||||
Duration="0:0:0.333" />
|
||||
<animations:ScaleAnimation
|
||||
From="0.5"
|
||||
To="1"
|
||||
Duration="0:0:0.350" />
|
||||
Duration="0:0:0.333" />
|
||||
<animations:TranslationAnimation
|
||||
From="16,0,0"
|
||||
To="0,0,0"
|
||||
Duration="0:0:0.350" />
|
||||
Duration="0:0:0.333" />
|
||||
</animations:Implicit.ShowAnimations>
|
||||
<animations:Implicit.HideAnimations>
|
||||
<animations:OpacityAnimation
|
||||
EasingMode="EaseOut"
|
||||
EasingType="Cubic"
|
||||
From="1.0"
|
||||
To="0"
|
||||
Duration="0:0:0.340" />
|
||||
Duration="0:0:0.333" />
|
||||
<animations:ScaleAnimation
|
||||
EasingMode="EaseOut"
|
||||
EasingType="Cubic"
|
||||
From="1"
|
||||
To="0.5"
|
||||
Duration="0:0:0.350" />
|
||||
Duration="0:0:0.333" />
|
||||
<animations:TranslationAnimation
|
||||
EasingMode="EaseOut"
|
||||
EasingType="Cubic"
|
||||
From="0,0,0"
|
||||
To="16,0,0"
|
||||
Duration="0:0:0.350" />
|
||||
Duration="0:0:0.187" />
|
||||
</animations:Implicit.HideAnimations>
|
||||
</Button>
|
||||
<cpcontrols:IconBox
|
||||
@@ -223,29 +240,29 @@
|
||||
<animations:OpacityAnimation
|
||||
From="0"
|
||||
To="1.0"
|
||||
Duration="0:0:0.450" />
|
||||
Duration="0:0:0.333" />
|
||||
<animations:ScaleAnimation
|
||||
From="0.8"
|
||||
To="1"
|
||||
Duration="0:0:0.500" />
|
||||
Duration="0:0:0.333" />
|
||||
<animations:TranslationAnimation
|
||||
From="8,0,0"
|
||||
To="0,0,0"
|
||||
Duration="0:0:0.400" />
|
||||
Duration="0:0:0.187" />
|
||||
</animations:Implicit.ShowAnimations>
|
||||
<animations:Implicit.HideAnimations>
|
||||
<animations:OpacityAnimation
|
||||
From="1.0"
|
||||
To="0"
|
||||
Duration="0:0:0.340" />
|
||||
Duration="0:0:0.333" />
|
||||
<animations:ScaleAnimation
|
||||
From="1"
|
||||
To="0.8"
|
||||
Duration="0:0:0.350" />
|
||||
Duration="0:0:0.333" />
|
||||
<animations:TranslationAnimation
|
||||
From="0,0,0"
|
||||
To="8,0,0"
|
||||
Duration="0:0:0.350" />
|
||||
Duration="0:0:0.187" />
|
||||
</animations:Implicit.HideAnimations>
|
||||
</cpcontrols:IconBox>
|
||||
</StackPanel>
|
||||
@@ -272,13 +289,13 @@
|
||||
<animations:OpacityAnimation
|
||||
From="0"
|
||||
To="1.0"
|
||||
Duration="0:0:0.340" />
|
||||
Duration="0:0:0.333" />
|
||||
</animations:Implicit.ShowAnimations>
|
||||
<animations:Implicit.HideAnimations>
|
||||
<animations:OpacityAnimation
|
||||
From="1.0"
|
||||
To="0"
|
||||
Duration="0:0:0.340" />
|
||||
Duration="0:0:0.333" />
|
||||
</animations:Implicit.HideAnimations>
|
||||
</ProgressBar>
|
||||
|
||||
@@ -313,21 +330,21 @@
|
||||
<animations:OpacityAnimation
|
||||
From="0"
|
||||
To="1.0"
|
||||
Duration="0:0:0.270" />
|
||||
Duration="0:0:0.187" />
|
||||
<animations:TranslationAnimation
|
||||
From="24,0,0"
|
||||
To="0,0,0"
|
||||
Duration="0:0:0.280" />
|
||||
Duration="0:0:0.187" />
|
||||
</animations:Implicit.ShowAnimations>
|
||||
<animations:Implicit.HideAnimations>
|
||||
<animations:OpacityAnimation
|
||||
From="1.0"
|
||||
To="0"
|
||||
Duration="0:0:0.180" />
|
||||
Duration="0:0:0.187" />
|
||||
<animations:TranslationAnimation
|
||||
From="0,0,0"
|
||||
To="24,0,0"
|
||||
Duration="0:0:0.220" />
|
||||
Duration="0:0:0.187" />
|
||||
</animations:Implicit.HideAnimations>
|
||||
<Grid Margin="12">
|
||||
<Grid.RowDefinitions>
|
||||
@@ -343,6 +360,7 @@
|
||||
MinHeight="64"
|
||||
MaxHeight="96"
|
||||
HorizontalAlignment="Center"
|
||||
AutomationProperties.AccessibilityView="Raw"
|
||||
SourceKey="{x:Bind ViewModel.Details.HeroImage, Mode=OneWay}"
|
||||
SourceRequested="{x:Bind help:IconCacheProvider.SourceRequested}"
|
||||
Visibility="{x:Bind HasHeroImage, Mode=OneWay}" />
|
||||
|
||||
@@ -187,6 +187,8 @@ public sealed partial class ShellPage : Microsoft.UI.Xaml.Controls.Page,
|
||||
|
||||
WeakReferenceMessenger.Default.Send<UpdateCommandBarMessage>(new(null));
|
||||
|
||||
WeakReferenceMessenger.Default.Send<UpdateItemKeybindingsMessage>(new(null));
|
||||
|
||||
var isMainPage = command is MainListPage;
|
||||
|
||||
// Construct our ViewModel of the appropriate type and pass it the UI Thread context.
|
||||
@@ -427,8 +429,6 @@ public sealed partial class ShellPage : Microsoft.UI.Xaml.Controls.Page,
|
||||
}
|
||||
|
||||
_settingsWindow.Activate();
|
||||
|
||||
WeakReferenceMessenger.Default.Send<UpdateCommandBarMessage>(new(null));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -379,9 +379,6 @@ Right-click to remove the key combination, thereby deactivating the shortcut.</v
|
||||
<data name="Settings_GeneralPage_NavigationViewItem_Extensions.Content" xml:space="preserve">
|
||||
<value>Extensions</value>
|
||||
</data>
|
||||
<data name="MoreCommandsButton.[using:Microsoft.UI.Xaml.Automation]AutomationProperties.Name" xml:space="preserve">
|
||||
<value>More commands</value>
|
||||
</data>
|
||||
<data name="SettingsButton.[using:Microsoft.UI.Xaml.Automation]AutomationProperties.Name" xml:space="preserve">
|
||||
<value>Open Command Palette settings</value>
|
||||
</data>
|
||||
@@ -397,4 +394,13 @@ Right-click to remove the key combination, thereby deactivating the shortcut.</v
|
||||
<data name="Settings_GeneralPage_ShowSystemTrayIcon_SettingsCard.Description" xml:space="preserve">
|
||||
<value>Choose if Command Palette is visible in the system tray</value>
|
||||
</data>
|
||||
<data name="BackButton.[using:Microsoft.UI.Xaml.Automation]AutomationProperties.Name" xml:space="preserve">
|
||||
<value>Back</value>
|
||||
</data>
|
||||
<data name="BackButton.[using:Microsoft.UI.Xaml.Controls]ToolTipService.ToolTip" xml:space="preserve">
|
||||
<value>Back</value>
|
||||
</data>
|
||||
<data name="MoreCommandsButton.[using:Microsoft.UI.Xaml.Automation]AutomationProperties.Name" xml:space="preserve">
|
||||
<value>More</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -2,176 +2,34 @@
|
||||
// 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.Data;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using Microsoft.CmdPal.Ext.Calc.Helper;
|
||||
using Microsoft.CmdPal.Ext.Calc.Pages;
|
||||
using Microsoft.CmdPal.Ext.Calc.Properties;
|
||||
using Microsoft.CommandPalette.Extensions;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.Calc;
|
||||
|
||||
public partial class CalculatorCommandProvider : CommandProvider
|
||||
{
|
||||
private readonly ListItem _listItem = new(new CalculatorListPage()) { Subtitle = Resources.calculator_top_level_subtitle };
|
||||
private readonly FallbackCalculatorItem _fallback = new();
|
||||
private readonly ListItem _listItem = new(new CalculatorListPage(settings))
|
||||
{
|
||||
Subtitle = Resources.calculator_top_level_subtitle,
|
||||
MoreCommands = [new CommandContextItem(settings.Settings.SettingsPage)],
|
||||
};
|
||||
|
||||
private readonly FallbackCalculatorItem _fallback = new(settings);
|
||||
private static SettingsManager settings = new();
|
||||
|
||||
public CalculatorCommandProvider()
|
||||
{
|
||||
Id = "Calculator";
|
||||
DisplayName = Resources.calculator_display_name;
|
||||
Icon = IconHelpers.FromRelativePath("Assets\\Calculator.svg");
|
||||
Icon = CalculatorIcons.ProviderIcon;
|
||||
Settings = settings.Settings;
|
||||
}
|
||||
|
||||
public override ICommandItem[] TopLevelCommands() => [_listItem];
|
||||
|
||||
public override IFallbackCommandItem[] FallbackCommands() => [_fallback];
|
||||
}
|
||||
|
||||
// The calculator page is a dynamic list page
|
||||
// * The first command is where we display the results. Title=result, Subtitle=query
|
||||
// - The default command is `SaveCommand`.
|
||||
// - When you save, insert into list at spot 1
|
||||
// - change SearchText to the result
|
||||
// - MoreCommands: a single `CopyCommand` to copy the result to the clipboard
|
||||
// * The rest of the items are previously saved results
|
||||
// - Command is a CopyCommand
|
||||
// - Each item also sets the TextToSuggest to the result
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "This is sample code")]
|
||||
public sealed partial class CalculatorListPage : DynamicListPage
|
||||
{
|
||||
private readonly List<ListItem> _items = [];
|
||||
private readonly SaveCommand _saveCommand = new();
|
||||
private readonly CopyTextCommand _copyContextCommand;
|
||||
private readonly CommandContextItem _copyContextMenuItem;
|
||||
private static readonly CompositeFormat ErrorMessage = System.Text.CompositeFormat.Parse(Properties.Resources.calculator_error);
|
||||
|
||||
public CalculatorListPage()
|
||||
{
|
||||
Icon = IconHelpers.FromRelativePath("Assets\\Calculator.svg");
|
||||
Name = Resources.calculator_title;
|
||||
PlaceholderText = Resources.calculator_placeholder_text;
|
||||
Id = "com.microsoft.cmdpal.calculator";
|
||||
|
||||
_copyContextCommand = new CopyTextCommand(string.Empty);
|
||||
_copyContextMenuItem = new CommandContextItem(_copyContextCommand);
|
||||
|
||||
_items.Add(new(_saveCommand) { Icon = new IconInfo("\uE94E") });
|
||||
|
||||
UpdateSearchText(string.Empty, string.Empty);
|
||||
|
||||
_saveCommand.SaveRequested += HandleSave;
|
||||
}
|
||||
|
||||
private void HandleSave(object sender, object args)
|
||||
{
|
||||
var lastResult = _items[0].Title;
|
||||
if (!string.IsNullOrEmpty(lastResult))
|
||||
{
|
||||
var li = new ListItem(new CopyTextCommand(lastResult))
|
||||
{
|
||||
Title = _items[0].Title,
|
||||
Subtitle = _items[0].Subtitle,
|
||||
TextToSuggest = lastResult,
|
||||
};
|
||||
_items.Insert(1, li);
|
||||
_items[0].Subtitle = string.Empty;
|
||||
SearchText = lastResult;
|
||||
this.RaiseItemsChanged(this._items.Count);
|
||||
}
|
||||
}
|
||||
|
||||
public override void UpdateSearchText(string oldSearch, string newSearch)
|
||||
{
|
||||
var firstItem = _items[0];
|
||||
if (string.IsNullOrEmpty(newSearch))
|
||||
{
|
||||
firstItem.Title = Resources.calculator_placeholder_text;
|
||||
firstItem.Subtitle = string.Empty;
|
||||
firstItem.MoreCommands = [];
|
||||
}
|
||||
else
|
||||
{
|
||||
_copyContextCommand.Text = ParseQuery(newSearch, out var result) ? result : string.Empty;
|
||||
firstItem.Title = result;
|
||||
firstItem.Subtitle = newSearch;
|
||||
firstItem.MoreCommands = [_copyContextMenuItem];
|
||||
}
|
||||
}
|
||||
|
||||
internal static bool ParseQuery(string equation, out string result)
|
||||
{
|
||||
try
|
||||
{
|
||||
var resultNumber = new DataTable().Compute(equation, null);
|
||||
result = resultNumber.ToString() ?? string.Empty;
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result = string.Format(CultureInfo.CurrentCulture, ErrorMessage, e.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override IListItem[] GetItems() => _items.ToArray();
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "This is sample code")]
|
||||
public sealed partial class SaveCommand : InvokableCommand
|
||||
{
|
||||
public event TypedEventHandler<object, object> SaveRequested;
|
||||
|
||||
public SaveCommand()
|
||||
{
|
||||
Name = Resources.calculator_save_command_name;
|
||||
}
|
||||
|
||||
public override ICommandResult Invoke()
|
||||
{
|
||||
SaveRequested?.Invoke(this, this);
|
||||
return CommandResult.KeepOpen();
|
||||
}
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "This is sample code")]
|
||||
internal sealed partial class FallbackCalculatorItem : FallbackCommandItem
|
||||
{
|
||||
private readonly CopyTextCommand _copyCommand = new(string.Empty);
|
||||
private static readonly IconInfo _cachedIcon = IconHelpers.FromRelativePath("Assets\\Calculator.svg");
|
||||
|
||||
public FallbackCalculatorItem()
|
||||
: base(new NoOpCommand(), Resources.calculator_title)
|
||||
{
|
||||
Command = _copyCommand;
|
||||
_copyCommand.Name = string.Empty;
|
||||
Title = string.Empty;
|
||||
Subtitle = Resources.calculator_placeholder_text;
|
||||
Icon = _cachedIcon;
|
||||
}
|
||||
|
||||
public override void UpdateQuery(string query)
|
||||
{
|
||||
if (CalculatorListPage.ParseQuery(query, out var result))
|
||||
{
|
||||
_copyCommand.Text = result;
|
||||
_copyCommand.Name = string.IsNullOrWhiteSpace(query) ? string.Empty : Resources.calculator_copy_command_name;
|
||||
Title = result;
|
||||
|
||||
// we have to make the subtitle the equation,
|
||||
// so that we will still string match the original query
|
||||
// Otherwise, something like 1+2 will have a title of "3" and not match
|
||||
Subtitle = query;
|
||||
}
|
||||
else
|
||||
{
|
||||
_copyCommand.Text = string.Empty;
|
||||
_copyCommand.Name = string.Empty;
|
||||
Title = string.Empty;
|
||||
Subtitle = string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.Calc.Helper;
|
||||
|
||||
public static class BracketHelper
|
||||
{
|
||||
public static bool IsBracketComplete(string query)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(query))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var valueTuples = query
|
||||
.Select(BracketTrail)
|
||||
.Where(r => r != default);
|
||||
|
||||
var trailTest = new Stack<TrailType>();
|
||||
|
||||
foreach (var (direction, type) in valueTuples)
|
||||
{
|
||||
switch (direction)
|
||||
{
|
||||
case TrailDirection.Open:
|
||||
trailTest.Push(type);
|
||||
break;
|
||||
case TrailDirection.Close:
|
||||
// Try to get item out of stack
|
||||
if (!trailTest.TryPop(out var popped))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (type != popped)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
continue;
|
||||
default:
|
||||
{
|
||||
throw new ArgumentOutOfRangeException($"Can't process value (Parameter direction: {direction})");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return trailTest.Count == 0;
|
||||
}
|
||||
|
||||
private static (TrailDirection Direction, TrailType Type) BracketTrail(char @char)
|
||||
{
|
||||
switch (@char)
|
||||
{
|
||||
case '(':
|
||||
return (TrailDirection.Open, TrailType.Round);
|
||||
case ')':
|
||||
return (TrailDirection.Close, TrailType.Round);
|
||||
case '[':
|
||||
return (TrailDirection.Open, TrailType.Bracket);
|
||||
case ']':
|
||||
return (TrailDirection.Close, TrailType.Bracket);
|
||||
default:
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
private enum TrailDirection
|
||||
{
|
||||
None,
|
||||
Open,
|
||||
Close,
|
||||
}
|
||||
|
||||
private enum TrailType
|
||||
{
|
||||
None,
|
||||
Bracket,
|
||||
Round,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
// 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 Mages.Core;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.Calc.Helper;
|
||||
|
||||
public static class CalculateEngine
|
||||
{
|
||||
private static readonly Engine _magesEngine = new Engine(new Configuration
|
||||
{
|
||||
Scope = new Dictionary<string, object>
|
||||
{
|
||||
{ "e", Math.E }, // e is not contained in the default mages engine
|
||||
},
|
||||
});
|
||||
|
||||
public const int RoundingDigits = 10;
|
||||
|
||||
public enum TrigMode
|
||||
{
|
||||
Radians,
|
||||
Degrees,
|
||||
Gradians,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interpret
|
||||
/// </summary>
|
||||
/// <param name="cultureInfo">Use CultureInfo.CurrentCulture if something is user facing</param>
|
||||
public static CalculateResult Interpret(SettingsManager settings, string input, CultureInfo cultureInfo, out string error)
|
||||
{
|
||||
error = default;
|
||||
|
||||
if (!CalculateHelper.InputValid(input))
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
// check for division by zero
|
||||
// We check if the string contains a slash followed by space (optional) and zero. Whereas the zero must not be followed by a dot, comma, 'b', 'o' or 'x' as these indicate a number with decimal digits or a binary/octal/hexadecimal value respectively. The zero must also not be followed by other digits.
|
||||
if (new Regex("\\/\\s*0(?!(?:[,\\.0-9]|[box]0*[1-9a-f]))", RegexOptions.IgnoreCase).Match(input).Success)
|
||||
{
|
||||
error = Properties.Resources.calculator_division_by_zero;
|
||||
return default;
|
||||
}
|
||||
|
||||
// mages has quirky log representation
|
||||
// mage has log == ln vs log10
|
||||
input = input.
|
||||
Replace("log(", "log10(", true, CultureInfo.CurrentCulture).
|
||||
Replace("ln(", "log(", true, CultureInfo.CurrentCulture);
|
||||
|
||||
input = CalculateHelper.FixHumanMultiplicationExpressions(input);
|
||||
|
||||
// Get the user selected trigonometry unit
|
||||
TrigMode trigMode = settings.TrigUnit;
|
||||
|
||||
// Modify trig functions depending on angle unit setting
|
||||
input = CalculateHelper.UpdateTrigFunctions(input, trigMode);
|
||||
|
||||
// Expand conversions between trig units
|
||||
input = CalculateHelper.ExpandTrigConversions(input, trigMode);
|
||||
|
||||
var result = _magesEngine.Interpret(input);
|
||||
|
||||
// This could happen for some incorrect queries, like pi(2)
|
||||
if (result == null)
|
||||
{
|
||||
error = Properties.Resources.calculator_expression_not_complete;
|
||||
return default;
|
||||
}
|
||||
|
||||
result = TransformResult(result);
|
||||
if (result is string)
|
||||
{
|
||||
error = result as string;
|
||||
return default;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(result?.ToString()))
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
var decimalResult = Convert.ToDecimal(result, cultureInfo);
|
||||
var roundedResult = Round(decimalResult);
|
||||
|
||||
return new CalculateResult()
|
||||
{
|
||||
Result = decimalResult,
|
||||
RoundedResult = roundedResult,
|
||||
};
|
||||
}
|
||||
|
||||
public static decimal Round(decimal value)
|
||||
{
|
||||
return Math.Round(value, RoundingDigits, MidpointRounding.AwayFromZero);
|
||||
}
|
||||
|
||||
private static dynamic TransformResult(object result)
|
||||
{
|
||||
if (result.ToString() == "NaN")
|
||||
{
|
||||
return Properties.Resources.calculator_not_a_number;
|
||||
}
|
||||
|
||||
if (result is Function)
|
||||
{
|
||||
return Properties.Resources.calculator_expression_not_complete;
|
||||
}
|
||||
|
||||
if (result is double[,])
|
||||
{
|
||||
// '[10,10]' is interpreted as array by mages engine
|
||||
return Properties.Resources.calculator_double_array_returned;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
// 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.Text.RegularExpressions;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.Calc.Helper;
|
||||
|
||||
public static class CalculateHelper
|
||||
{
|
||||
private static readonly Regex RegValidExpressChar = new Regex(
|
||||
@"^(" +
|
||||
@"%|" +
|
||||
@"ceil\s*\(|floor\s*\(|exp\s*\(|max\s*\(|min\s*\(|abs\s*\(|log(?:2|10)?\s*\(|ln\s*\(|sqrt\s*\(|pow\s*\(|" +
|
||||
@"factorial\s*\(|sign\s*\(|round\s*\(|rand\s*\(\)|randi\s*\([^\)]|" +
|
||||
@"sin\s*\(|cos\s*\(|tan\s*\(|arcsin\s*\(|arccos\s*\(|arctan\s*\(|" +
|
||||
@"sinh\s*\(|cosh\s*\(|tanh\s*\(|arsinh\s*\(|arcosh\s*\(|artanh\s*\(|" +
|
||||
@"rad\s*\(|deg\s*\(|grad\s*\(|" + /* trigonometry unit conversion macros */
|
||||
@"pi|" +
|
||||
@"==|~=|&&|\|\||" +
|
||||
@"((-?(\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);
|
||||
|
||||
private const string DegToRad = "(pi / 180) * ";
|
||||
private const string DegToGrad = "(10 / 9) * ";
|
||||
private const string GradToRad = "(pi / 200) * ";
|
||||
private const string GradToDeg = "(9 / 10) * ";
|
||||
private const string RadToDeg = "(180 / pi) * ";
|
||||
private const string RadToGrad = "(200 / pi) * ";
|
||||
|
||||
public static bool InputValid(string input)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
{
|
||||
throw new ArgumentNullException(paramName: nameof(input));
|
||||
}
|
||||
|
||||
if (!RegValidExpressChar.IsMatch(input))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!BracketHelper.IsBracketComplete(input))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// 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('%'))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static string FixHumanMultiplicationExpressions(string input)
|
||||
{
|
||||
var output = CheckScientificNotation(input);
|
||||
output = CheckNumberOrConstantThenParenthesisExpr(output);
|
||||
output = CheckNumberOrConstantThenFunc(output);
|
||||
output = CheckParenthesisExprThenFunc(output);
|
||||
output = CheckParenthesisExprThenParenthesisExpr(output);
|
||||
output = CheckNumberThenConstant(output);
|
||||
output = CheckConstantThenConstant(output);
|
||||
return output;
|
||||
}
|
||||
|
||||
private static string CheckScientificNotation(string input)
|
||||
{
|
||||
/**
|
||||
* NOTE: By the time 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);
|
||||
}
|
||||
|
||||
/*
|
||||
* num (exp)
|
||||
* const (exp)
|
||||
*/
|
||||
private static string CheckNumberOrConstantThenParenthesisExpr(string input)
|
||||
{
|
||||
var output = input;
|
||||
do
|
||||
{
|
||||
input = output;
|
||||
output = Regex.Replace(input, @"(\d+|pi|e)\s*(\()", m =>
|
||||
{
|
||||
if (m.Index > 0 && char.IsLetter(input[m.Index - 1]))
|
||||
{
|
||||
return m.Value;
|
||||
}
|
||||
|
||||
return $"{m.Groups[1].Value} * {m.Groups[2].Value}";
|
||||
});
|
||||
}
|
||||
while (output != input);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/*
|
||||
* num func
|
||||
* const func
|
||||
*/
|
||||
private static string CheckNumberOrConstantThenFunc(string input)
|
||||
{
|
||||
var output = input;
|
||||
do
|
||||
{
|
||||
input = output;
|
||||
output = Regex.Replace(input, @"(\d+|pi|e)\s*([a-zA-Z]+[0-9]*\s*\()", m =>
|
||||
{
|
||||
if (input[m.Index] == 'e' && input[m.Index + 1] == 'x' && input[m.Index + 2] == 'p')
|
||||
{
|
||||
return m.Value;
|
||||
}
|
||||
|
||||
if (m.Index > 0 && char.IsLetter(input[m.Index - 1]))
|
||||
{
|
||||
return m.Value;
|
||||
}
|
||||
|
||||
return $"{m.Groups[1].Value} * {m.Groups[2].Value}";
|
||||
});
|
||||
}
|
||||
while (output != input);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/*
|
||||
* (exp) func
|
||||
* func func
|
||||
*/
|
||||
private static string CheckParenthesisExprThenFunc(string input)
|
||||
{
|
||||
var p = @"(\))\s*([a-zA-Z]+[0-9]*\s*\()";
|
||||
var r = "$1 * $2";
|
||||
return Regex.Replace(input, p, r);
|
||||
}
|
||||
|
||||
/*
|
||||
* (exp) (exp)
|
||||
* func (exp)
|
||||
*/
|
||||
private static string CheckParenthesisExprThenParenthesisExpr(string input)
|
||||
{
|
||||
var p = @"(\))\s*(\()";
|
||||
var r = "$1 * $2";
|
||||
return Regex.Replace(input, p, r);
|
||||
}
|
||||
|
||||
/*
|
||||
* num const
|
||||
*/
|
||||
private static string CheckNumberThenConstant(string input)
|
||||
{
|
||||
var output = input;
|
||||
do
|
||||
{
|
||||
input = output;
|
||||
output = Regex.Replace(input, @"(\d+)\s*(pi|e)", m =>
|
||||
{
|
||||
if (m.Index > 0 && char.IsLetter(input[m.Index - 1]))
|
||||
{
|
||||
return m.Value;
|
||||
}
|
||||
|
||||
return $"{m.Groups[1].Value} * {m.Groups[2].Value}";
|
||||
});
|
||||
}
|
||||
while (output != input);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/*
|
||||
* const const
|
||||
*/
|
||||
private static string CheckConstantThenConstant(string input)
|
||||
{
|
||||
var output = input;
|
||||
do
|
||||
{
|
||||
input = output;
|
||||
output = Regex.Replace(input, @"(pi|e)\s*(pi|e)", m =>
|
||||
{
|
||||
if (m.Index > 0 && char.IsLetter(input[m.Index - 1]))
|
||||
{
|
||||
return m.Value;
|
||||
}
|
||||
|
||||
return $"{m.Groups[1].Value} * {m.Groups[2].Value}";
|
||||
});
|
||||
}
|
||||
while (output != input);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
// Gets the index of the closing bracket of a function
|
||||
private static int FindClosingBracketIndex(string input, int start)
|
||||
{
|
||||
var bracketCount = 0; // Set count to zero
|
||||
for (var i = start; i < input.Length; i++)
|
||||
{
|
||||
if (input[i] == '(')
|
||||
{
|
||||
bracketCount++;
|
||||
}
|
||||
else if (input[i] == ')')
|
||||
{
|
||||
bracketCount--;
|
||||
if (bracketCount == 0)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1; // Unmatched brackets
|
||||
}
|
||||
|
||||
private static string ModifyTrigFunction(string input, string function, string modification)
|
||||
{
|
||||
// Get the RegEx pattern to match, depending on whether the function is inverse or normal
|
||||
var pattern = function.StartsWith("arc", StringComparison.Ordinal) ? string.Empty : @"(?<!c)";
|
||||
pattern += $@"{function}\s*\(";
|
||||
|
||||
var index = 0; // Index for match to ensure that the same match is not found twice
|
||||
|
||||
Regex regex = new Regex(pattern);
|
||||
Match match;
|
||||
|
||||
while ((match = regex.Match(input, index)).Success)
|
||||
{
|
||||
index = match.Index + match.Groups[0].Length + modification.Length; // Get the next index to look from for further matches
|
||||
|
||||
var endIndex = FindClosingBracketIndex(input, match.Index + match.Groups[0].Length - 1); // Find the index of the closing bracket of the function
|
||||
|
||||
// If no valid bracket index was found, try the next match
|
||||
if (endIndex == -1)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var argument = input.Substring(match.Index + match.Groups[0].Length, endIndex - (match.Index + match.Groups[0].Length)); // Extract the argument between the brackets
|
||||
var replaced = function.StartsWith("arc", StringComparison.Ordinal) ? $"{modification}({match.Groups[0].Value}{argument}))" : $"{match.Groups[0].Value}{modification}({argument}))"; // The string to substitute in, handles differing formats of inverse functions
|
||||
|
||||
input = input.Remove(match.Index, endIndex - match.Index + 1); // Remove the match from the input
|
||||
input = input.Insert(match.Index, replaced); // Substitute with the new string
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
public static string UpdateTrigFunctions(string input, CalculateEngine.TrigMode mode)
|
||||
{
|
||||
var modifiedInput = input;
|
||||
if (mode == CalculateEngine.TrigMode.Degrees)
|
||||
{
|
||||
modifiedInput = ModifyTrigFunction(modifiedInput, "sin", DegToRad);
|
||||
modifiedInput = ModifyTrigFunction(modifiedInput, "cos", DegToRad);
|
||||
modifiedInput = ModifyTrigFunction(modifiedInput, "tan", DegToRad);
|
||||
modifiedInput = ModifyTrigFunction(modifiedInput, "arcsin", RadToDeg);
|
||||
modifiedInput = ModifyTrigFunction(modifiedInput, "arccos", RadToDeg);
|
||||
modifiedInput = ModifyTrigFunction(modifiedInput, "arctan", RadToDeg);
|
||||
}
|
||||
else if (mode == CalculateEngine.TrigMode.Gradians)
|
||||
{
|
||||
modifiedInput = ModifyTrigFunction(modifiedInput, "sin", GradToRad);
|
||||
modifiedInput = ModifyTrigFunction(modifiedInput, "cos", GradToRad);
|
||||
modifiedInput = ModifyTrigFunction(modifiedInput, "tan", GradToRad);
|
||||
modifiedInput = ModifyTrigFunction(modifiedInput, "arcsin", RadToGrad);
|
||||
modifiedInput = ModifyTrigFunction(modifiedInput, "arccos", RadToGrad);
|
||||
modifiedInput = ModifyTrigFunction(modifiedInput, "arctan", RadToGrad);
|
||||
}
|
||||
|
||||
return modifiedInput;
|
||||
}
|
||||
|
||||
private static string ModifyMathFunction(string input, string function, string modification)
|
||||
{
|
||||
// Create the pattern to match the function, opening bracket, and any spaces in between
|
||||
var pattern = $@"{function}\s*\(";
|
||||
return Regex.Replace(input, pattern, modification + "(");
|
||||
}
|
||||
|
||||
public static string ExpandTrigConversions(string input, CalculateEngine.TrigMode mode)
|
||||
{
|
||||
var modifiedInput = input;
|
||||
|
||||
// Expand "rad", "deg" and "grad" to their respective conversions for the current trig unit
|
||||
if (mode == CalculateEngine.TrigMode.Radians)
|
||||
{
|
||||
modifiedInput = ModifyMathFunction(modifiedInput, "deg", DegToRad);
|
||||
modifiedInput = ModifyMathFunction(modifiedInput, "grad", GradToRad);
|
||||
modifiedInput = ModifyMathFunction(modifiedInput, "rad", string.Empty);
|
||||
}
|
||||
else if (mode == CalculateEngine.TrigMode.Degrees)
|
||||
{
|
||||
modifiedInput = ModifyMathFunction(modifiedInput, "deg", string.Empty);
|
||||
modifiedInput = ModifyMathFunction(modifiedInput, "grad", GradToDeg);
|
||||
modifiedInput = ModifyMathFunction(modifiedInput, "rad", RadToDeg);
|
||||
}
|
||||
else if (mode == CalculateEngine.TrigMode.Gradians)
|
||||
{
|
||||
modifiedInput = ModifyMathFunction(modifiedInput, "deg", DegToGrad);
|
||||
modifiedInput = ModifyMathFunction(modifiedInput, "grad", string.Empty);
|
||||
modifiedInput = ModifyMathFunction(modifiedInput, "rad", RadToGrad);
|
||||
}
|
||||
|
||||
return modifiedInput;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// 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;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.Calc.Helper;
|
||||
|
||||
public struct CalculateResult : IEquatable<CalculateResult>
|
||||
{
|
||||
public decimal? Result { get; set; }
|
||||
|
||||
public decimal? RoundedResult { get; set; }
|
||||
|
||||
public bool Equals(CalculateResult other)
|
||||
{
|
||||
return Result == other.Result && RoundedResult == other.RoundedResult;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return obj is CalculateResult other && Equals(other);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(Result, RoundedResult);
|
||||
}
|
||||
|
||||
public static bool operator ==(CalculateResult left, CalculateResult right)
|
||||
{
|
||||
return left.Equals(right);
|
||||
}
|
||||
|
||||
public static bool operator !=(CalculateResult left, CalculateResult right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// 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.Toolkit;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.Calc.Helper;
|
||||
|
||||
public static class CalculatorIcons
|
||||
{
|
||||
public static IconInfo ResultIcon => new("\uE94E");
|
||||
|
||||
public static IconInfo SaveIcon => new("\uE74E");
|
||||
|
||||
public static IconInfo ErrorIcon => new("\uE783");
|
||||
|
||||
public static IconInfo ProviderIcon => IconHelpers.FromRelativePath("Assets\\Calculator.svg");
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// 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 ManagedCommon;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.Calc.Helper;
|
||||
|
||||
internal static class ErrorHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Method to handles errors while calculating
|
||||
/// </summary>
|
||||
/// <param name="isFallbackSearch">Bool to indicate if it is a fallback query.</param>
|
||||
/// <param name="queryInput">User input as string including the action keyword.</param>
|
||||
/// <param name="errorMessage">Error message if applicable.</param>
|
||||
/// <param name="exception">Exception if applicable.</param>
|
||||
/// <returns>List of results to show. Either an error message or an empty list.</returns>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="errorMessage"/> and <paramref name="exception"/> are both filled with their default values.</exception>
|
||||
internal static ListItem OnError(bool isFallbackSearch, string queryInput, string errorMessage, Exception exception = default)
|
||||
{
|
||||
string userMessage;
|
||||
|
||||
if (errorMessage != default)
|
||||
{
|
||||
Logger.LogError($"Failed to calculate <{queryInput}>: {errorMessage}");
|
||||
userMessage = errorMessage;
|
||||
}
|
||||
else if (exception != default)
|
||||
{
|
||||
Logger.LogError($"Exception when query for <{queryInput}>", exception);
|
||||
userMessage = exception.Message;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("The arguments error and exception have default values. One of them has to be filled with valid error data (error message/exception)!");
|
||||
}
|
||||
|
||||
return isFallbackSearch ? null : CreateErrorResult(userMessage);
|
||||
}
|
||||
|
||||
private static ListItem CreateErrorResult(string errorMessage)
|
||||
{
|
||||
return new ListItem(new NoOpCommand())
|
||||
{
|
||||
Title = Properties.Resources.calculator_calculation_failed_title,
|
||||
Subtitle = errorMessage,
|
||||
Icon = CalculatorIcons.ErrorIcon,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.Calc.Helper;
|
||||
|
||||
/// <summary>
|
||||
/// Tries to convert all numbers in a text from one culture format to another.
|
||||
/// </summary>
|
||||
public class NumberTranslator
|
||||
{
|
||||
private readonly CultureInfo sourceCulture;
|
||||
private readonly CultureInfo targetCulture;
|
||||
private readonly Regex splitRegexForSource;
|
||||
private readonly Regex splitRegexForTarget;
|
||||
|
||||
private NumberTranslator(CultureInfo sourceCulture, CultureInfo targetCulture)
|
||||
{
|
||||
this.sourceCulture = sourceCulture;
|
||||
this.targetCulture = targetCulture;
|
||||
|
||||
splitRegexForSource = GetSplitRegex(this.sourceCulture);
|
||||
splitRegexForTarget = GetSplitRegex(this.targetCulture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new <see cref="NumberTranslator"/>.
|
||||
/// </summary>
|
||||
/// <param name="sourceCulture">source culture</param>
|
||||
/// <param name="targetCulture">target culture</param>
|
||||
/// <returns>Number translator for target culture</returns>
|
||||
public static NumberTranslator Create(CultureInfo sourceCulture, CultureInfo targetCulture)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(sourceCulture);
|
||||
|
||||
ArgumentNullException.ThrowIfNull(targetCulture);
|
||||
|
||||
return new NumberTranslator(sourceCulture, targetCulture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Translate from source to target culture.
|
||||
/// </summary>
|
||||
/// <param name="input">input string to translate</param>
|
||||
/// <returns>translated string</returns>
|
||||
public string Translate(string input)
|
||||
{
|
||||
return Translate(input, sourceCulture, targetCulture, splitRegexForSource);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Translate from target to source culture.
|
||||
/// </summary>
|
||||
/// <param name="input">input string to translate back to source culture</param>
|
||||
/// <returns>source culture string</returns>
|
||||
public string TranslateBack(string input)
|
||||
{
|
||||
return Translate(input, targetCulture, sourceCulture, splitRegexForTarget);
|
||||
}
|
||||
|
||||
private static string Translate(string input, CultureInfo cultureFrom, CultureInfo cultureTo, Regex splitRegex)
|
||||
{
|
||||
var outputBuilder = new StringBuilder();
|
||||
var hexRegex = new Regex(@"(?:(0x[\da-fA-F]+))");
|
||||
|
||||
var hexTokens = hexRegex.Split(input);
|
||||
|
||||
foreach (var hexToken in hexTokens)
|
||||
{
|
||||
if (hexToken.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
// Mages engine has issues processing large hex number (larger than 7 hex digits + 0x prefix = 9 characters). So we convert it to decimal and pass it to the engine.
|
||||
if (hexToken.Length > 9)
|
||||
{
|
||||
try
|
||||
{
|
||||
var num = Convert.ToInt64(hexToken, 16);
|
||||
var numStr = num.ToString(cultureFrom);
|
||||
outputBuilder.Append(numStr);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
outputBuilder.Append(hexToken);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
outputBuilder.Append(hexToken);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
var tokens = splitRegex.Split(hexToken);
|
||||
foreach (var token in tokens)
|
||||
{
|
||||
var leadingZeroCount = 0;
|
||||
|
||||
// Count leading zero characters.
|
||||
foreach (var c in token)
|
||||
{
|
||||
if (c != '0')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
leadingZeroCount++;
|
||||
}
|
||||
|
||||
// number is all zero characters. no need to add zero characters at the end.
|
||||
if (token.Length == leadingZeroCount)
|
||||
{
|
||||
leadingZeroCount = 0;
|
||||
}
|
||||
|
||||
decimal number;
|
||||
|
||||
outputBuilder.Append(
|
||||
decimal.TryParse(token, NumberStyles.Number, cultureFrom, out number)
|
||||
? (new string('0', leadingZeroCount) + number.ToString(cultureTo))
|
||||
: token.Replace(cultureFrom.TextInfo.ListSeparator, cultureTo.TextInfo.ListSeparator));
|
||||
}
|
||||
}
|
||||
|
||||
return outputBuilder.ToString();
|
||||
}
|
||||
|
||||
private static Regex GetSplitRegex(CultureInfo culture)
|
||||
{
|
||||
var splitPattern = $"((?:\\d|{Regex.Escape(culture.NumberFormat.NumberDecimalSeparator)}";
|
||||
if (!string.IsNullOrEmpty(culture.NumberFormat.NumberGroupSeparator))
|
||||
{
|
||||
splitPattern += $"|{Regex.Escape(culture.NumberFormat.NumberGroupSeparator)}";
|
||||
}
|
||||
|
||||
splitPattern += ")+)";
|
||||
return new Regex(splitPattern);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.Calc.Helper;
|
||||
|
||||
public static partial class QueryHelper
|
||||
{
|
||||
public static ListItem Query(string query, SettingsManager settings, bool isFallbackSearch, TypedEventHandler<object, object> handleSave = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(query);
|
||||
if (!isFallbackSearch)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(handleSave);
|
||||
}
|
||||
|
||||
CultureInfo inputCulture = settings.InputUseEnglishFormat ? new CultureInfo("en-us") : CultureInfo.CurrentCulture;
|
||||
CultureInfo outputCulture = settings.OutputUseEnglishFormat ? new CultureInfo("en-us") : CultureInfo.CurrentCulture;
|
||||
|
||||
// Happens if the user has only typed the action key so far
|
||||
if (string.IsNullOrEmpty(query))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
NumberTranslator translator = NumberTranslator.Create(inputCulture, new CultureInfo("en-US"));
|
||||
var input = translator.Translate(query.Normalize(NormalizationForm.FormKC));
|
||||
|
||||
if (!CalculateHelper.InputValid(input))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Using CurrentUICulture since this is user facing
|
||||
var result = CalculateEngine.Interpret(settings, input, outputCulture, out var errorMessage);
|
||||
|
||||
// This could happen for some incorrect queries, like pi(2)
|
||||
if (result.Equals(default(CalculateResult)))
|
||||
{
|
||||
// If errorMessage is not default then do error handling
|
||||
return errorMessage == default ? null : ErrorHandler.OnError(isFallbackSearch, query, errorMessage);
|
||||
}
|
||||
|
||||
if (isFallbackSearch)
|
||||
{
|
||||
// Fallback search
|
||||
return ResultHelper.CreateResult(result.RoundedResult, inputCulture, outputCulture, query);
|
||||
}
|
||||
|
||||
return ResultHelper.CreateResult(result.RoundedResult, inputCulture, outputCulture, query, handleSave);
|
||||
}
|
||||
catch (Mages.Core.ParseException)
|
||||
{
|
||||
// Invalid input
|
||||
return ErrorHandler.OnError(isFallbackSearch, query, Properties.Resources.calculator_expression_not_complete);
|
||||
}
|
||||
catch (OverflowException)
|
||||
{
|
||||
// Result to big to convert to decimal
|
||||
return ErrorHandler.OnError(isFallbackSearch, query, Properties.Resources.calculator_not_covert_to_decimal);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// Any other crash occurred
|
||||
// We want to keep the process alive if any the mages library throws any exceptions.
|
||||
return ErrorHandler.OnError(isFallbackSearch, query, default, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
// 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 ManagedCommon;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.Calc.Helper;
|
||||
|
||||
public static class ResultHelper
|
||||
{
|
||||
public static ListItem CreateResult(decimal? roundedResult, CultureInfo inputCulture, CultureInfo outputCulture, string query, TypedEventHandler<object, object> handleSave)
|
||||
{
|
||||
// Return null when the expression is not a valid calculator query.
|
||||
if (roundedResult == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var result = roundedResult?.ToString(outputCulture);
|
||||
|
||||
// Create a SaveCommand and subscribe to the SaveRequested event
|
||||
// This can append the result to the history list.
|
||||
var saveCommand = new SaveCommand(result);
|
||||
saveCommand.SaveRequested += handleSave;
|
||||
|
||||
var copyCommandItem = CreateResult(roundedResult, inputCulture, outputCulture, query);
|
||||
|
||||
return new ListItem(saveCommand)
|
||||
{
|
||||
// Using CurrentCulture since this is user facing
|
||||
Icon = CalculatorIcons.ResultIcon,
|
||||
Title = result,
|
||||
Subtitle = query,
|
||||
TextToSuggest = result,
|
||||
MoreCommands = [
|
||||
new CommandContextItem(copyCommandItem.Command)
|
||||
{
|
||||
Icon = copyCommandItem.Icon,
|
||||
Title = copyCommandItem.Title,
|
||||
Subtitle = copyCommandItem.Subtitle,
|
||||
},
|
||||
..copyCommandItem.MoreCommands,
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
public static ListItem CreateResult(decimal? roundedResult, CultureInfo inputCulture, CultureInfo outputCulture, string query)
|
||||
{
|
||||
// Return null when the expression is not a valid calculator query.
|
||||
if (roundedResult == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var decimalResult = roundedResult?.ToString(outputCulture);
|
||||
|
||||
List<CommandContextItem> context = [];
|
||||
|
||||
if (decimal.IsInteger((decimal)roundedResult))
|
||||
{
|
||||
var i = decimal.ToInt64((decimal)roundedResult);
|
||||
try
|
||||
{
|
||||
var hexResult = "0x" + i.ToString("X", outputCulture);
|
||||
context.Add(new CommandContextItem(new CopyTextCommand(hexResult) { Name = Properties.Resources.calculator_copy_hex })
|
||||
{
|
||||
Title = hexResult,
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError("Error parsing hex format", ex);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var binaryResult = "0b" + i.ToString("B", outputCulture);
|
||||
context.Add(new CommandContextItem(new CopyTextCommand(binaryResult) { Name = Properties.Resources.calculator_copy_binary })
|
||||
{
|
||||
Title = binaryResult,
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError("Error parsing binary format", ex);
|
||||
}
|
||||
}
|
||||
|
||||
return new ListItem(new CopyTextCommand(decimalResult))
|
||||
{
|
||||
// Using CurrentCulture since this is user facing
|
||||
Title = decimalResult,
|
||||
Subtitle = query,
|
||||
TextToSuggest = decimalResult,
|
||||
MoreCommands = context.ToArray(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// 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.Properties;
|
||||
using Microsoft.CommandPalette.Extensions;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.Calc.Helper;
|
||||
|
||||
public sealed partial class SaveCommand : InvokableCommand
|
||||
{
|
||||
private readonly string _result;
|
||||
|
||||
public event TypedEventHandler<object, object> SaveRequested;
|
||||
|
||||
public SaveCommand(string result)
|
||||
{
|
||||
Name = Resources.calculator_save_command_name;
|
||||
Icon = CalculatorIcons.SaveIcon;
|
||||
_result = result;
|
||||
}
|
||||
|
||||
public override ICommandResult Invoke()
|
||||
{
|
||||
SaveRequested?.Invoke(this, this);
|
||||
ClipboardHelper.SetText(_result);
|
||||
return CommandResult.KeepOpen();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
// 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 System.IO;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.Calc.Helper;
|
||||
|
||||
public class SettingsManager : JsonSettingsManager
|
||||
{
|
||||
private static readonly string _namespace = "calculator";
|
||||
|
||||
private static string Namespaced(string propertyName) => $"{_namespace}.{propertyName}";
|
||||
|
||||
private static readonly List<ChoiceSetSetting.Choice> _trigUnitChoices = new()
|
||||
{
|
||||
new ChoiceSetSetting.Choice(Properties.Resources.calculator_settings_trig_unit_radians, "0"),
|
||||
new ChoiceSetSetting.Choice(Properties.Resources.calculator_settings_trig_unit_degrees, "1"),
|
||||
new ChoiceSetSetting.Choice(Properties.Resources.calculator_settings_trig_unit_gradians, "2"),
|
||||
};
|
||||
|
||||
private readonly ChoiceSetSetting _trigUnit = new(
|
||||
Namespaced(nameof(TrigUnit)),
|
||||
Properties.Resources.calculator_settings_trig_unit_mode,
|
||||
Properties.Resources.calculator_settings_trig_unit_mode_description,
|
||||
_trigUnitChoices);
|
||||
|
||||
private readonly ToggleSetting _inputUseEnNumberFormat = new(
|
||||
Namespaced(nameof(InputUseEnglishFormat)),
|
||||
Properties.Resources.calculator_settings_in_en_format,
|
||||
Properties.Resources.calculator_settings_in_en_format_description,
|
||||
false);
|
||||
|
||||
private readonly ToggleSetting _outputUseEnNumberFormat = new(
|
||||
Namespaced(nameof(OutputUseEnglishFormat)),
|
||||
Properties.Resources.calculator_settings_out_en_format,
|
||||
Properties.Resources.calculator_settings_out_en_format_description,
|
||||
false);
|
||||
|
||||
public CalculateEngine.TrigMode TrigUnit
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_trigUnit.Value == null || string.IsNullOrEmpty(_trigUnit.Value))
|
||||
{
|
||||
return CalculateEngine.TrigMode.Radians;
|
||||
}
|
||||
|
||||
var success = int.TryParse(_trigUnit.Value, out var result);
|
||||
|
||||
if (!success)
|
||||
{
|
||||
return CalculateEngine.TrigMode.Radians;
|
||||
}
|
||||
|
||||
switch (result)
|
||||
{
|
||||
case 0:
|
||||
return CalculateEngine.TrigMode.Radians;
|
||||
case 1:
|
||||
return CalculateEngine.TrigMode.Degrees;
|
||||
case 2:
|
||||
return CalculateEngine.TrigMode.Gradians;
|
||||
default:
|
||||
return CalculateEngine.TrigMode.Radians;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool InputUseEnglishFormat => _inputUseEnNumberFormat.Value;
|
||||
|
||||
public bool OutputUseEnglishFormat => _outputUseEnNumberFormat.Value;
|
||||
|
||||
internal static string SettingsJsonPath()
|
||||
{
|
||||
var directory = Utilities.BaseSettingsPath("Microsoft.CmdPal");
|
||||
Directory.CreateDirectory(directory);
|
||||
|
||||
// now, the state is just next to the exe
|
||||
return Path.Combine(directory, "settings.json");
|
||||
}
|
||||
|
||||
public SettingsManager()
|
||||
{
|
||||
FilePath = SettingsJsonPath();
|
||||
|
||||
Settings.Add(_trigUnit);
|
||||
Settings.Add(_inputUseEnNumberFormat);
|
||||
Settings.Add(_outputUseEnNumberFormat);
|
||||
|
||||
// Load settings from file upon initialization
|
||||
LoadSettings();
|
||||
|
||||
Settings.SettingsChanged += (s, a) => this.SaveSettings();
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,14 @@
|
||||
<ProjectPriFileName>Microsoft.CmdPal.Ext.Calc.pri</ProjectPriFileName>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\common\ManagedCommon\ManagedCommon.csproj" />
|
||||
<ProjectReference Include="..\..\extensionsdk\Microsoft.CommandPalette.Extensions.Toolkit\Microsoft.CommandPalette.Extensions.Toolkit.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Mages" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Update="Properties\Resources.Designer.cs">
|
||||
<DependentUpon>Resources.resx</DependentUpon>
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
// 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 System.Threading;
|
||||
using Microsoft.CmdPal.Ext.Calc.Helper;
|
||||
using Microsoft.CmdPal.Ext.Calc.Properties;
|
||||
using Microsoft.CommandPalette.Extensions;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.Calc.Pages;
|
||||
|
||||
// The calculator page is a dynamic list page
|
||||
// * The first command is where we display the results. Title=result, Subtitle=query
|
||||
// - The default command is `SaveCommand`.
|
||||
// - When you save, insert into list at spot 1
|
||||
// - change SearchText to the result
|
||||
// - MoreCommands: a single `CopyCommand` to copy the result to the clipboard
|
||||
// * The rest of the items are previously saved results
|
||||
// - Command is a CopyCommand
|
||||
// - Each item also sets the TextToSuggest to the result
|
||||
public sealed partial class CalculatorListPage : DynamicListPage
|
||||
{
|
||||
private readonly Lock _resultsLock = new();
|
||||
private readonly SettingsManager _settingsManager;
|
||||
private readonly List<ListItem> _items = [];
|
||||
private readonly List<ListItem> 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;
|
||||
|
||||
public CalculatorListPage(SettingsManager settings)
|
||||
{
|
||||
_settingsManager = settings;
|
||||
Icon = CalculatorIcons.ProviderIcon;
|
||||
Name = Resources.calculator_title;
|
||||
PlaceholderText = Resources.calculator_placeholder_text;
|
||||
Id = "com.microsoft.cmdpal.calculator";
|
||||
|
||||
_emptyItem = new ListItem(new NoOpCommand())
|
||||
{
|
||||
Title = Resources.calculator_placeholder_text,
|
||||
Icon = CalculatorIcons.ResultIcon,
|
||||
};
|
||||
EmptyContent = new CommandItem(new NoOpCommand())
|
||||
{
|
||||
Icon = CalculatorIcons.ProviderIcon,
|
||||
Title = Resources.calculator_placeholder_text,
|
||||
};
|
||||
|
||||
UpdateSearchText(string.Empty, string.Empty);
|
||||
}
|
||||
|
||||
public override void UpdateSearchText(string oldSearch, string newSearch)
|
||||
{
|
||||
if (oldSearch == newSearch)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(skipQuerySearchText) && newSearch == skipQuerySearchText)
|
||||
{
|
||||
// only skip once.
|
||||
skipQuerySearchText = string.Empty;
|
||||
return;
|
||||
}
|
||||
|
||||
skipQuerySearchText = string.Empty;
|
||||
|
||||
_emptyItem.Subtitle = newSearch;
|
||||
|
||||
var result = QueryHelper.Query(newSearch, _settingsManager, false, HandleSave);
|
||||
UpdateResult(result);
|
||||
}
|
||||
|
||||
private void UpdateResult(ListItem result)
|
||||
{
|
||||
lock (_resultsLock)
|
||||
{
|
||||
this._items.Clear();
|
||||
|
||||
if (result != null)
|
||||
{
|
||||
this._items.Add(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
_items.Add(_emptyItem);
|
||||
}
|
||||
|
||||
this._items.AddRange(history);
|
||||
}
|
||||
|
||||
RaiseItemsChanged(this._items.Count);
|
||||
}
|
||||
|
||||
private void HandleSave(object sender, object args)
|
||||
{
|
||||
var lastResult = _items[0].Title;
|
||||
if (!string.IsNullOrEmpty(lastResult))
|
||||
{
|
||||
var li = new ListItem(new CopyTextCommand(lastResult))
|
||||
{
|
||||
Title = _items[0].Title,
|
||||
Subtitle = _items[0].Subtitle,
|
||||
TextToSuggest = lastResult,
|
||||
};
|
||||
|
||||
history.Insert(0, li);
|
||||
_items.Insert(1, li);
|
||||
|
||||
// Why we need to clean the query record? Removed, but if necessary, please move it back.
|
||||
// _items[0].Subtitle = string.Empty;
|
||||
|
||||
// this change will call the UpdateSearchText again.
|
||||
// We need to avoid it.
|
||||
skipQuerySearchText = lastResult;
|
||||
SearchText = lastResult;
|
||||
this.RaiseItemsChanged(this._items.Count);
|
||||
}
|
||||
}
|
||||
|
||||
public override IListItem[] GetItems() => _items.ToArray();
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using Microsoft.CmdPal.Ext.Calc.Helper;
|
||||
using Microsoft.CmdPal.Ext.Calc.Properties;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.Calc.Pages;
|
||||
|
||||
public sealed partial class FallbackCalculatorItem : FallbackCommandItem
|
||||
{
|
||||
private readonly CopyTextCommand _copyCommand = new(string.Empty);
|
||||
private readonly SettingsManager _settings;
|
||||
|
||||
public FallbackCalculatorItem(SettingsManager settings)
|
||||
: base(new NoOpCommand(), Resources.calculator_title)
|
||||
{
|
||||
Command = _copyCommand;
|
||||
_copyCommand.Name = string.Empty;
|
||||
Title = string.Empty;
|
||||
Subtitle = Resources.calculator_placeholder_text;
|
||||
Icon = CalculatorIcons.ProviderIcon;
|
||||
_settings = settings;
|
||||
}
|
||||
|
||||
public override void UpdateQuery(string query)
|
||||
{
|
||||
var result = QueryHelper.Query(query, _settings, true, null);
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
_copyCommand.Text = string.Empty;
|
||||
_copyCommand.Name = string.Empty;
|
||||
Title = string.Empty;
|
||||
Subtitle = string.Empty;
|
||||
MoreCommands = [];
|
||||
return;
|
||||
}
|
||||
|
||||
_copyCommand.Text = result.Title;
|
||||
_copyCommand.Name = string.IsNullOrWhiteSpace(query) ? string.Empty : Resources.calculator_copy_command_name;
|
||||
Title = result.Title;
|
||||
|
||||
// we have to make the subtitle the equation,
|
||||
// so that we will still string match the original query
|
||||
// Otherwise, something like 1+2 will have a title of "3" and not match
|
||||
Subtitle = query;
|
||||
|
||||
MoreCommands = result.MoreCommands;
|
||||
}
|
||||
}
|
||||
@@ -60,6 +60,24 @@ namespace Microsoft.CmdPal.Ext.Calc.Properties {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Failed to calculate the input.
|
||||
/// </summary>
|
||||
public static string calculator_calculation_failed_title {
|
||||
get {
|
||||
return ResourceManager.GetString("calculator_calculation_failed_title", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Copy binary.
|
||||
/// </summary>
|
||||
public static string calculator_copy_binary {
|
||||
get {
|
||||
return ResourceManager.GetString("calculator_copy_binary", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Copy.
|
||||
/// </summary>
|
||||
@@ -69,6 +87,15 @@ namespace Microsoft.CmdPal.Ext.Calc.Properties {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Copy hexadecimal.
|
||||
/// </summary>
|
||||
public static string calculator_copy_hex {
|
||||
get {
|
||||
return ResourceManager.GetString("calculator_copy_hex", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Calculator.
|
||||
/// </summary>
|
||||
@@ -78,6 +105,24 @@ namespace Microsoft.CmdPal.Ext.Calc.Properties {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Expression contains division by zero.
|
||||
/// </summary>
|
||||
public static string calculator_division_by_zero {
|
||||
get {
|
||||
return ResourceManager.GetString("calculator_division_by_zero", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Unsupported use of square brackets.
|
||||
/// </summary>
|
||||
public static string calculator_double_array_returned {
|
||||
get {
|
||||
return ResourceManager.GetString("calculator_double_array_returned", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Error: {0}.
|
||||
/// </summary>
|
||||
@@ -87,6 +132,33 @@ namespace Microsoft.CmdPal.Ext.Calc.Properties {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Expression wrong or incomplete.
|
||||
/// </summary>
|
||||
public static string calculator_expression_not_complete {
|
||||
get {
|
||||
return ResourceManager.GetString("calculator_expression_not_complete", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Calculation result is not a valid number (NaN).
|
||||
/// </summary>
|
||||
public static string calculator_not_a_number {
|
||||
get {
|
||||
return ResourceManager.GetString("calculator_not_a_number", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Result value was either too large or too small for a decimal number.
|
||||
/// </summary>
|
||||
public static string calculator_not_covert_to_decimal {
|
||||
get {
|
||||
return ResourceManager.GetString("calculator_not_covert_to_decimal", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Type an equation....
|
||||
/// </summary>
|
||||
@@ -105,6 +177,105 @@ namespace Microsoft.CmdPal.Ext.Calc.Properties {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Use English (United States) number format for input.
|
||||
/// </summary>
|
||||
public static string calculator_settings_in_en_format {
|
||||
get {
|
||||
return ResourceManager.GetString("calculator_settings_in_en_format", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Ignores your system setting and expects numbers in the format '{0}'..
|
||||
/// </summary>
|
||||
public static string calculator_settings_in_en_format_description {
|
||||
get {
|
||||
return ResourceManager.GetString("calculator_settings_in_en_format_description", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Use English (United States) number format for output.
|
||||
/// </summary>
|
||||
public static string calculator_settings_out_en_format {
|
||||
get {
|
||||
return ResourceManager.GetString("calculator_settings_out_en_format", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Ignores your system setting and returns numbers in the format '{0}'..
|
||||
/// </summary>
|
||||
public static string calculator_settings_out_en_format_description {
|
||||
get {
|
||||
return ResourceManager.GetString("calculator_settings_out_en_format_description", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Replace input if query ends with '='.
|
||||
/// </summary>
|
||||
public static string calculator_settings_replace_input {
|
||||
get {
|
||||
return ResourceManager.GetString("calculator_settings_replace_input", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to When using direct activation, appending '=' to the expression will replace the input with the calculated result (e.g. '=5*3-2=' will change the query to '=13')..
|
||||
/// </summary>
|
||||
public static string calculator_settings_replace_input_description {
|
||||
get {
|
||||
return ResourceManager.GetString("calculator_settings_replace_input_description", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Degrees.
|
||||
/// </summary>
|
||||
public static string calculator_settings_trig_unit_degrees {
|
||||
get {
|
||||
return ResourceManager.GetString("calculator_settings_trig_unit_degrees", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Gradians.
|
||||
/// </summary>
|
||||
public static string calculator_settings_trig_unit_gradians {
|
||||
get {
|
||||
return ResourceManager.GetString("calculator_settings_trig_unit_gradians", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Trigonometry Unit.
|
||||
/// </summary>
|
||||
public static string calculator_settings_trig_unit_mode {
|
||||
get {
|
||||
return ResourceManager.GetString("calculator_settings_trig_unit_mode", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Specifies the angle unit to use for trigonometry operations.
|
||||
/// </summary>
|
||||
public static string calculator_settings_trig_unit_mode_description {
|
||||
get {
|
||||
return ResourceManager.GetString("calculator_settings_trig_unit_mode_description", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Radians.
|
||||
/// </summary>
|
||||
public static string calculator_settings_trig_unit_radians {
|
||||
get {
|
||||
return ResourceManager.GetString("calculator_settings_trig_unit_radians", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Calculator.
|
||||
/// </summary>
|
||||
|
||||
@@ -140,4 +140,63 @@
|
||||
<data name="calculator_copy_command_name" xml:space="preserve">
|
||||
<value>Copy</value>
|
||||
</data>
|
||||
<data name="calculator_calculation_failed_title" xml:space="preserve">
|
||||
<value>Failed to calculate the input</value>
|
||||
</data>
|
||||
<data name="calculator_division_by_zero" xml:space="preserve">
|
||||
<value>Expression contains division by zero</value>
|
||||
</data>
|
||||
<data name="calculator_expression_not_complete" xml:space="preserve">
|
||||
<value>Expression wrong or incomplete</value>
|
||||
</data>
|
||||
<data name="calculator_not_a_number" xml:space="preserve">
|
||||
<value>Calculation result is not a valid number (NaN)</value>
|
||||
</data>
|
||||
<data name="calculator_double_array_returned" xml:space="preserve">
|
||||
<value>Unsupported use of square brackets</value>
|
||||
</data>
|
||||
<data name="calculator_settings_trig_unit_gradians" xml:space="preserve">
|
||||
<value>Gradians</value>
|
||||
</data>
|
||||
<data name="calculator_settings_trig_unit_degrees" xml:space="preserve">
|
||||
<value>Degrees</value>
|
||||
</data>
|
||||
<data name="calculator_settings_trig_unit_radians" xml:space="preserve">
|
||||
<value>Radians</value>
|
||||
</data>
|
||||
<data name="calculator_settings_trig_unit_mode" xml:space="preserve">
|
||||
<value>Trigonometry Unit</value>
|
||||
</data>
|
||||
<data name="calculator_settings_trig_unit_mode_description" xml:space="preserve">
|
||||
<value>Specifies the angle unit to use for trigonometry operations</value>
|
||||
</data>
|
||||
<data name="calculator_settings_out_en_format" xml:space="preserve">
|
||||
<value>Use English (United States) number format for output</value>
|
||||
</data>
|
||||
<data name="calculator_settings_out_en_format_description" xml:space="preserve">
|
||||
<value>Ignores your system setting and returns numbers in the format '{0}'.</value>
|
||||
<comment>{0} is a placeholder and will be replaced in code.</comment>
|
||||
</data>
|
||||
<data name="calculator_settings_in_en_format" xml:space="preserve">
|
||||
<value>Use English (United States) number format for input</value>
|
||||
</data>
|
||||
<data name="calculator_settings_in_en_format_description" xml:space="preserve">
|
||||
<value>Ignores your system setting and expects numbers in the format '{0}'.</value>
|
||||
<comment>{0} is a placeholder and will be replaced in code.</comment>
|
||||
</data>
|
||||
<data name="calculator_settings_replace_input" xml:space="preserve">
|
||||
<value>Replace input if query ends with '='</value>
|
||||
</data>
|
||||
<data name="calculator_settings_replace_input_description" xml:space="preserve">
|
||||
<value>When using direct activation, appending '=' to the expression will replace the input with the calculated result (e.g. '=5*3-2=' will change the query to '=13').</value>
|
||||
</data>
|
||||
<data name="calculator_not_covert_to_decimal" xml:space="preserve">
|
||||
<value>Result value was either too large or too small for a decimal number</value>
|
||||
</data>
|
||||
<data name="calculator_copy_hex" xml:space="preserve">
|
||||
<value>Copy hexadecimal</value>
|
||||
</data>
|
||||
<data name="calculator_copy_binary" xml:space="preserve">
|
||||
<value>Copy binary</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
|
||||
using Microsoft.CmdPal.Ext.System.Helpers;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.Shell;
|
||||
namespace Microsoft.CmdPal.Ext.System;
|
||||
|
||||
public sealed partial class EmptyRecycleBinCommand : InvokableCommand
|
||||
{
|
||||
|
||||
@@ -2,7 +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.Shell;
|
||||
using Microsoft.CmdPal.Ext.System.Helpers;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
// 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 Microsoft.CmdPal.Ext.System.Helpers;
|
||||
using Microsoft.CommandPalette.Extensions;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.System;
|
||||
|
||||
internal sealed partial class FallbackSystemCommandItem : FallbackCommandItem
|
||||
{
|
||||
public FallbackSystemCommandItem(SettingsManager settings)
|
||||
: base(new NoOpCommand(), Resources.Microsoft_plugin_ext_fallback_display_title)
|
||||
{
|
||||
Title = string.Empty;
|
||||
Subtitle = string.Empty;
|
||||
|
||||
var isBootedInUefiMode = Win32Helpers.GetSystemFirmwareType() == FirmwareType.Uefi;
|
||||
var hideEmptyRB = settings.HideEmptyRecycleBin;
|
||||
var confirmSystemCommands = settings.ShowDialogToConfirmCommand;
|
||||
var showSuccessOnEmptyRB = settings.ShowSuccessMessageAfterEmptyingRecycleBin;
|
||||
|
||||
systemCommands = Commands.GetSystemCommands(isBootedInUefiMode, hideEmptyRB, confirmSystemCommands, showSuccessOnEmptyRB);
|
||||
}
|
||||
|
||||
private readonly List<IListItem> systemCommands;
|
||||
|
||||
public override void UpdateQuery(string query)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(query))
|
||||
{
|
||||
Title = string.Empty;
|
||||
Subtitle = string.Empty;
|
||||
return;
|
||||
}
|
||||
|
||||
IListItem? result = null;
|
||||
var resultScore = 0;
|
||||
|
||||
// find the max score for the query
|
||||
foreach (var command in systemCommands)
|
||||
{
|
||||
var title = command.Title;
|
||||
var subTitle = command.Subtitle;
|
||||
var titleScore = StringMatcher.FuzzySearch(query, title).Score;
|
||||
var subTitleScore = StringMatcher.FuzzySearch(query, subTitle).Score;
|
||||
|
||||
var maxScore = Math.Max(titleScore, subTitleScore);
|
||||
if (maxScore > resultScore)
|
||||
{
|
||||
resultScore = maxScore;
|
||||
result = command;
|
||||
}
|
||||
}
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
Title = string.Empty;
|
||||
Subtitle = string.Empty;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Title = result.Title;
|
||||
Subtitle = result.Subtitle;
|
||||
Icon = result.Icon;
|
||||
Command = result.Command;
|
||||
}
|
||||
}
|
||||
@@ -85,7 +85,7 @@ internal static class Commands
|
||||
{
|
||||
results.AddRange(new[]
|
||||
{
|
||||
new ListItem(new OpenInShellCommand(Resources.Microsoft_plugin_command_name_empty, "explorer.exe", "shell:RecycleBinFolder"))
|
||||
new ListItem(new OpenInShellCommand(Resources.Microsoft_plugin_command_name_open, "explorer.exe", "shell:RecycleBinFolder"))
|
||||
{
|
||||
Title = Resources.Microsoft_plugin_sys_RecycleBinOpen,
|
||||
Subtitle = Resources.Microsoft_plugin_sys_RecycleBin_description,
|
||||
@@ -102,7 +102,7 @@ internal static class Commands
|
||||
else
|
||||
{
|
||||
results.Add(
|
||||
new ListItem(new OpenInShellCommand(Resources.Microsoft_plugin_command_name_empty, "explorer.exe", "shell:RecycleBinFolder"))
|
||||
new ListItem(new OpenInShellCommand(Resources.Microsoft_plugin_command_name_open, "explorer.exe", "shell:RecycleBinFolder"))
|
||||
{
|
||||
Title = Resources.Microsoft_plugin_sys_RecycleBin,
|
||||
Subtitle = Resources.Microsoft_plugin_sys_RecycleBin_description,
|
||||
|
||||
@@ -186,6 +186,15 @@ namespace Microsoft.CmdPal.Ext.System {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Open System Command.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_ext_fallback_display_title {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_ext_fallback_display_title", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Hide disconnected network info.
|
||||
/// </summary>
|
||||
|
||||
@@ -411,4 +411,7 @@
|
||||
<data name="Microsoft_plugin_command_name_sleep" xml:space="preserve">
|
||||
<value>Sleep</value>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_ext_fallback_display_title" xml:space="preserve">
|
||||
<value>Open System Command</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -14,6 +14,7 @@ public partial class SystemCommandExtensionProvider : CommandProvider
|
||||
private readonly ICommandItem[] _commands;
|
||||
private static readonly SettingsManager _settingsManager = new();
|
||||
public static readonly SystemCommandPage Page = new(_settingsManager);
|
||||
private readonly FallbackSystemCommandItem _fallbackFileItem = new(_settingsManager);
|
||||
|
||||
public SystemCommandExtensionProvider()
|
||||
{
|
||||
@@ -36,4 +37,6 @@ public partial class SystemCommandExtensionProvider : CommandProvider
|
||||
{
|
||||
return _commands;
|
||||
}
|
||||
|
||||
public override IFallbackCommandItem[] FallbackCommands() => [_fallbackFileItem];
|
||||
}
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.CmdPal.Ext.System.Helpers;
|
||||
using Microsoft.CommandPalette.Extensions;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.System;
|
||||
|
||||
public sealed partial class SystemCommandsCache
|
||||
{
|
||||
public SystemCommandsCache(SettingsManager manager)
|
||||
{
|
||||
var list = new List<IListItem>();
|
||||
var listLock = new object();
|
||||
|
||||
var a = Task.Run(() =>
|
||||
{
|
||||
var isBootedInUefiMode = Win32Helpers.GetSystemFirmwareType() == FirmwareType.Uefi;
|
||||
|
||||
var separateEmptyRB = manager.HideEmptyRecycleBin;
|
||||
var confirmSystemCommands = manager.ShowDialogToConfirmCommand;
|
||||
var showSuccessOnEmptyRB = manager.ShowSuccessMessageAfterEmptyingRecycleBin;
|
||||
|
||||
// normal system commands are fast and can be returned immediately
|
||||
var systemCommands = Commands.GetSystemCommands(isBootedInUefiMode, separateEmptyRB, confirmSystemCommands, showSuccessOnEmptyRB);
|
||||
lock (listLock)
|
||||
{
|
||||
list.AddRange(systemCommands);
|
||||
}
|
||||
});
|
||||
|
||||
var b = Task.Run(() =>
|
||||
{
|
||||
// Network (ip and mac) results are slow with many network cards and returned delayed.
|
||||
// On global queries the first word/part has to be 'ip', 'mac' or 'address' for network results
|
||||
var networkConnectionResults = Commands.GetNetworkConnectionResults(manager);
|
||||
lock (listLock)
|
||||
{
|
||||
list.AddRange(networkConnectionResults);
|
||||
}
|
||||
});
|
||||
|
||||
Task.WaitAll(a, b);
|
||||
CachedCommands = list.ToArray();
|
||||
}
|
||||
|
||||
public IListItem[] CachedCommands { get; }
|
||||
}
|
||||
@@ -4,11 +4,9 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
|
||||
[assembly: InternalsVisibleTo("Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests")]
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.TimeDate.Helpers;
|
||||
|
||||
internal class AvailableResult
|
||||
internal sealed class AvailableResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the time/date value
|
||||
@@ -30,6 +28,11 @@ internal class AvailableResult
|
||||
/// </summary>
|
||||
internal ResultIconType IconType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value to show additional error details
|
||||
/// </summary>
|
||||
internal string ErrorDetails { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Returns the path to the icon
|
||||
/// </summary>
|
||||
@@ -42,6 +45,7 @@ internal class AvailableResult
|
||||
ResultIconType.Time => ResultHelper.TimeIcon,
|
||||
ResultIconType.Date => ResultHelper.CalendarIcon,
|
||||
ResultIconType.DateTime => ResultHelper.TimeDateIcon,
|
||||
ResultIconType.Error => ResultHelper.ErrorIcon,
|
||||
_ => null,
|
||||
};
|
||||
}
|
||||
@@ -53,6 +57,7 @@ internal class AvailableResult
|
||||
Title = this.Value,
|
||||
Subtitle = this.Label,
|
||||
Icon = this.GetIconInfo(),
|
||||
Details = string.IsNullOrEmpty(this.ErrorDetails) ? null : new Details() { Body = this.ErrorDetails },
|
||||
};
|
||||
}
|
||||
|
||||
@@ -81,4 +86,5 @@ public enum ResultIconType
|
||||
Time,
|
||||
Date,
|
||||
DateTime,
|
||||
Error,
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.TimeDate.Helpers;
|
||||
|
||||
@@ -69,6 +70,86 @@ internal static class AvailableResultsList
|
||||
var era = DateTimeFormatInfo.CurrentInfo.GetEraName(calendar.GetEra(dateTimeNow));
|
||||
var eraShort = DateTimeFormatInfo.CurrentInfo.GetAbbreviatedEraName(calendar.GetEra(dateTimeNow));
|
||||
|
||||
// Custom formats
|
||||
foreach (var f in settings.CustomFormats)
|
||||
{
|
||||
var formatParts = f.Split("=", 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
var formatSyntax = formatParts.Length == 2 ? formatParts[1] : string.Empty;
|
||||
var searchTags = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagCustom");
|
||||
var dtObject = dateTimeNow;
|
||||
|
||||
// If Length = 0 then empty string.
|
||||
if (formatParts.Length >= 1)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Verify and check input and update search tags
|
||||
if (formatParts.Length == 1)
|
||||
{
|
||||
throw new FormatException("Format syntax part after equal sign is missing.");
|
||||
}
|
||||
|
||||
var containsCustomSyntax = TimeAndDateHelper.StringContainsCustomFormatSyntax(formatSyntax);
|
||||
if (formatSyntax.StartsWith("UTC:", StringComparison.InvariantCulture))
|
||||
{
|
||||
searchTags = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagCustomUtc");
|
||||
dtObject = dateTimeNowUtc;
|
||||
}
|
||||
|
||||
// Get formated date
|
||||
var value = TimeAndDateHelper.ConvertToCustomFormat(dtObject, unixTimestamp, unixTimestampMilliseconds, weekOfYear, eraShort, Regex.Replace(formatSyntax, "^UTC:", string.Empty), firstWeekRule, firstDayOfTheWeek);
|
||||
try
|
||||
{
|
||||
value = dtObject.ToString(value, CultureInfo.CurrentCulture);
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (!containsCustomSyntax)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Do not fail as we have custom format syntax. Instead fix backslashes.
|
||||
value = Regex.Replace(value, @"(?<!\\)\\", string.Empty).Replace("\\\\", "\\");
|
||||
}
|
||||
}
|
||||
|
||||
// Add result
|
||||
results.Add(new AvailableResult()
|
||||
{
|
||||
Value = value,
|
||||
Label = formatParts[0],
|
||||
AlternativeSearchTag = searchTags,
|
||||
IconType = ResultIconType.DateTime,
|
||||
});
|
||||
}
|
||||
catch (ArgumentOutOfRangeException e)
|
||||
{
|
||||
results.Add(new AvailableResult()
|
||||
{
|
||||
Value = Resources.Microsoft_plugin_timedate_ErrorConvertCustomFormat,
|
||||
Label = formatParts[0] + " - " + Resources.Microsoft_plugin_timedate_show_details,
|
||||
AlternativeSearchTag = searchTags,
|
||||
IconType = ResultIconType.Error,
|
||||
ErrorDetails = e.Message,
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
results.Add(new AvailableResult()
|
||||
{
|
||||
Value = Resources.Microsoft_plugin_timedate_InvalidCustomFormat + " " + formatSyntax,
|
||||
Label = formatParts[0] + " - " + Resources.Microsoft_plugin_timedate_show_details,
|
||||
AlternativeSearchTag = searchTags,
|
||||
IconType = ResultIconType.Error,
|
||||
ErrorDetails = e.Message,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Predefined formats
|
||||
results.AddRange(new[]
|
||||
{
|
||||
new AvailableResult()
|
||||
@@ -149,6 +230,13 @@ internal static class AvailableResultsList
|
||||
IconType = ResultIconType.Date,
|
||||
},
|
||||
new AvailableResult()
|
||||
{
|
||||
Value = DateTime.DaysInMonth(dateTimeNow.Year, dateTimeNow.Month).ToString(CultureInfo.CurrentCulture),
|
||||
Label = Resources.Microsoft_plugin_timedate_DaysInMonth,
|
||||
AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagDate"),
|
||||
IconType = ResultIconType.Date,
|
||||
},
|
||||
new AvailableResult()
|
||||
{
|
||||
Value = dateTimeNow.DayOfYear.ToString(CultureInfo.CurrentCulture),
|
||||
Label = Resources.Microsoft_plugin_timedate_DayOfYear,
|
||||
@@ -198,6 +286,13 @@ internal static class AvailableResultsList
|
||||
IconType = ResultIconType.Date,
|
||||
},
|
||||
new AvailableResult()
|
||||
{
|
||||
Value = DateTime.IsLeapYear(dateTimeNow.Year) ? Resources.Microsoft_plugin_timedate_LeapYear : Resources.Microsoft_plugin_timedate_NoLeapYear,
|
||||
Label = Resources.Microsoft_plugin_timedate_LeapYear,
|
||||
AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagDate"),
|
||||
IconType = ResultIconType.Date,
|
||||
},
|
||||
new AvailableResult()
|
||||
{
|
||||
Value = era,
|
||||
Label = Resources.Microsoft_plugin_timedate_Era,
|
||||
@@ -218,13 +313,31 @@ internal static class AvailableResultsList
|
||||
AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagDate"),
|
||||
IconType = ResultIconType.Date,
|
||||
},
|
||||
new AvailableResult()
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
results.Add(new AvailableResult()
|
||||
{
|
||||
Value = dateTimeNow.ToFileTime().ToString(CultureInfo.CurrentCulture),
|
||||
Label = Resources.Microsoft_plugin_timedate_WindowsFileTime,
|
||||
AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagFormat"),
|
||||
IconType = ResultIconType.DateTime,
|
||||
},
|
||||
});
|
||||
}
|
||||
catch
|
||||
{
|
||||
results.Add(new AvailableResult()
|
||||
{
|
||||
Value = Resources.Microsoft_plugin_timedate_ErrorConvertWft,
|
||||
Label = Resources.Microsoft_plugin_timedate_WindowsFileTime,
|
||||
AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagFormat"),
|
||||
IconType = ResultIconType.Error,
|
||||
});
|
||||
}
|
||||
|
||||
results.AddRange(new[]
|
||||
{
|
||||
new AvailableResult()
|
||||
{
|
||||
Value = dateTimeNowUtc.ToString("u"),
|
||||
|
||||
@@ -2,9 +2,8 @@
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using Microsoft.CommandPalette.Extensions;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.TimeDate.Helpers;
|
||||
@@ -33,21 +32,24 @@ internal static class ResultHelper
|
||||
|
||||
public static IconInfo TimeDateIcon { get; } = new IconInfo("\uEC92");
|
||||
|
||||
public static IconInfo ErrorIcon { get; } = IconHelpers.FromRelativePaths("Microsoft.CmdPal.Ext.TimeDate\\Assets\\Warning.light.png", "Microsoft.CmdPal.Ext.TimeDate\\Assets\\Warning.dark.png");
|
||||
|
||||
/// <summary>
|
||||
/// Gets a result with an error message that only numbers can't be parsed
|
||||
/// Gets a result with an error message that input can't be parsed
|
||||
/// </summary>
|
||||
/// <returns>Element of type <see cref="Result"/>.</returns>
|
||||
internal static ListItem CreateNumberErrorResult() => new ListItem(new NoOpCommand())
|
||||
{
|
||||
Title = Resources.Microsoft_plugin_timedate_ErrorResultTitle,
|
||||
Subtitle = Resources.Microsoft_plugin_timedate_ErrorResultSubTitle,
|
||||
Icon = IconHelpers.FromRelativePaths("Microsoft.CmdPal.Ext.TimeDate\\Assets\\Warning.light.png", "Microsoft.CmdPal.Ext.TimeDate\\Assets\\Warning.dark.png"),
|
||||
};
|
||||
|
||||
#pragma warning disable CA1863 // Use 'CompositeFormat'
|
||||
internal static ListItem CreateInvalidInputErrorResult() => new ListItem(new NoOpCommand())
|
||||
{
|
||||
Title = Resources.Microsoft_plugin_timedate_InvalidInput_ErrorMessageTitle,
|
||||
Subtitle = Resources.Microsoft_plugin_timedate_InvalidInput_ErrorMessageSubTitle,
|
||||
Icon = IconHelpers.FromRelativePaths("Microsoft.CmdPal.Ext.TimeDate\\Assets\\Warning.light.png", "Microsoft.CmdPal.Ext.TimeDate\\Assets\\Warning.dark.png"),
|
||||
Icon = ErrorIcon,
|
||||
Details = new Details()
|
||||
{
|
||||
Title = Resources.Microsoft_plugin_timedate_InvalidInput_DetailsHeader,
|
||||
|
||||
// Because of translation we can't use 'CompositeFormat'.
|
||||
Body = string.Format(CultureInfo.CurrentCulture, Resources.Microsoft_plugin_timedate_InvalidInput_SupportedInput, "**", "\n\n", "\n\n* "),
|
||||
},
|
||||
};
|
||||
#pragma warning restore CA1863 // Use 'CompositeFormat'
|
||||
}
|
||||
|
||||
@@ -13,6 +13,11 @@ namespace Microsoft.CmdPal.Ext.TimeDate.Helpers;
|
||||
|
||||
public class SettingsManager : JsonSettingsManager
|
||||
{
|
||||
// Line break character used in WinUI3 TextBox and TextBlock.
|
||||
private const char TEXTBOXNEWLINE = '\r';
|
||||
|
||||
private const string CUSTOMFORMATPLACEHOLDER = "MyFormat=dd-MMM-yyyy\rMySecondFormat=dddd (Da\\y nu\\mber: DOW)\rMyUtcFormat=UTC:hh:mm:ss";
|
||||
|
||||
private static readonly string _namespace = "timeDate";
|
||||
|
||||
private static string Namespaced(string propertyName) => $"{_namespace}.{propertyName}";
|
||||
@@ -94,6 +99,12 @@ public class SettingsManager : JsonSettingsManager
|
||||
Resources.Microsoft_plugin_timedate_SettingHideNumberMessageOnGlobalQuery,
|
||||
true); // TODO -- double check default value
|
||||
|
||||
private readonly TextSetting _customFormats = new(
|
||||
Namespaced(nameof(CustomFormats)),
|
||||
Resources.Microsoft_plugin_timedate_Setting_CustomFormats,
|
||||
Resources.Microsoft_plugin_timedate_Setting_CustomFormats + TEXTBOXNEWLINE + string.Format(CultureInfo.CurrentCulture, Resources.Microsoft_plugin_timedate_Setting_CustomFormatsDescription.ToString(), "DOW", "DIM", "WOM", "WOY", "EAB", "WFT", "UXT", "UMS", "OAD", "EXC", "EXF", "UTC:"),
|
||||
string.Empty);
|
||||
|
||||
public int FirstWeekOfYear
|
||||
{
|
||||
get
|
||||
@@ -142,6 +153,8 @@ public class SettingsManager : JsonSettingsManager
|
||||
|
||||
public bool HideNumberMessageOnGlobalQuery => _hideNumberMessageOnGlobalQuery.Value;
|
||||
|
||||
public List<string> CustomFormats => _customFormats.Value.Split(TEXTBOXNEWLINE).ToList();
|
||||
|
||||
internal static string SettingsJsonPath()
|
||||
{
|
||||
var directory = Utilities.BaseSettingsPath("Microsoft.CmdPal");
|
||||
@@ -155,12 +168,18 @@ public class SettingsManager : JsonSettingsManager
|
||||
{
|
||||
FilePath = SettingsJsonPath();
|
||||
|
||||
Settings.Add(_firstWeekOfYear);
|
||||
Settings.Add(_firstDayOfWeek);
|
||||
/* The following two settings make no sense with current CmdPal behavior.
|
||||
Settings.Add(_onlyDateTimeNowGlobal);
|
||||
Settings.Add(_hideNumberMessageOnGlobalQuery); */
|
||||
|
||||
Settings.Add(_timeWithSeconds);
|
||||
Settings.Add(_dateWithWeekday);
|
||||
Settings.Add(_hideNumberMessageOnGlobalQuery);
|
||||
Settings.Add(_firstWeekOfYear);
|
||||
Settings.Add(_firstDayOfWeek);
|
||||
|
||||
_customFormats.Multiline = true;
|
||||
_customFormats.Placeholder = CUSTOMFORMATPLACEHOLDER;
|
||||
Settings.Add(_customFormats);
|
||||
|
||||
// Load settings from file upon initialization
|
||||
LoadSettings();
|
||||
|
||||
@@ -4,12 +4,42 @@
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.TimeDate.Helpers;
|
||||
|
||||
internal static class TimeAndDateHelper
|
||||
{
|
||||
/* htcfreek:Currently not used.
|
||||
* private static readonly Regex _regexSpecialInputFormats = new Regex(@"^.*(u|ums|ft|oa|exc|exf)\d"); */
|
||||
|
||||
private static readonly Regex _regexCustomDateTimeFormats = new Regex(@"(?<!\\)(DOW|DIM|WOM|WOY|EAB|WFT|UXT|UMS|OAD|EXC|EXF)");
|
||||
private static readonly Regex _regexCustomDateTimeDim = new Regex(@"(?<!\\)DIM");
|
||||
private static readonly Regex _regexCustomDateTimeDow = new Regex(@"(?<!\\)DOW");
|
||||
private static readonly Regex _regexCustomDateTimeWom = new Regex(@"(?<!\\)WOM");
|
||||
private static readonly Regex _regexCustomDateTimeWoy = new Regex(@"(?<!\\)WOY");
|
||||
private static readonly Regex _regexCustomDateTimeEab = new Regex(@"(?<!\\)EAB");
|
||||
private static readonly Regex _regexCustomDateTimeWft = new Regex(@"(?<!\\)WFT");
|
||||
private static readonly Regex _regexCustomDateTimeUxt = new Regex(@"(?<!\\)UXT");
|
||||
private static readonly Regex _regexCustomDateTimeUms = new Regex(@"(?<!\\)UMS");
|
||||
private static readonly Regex _regexCustomDateTimeOad = new Regex(@"(?<!\\)OAD");
|
||||
private static readonly Regex _regexCustomDateTimeExc = new Regex(@"(?<!\\)EXC");
|
||||
private static readonly Regex _regexCustomDateTimeExf = new Regex(@"(?<!\\)EXF");
|
||||
|
||||
private const long UnixTimeSecondsMin = -62135596800;
|
||||
private const long UnixTimeSecondsMax = 253402300799;
|
||||
private const long UnixTimeMillisecondsMin = -62135596800000;
|
||||
private const long UnixTimeMillisecondsMax = 253402300799999;
|
||||
private const long WindowsFileTimeMin = 0;
|
||||
private const long WindowsFileTimeMax = 2650467707991000000;
|
||||
private const double OADateMin = -657434.99999999;
|
||||
private const double OADateMax = 2958465.99999999;
|
||||
private const double Excel1900DateMin = 1;
|
||||
private const double Excel1900DateMax = 2958465.99998843;
|
||||
private const double Excel1904DateMin = 0;
|
||||
private const double Excel1904DateMax = 2957003.99998843;
|
||||
|
||||
/// <summary>
|
||||
/// Get the format for the time string
|
||||
/// </summary>
|
||||
@@ -53,18 +83,25 @@ internal static class TimeAndDateHelper
|
||||
/// Returns the number week in the month (Used code from 'David Morton' from <see href="https://social.msdn.microsoft.com/Forums/vstudio/bf504bba-85cb-492d-a8f7-4ccabdf882cb/get-week-number-for-month"/>)
|
||||
/// </summary>
|
||||
/// <param name="date">date</param>
|
||||
/// <param name="formatSettingFirstDayOfWeek">Setting for the first day in the week.</param>
|
||||
/// <returns>Number of week in the month</returns>
|
||||
internal static int GetWeekOfMonth(DateTime date, DayOfWeek formatSettingFirstDayOfWeek)
|
||||
{
|
||||
var beginningOfMonth = new DateTime(date.Year, date.Month, 1);
|
||||
var adjustment = 1; // We count from 1 to 7 and not from 0 to 6
|
||||
var weekCount = 1;
|
||||
|
||||
while (date.Date.AddDays(1).DayOfWeek != formatSettingFirstDayOfWeek)
|
||||
for (var i = 1; i <= date.Day; i++)
|
||||
{
|
||||
date = date.AddDays(1);
|
||||
DateTime d = new(date.Year, date.Month, i);
|
||||
|
||||
// Count week number +1 if day is the first day of a week and not day 1 of the month.
|
||||
// (If we count on day one of a month we would start the month with week number 2.)
|
||||
if (i > 1 && d.DayOfWeek == formatSettingFirstDayOfWeek)
|
||||
{
|
||||
weekCount += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return (int)Math.Truncate((double)date.Subtract(beginningOfMonth).TotalDays / 7f) + adjustment;
|
||||
return weekCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -80,40 +117,170 @@ internal static class TimeAndDateHelper
|
||||
return ((date.DayOfWeek + daysInWeek - formatSettingFirstDayOfWeek) % daysInWeek) + adjustment;
|
||||
}
|
||||
|
||||
internal static double ConvertToOleAutomationFormat(DateTime date, OADateFormats type)
|
||||
{
|
||||
var v = date.ToOADate();
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case OADateFormats.Excel1904:
|
||||
// Excel with base 1904: Adjust by -1462
|
||||
v -= 1462;
|
||||
|
||||
// Date starts at 1/1/1904 = 0
|
||||
if (Math.Truncate(v) < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("Not a valid Excel date.", innerException: null);
|
||||
}
|
||||
|
||||
return v;
|
||||
case OADateFormats.Excel1900:
|
||||
// Excel with base 1900: Adjust by -1 if v < 61
|
||||
v = v < 61 ? v - 1 : v;
|
||||
|
||||
// Date starts at 1/1/1900 = 1
|
||||
if (Math.Truncate(v) < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("Not a valid Excel date.", innerException: null);
|
||||
}
|
||||
|
||||
return v;
|
||||
default:
|
||||
// OLE Automation date: Return as is.
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert input string to a <see cref="DateTime"/> object in local time
|
||||
/// </summary>
|
||||
/// <param name="input">String with date/time</param>
|
||||
/// <param name="timestamp">The new <see cref="DateTime"/> object</param>
|
||||
/// <param name="inputParsingErrorMsg">Error message shown to the user</param>
|
||||
/// <returns>True on success, otherwise false</returns>
|
||||
internal static bool ParseStringAsDateTime(in string input, out DateTime timestamp)
|
||||
internal static bool ParseStringAsDateTime(in string input, out DateTime timestamp, out string inputParsingErrorMsg)
|
||||
{
|
||||
inputParsingErrorMsg = string.Empty;
|
||||
CompositeFormat errorMessage = CompositeFormat.Parse(Resources.Microsoft_plugin_timedate_InvalidInput_SupportedRange);
|
||||
|
||||
if (DateTime.TryParse(input, out timestamp))
|
||||
{
|
||||
// Known date/time format
|
||||
return true;
|
||||
}
|
||||
else if (Regex.IsMatch(input, @"^u[\+-]?\d{1,10}$") && long.TryParse(input.TrimStart('u'), out var secondsU))
|
||||
else if (Regex.IsMatch(input, @"^u[\+-]?\d+$"))
|
||||
{
|
||||
// Unix time stamp
|
||||
// We use long instead of int, because int is too small after 03:14:07 UTC 2038-01-19
|
||||
var canParse = long.TryParse(input.TrimStart('u'), out var secondsU);
|
||||
|
||||
// Value has to be in the range from -62135596800 to 253402300799
|
||||
if (!canParse || secondsU < UnixTimeSecondsMin || secondsU > UnixTimeSecondsMax)
|
||||
{
|
||||
inputParsingErrorMsg = string.Format(CultureInfo.CurrentCulture, errorMessage, Resources.Microsoft_plugin_timedate_Unix, UnixTimeSecondsMin, UnixTimeSecondsMax);
|
||||
timestamp = new DateTime(1, 1, 1, 1, 1, 1);
|
||||
return false;
|
||||
}
|
||||
|
||||
timestamp = DateTimeOffset.FromUnixTimeSeconds(secondsU).LocalDateTime;
|
||||
return true;
|
||||
}
|
||||
else if (Regex.IsMatch(input, @"^ums[\+-]?\d{1,13}$") && long.TryParse(input.TrimStart("ums".ToCharArray()), out var millisecondsUms))
|
||||
else if (Regex.IsMatch(input, @"^ums[\+-]?\d+$"))
|
||||
{
|
||||
// Unix time stamp in milliseconds
|
||||
// We use long instead of int because int is too small after 03:14:07 UTC 2038-01-19
|
||||
var canParse = long.TryParse(input.TrimStart("ums".ToCharArray()), out var millisecondsUms);
|
||||
|
||||
// Value has to be in the range from -62135596800000 to 253402300799999
|
||||
if (!canParse || millisecondsUms < UnixTimeMillisecondsMin || millisecondsUms > UnixTimeMillisecondsMax)
|
||||
{
|
||||
inputParsingErrorMsg = string.Format(CultureInfo.CurrentCulture, errorMessage, Resources.Microsoft_plugin_timedate_Unix_Milliseconds, UnixTimeMillisecondsMin, UnixTimeMillisecondsMax);
|
||||
timestamp = new DateTime(1, 1, 1, 1, 1, 1);
|
||||
return false;
|
||||
}
|
||||
|
||||
timestamp = DateTimeOffset.FromUnixTimeMilliseconds(millisecondsUms).LocalDateTime;
|
||||
return true;
|
||||
}
|
||||
else if (Regex.IsMatch(input, @"^ft\d+$") && long.TryParse(input.TrimStart("ft".ToCharArray()), out var secondsFt))
|
||||
else if (Regex.IsMatch(input, @"^ft\d+$"))
|
||||
{
|
||||
var canParse = long.TryParse(input.TrimStart("ft".ToCharArray()), out var secondsFt);
|
||||
|
||||
// Windows file time
|
||||
// Value has to be in the range from 0 to 2650467707991000000
|
||||
if (!canParse || secondsFt < WindowsFileTimeMin || secondsFt > WindowsFileTimeMax)
|
||||
{
|
||||
inputParsingErrorMsg = string.Format(CultureInfo.CurrentCulture, errorMessage, Resources.Microsoft_plugin_timedate_WindowsFileTime, WindowsFileTimeMin, WindowsFileTimeMax);
|
||||
timestamp = new DateTime(1, 1, 1, 1, 1, 1);
|
||||
return false;
|
||||
}
|
||||
|
||||
// DateTime.FromFileTime returns as local time.
|
||||
timestamp = DateTime.FromFileTime(secondsFt);
|
||||
return true;
|
||||
}
|
||||
else if (Regex.IsMatch(input, @"^oa[+-]?\d+[,.0-9]*$"))
|
||||
{
|
||||
var canParse = double.TryParse(input.TrimStart("oa".ToCharArray()), out var oADate);
|
||||
|
||||
// OLE Automation date
|
||||
// Input has to be in the range from -657434.99999999 to 2958465.99999999
|
||||
// DateTime.FromOADate returns as local time.
|
||||
if (!canParse || oADate < OADateMin || oADate > OADateMax)
|
||||
{
|
||||
inputParsingErrorMsg = string.Format(CultureInfo.CurrentCulture, errorMessage, Resources.Microsoft_plugin_timedate_OADate, OADateMin, OADateMax);
|
||||
timestamp = new DateTime(1, 1, 1, 1, 1, 1);
|
||||
return false;
|
||||
}
|
||||
|
||||
timestamp = DateTime.FromOADate(oADate);
|
||||
return true;
|
||||
}
|
||||
else if (Regex.IsMatch(input, @"^exc[+-]?\d+[,.0-9]*$"))
|
||||
{
|
||||
var canParse = double.TryParse(input.TrimStart("exc".ToCharArray()), out var excDate);
|
||||
|
||||
// Excel's 1900 date value
|
||||
// Input has to be in the range from 1 (0 = Fake date) to 2958465.99998843 and not 60 whole number
|
||||
// Because of a bug in Excel and the way it behaves before 3/1/1900 we have to adjust all inputs lower than 61 for +1
|
||||
// DateTime.FromOADate returns as local time.
|
||||
if (!canParse || excDate < 0 || excDate > Excel1900DateMax)
|
||||
{
|
||||
// For the if itself we use 0 as min value that we can show a special message if input is 0.
|
||||
inputParsingErrorMsg = string.Format(CultureInfo.CurrentCulture, errorMessage, Resources.Microsoft_plugin_timedate_Excel1900, Excel1900DateMin, Excel1900DateMax);
|
||||
timestamp = new DateTime(1, 1, 1, 1, 1, 1);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Math.Truncate(excDate) == 0 || Math.Truncate(excDate) == 60)
|
||||
{
|
||||
inputParsingErrorMsg = Resources.Microsoft_plugin_timedate_InvalidInput_FakeExcel1900;
|
||||
timestamp = new DateTime(1, 1, 1, 1, 1, 1);
|
||||
return false;
|
||||
}
|
||||
|
||||
excDate = excDate <= 60 ? excDate + 1 : excDate;
|
||||
timestamp = DateTime.FromOADate(excDate);
|
||||
return true;
|
||||
}
|
||||
else if (Regex.IsMatch(input, @"^exf[+-]?\d+[,.0-9]*$"))
|
||||
{
|
||||
var canParse = double.TryParse(input.TrimStart("exf".ToCharArray()), out var exfDate);
|
||||
|
||||
// Excel's 1904 date value
|
||||
// Input has to be in the range from 0 to 2957003.99998843
|
||||
// Because Excel uses 01/01/1904 as base we need to adjust for +1462
|
||||
// DateTime.FromOADate returns as local time.
|
||||
if (!canParse || exfDate < Excel1904DateMin || exfDate > Excel1904DateMax)
|
||||
{
|
||||
inputParsingErrorMsg = string.Format(CultureInfo.CurrentCulture, errorMessage, Resources.Microsoft_plugin_timedate_Excel1904, Excel1904DateMin, Excel1904DateMax);
|
||||
timestamp = new DateTime(1, 1, 1, 1, 1, 1);
|
||||
return false;
|
||||
}
|
||||
|
||||
timestamp = DateTime.FromOADate(exfDate + 1462);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
timestamp = new DateTime(1, 1, 1, 1, 1, 1);
|
||||
@@ -121,14 +288,87 @@ internal static class TimeAndDateHelper
|
||||
}
|
||||
}
|
||||
|
||||
/* htcfreek:Currently not required
|
||||
/// <summary>
|
||||
/// Test if input is special parsing for Unix time, Unix time in milliseconds or File time.
|
||||
/// Test if input is special parsing for Unix time, Unix time in milliseconds, file time, ...
|
||||
/// </summary>
|
||||
/// <param name="input">String with date/time</param>
|
||||
/// <returns>True if yes, otherwise false</returns>
|
||||
internal static bool IsSpecialInputParsing(string input)
|
||||
{
|
||||
return Regex.IsMatch(input, @"^.*(u|ums|ft)\d");
|
||||
return _regexSpecialInputFormats.IsMatch(input);
|
||||
}*/
|
||||
|
||||
/// <summary>
|
||||
/// Converts a DateTime object based on the format string
|
||||
/// </summary>
|
||||
/// <param name="date">Date/time object.</param>
|
||||
/// <param name="unix">Value for replacing "Unix Time Stamp".</param>
|
||||
/// <param name="unixMilliseconds">Value for replacing "Unix Time Stamp in milliseconds".</param>
|
||||
/// <param name="calWeek">Value for relacing calendar week.</param>
|
||||
/// <param name="eraShortFormat">Era abbreviation.</param>
|
||||
/// <param name="format">Format definition.</param>
|
||||
/// <returns>Formated date/time string.</returns>
|
||||
internal static string ConvertToCustomFormat(DateTime date, long unix, long unixMilliseconds, int calWeek, string eraShortFormat, string format, CalendarWeekRule firstWeekRule, DayOfWeek firstDayOfTheWeek)
|
||||
{
|
||||
var result = format;
|
||||
|
||||
// DOW: Number of day in week
|
||||
result = _regexCustomDateTimeDow.Replace(result, GetNumberOfDayInWeek(date, firstDayOfTheWeek).ToString(CultureInfo.CurrentCulture));
|
||||
|
||||
// DIM: Days in Month
|
||||
result = _regexCustomDateTimeDim.Replace(result, DateTime.DaysInMonth(date.Year, date.Month).ToString(CultureInfo.CurrentCulture));
|
||||
|
||||
// WOM: Week of Month
|
||||
result = _regexCustomDateTimeWom.Replace(result, GetWeekOfMonth(date, firstDayOfTheWeek).ToString(CultureInfo.CurrentCulture));
|
||||
|
||||
// WOY: Week of Year
|
||||
result = _regexCustomDateTimeWoy.Replace(result, calWeek.ToString(CultureInfo.CurrentCulture));
|
||||
|
||||
// EAB: Era abbreviation
|
||||
result = _regexCustomDateTimeEab.Replace(result, eraShortFormat);
|
||||
|
||||
// WFT: Week of Month
|
||||
if (_regexCustomDateTimeWft.IsMatch(result))
|
||||
{
|
||||
// Special handling as very early dates can't convert.
|
||||
result = _regexCustomDateTimeWft.Replace(result, date.ToFileTime().ToString(CultureInfo.CurrentCulture));
|
||||
}
|
||||
|
||||
// UXT: Unix time stamp
|
||||
result = _regexCustomDateTimeUxt.Replace(result, unix.ToString(CultureInfo.CurrentCulture));
|
||||
|
||||
// UMS: Unix time stamp milli seconds
|
||||
result = _regexCustomDateTimeUms.Replace(result, unixMilliseconds.ToString(CultureInfo.CurrentCulture));
|
||||
|
||||
// OAD: OLE Automation date
|
||||
result = _regexCustomDateTimeOad.Replace(result, ConvertToOleAutomationFormat(date, OADateFormats.OLEAutomation).ToString(CultureInfo.CurrentCulture));
|
||||
|
||||
// EXC: Excel date value with base 1900
|
||||
if (_regexCustomDateTimeExc.IsMatch(result))
|
||||
{
|
||||
// Special handling as very early dates can't convert.
|
||||
result = _regexCustomDateTimeExc.Replace(result, ConvertToOleAutomationFormat(date, OADateFormats.Excel1900).ToString(CultureInfo.CurrentCulture));
|
||||
}
|
||||
|
||||
// EXF: Excel date value with base 1904
|
||||
if (_regexCustomDateTimeExf.IsMatch(result))
|
||||
{
|
||||
// Special handling as very early dates can't convert.
|
||||
result = _regexCustomDateTimeExf.Replace(result, ConvertToOleAutomationFormat(date, OADateFormats.Excel1904).ToString(CultureInfo.CurrentCulture));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test a string for our custom date and time format syntax
|
||||
/// </summary>
|
||||
/// <param name="str">String to test.</param>
|
||||
/// <returns>True if yes and otherwise false</returns>
|
||||
internal static bool StringContainsCustomFormatSyntax(string str)
|
||||
{
|
||||
return _regexCustomDateTimeFormats.IsMatch(str);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -187,3 +427,13 @@ internal enum FormatStringType
|
||||
Date,
|
||||
DateTime,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Different versions of Date formats based on OLE Automation date
|
||||
/// </summary>
|
||||
internal enum OADateFormats
|
||||
{
|
||||
OLEAutomation,
|
||||
Excel1900,
|
||||
Excel1904,
|
||||
}
|
||||
|
||||
@@ -29,13 +29,16 @@ public sealed partial class TimeDateCalculator
|
||||
/// <returns>List of Wox <see cref="Result"/>s.</returns>
|
||||
public static List<ListItem> ExecuteSearch(SettingsManager settings, string query)
|
||||
{
|
||||
var isEmptySearchInput = string.IsNullOrEmpty(query);
|
||||
var isEmptySearchInput = string.IsNullOrWhiteSpace(query);
|
||||
List<AvailableResult> availableFormats = new List<AvailableResult>();
|
||||
List<ListItem> results = new List<ListItem>();
|
||||
|
||||
// currently, all of the search in V2 is keyword search.
|
||||
var isKeywordSearch = true;
|
||||
|
||||
// Last input parsing error
|
||||
var lastInputParsingErrorMsg = string.Empty;
|
||||
|
||||
// Switch search type
|
||||
if (isEmptySearchInput || (!isKeywordSearch && settings.OnlyDateTimeNowGlobal))
|
||||
{
|
||||
@@ -47,13 +50,13 @@ public sealed partial class TimeDateCalculator
|
||||
{
|
||||
// Search for specified format with specified time/date value
|
||||
var userInput = query.Split(InputDelimiter);
|
||||
if (TimeAndDateHelper.ParseStringAsDateTime(userInput[1], out DateTime timestamp))
|
||||
if (TimeAndDateHelper.ParseStringAsDateTime(userInput[1], out DateTime timestamp, out lastInputParsingErrorMsg))
|
||||
{
|
||||
availableFormats.AddRange(AvailableResultsList.GetList(isKeywordSearch, settings, null, null, timestamp));
|
||||
query = userInput[0];
|
||||
}
|
||||
}
|
||||
else if (TimeAndDateHelper.ParseStringAsDateTime(query, out DateTime timestamp))
|
||||
else if (TimeAndDateHelper.ParseStringAsDateTime(query, out DateTime timestamp, out lastInputParsingErrorMsg))
|
||||
{
|
||||
// Return all formats for specified time/date value
|
||||
availableFormats.AddRange(AvailableResultsList.GetList(isKeywordSearch, settings, null, null, timestamp));
|
||||
@@ -88,19 +91,32 @@ public sealed partial class TimeDateCalculator
|
||||
}
|
||||
}
|
||||
|
||||
/*htcfreek:Code obsolete with current CmdPal behavior.
|
||||
// If search term is only a number that can't be parsed return an error message
|
||||
if (!isEmptySearchInput && results.Count == 0 && Regex.IsMatch(query, @"\w+\d+.*$") && !query.Any(char.IsWhiteSpace) && (TimeAndDateHelper.IsSpecialInputParsing(query) || !Regex.IsMatch(query, @"\d+[\.:/]\d+")))
|
||||
{
|
||||
// Without plugin key word show only if message is not hidden by setting
|
||||
if (!settings.HideNumberMessageOnGlobalQuery)
|
||||
{
|
||||
results.Add(ResultHelper.CreateNumberErrorResult());
|
||||
var er = ResultHelper.CreateInvalidInputErrorResult();
|
||||
if (!string.IsNullOrEmpty(lastInputParsingErrorMsg))
|
||||
{
|
||||
er.Details = new Details() { Body = lastInputParsingErrorMsg };
|
||||
}
|
||||
|
||||
results.Add(er);
|
||||
}
|
||||
}
|
||||
} */
|
||||
|
||||
if (results.Count == 0)
|
||||
{
|
||||
results.Add(ResultHelper.CreateInvalidInputErrorResult());
|
||||
var er = ResultHelper.CreateInvalidInputErrorResult();
|
||||
if (!string.IsNullOrEmpty(lastInputParsingErrorMsg))
|
||||
{
|
||||
er.Details = new Details() { Body = lastInputParsingErrorMsg };
|
||||
}
|
||||
|
||||
results.Add(er);
|
||||
}
|
||||
|
||||
return results;
|
||||
|
||||
@@ -30,6 +30,7 @@ internal sealed partial class TimeDateExtensionPage : DynamicListPage
|
||||
PlaceholderText = Resources.Microsoft_plugin_timedate_placeholder_text;
|
||||
Id = "com.microsoft.cmdpal.timedate";
|
||||
_settingsManager = settingsManager;
|
||||
ShowDetails = true;
|
||||
}
|
||||
|
||||
public override IListItem[] GetItems()
|
||||
|
||||
@@ -150,6 +150,15 @@ namespace Microsoft.CmdPal.Ext.TimeDate {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Days in month.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_DaysInMonth {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_DaysInMonth", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Era.
|
||||
/// </summary>
|
||||
@@ -169,20 +178,38 @@ namespace Microsoft.CmdPal.Ext.TimeDate {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Valid prefixes: 'u' for Unix Timestamp, 'ums' for Unix Timestamp in milliseconds, 'ft' for Windows file time.
|
||||
/// Looks up a localized string similar to Failed to convert into custom format.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_ErrorResultSubTitle {
|
||||
public static string Microsoft_plugin_timedate_ErrorConvertCustomFormat {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_ErrorResultSubTitle", resourceCulture);
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_ErrorConvertCustomFormat", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Error: Invalid number input.
|
||||
/// Looks up a localized string similar to Not a valid Windows file time.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_ErrorResultTitle {
|
||||
public static string Microsoft_plugin_timedate_ErrorConvertWft {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_ErrorResultTitle", resourceCulture);
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_ErrorConvertWft", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Excel's 1900 date value.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_Excel1900 {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_Excel1900", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Excel's 1904 date value.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_Excel1904 {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_Excel1904", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,11 +232,20 @@ namespace Microsoft.CmdPal.Ext.TimeDate {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Valid prefixes: 'u' for Unix Timestamp, 'ums' for Unix Timestamp in milliseconds, 'ft' for Windows file time.
|
||||
/// Looks up a localized string similar to Invalid custom format:.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_InvalidInput_ErrorMessageSubTitle {
|
||||
public static string Microsoft_plugin_timedate_InvalidCustomFormat {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_InvalidInput_ErrorMessageSubTitle", resourceCulture);
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_InvalidCustomFormat", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Supported input.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_InvalidInput_DetailsHeader {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_InvalidInput_DetailsHeader", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,6 +258,33 @@ namespace Microsoft.CmdPal.Ext.TimeDate {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Cannot parse the input as Excel's 1900 date value because it is a fake date. (In Excel 0 stands for 0/1/1900 and this date doesn't exist. And 60 stands for 2/29/1900 and this date only exists in Excel for compatibility with Lotus 123.).
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_InvalidInput_FakeExcel1900 {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_InvalidInput_FakeExcel1900", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to A {0}format name{0}, a {0}valid date or time value{0}, or a {0}prefixed number{0}. To search for a format in a specific date/time please use the syntax {0}format::date/time/number{0}.{1}Supported prefixes:{2}'{0}u{0}' for Unix Timestamp{2}'{0}ums{0}' for Unix Timestamp in milliseconds{2}'{0}ft{0}' for Windows file time{2}'{0}oa{0}' for OLE Automation Date{2}'{0}exc{0}' for Excel's 1900 date value{2}'{0}exf{0}' for Excel's 1904 date value.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_InvalidInput_SupportedInput {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_InvalidInput_SupportedInput", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Your input for {0} is outside the range **from {1} to {2}**..
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_InvalidInput_SupportedRange {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_InvalidInput_SupportedRange", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to ISO 8601.
|
||||
/// </summary>
|
||||
@@ -258,6 +321,15 @@ namespace Microsoft.CmdPal.Ext.TimeDate {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Leap year.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_LeapYear {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_LeapYear", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Open.
|
||||
/// </summary>
|
||||
@@ -321,6 +393,15 @@ namespace Microsoft.CmdPal.Ext.TimeDate {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Not a leap year.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_NoLeapYear {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_NoLeapYear", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Now.
|
||||
/// </summary>
|
||||
@@ -339,6 +420,15 @@ namespace Microsoft.CmdPal.Ext.TimeDate {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to OLE Automation Date.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_OADate {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_OADate", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Search values or type a custom time stamp....
|
||||
/// </summary>
|
||||
@@ -411,6 +501,42 @@ namespace Microsoft.CmdPal.Ext.TimeDate {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Date and time; Time and Date; Custom format.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_SearchTagCustom {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_SearchTagCustom", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Current date and time; Current time and date; Now; Custom format.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_SearchTagCustomNow {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_SearchTagCustomNow", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Date and time UTC; Time UTC and Date; Custom UTC format.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_SearchTagCustomUtc {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_SearchTagCustomUtc", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Current date and time UTC; Current time UTC and date; Now UTC; Custom UTC format.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_SearchTagCustomUtcNow {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_SearchTagCustomUtcNow", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Date.
|
||||
/// </summary>
|
||||
@@ -492,6 +618,24 @@ namespace Microsoft.CmdPal.Ext.TimeDate {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Custom formats.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_Setting_CustomFormats {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_Setting_CustomFormats", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Use date and time string format syntax and {0} (Day of Week), {1} (Days in Month), {2} (Week of Month), {3} (Week of the year), {4} (Era abbreviation), {5} (Windows File Time), {6} (Unix Time), {7} (Unix Time in milliseconds), {8} (OLE Automation date), {9} (Excel's 1900 based date value), {10} (Excel's 1904 based date value). If the format starts with {11}, then Universal Time (UTC) is used. (Use a backslash to escape format sequences and the backslash character as text.).
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_Setting_CustomFormatsDescription {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_Setting_CustomFormatsDescription", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Use system setting.
|
||||
/// </summary>
|
||||
@@ -681,6 +825,15 @@ namespace Microsoft.CmdPal.Ext.TimeDate {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Select for more details..
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_timedate_show_details {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_timedate_show_details", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Select or press Ctrl+C to copy.
|
||||
/// </summary>
|
||||
|
||||
@@ -155,11 +155,8 @@
|
||||
<data name="Microsoft_plugin_timedate_EraAbbreviation" xml:space="preserve">
|
||||
<value>Era abbreviation</value>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_ErrorResultSubTitle" xml:space="preserve">
|
||||
<value>Valid prefixes: 'u' for Unix Timestamp, 'ums' for Unix Timestamp in milliseconds, 'ft' for Windows file time</value>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_ErrorResultTitle" xml:space="preserve">
|
||||
<value>Error: Invalid number input</value>
|
||||
<data name="Microsoft_plugin_timedate_InvalidInput_DetailsHeader" xml:space="preserve">
|
||||
<value>Supported input</value>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_Hour" xml:space="preserve">
|
||||
<value>Hour</value>
|
||||
@@ -372,7 +369,68 @@
|
||||
<data name="Microsoft_plugin_timedate_main_page_title" xml:space="preserve">
|
||||
<value>Time and Date</value>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_InvalidInput_ErrorMessageSubTitle" xml:space="preserve">
|
||||
<value>Valid prefixes: 'u' for Unix Timestamp, 'ums' for Unix Timestamp in milliseconds, 'ft' for Windows file time</value>
|
||||
<data name="Microsoft_plugin_timedate_InvalidInput_SupportedInput" xml:space="preserve">
|
||||
<value>A {0}format name{0}, a {0}valid date or time value{0}, or a {0}prefixed number{0}. To search for a format in a specific date/time please use the syntax {0}format::date/time/number{0}.{1}Supported prefixes:{2}'{0}u{0}' for Unix Timestamp{2}'{0}ums{0}' for Unix Timestamp in milliseconds{2}'{0}ft{0}' for Windows file time{2}'{0}oa{0}' for OLE Automation Date{2}'{0}exc{0}' for Excel's 1900 date value{2}'{0}exf{0}' for Excel's 1904 date value</value>
|
||||
<comment>The placed holders are replaced with formatting syntax in code.</comment>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_SearchTagCustom" xml:space="preserve">
|
||||
<value>Date and time; Time and Date; Custom format</value>
|
||||
<comment>Don't change order</comment>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_SearchTagCustomUtc" xml:space="preserve">
|
||||
<value>Date and time UTC; Time UTC and Date; Custom UTC format</value>
|
||||
<comment>Don't change order</comment>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_SearchTagCustomNow" xml:space="preserve">
|
||||
<value>Current date and time; Current time and date; Now; Custom format</value>
|
||||
<comment>Don't change order</comment>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_SearchTagCustomUtcNow" xml:space="preserve">
|
||||
<value>Current date and time UTC; Current time UTC and date; Now UTC; Custom UTC format</value>
|
||||
<comment>Don't change order</comment>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_InvalidCustomFormat" xml:space="preserve">
|
||||
<value>Invalid custom format:</value>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_Setting_CustomFormats" xml:space="preserve">
|
||||
<value>Custom formats</value>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_Setting_CustomFormatsDescription" xml:space="preserve">
|
||||
<value>Use date and time string format syntax and {0} (Day of Week), {1} (Days in Month), {2} (Week of Month), {3} (Week of the year), {4} (Era abbreviation), {5} (Windows File Time), {6} (Unix Time), {7} (Unix Time in milliseconds), {8} (OLE Automation date), {9} (Excel's 1900 based date value), {10} (Excel's 1904 based date value). If the format starts with {11}, then Universal Time (UTC) is used. (Use a backslash to escape format sequences and the backslash character as text.)</value>
|
||||
<comment>The {n} parts are place holders and get replaced in the code.</comment>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_show_details" xml:space="preserve">
|
||||
<value>Select for more details.</value>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_ErrorConvertCustomFormat" xml:space="preserve">
|
||||
<value>Failed to convert into custom format</value>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_ErrorConvertWft" xml:space="preserve">
|
||||
<value>Not a valid Windows file time</value>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_InvalidInput_SupportedRange" xml:space="preserve">
|
||||
<value>Your input for {0} is outside the range **from {1} to {2}**.</value>
|
||||
<comment>The placeholder will be replace in code.</comment>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_InvalidInput_FakeExcel1900" xml:space="preserve">
|
||||
<value>Cannot parse the input as Excel's 1900 date value because it is a fake date. (In Excel 0 stands for 0/1/1900 and this date doesn't exist. And 60 stands for 2/29/1900 and this date only exists in Excel for compatibility with Lotus 123.)</value>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_OADate" xml:space="preserve">
|
||||
<value>OLE Automation Date</value>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_Excel1900" xml:space="preserve">
|
||||
<value>Excel's 1900 date value</value>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_Excel1904" xml:space="preserve">
|
||||
<value>Excel's 1904 date value</value>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_LeapYear" xml:space="preserve">
|
||||
<value>Leap year</value>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_NoLeapYear" xml:space="preserve">
|
||||
<value>Not a leap year</value>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_timedate_DaysInMonth" xml:space="preserve">
|
||||
<value>Days in month</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -0,0 +1,3 @@
|
||||
GetForegroundWindow
|
||||
GetWindowTextLength
|
||||
GetWindowText
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
using Microsoft.CommandPalette.Extensions;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
using Windows.System;
|
||||
using Windows.Win32;
|
||||
|
||||
namespace SamplePagesExtension;
|
||||
|
||||
@@ -65,6 +67,68 @@ internal sealed partial class SampleListPage : ListPage
|
||||
Subtitle = "and I'll take you to a page with markdown content",
|
||||
Tags = [new Tag("Sample Tag")],
|
||||
},
|
||||
|
||||
new ListItem(
|
||||
new AnonymousCommand(() =>
|
||||
{
|
||||
var t = new ToastStatusMessage(new StatusMessage()
|
||||
{
|
||||
Message = "Primary command invoked",
|
||||
State = MessageState.Info,
|
||||
});
|
||||
t.Show();
|
||||
})
|
||||
{
|
||||
Result = CommandResult.KeepOpen(),
|
||||
Icon = new IconInfo("\uE712"),
|
||||
})
|
||||
{
|
||||
Title = "You can add context menu items too. Press Ctrl+k",
|
||||
Subtitle = "Try pressing Ctrl+1 with me selected",
|
||||
Icon = new IconInfo("\uE712"),
|
||||
MoreCommands = [
|
||||
new CommandContextItem(
|
||||
new AnonymousCommand(() =>
|
||||
{
|
||||
var t = new ToastStatusMessage(new StatusMessage()
|
||||
{
|
||||
Message = "Secondary command invoked",
|
||||
State = MessageState.Warning,
|
||||
});
|
||||
t.Show();
|
||||
})
|
||||
{
|
||||
Name = "Secondary command",
|
||||
Icon = new IconInfo("\uF147"), // Dial 2
|
||||
Result = CommandResult.KeepOpen(),
|
||||
})
|
||||
{
|
||||
Title = "I'm a second command",
|
||||
RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, vkey: VirtualKey.Number1),
|
||||
},
|
||||
new CommandContextItem(
|
||||
new AnonymousCommand(() =>
|
||||
{
|
||||
var t = new ToastStatusMessage(new StatusMessage()
|
||||
{
|
||||
Message = "Third command invoked",
|
||||
State = MessageState.Error,
|
||||
});
|
||||
t.Show();
|
||||
})
|
||||
{
|
||||
Name = "Do it",
|
||||
Icon = new IconInfo("\uF148"), // dial 3
|
||||
Result = CommandResult.KeepOpen(),
|
||||
})
|
||||
{
|
||||
Title = "A third command too",
|
||||
Icon = new IconInfo("\uF148"),
|
||||
RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, vkey: VirtualKey.Number2),
|
||||
}
|
||||
],
|
||||
},
|
||||
|
||||
new ListItem(new SendMessageCommand())
|
||||
{
|
||||
Title = "I send lots of messages",
|
||||
@@ -91,7 +155,35 @@ internal sealed partial class SampleListPage : ListPage
|
||||
})
|
||||
{
|
||||
Title = "Confirm twice before doing something",
|
||||
}
|
||||
},
|
||||
new ListItem(
|
||||
new AnonymousCommand(() =>
|
||||
{
|
||||
var fg = PInvoke.GetForegroundWindow();
|
||||
var bufferSize = PInvoke.GetWindowTextLength(fg) + 1;
|
||||
unsafe
|
||||
{
|
||||
fixed (char* windowNameChars = new char[bufferSize])
|
||||
{
|
||||
if (PInvoke.GetWindowText(fg, windowNameChars, bufferSize) == 0)
|
||||
{
|
||||
var emptyToast = new ToastStatusMessage(new StatusMessage() { Message = "FG Window didn't have a title", State = MessageState.Warning });
|
||||
emptyToast.Show();
|
||||
}
|
||||
|
||||
var windowName = new string(windowNameChars);
|
||||
var nameToast = new ToastStatusMessage(new StatusMessage() { Message = $"FG Window is {windowName}", State = MessageState.Success });
|
||||
nameToast.Show();
|
||||
}
|
||||
}
|
||||
})
|
||||
{
|
||||
Result = CommandResult.KeepOpen(),
|
||||
})
|
||||
{
|
||||
Title = "Get the name of the Foreground window",
|
||||
},
|
||||
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,12 @@
|
||||
<ProjectReference Include="..\..\extensionsdk\Microsoft.CommandPalette.Extensions.Toolkit\Microsoft.CommandPalette.Extensions.Toolkit.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Windows.CsWin32">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
<!--
|
||||
Defining the "Msix" ProjectCapability here allows the Single-project MSIX Packaging
|
||||
Tools extension to be activated for this project even if the Windows App SDK Nuget
|
||||
|
||||
@@ -2,14 +2,19 @@
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using Windows.Foundation;
|
||||
using Windows.System;
|
||||
|
||||
namespace Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
|
||||
public partial class KeyChordHelpers
|
||||
{
|
||||
public static KeyChord FromModifiers(bool ctrl, bool alt, bool shift, bool win, int vkey, int scanCode)
|
||||
public static KeyChord FromModifiers(
|
||||
bool ctrl = false,
|
||||
bool alt = false,
|
||||
bool shift = false,
|
||||
bool win = false,
|
||||
int vkey = 0,
|
||||
int scanCode = 0)
|
||||
{
|
||||
var modifiers = (ctrl ? VirtualKeyModifiers.Control : VirtualKeyModifiers.None)
|
||||
| (alt ? VirtualKeyModifiers.Menu : VirtualKeyModifiers.None)
|
||||
@@ -18,4 +23,15 @@ public partial class KeyChordHelpers
|
||||
;
|
||||
return new(modifiers, vkey, scanCode);
|
||||
}
|
||||
|
||||
public static KeyChord FromModifiers(
|
||||
bool ctrl = false,
|
||||
bool alt = false,
|
||||
bool shift = false,
|
||||
bool win = false,
|
||||
VirtualKey vkey = VirtualKey.None,
|
||||
int scanCode = 0)
|
||||
{
|
||||
return FromModifiers(ctrl, alt, shift, win, (int)vkey, scanCode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
{
|
||||
"configVersion": 3,
|
||||
"entries": [
|
||||
{
|
||||
"Fuzzer": {
|
||||
"$type": "libfuzzer",
|
||||
"FuzzingHarnessExecutableName": "PowerRename.FuzzingTest.exe"
|
||||
},
|
||||
"adoTemplate": {
|
||||
// supply the values appropriate to your
|
||||
// project, where bugs will be filed
|
||||
"org": "microsoft",
|
||||
"project": "OS",
|
||||
"AssignedTo": "leilzh@microsoft.com",
|
||||
"AreaPath": "OS\\Windows Client and Services\\WinPD\\DEEP-Developer Experience, Ecosystem and Partnerships\\DIVE\\SALT",
|
||||
"IterationPath": "OS\\Future"
|
||||
},
|
||||
"jobNotificationEmail": "PowerToys@microsoft.com",
|
||||
"skip": false,
|
||||
"rebootAfterSetup": false,
|
||||
"oneFuzzJobs": [
|
||||
// at least one job is required
|
||||
{
|
||||
"projectName": "PowerToys.PowerRename",
|
||||
"targetName": "PowerRename_Fuzzer"
|
||||
}
|
||||
],
|
||||
"jobDependencies": [
|
||||
// this should contain, at minimum,
|
||||
// the DLL and PDB files
|
||||
// you will need to add any other files required
|
||||
// (globs are supported)
|
||||
"PowerRename.FuzzingTest.exe",
|
||||
"PowerRename.FuzzingTest.pdb",
|
||||
"PowerRename.FuzzingTest.lib",
|
||||
"clang_rt.asan_dynamic-x86_64.dll"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
// Test.cpp : This file contains the 'main' function. Program execution begins and ends there.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <PowerRenameRegEx.h>
|
||||
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
|
||||
{
|
||||
if (size < 6)
|
||||
return 0;
|
||||
|
||||
size_t offset = 0;
|
||||
|
||||
size_t input_len = size / 3;
|
||||
size_t find_len = size / 3;
|
||||
size_t replace_len = size - input_len - find_len;
|
||||
|
||||
auto read_wstring = [&](size_t len) -> std::wstring {
|
||||
std::wstring result;
|
||||
if (offset + len > size)
|
||||
len = size - offset;
|
||||
|
||||
result.assign(reinterpret_cast<const wchar_t*>(data + offset), len / sizeof(wchar_t));
|
||||
offset += len;
|
||||
return result;
|
||||
};
|
||||
|
||||
std::wstring input = read_wstring(input_len);
|
||||
std::wstring find = read_wstring(find_len);
|
||||
std::wstring replace = read_wstring(replace_len);
|
||||
|
||||
if (find.empty() || replace.empty())
|
||||
return 0;
|
||||
|
||||
CComPtr<IPowerRenameRegEx> renamer;
|
||||
CPowerRenameRegEx::s_CreateInstance(&renamer);
|
||||
|
||||
renamer->PutFlags(UseRegularExpressions | CaseSensitive);
|
||||
|
||||
renamer->PutSearchTerm(find.c_str());
|
||||
renamer->PutReplaceTerm(replace.c_str());
|
||||
|
||||
PWSTR result = nullptr;
|
||||
unsigned long index = 0;
|
||||
HRESULT hr = renamer->Replace(input.c_str(), &result, index);
|
||||
if (SUCCEEDED(hr) && result != nullptr)
|
||||
{
|
||||
CoTaskMemFree(result);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifndef DISABLE_FOR_FUZZING
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
const char8_t raw[] = u8"test_string";
|
||||
|
||||
std::vector<uint8_t> data(reinterpret_cast<const uint8_t*>(raw), reinterpret_cast<const uint8_t*>(raw) + sizeof(raw) - 1);
|
||||
|
||||
LLVMFuzzerTestOneInput(data.data(), data.size());
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,25 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Resource Files">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="PowerRename.FuzzingTest.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<CopyFileToFolders Include="OneFuzzConfig.json" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,107 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.props')" />
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>17.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{2694e2fb-dcd5-4bff-a418-b6c3c7ce3b8e}</ProjectGuid>
|
||||
<RootNamespace>Test</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0.22621.0</WindowsTargetPlatformVersion>
|
||||
<ProjectName>PowerRename.FuzzingTest</ProjectName>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<EnableASAN>true</EnableASAN>
|
||||
<EnableFuzzer>true</EnableFuzzer>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup>
|
||||
<LibraryPath>$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(VCToolsInstallDir)\lib\$(Platform)</LibraryPath>
|
||||
<OutDir>..\..\..\..\$(Platform)\$(Configuration)\tests\PowerRename.FuzzTests\</OutDir>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;DISABLE_FOR_FUZZING;%(PreprocessorDefinitions);_DISABLE_VECTOR_ANNOTATION;_DISABLE_STRING_ANNOTATION</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<AdditionalOptions>/fsanitize=address /fsanitize-coverage=inline-8bit-counters /fsanitize-coverage=edge /fsanitize-coverage=trace-cmp /fsanitize-coverage=trace-div %(AdditionalOptions)</AdditionalOptions>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
<LanguageStandard>stdcpplatest</LanguageStandard>
|
||||
<AdditionalIncludeDirectories>..\;..\lib\;..\..\..\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>legacy_stdio_definitions.lib;$(VCToolsInstallDir)lib\$(Platform)\libsancov.lib;$(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
<PostBuildEvent>
|
||||
<Command>xcopy /y "$(VCToolsInstallDir)bin\Hostx64\x64\clang_rt.asan_dynamic-x86_64.dll" "$(OutDir)"</Command>
|
||||
<Message>Copy the required ASan runtime DLL to the output directory.</Message>
|
||||
</PostBuildEvent>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<AdditionalIncludeDirectories>..\;..\lib\;..\..\..\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<AdditionalDependencies>$(OutDir)\..\..\WinUI3Apps\PowerRenameLib.lib;comctl32.lib;pathcch.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;Pathcch.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="PowerRename.FuzzingTest.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<CopyFileToFolders Include="OneFuzzConfig.json" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\lib\PowerRenameLib.vcxproj" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Project>{51920f1f-c28c-4adf-8660-4238766796c2}</Project>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\..\common\SettingsAPI\SettingsAPI.vcxproj" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Project>{6955446d-23f7-4023-9bb3-8657f904af99}</Project>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
<Import Project="..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.targets')" />
|
||||
<Import Project="..\..\..\..\packages\boost.1.84.0\build\boost.targets" Condition="Exists('..\..\..\..\packages\boost.1.84.0\build\boost.targets')" />
|
||||
<Import Project="..\..\..\..\packages\boost_regex-vc143.1.84.0\build\boost_regex-vc143.targets" Condition="Exists('..\..\..\..\packages\boost_regex-vc143.1.84.0\build\boost_regex-vc143.targets')" />
|
||||
</ImportGroup>
|
||||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
||||
<PropertyGroup>
|
||||
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
|
||||
</PropertyGroup>
|
||||
<Error Condition="!Exists('..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.props'))" />
|
||||
<Error Condition="!Exists('..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.targets'))" />
|
||||
<Error Condition="!Exists('..\..\..\..\packages\boost.1.84.0\build\boost.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\packages\boost.1.84.0\build\boost.targets'))" />
|
||||
<Error Condition="!Exists('..\..\..\..\packages\boost_regex-vc143.1.84.0\build\boost_regex-vc143.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\packages\boost_regex-vc143.1.84.0\build\boost_regex-vc143.targets'))" />
|
||||
</Target>
|
||||
</Project>
|
||||
@@ -192,12 +192,70 @@ namespace RegistryPreviewUILib
|
||||
// reload the current Registry file and update the toolbar accordingly.
|
||||
UpdateToolBarAndUI(await OpenRegistryFile(_appFileName), true, true);
|
||||
|
||||
// disable the Save button as it's a new file
|
||||
saveButton.IsEnabled = false;
|
||||
|
||||
// restore the TextChanged handler
|
||||
MonacoEditor.TextChanged += MonacoEditor_TextChanged;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets the editor content
|
||||
/// </summary>
|
||||
private async void NewButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// Check to see if the current file has been saved
|
||||
if (saveButton.IsEnabled)
|
||||
{
|
||||
ContentDialog contentDialog = new ContentDialog()
|
||||
{
|
||||
Title = resourceLoader.GetString("YesNoCancelDialogTitle"),
|
||||
Content = resourceLoader.GetString("YesNoCancelDialogContent"),
|
||||
PrimaryButtonText = resourceLoader.GetString("YesNoCancelDialogPrimaryButtonText"),
|
||||
SecondaryButtonText = resourceLoader.GetString("YesNoCancelDialogSecondaryButtonText"),
|
||||
CloseButtonText = resourceLoader.GetString("YesNoCancelDialogCloseButtonText"),
|
||||
DefaultButton = ContentDialogButton.Primary,
|
||||
};
|
||||
|
||||
// Use this code to associate the dialog to the appropriate AppWindow by setting
|
||||
// the dialog's XamlRoot to the same XamlRoot as an element that is already present in the AppWindow.
|
||||
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
|
||||
{
|
||||
contentDialog.XamlRoot = this.Content.XamlRoot;
|
||||
}
|
||||
|
||||
ContentDialogResult contentDialogResult = await contentDialog.ShowAsync();
|
||||
switch (contentDialogResult)
|
||||
{
|
||||
case ContentDialogResult.Primary:
|
||||
// Save, then continue the file open
|
||||
SaveFile();
|
||||
break;
|
||||
case ContentDialogResult.Secondary:
|
||||
// Don't save and continue the file open!
|
||||
saveButton.IsEnabled = false;
|
||||
break;
|
||||
default:
|
||||
// Don't open the new file!
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// mute the TextChanged handler to make for clean UI
|
||||
MonacoEditor.TextChanged -= MonacoEditor_TextChanged;
|
||||
|
||||
// reset editor, file info and ui.
|
||||
_appFileName = string.Empty;
|
||||
ResetEditorAndFile();
|
||||
|
||||
// restore the TextChanged handler
|
||||
MonacoEditor.TextChanged += MonacoEditor_TextChanged;
|
||||
|
||||
// disable buttons that do not make sense
|
||||
saveButton.IsEnabled = false;
|
||||
refreshButton.IsEnabled = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens the Registry Editor; UAC is handled by the request to open
|
||||
/// </summary>
|
||||
|
||||
@@ -22,6 +22,8 @@ namespace RegistryPreviewUILib
|
||||
{
|
||||
public sealed partial class RegistryPreviewMainPage : Page
|
||||
{
|
||||
private const string NEWFILEHEADER = "Windows Registry Editor Version 5.00\r\n\r\n";
|
||||
|
||||
private static SemaphoreSlim _dialogSemaphore = new(1);
|
||||
private string lastKeyPath;
|
||||
|
||||
@@ -77,6 +79,9 @@ namespace RegistryPreviewUILib
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Set default value for empty opening
|
||||
await MonacoEditor.SetTextAsync(NEWFILEHEADER);
|
||||
|
||||
// restore TextChanged handler to make for clean UI
|
||||
MonacoEditor.TextChanged += MonacoEditor_TextChanged;
|
||||
|
||||
@@ -167,6 +172,25 @@ namespace RegistryPreviewUILib
|
||||
ChangeCursor(gridPreview, false);
|
||||
}
|
||||
|
||||
private async void ResetEditorAndFile()
|
||||
{
|
||||
// Disable parts of the UI that can cause trouble when loading
|
||||
ChangeCursor(gridPreview, true);
|
||||
|
||||
// clear the treeView and dataGrid no matter what
|
||||
treeView.RootNodes.Clear();
|
||||
ClearTable();
|
||||
|
||||
// update the current window's title with the current filename
|
||||
_updateWindowTitleFunction(string.Empty);
|
||||
|
||||
// Set default value for empty opening
|
||||
await MonacoEditor.SetTextAsync(NEWFILEHEADER);
|
||||
|
||||
// Reset the cursor but leave editor disabled as no content got loaded
|
||||
ChangeCursor(gridPreview, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses the text that is passed in, which should be the same text that's in editor
|
||||
/// </summary>
|
||||
|
||||
@@ -55,6 +55,15 @@
|
||||
HorizontalAlignment="Left"
|
||||
DefaultLabelPosition="Right">
|
||||
|
||||
<AppBarButton
|
||||
x:Name="newButton"
|
||||
x:Uid="NewButton"
|
||||
Click="NewButton_Click"
|
||||
Icon="{ui:FontIcon Glyph=}">
|
||||
<AppBarButton.KeyboardAccelerators>
|
||||
<KeyboardAccelerator Key="N" Modifiers="Control" />
|
||||
</AppBarButton.KeyboardAccelerators>
|
||||
</AppBarButton>
|
||||
<AppBarButton
|
||||
x:Name="openButton"
|
||||
x:Uid="OpenButton"
|
||||
|
||||
@@ -297,4 +297,7 @@
|
||||
<value>Copy value with key path</value>
|
||||
<comment>Like "Copy item"</comment>
|
||||
</data>
|
||||
<data name="NewButton.Label" xml:space="preserve">
|
||||
<value>New</value>
|
||||
</data>
|
||||
</root>
|
||||
Reference in New Issue
Block a user