Skip to content

A Swiss Army knife for developers.

Added Hash Generator. But it's broken

Etienne Baudouxa9f09ede4032
diff --git a/src/dev/impl/DevTools.Common/DevTools.Common.csproj b/src/dev/impl/DevTools.Common/DevTools.Common.csproj
index 942e6c70d..c5280222b 100644
--- a/src/dev/impl/DevTools.Common/DevTools.Common.csproj
+++ b/src/dev/impl/DevTools.Common/DevTools.Common.csproj
@@ -80,6 +80,9 @@
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
</Compile>
+ <Compile Include="UI\Controls\CustomTextBox.xaml.cs">
+ <DependentUpon>CustomTextBox.xaml</DependentUpon>
+ </Compile>
<Compile Include="UI\Controls\FormattedTextBlock\CustomDelimiterChunker.cs" />
<Compile Include="UI\Controls\FormattedTextBlock\CustomWordChunker.cs" />
<Compile Include="UI\Controls\FormattedTextBlock\FormattedTextBlock.xaml.cs">
@@ -94,6 +97,7 @@
<Compile Include="UI\Controls\TextEditor\RtfBuilder.cs" />
<Compile Include="UI\Controls\TextEditor\RtfFormat.cs" />
<Compile Include="UI\Controls\TextEditor\TextFormatter.cs" />
+ <Compile Include="UI\Converters\BooleanToTextWrappingConverter.cs" />
<Compile Include="UI\Converters\BooleanToScrollBarVisibilityConverter.cs" />
<Compile Include="UI\Converters\BooleanToScrollModeConverter.cs" />
<Compile Include="UI\Converters\BooleanToGridLengthConverter.cs" />
@@ -144,6 +148,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
+ <Page Include="UI\Controls\CustomTextBox.xaml">
+ <SubType>Designer</SubType>
+ <Generator>MSBuild:Compile</Generator>
+ </Page>
<Page Include="UI\Controls\ExpandableSettingControl.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
diff --git a/src/dev/impl/DevTools.Common/Themes/Generic.xaml b/src/dev/impl/DevTools.Common/Themes/Generic.xaml
index 32463e223..529cbca55 100644
--- a/src/dev/impl/DevTools.Common/Themes/Generic.xaml
+++ b/src/dev/impl/DevTools.Common/Themes/Generic.xaml
@@ -16,7 +16,7 @@
<SolidColorBrush x:Key="TextEditorForeground" Color="{ThemeResource TextControlForeground}"/>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
-
+
<common:LanguageManager x:Key="LanguageManager"/>
<Style TargetType="controls:CustomPathIcon">
@@ -29,10 +29,10 @@
<Style TargetType="FontIcon">
<Setter Property="FontFamily" Value="{StaticResource FluentSystemIcons}"/>
</Style>
-
+
<Style TargetType="ToggleSwitch">
- <Setter Property="OnContent" Value="{Binding Instance.Common.ToggleSwitchOn, Source={StaticResource LanguageManager}}"/>
- <Setter Property="OffContent" Value="{Binding Instance.Common.ToggleSwitchOff, Source={StaticResource LanguageManager}}"/>
+ <Setter Property="OnContent" Value="{Binding Instance.Common.ToggleSwitchOn, Mode=OneTime, Source={StaticResource LanguageManager}}"/>
+ <Setter Property="OffContent" Value="{Binding Instance.Common.ToggleSwitchOff, Mode=OneTime, Source={StaticResource LanguageManager}}"/>
</Style>
<Style TargetType="textEditor:TextEditor">
diff --git a/src/dev/impl/DevTools.Common/UI/Controls/CustomTextBox.xaml b/src/dev/impl/DevTools.Common/UI/Controls/CustomTextBox.xaml
new file mode 100644
index 000000000..ac3d8de48
--- /dev/null
+++ b/src/dev/impl/DevTools.Common/UI/Controls/CustomTextBox.xaml
@@ -0,0 +1,88 @@
+<UserControl
+ x:Class="DevTools.Common.UI.Controls.CustomTextBox"
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+ xmlns:local="using:DevTools.Common.UI.Controls"
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+ xmlns:common="using:DevTools.Common"
+ xmlns:converters="using:DevTools.Common.UI.Converters"
+ mc:Ignorable="d"
+ d:DesignHeight="24"
+ d:DesignWidth="200">
+ <UserControl.Resources>
+ <common:LanguageManager x:Key="LanguageManager"/>
+ <converters:BooleanToTextWrappingConverter x:Key="BooleanToTextWrappingConverter" TextWrappingOnTrue="Wrap" TextWrappingOnFalse="NoWrap"/>
+ </UserControl.Resources>
+
+ <Grid>
+ <Grid.RowDefinitions>
+ <RowDefinition Height="Auto"/>
+ <RowDefinition Height="*"/>
+ </Grid.RowDefinitions>
+ <Grid.ColumnDefinitions>
+ <ColumnDefinition Width="*"/>
+ <ColumnDefinition Width="Auto"/>
+ </Grid.ColumnDefinitions>
+ <ContentPresenter
+ x:Name="HeaderContentPresenter"
+ x:DeferLoadStrategy="Lazy"
+ Grid.Row="0"
+ Grid.Column="0"
+ Content="{x:Bind Header}"
+ Foreground="{ThemeResource TextControlHeaderForeground}"
+ Margin="{ThemeResource TextBoxTopHeaderMargin}"
+ FontWeight="Normal"
+ TextWrapping="Wrap"
+ VerticalAlignment="Bottom"
+ Visibility="Collapsed"/>
+ <TextBox
+ x:Name="TextBox"
+ Grid.Row="1"
+ Grid.Column="0"
+ IsSpellCheckEnabled="False"
+ TextWrapping="{x:Bind AcceptsReturn, Mode=OneWay, Converter={StaticResource BooleanToTextWrappingConverter}}"
+ IsReadOnly="{x:Bind IsReadOnly, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
+ AcceptsReturn="{x:Bind AcceptsReturn, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
+ Text="{x:Bind Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
+ <StackPanel
+ Grid.Row="0"
+ Grid.Column="0"
+ Orientation="Horizontal"
+ HorizontalAlignment="Right"
+ Spacing="8"
+ Margin="0,0,0,8">
+ <Button
+ x:Name="PasteButton"
+ x:DeferLoadStrategy="Lazy"
+ Visibility="Collapsed"
+ Click="PasteButton_Click">
+ <StackPanel Orientation="Horizontal" Spacing="4">
+ <FontIcon Glyph="&#xF2D5;"/>
+ <TextBlock VerticalAlignment="Center" Text="{Binding Instance.Common.Paste, Mode=OneTime, Source={StaticResource LanguageManager}}"/>
+ </StackPanel>
+ </Button>
+ <Button
+ x:Name="CopyButton"
+ x:DeferLoadStrategy="Lazy"
+ Visibility="Collapsed"
+ Click="CopyButton_Click">
+ <StackPanel Orientation="Horizontal" Spacing="4">
+ <FontIcon Glyph="&#xF32B;"/>
+ <TextBlock VerticalAlignment="Center" Text="{Binding Instance.Common.Copy, Mode=OneTime, Source={StaticResource LanguageManager}}"/>
+ </StackPanel>
+ </Button>
+ </StackPanel>
+ <Button
+ x:Name="InlinedCopyButton"
+ x:DeferLoadStrategy="Lazy"
+ Grid.Row="1"
+ Grid.Column="1"
+ Margin="8,0,0,0"
+ Visibility="Collapsed"
+ ToolTipService.ToolTip="{Binding Instance.Common.Copy, Mode=OneTime, Source={StaticResource LanguageManager}}"
+ Click="CopyButton_Click">
+ <FontIcon Glyph="&#xF32B;"/>
+ </Button>
+ </Grid>
+</UserControl>
diff --git a/src/dev/impl/DevTools.Common/UI/Controls/CustomTextBox.xaml.cs b/src/dev/impl/DevTools.Common/UI/Controls/CustomTextBox.xaml.cs
new file mode 100644
index 000000000..93304b1a3
--- /dev/null
+++ b/src/dev/impl/DevTools.Common/UI/Controls/CustomTextBox.xaml.cs
@@ -0,0 +1,161 @@
+#nullable enable
+
+using Windows.ApplicationModel.DataTransfer;
+using Windows.UI.Xaml;
+using Windows.UI.Xaml.Controls;
+
+namespace DevTools.Common.UI.Controls
+{
+ public sealed partial class CustomTextBox : UserControl
+ {
+ public static readonly DependencyProperty HeaderProperty
+ = DependencyProperty.Register(
+ nameof(Header),
+ typeof(object),
+ typeof(CustomTextBox),
+ new PropertyMetadata(null));
+
+ public object Header
+ {
+ get => (object)GetValue(HeaderProperty);
+ set => SetValue(HeaderProperty, value);
+ }
+
+ public static readonly DependencyProperty IsReadOnlyProperty
+ = DependencyProperty.Register(
+ nameof(IsReadOnly),
+ typeof(bool),
+ typeof(CustomTextBox),
+ new PropertyMetadata(false, OnIsReadOnlyPropertyChangedCalled));
+
+ public bool IsReadOnly
+ {
+ get => (bool)GetValue(IsReadOnlyProperty);
+ set => SetValue(IsReadOnlyProperty, value);
+ }
+
+ public static readonly DependencyProperty AcceptsReturnProperty
+ = DependencyProperty.Register(
+ nameof(AcceptsReturn),
+ typeof(bool),
+ typeof(CustomTextBox),
+ new PropertyMetadata(false, OnAcceptsReturnPropertyChangedCalled));
+
+ public bool AcceptsReturn
+ {
+ get => (bool)GetValue(AcceptsReturnProperty);
+ set => SetValue(AcceptsReturnProperty, value);
+ }
+
+ public static readonly DependencyProperty TextProperty
+ = DependencyProperty.Register(
+ nameof(Text),
+ typeof(string),
+ typeof(CustomTextBox),
+ new PropertyMetadata(string.Empty));
+
+ public string Text
+ {
+ get => (string)GetValue(TextProperty);
+ set => SetValue(TextProperty, value);
+ }
+
+ public CustomTextBox()
+ {
+ InitializeComponent();
+
+ Loaded += OnLoaded;
+ }
+
+ private void UpdateCopyButtons()
+ {
+ if (IsReadOnly)
+ {
+ if (!AcceptsReturn)
+ {
+ GetInlinedCopyButton().Visibility = Visibility.Visible;
+ }
+ else
+ {
+ GetCopyButton().Visibility = Visibility.Visible;
+ }
+
+ if (PasteButton is not null)
+ {
+ GetPasteButton().Visibility = Visibility.Collapsed;
+ }
+ }
+ else
+ {
+ if (AcceptsReturn)
+ {
+ GetPasteButton().Visibility = Visibility.Visible;
+ }
+
+ if (InlinedCopyButton is not null)
+ {
+ GetInlinedCopyButton().Visibility = Visibility.Collapsed;
+ }
+
+ if (CopyButton is not null)
+ {
+ GetCopyButton().Visibility = Visibility.Collapsed;
+ }
+ }
+ }
+
+ private Button GetCopyButton()
+ {
+ return (Button)(CopyButton ?? FindName(nameof(CopyButton)));
+ }
+
+ private Button GetPasteButton()
+ {
+ return (Button)(PasteButton ?? FindName(nameof(PasteButton)));
+ }
+
+ private Button GetInlinedCopyButton()
+ {
+ return (Button)(InlinedCopyButton ?? FindName(nameof(InlinedCopyButton)));
+ }
+
+ private ContentPresenter GetHeaderContentPresenter()
+ {
+ return (ContentPresenter)(HeaderContentPresenter ?? FindName(nameof(HeaderContentPresenter)));
+ }
+
+ private void OnLoaded(object sender, RoutedEventArgs e)
+ {
+ if (Header is not null)
+ {
+ GetHeaderContentPresenter().Visibility = Visibility.Visible;
+ }
+ }
+
+ private void PasteButton_Click(object sender, RoutedEventArgs e)
+ {
+ TextBox.PasteFromClipboard();
+ }
+
+ private void CopyButton_Click(object sender, RoutedEventArgs e)
+ {
+ var data = new DataPackage
+ {
+ RequestedOperation = DataPackageOperation.Copy
+ };
+ data.SetText(Text);
+
+ Clipboard.SetContent(data);
+ }
+
+ private static void OnIsReadOnlyPropertyChangedCalled(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs)
+ {
+ ((CustomTextBox)sender).UpdateCopyButtons();
+ }
+
+ private static void OnAcceptsReturnPropertyChangedCalled(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs)
+ {
+ ((CustomTextBox)sender).UpdateCopyButtons();
+ }
+ }
+}
diff --git a/src/dev/impl/DevTools.Common/UI/Converters/BooleanToTextWrappingConverter.cs b/src/dev/impl/DevTools.Common/UI/Converters/BooleanToTextWrappingConverter.cs
new file mode 100644
index 000000000..5a901bb70
--- /dev/null
+++ b/src/dev/impl/DevTools.Common/UI/Converters/BooleanToTextWrappingConverter.cs
@@ -0,0 +1,40 @@
+#nullable enable
+
+using System;
+using Windows.UI.Xaml;
+using Windows.UI.Xaml.Data;
+
+namespace DevTools.Common.UI.Converters
+{
+ /// <summary>
+ /// Convert a <see cref="bool"/> to a <see cref="TextWrapping"/> value.
+ /// </summary>
+ public sealed class BooleanToTextWrappingConverter : IValueConverter
+ {
+ /// <summary>
+ /// Gets or sets the text wrapping to apply when the input is true.
+ /// </summary>
+ public TextWrapping TextWrappingOnTrue { get; set; }
+
+ /// <summary>
+ /// Gets or sets the text wrapping to apply when the input is false.
+ /// </summary>
+ public TextWrapping TextWrappingOnFalse { get; set; }
+
+ public object Convert(object value, Type targetType, object parameter, string language)
+ {
+ var valueBool = value as bool?;
+ if (valueBool == null)
+ {
+ return DependencyProperty.UnsetValue;
+ }
+
+ return valueBool.Value ? TextWrappingOnTrue : TextWrappingOnFalse;
+ }
+
+ public object ConvertBack(object value, Type targetType, object parameter, string language)
+ {
+ throw new NotImplementedException();
+ }
+ }
+}
diff --git a/src/dev/impl/DevTools.Providers.Impl/Tools/HashGenerator/HashGeneratorToolPage.xaml b/src/dev/impl/DevTools.Providers.Impl/Tools/HashGenerator/HashGeneratorToolPage.xaml
index e0e8295e4..269a281ab 100644
--- a/src/dev/impl/DevTools.Providers.Impl/Tools/HashGenerator/HashGeneratorToolPage.xaml
+++ b/src/dev/impl/DevTools.Providers.Impl/Tools/HashGenerator/HashGeneratorToolPage.xaml
@@ -5,10 +5,51 @@
xmlns:local="using:DevTools.Providers.Impl.Tools.HashGenerator"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+ xmlns:controls="using:DevTools.Common.UI.Controls"
mc:Ignorable="d"
- Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
+ NavigationCacheMode="Required">
- <Grid>
+ <StackPanel Spacing="12">
+ <StackPanel.ChildrenTransitions>
+ <TransitionCollection>
+ <EntranceThemeTransition IsStaggeringEnabled="True" FromVerticalOffset="50"/>
+ </TransitionCollection>
+ </StackPanel.ChildrenTransitions>
- </Grid>
+ <TextBlock Style="{StaticResource SubTitleTextBlock}" Text="Configuration" />
+
+ <controls:ExpandableSettingControl
+ Title="Uppercase">
+ <controls:ExpandableSettingControl.Icon>
+ <FontIcon Glyph="&#xF7B2;" />
+ </controls:ExpandableSettingControl.Icon>
+ <ToggleSwitch Style="{StaticResource RightAlignedToggleSwitchStyle}" IsOn="{x:Bind ViewModel.IsUppercase, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
+ </controls:ExpandableSettingControl>
+
+ <controls:CustomTextBox
+ Header="Input"
+ Height="150"
+ AcceptsReturn="True"
+ Text="{x:Bind ViewModel.Input, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
+
+ <controls:CustomTextBox
+ Header="MD5"
+ IsReadOnly="True"
+ Text="{x:Bind ViewModel.MD5, Mode=OneWay}"/>
+
+ <controls:CustomTextBox
+ Header="SHA1"
+ IsReadOnly="True"
+ Text="{x:Bind ViewModel.SHA1, Mode=OneWay}"/>
+
+ <controls:CustomTextBox
+ Header="SHA256"
+ IsReadOnly="True"
+ Text="{x:Bind ViewModel.SHA256, Mode=OneWay}"/>
+
+ <controls:CustomTextBox
+ Header="SHA512"
+ IsReadOnly="True"
+ Text="{x:Bind ViewModel.SHA512, Mode=OneWay}"/>
+ </StackPanel>
</Page>
diff --git a/src/dev/impl/DevTools.Providers.Impl/Tools/HashGenerator/HashGeneratorToolViewModel.cs b/src/dev/impl/DevTools.Providers.Impl/Tools/HashGenerator/HashGeneratorToolViewModel.cs
index f1f72a7ef..fa92862c9 100644
--- a/src/dev/impl/DevTools.Providers.Impl/Tools/HashGenerator/HashGeneratorToolViewModel.cs
+++ b/src/dev/impl/DevTools.Providers.Impl/Tools/HashGenerator/HashGeneratorToolViewModel.cs
@@ -1,14 +1,147 @@
#nullable enable
+using DevTools.Core.Threading;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using System;
+using System.Collections.Generic;
using System.Composition;
+using System.Threading.Tasks;
+using Windows.Security.Cryptography;
+using Windows.Security.Cryptography.Core;
+using Windows.Storage.Streams;
namespace DevTools.Providers.Impl.Tools.HashGenerator
{
[Export(typeof(HashGeneratorToolViewModel))]
public sealed class HashGeneratorToolViewModel : ObservableRecipient, IToolViewModel
{
+ private readonly IThread _thread;
+ private readonly Queue<string> _hashCalculationQueue = new();
+
+ private bool _calculationInProgress;
+ private bool _isUppercase;
+ private string? _input;
+ private string? _md5;
+ private string? _sha1;
+ private string? _sha256;
+ private string? _sha512;
+
public Type View { get; } = typeof(HashGeneratorToolPage);
+
+ internal bool IsUppercase
+ {
+ get => _isUppercase;
+ set
+ {
+ SetProperty(ref _isUppercase, value);
+ QueueHasCalculation();
+ }
+ }
+
+ internal string? Input
+ {
+ get => _input;
+ set
+ {
+ SetProperty(ref _input, value);
+ QueueHasCalculation();
+ }
+ }
+
+ internal string? MD5
+ {
+ get => _md5;
+ private set => SetProperty(ref _md5, value);
+ }
+
+ internal string? SHA1
+ {
+ get => _sha1;
+ private set => SetProperty(ref _sha1, value);
+ }
+
+ internal string? SHA256
+ {
+ get => _sha256;
+ private set => SetProperty(ref _sha256, value);
+ }
+
+ internal string? SHA512
+ {
+ get => _sha512;
+ private set => SetProperty(ref _sha512, value);
+ }
+
+ [ImportingConstructor]
+ public HashGeneratorToolViewModel(IThread thread)
+ {
+ _thread = thread;
+ }
+
+ private void QueueHasCalculation()
+ {
+ _hashCalculationQueue.Enqueue(Input ?? string.Empty);
+ TreatQueueAsync().Forget();
+ }
+
+ private async Task TreatQueueAsync()
+ {
+ if (_calculationInProgress)
+ {
+ return;
+ }
+
+ _calculationInProgress = true;
+
+ await TaskScheduler.Default;
+
+ while (_hashCalculationQueue.TryDequeue(out string text))
+ {
+ Task<string> md5CalculationTask = CalculateHashAsync(HashAlgorithmNames.Md5, text);
+ Task<string> sha1CalculationTask = CalculateHashAsync(HashAlgorithmNames.Sha1, text);
+ Task<string> sha256CalculationTask = CalculateHashAsync(HashAlgorithmNames.Sha256, text);
+ Task<string> sha512CalculationTask = CalculateHashAsync(HashAlgorithmNames.Sha512, text);
+
+ await Task.WhenAll(md5CalculationTask).ConfigureAwait(false);
+
+ _thread.RunOnUIThreadAsync(() =>
+ {
+ MD5 = md5CalculationTask.Result;
+ SHA1 = sha1CalculationTask.Result;
+ SHA256 = sha256CalculationTask.Result;
+ SHA512 = sha512CalculationTask.Result;
+ }).ForgetSafely();
+ }
+
+ _calculationInProgress = false;
+ }
+
+ private async Task<string> CalculateHashAsync(string alrogithmName, string text)
+ {
+ if (string.IsNullOrEmpty(text))
+ {
+ return string.Empty;
+ }
+
+ await TaskScheduler.Default;
+
+ HashAlgorithmProvider algorithmProvider = HashAlgorithmProvider.OpenAlgorithm(alrogithmName);
+ CryptographicHash objHash = algorithmProvider.CreateHash();
+
+ IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(text, BinaryStringEncoding.Utf8);
+ objHash.Append(buffer);
+
+ buffer = objHash.GetValueAndReset();
+ string hash = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, buffer);
+
+ if (IsUppercase)
+ {
+ return hash.ToUpperInvariant();
+ }
+ else
+ {
+ return hash.ToLowerInvariant();
+ }
+ }
}
}