Skip to content

Commit

Permalink
Feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamparter committed Feb 1, 2025
1 parent d079525 commit e165254
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -4043,4 +4043,20 @@
<value>Add to shelf</value>
<comment>Tooltip that displays when dragging items to the Shelf Pane</comment>
</data>
<data name="SelectAlgorithm" xml:space="preserve">
<value>Select algorithm</value>
<comment>Placeholder that displays in the hash properties combo box</comment>
</data>
<data name="EnterHashToCompare" xml:space="preserve">
<value>Enter a hash to compare</value>
<comment>Placeholder that appears in the compare hash text box</comment>
</data>
<data name="HashesMatch" xml:space="preserve">
<value>Hashes match!</value>
<comment>Appears when two compared hashes match</comment>
</data>
<data name="HashesDoNotMatch" xml:space="preserve">
<value>Hashes don't match</value>
<comment>Appears when two compared hashes don't match</comment>
</data>
</root>
13 changes: 12 additions & 1 deletion src/Files.App/ViewModels/Properties/HashesViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Files Community
// Copyright (c) Files Community
// Licensed under the MIT License.

using Files.Shared.Helpers;
Expand Down Expand Up @@ -120,6 +120,17 @@ private void ToggleIsEnabled(string? algorithm)
}
}

public bool CompareHash(string algorithm, string hashToCompare)
{
var hashInfoItem = Hashes.FirstOrDefault(x => x.Algorithm == algorithm);
if (hashInfoItem == null || hashInfoItem.HashValue == null)
{
return false;
}

return hashInfoItem.HashValue.Equals(hashToCompare, StringComparison.OrdinalIgnoreCase);
}

public void Dispose()
{
_cancellationTokenSource.Cancel();
Expand Down
52 changes: 51 additions & 1 deletion src/Files.App/Views/Properties/HashesPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@
Text="SHA1" />
<ToggleMenuFlyoutItem
Click="ToggleMenuFlyoutItem_Click"
Command="{x:Bind HashesViewModel.ToggleIsEnabledCommand}"
CommandParameter="SHA256"
IsChecked="{x:Bind HashesViewModel.ShowHashes['SHA256']}"
Text="SHA256" />
Expand Down Expand Up @@ -201,5 +200,56 @@
</ListView>
</Grid>

<!-- Hash Comparison Section -->
<Grid
x:Name="HashComparisonGrid"
Margin="12,0,12,12"
VerticalAlignment="Bottom"
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
BorderThickness="1"
CornerRadius="4">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<!-- Algorithm Selection -->
<ComboBox
x:Name="AlgorithmComboBox"
Grid.Row="0"
Grid.Column="0"
Margin="12"
DisplayMemberPath="Algorithm"
ItemsSource="{x:Bind HashesViewModel.Hashes}"
PlaceholderText="{helpers:ResourceString Name=SelectAlgorithm}"
SelectedValue="{x:Bind HashesViewModel.SelectedItem.Algorithm, Mode=TwoWay}"
SelectedValuePath="Algorithm"
SelectionChanged="AlgorithmComboBox_SelectionChanged" />

<!-- Hash Input -->
<TextBox
x:Name="HashInputTextBox"
Grid.Row="0"
Grid.Column="1"
Margin="12"
PlaceholderText="{helpers:ResourceString Name=EnterHashToCompare}"
TextChanged="HashInputTextBox_TextChanged" />

<!-- Comparison Result -->
<TextBlock
x:Name="ComparisonResultTextBlock"
Grid.Row="0"
Grid.Column="2"
Margin="12"
VerticalAlignment="Center"
Style="{StaticResource App.Theme.BodyTextBlockStyle}" />
</Grid>
</Grid>
</vm:BasePropertiesPage>

18 changes: 18 additions & 0 deletions src/Files.App/Views/Properties/HashesPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ private void ToggleMenuFlyoutItem_Click(object sender, RoutedEventArgs e)
_cancel = true;
}

private void HashInputTextBox_TextChanged(object sender, TextChangedEventArgs e)
=> CompareHashes();

private void AlgorithmComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
=> CompareHashes();

private void CompareHashes()
{
var selectedAlgorithm = AlgorithmComboBox.SelectedValue as string;
var hashToCompare = HashInputTextBox.Text;

if (string.IsNullOrEmpty(selectedAlgorithm) || string.IsNullOrEmpty(hashToCompare))
return;

var result = HashesViewModel.CompareHash(selectedAlgorithm, hashToCompare);
ComparisonResultTextBlock.Text = result ? Strings.HashesMatch.GetLocalizedResource() : Strings.HashesDoNotMatch.GetLocalizedResource();
}

private void MenuFlyout_Closing(FlyoutBase sender, FlyoutBaseClosingEventArgs e)
{
e.Cancel = _cancel;
Expand Down

0 comments on commit e165254

Please sign in to comment.