Hash functions

This commit is contained in:
2022-02-16 21:03:34 +01:00
parent 27a974e3e2
commit d1ceb95884
18 changed files with 240 additions and 39 deletions

View File

@@ -202,5 +202,25 @@
<Style Selector="Border.SelectedTimelineCommand">
<Setter Property="BorderBrush" Value="{DynamicResource ForegroundBrush}"/>
</Style>
<Style Selector="ListBox.RadioButtonListBox">
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Background" Value="Transparent"/>
</Style>
<Style Selector="ListBox.RadioButtonListBox ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border>
<RadioButton
Content="{TemplateBinding ContentPresenter.Content}"
VerticalAlignment="Center"
IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"
Foreground="{DynamicResource ForegroundBrush}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Styles>
</Application>

View File

@@ -55,7 +55,7 @@ namespace FileTime.Avalonia.Application
{*/
/*var task = SetSelectedItemAsync(value, true);
Task.WaitAll(new Task[] { task }, 100);*/
SetSelectedItemAsync(value, true);
Task.Run(async () => await SetSelectedItemAsync(value, true)).Wait();
/*}
catch
{

View File

@@ -46,6 +46,7 @@ namespace FileTime.Avalonia.Configuration
new CommandBindingConfiguration(Commands.ChangeTimelineMode, new[] { Key.T, Key.M }),
new CommandBindingConfiguration(Commands.CloseTab, Key.Q),
new CommandBindingConfiguration(Commands.Copy, new[] { Key.Y, Key.Y }),
new CommandBindingConfiguration(Commands.CopyHash, new[] { Key.C, Key.H }),
new CommandBindingConfiguration(Commands.CopyPath, new[] { Key.C, Key.P }),
new CommandBindingConfiguration(Commands.CreateContainer, Key.F7),
new CommandBindingConfiguration(Commands.CreateContainer, new[] { Key.C, Key.C }),

View File

@@ -8,6 +8,8 @@ namespace FileTime.Avalonia.Misc
public string Value { get; set; }
public object? Option { get; set; }
public char? PasswordChar { get; set; }
public InputElementWrapper(InputElement inputElement, string? defaultValue = null)

View File

@@ -0,0 +1,10 @@
namespace FileTime.Avalonia.Models
{
public enum HashFunction
{
MD5,
SHA256,
SHA384,
SHA512,
}
}

View File

@@ -2,12 +2,14 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
using FileTime.App.Core.Clipboard;
using FileTime.App.Core.Command;
using FileTime.Avalonia.Application;
using FileTime.Avalonia.IconProviders;
using FileTime.Avalonia.Misc;
using FileTime.Avalonia.Models;
using FileTime.Avalonia.ViewModels;
using FileTime.Core.Command;
using FileTime.Core.Components;
@@ -65,6 +67,7 @@ namespace FileTime.Avalonia.Services
{Commands.ChangeTimelineMode, ChangeTimelineMode},
{Commands.CloseTab, CloseTab},
{Commands.Copy, Copy},
{Commands.CopyHash, CopyHash},
{Commands.CopyPath, CopyPath},
{Commands.CreateContainer, CreateContainer},
{Commands.CreateElement, CreateElement},
@@ -276,13 +279,13 @@ namespace FileTime.Avalonia.Services
var createContainerCommand = new CreateContainerCommand(new AbsolutePath(container), containerName);
await AddCommand(createContainerCommand);
}
catch(Exception e)
catch (Exception e)
{
_logger.LogError(e, "Error while creating container {Container}", containerName);
}
};
_dialogService.ReadInputs(new List<InputElement>() { new InputElement("Container name", InputType.Text) }, handler);
_dialogService.ReadInputs(new List<InputElement>() { InputElement.ForText("Container name") }, handler);
return Task.CompletedTask;
}
@@ -299,13 +302,13 @@ namespace FileTime.Avalonia.Services
var createElementCommand = new CreateElementCommand(new AbsolutePath(container), elementName);
await AddCommand(createElementCommand);
}
catch(Exception e)
catch (Exception e)
{
_logger.LogError(e, "Error while creating element {Element}", elementName);
}
};
_dialogService.ReadInputs(new List<InputElement>() { new InputElement("Element name", InputType.Text) }, handler);
_dialogService.ReadInputs(new List<InputElement>() { InputElement.ForText("Element name") }, handler);
return Task.CompletedTask;
}
@@ -487,7 +490,7 @@ namespace FileTime.Avalonia.Services
await AddCommand(renameCommand);
};
_dialogService.ReadInputs(new List<InputElement>() { new InputElement("New name", InputType.Text, selectedItem.Name) }, handler);
_dialogService.ReadInputs(new List<InputElement>() { InputElement.ForText("New name", selectedItem.Name) }, handler);
}
return Task.CompletedTask;
}
@@ -543,7 +546,7 @@ namespace FileTime.Avalonia.Services
}
};
_dialogService.ReadInputs(new List<InputElement>() { new InputElement("Path", InputType.Text) }, handler);
_dialogService.ReadInputs(new List<InputElement>() { InputElement.ForText("Path") }, handler);
return Task.CompletedTask;
}
@@ -580,9 +583,17 @@ namespace FileTime.Avalonia.Services
var currentContainer = _appState.SelectedTab.CurrentLocation.Container;
var textToCopy = currentContainer.NativePath;
if (textToCopy != null && global::Avalonia.Application.Current?.Clipboard is global::Avalonia.Input.Platform.IClipboard clipboard)
if (textToCopy != null)
{
await clipboard.SetTextAsync(textToCopy);
await CopyToClipboard(textToCopy);
}
}
private static async Task CopyToClipboard(string text)
{
if (global::Avalonia.Application.Current?.Clipboard is global::Avalonia.Input.Platform.IClipboard clipboard)
{
await clipboard.SetTextAsync(text);
}
}
@@ -633,7 +644,7 @@ namespace FileTime.Avalonia.Services
return Task.CompletedTask;
};
_dialogService.ReadInputs(new List<InputElement>() { new InputElement("Command", InputType.Text) }, handler);
_dialogService.ReadInputs(new List<InputElement>() { InputElement.ForText("Command") }, handler);
return Task.CompletedTask;
}
@@ -840,5 +851,40 @@ namespace FileTime.Avalonia.Services
//TODO: else
return Task.CompletedTask;
}
private Task CopyHash()
{
var handler = async (List<InputElementWrapper> inputs) =>
{
var hashFunction = (HashFunction?)inputs[0].Option;
if (hashFunction != null && _appState.SelectedTab.SelectedItem?.Item is IElement element && element.Provider.SupportsContentStreams)
{
using var stream = new ContentProviderStream(await element.GetContentReaderAsync());
string? hashString = null;
using HashAlgorithm hashFunc = hashFunction switch
{
HashFunction.MD5 => MD5.Create(),
HashFunction.SHA256 => SHA256.Create(),
HashFunction.SHA384 => SHA384.Create(),
HashFunction.SHA512 => SHA512.Create(),
_ => throw new NotImplementedException()
};
var hash = hashFunc.ComputeHash(stream);
hashString = string.Concat(hash.Select(b => b.ToString("X2")));
_dialogService.ShowToastMessage($"Hash copied ({hashString})");
await CopyToClipboard(hashString);
}
};
_dialogService.ReadInputs(new List<InputElement>()
{
InputElement.ForOptions("Hash function", Enum.GetValues<HashFunction>().Cast<object>().ToList())
}, handler);
return Task.CompletedTask;
}
}
}

View File

@@ -5,6 +5,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:FileTime.Avalonia.ViewModels"
xmlns:interactions="using:FileTime.Core.Interactions"
xmlns:local="using:FileTime.Avalonia.Views"
xmlns:models="using:FileTime.Avalonia.Models"
Title="FileTime"
@@ -470,20 +471,35 @@
Items="{Binding AppState.Inputs}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid MinWidth="500" ColumnDefinitions="250,*" Margin="10,5">
<Grid MinWidth="500" ColumnDefinitions="250,*" Margin="10,5" x:Name="ItemRoot">
<TextBlock
HorizontalAlignment="Left"
VerticalAlignment="Center"
Text="{Binding InputElement.Text}" />
<TextBox
PasswordChar="{Binding PasswordChar}"
AttachedToVisualTree="InputText_AttachedToVisualTree"
IsTabStop="True"
Grid.Column="1"
GotFocus="InputText_GotFocus"
LostFocus="InputText_LostFocus"
KeyDown="InputText_KeyDown"
Text="{Binding Value, Mode=TwoWay}" />
VerticalAlignment="Top"
Text="{Binding InputElement.Label}" />
<Grid Grid.Column="1">
<TextBox
IsVisible="{Binding InputElement.InputType, Converter={StaticResource EqualityConverter}, ConverterParameter={x:Static interactions:InputType.Text}}"
AttachedToVisualTree="InputText_AttachedToVisualTree"
GotFocus="InputText_GotFocus"
LostFocus="InputText_LostFocus"
KeyDown="InputText_KeyDown"
VerticalAlignment="Top"
Text="{Binding Value, Mode=TwoWay}" />
<TextBox
IsVisible="{Binding InputElement.InputType, Converter={StaticResource EqualityConverter}, ConverterParameter={x:Static interactions:InputType.Password}}"
PasswordChar="{Binding PasswordChar}"
AttachedToVisualTree="InputText_AttachedToVisualTree"
GotFocus="InputText_GotFocus"
LostFocus="InputText_LostFocus"
KeyDown="InputText_KeyDown"
VerticalAlignment="Top"
Text="{Binding Value, Mode=TwoWay}" />
<ListBox
Classes="RadioButtonListBox"
IsVisible="{Binding InputElement.InputType, Converter={StaticResource EqualityConverter}, ConverterParameter={x:Static interactions:InputType.Options}}"
Items="{Binding InputElement.Options}"
SelectedItem="{Binding Option}"/>
</Grid>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>