Skip to content

Commit

Permalink
added password generator dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
George Patrut committed Feb 27, 2024
1 parent fe7f2c4 commit 85789f7
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Orderly/ViewModels/Pages/DashboardViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ public void SaveEditing(Credential credential)
db.SaveChanges();
}

[RelayCommand]
public void GeneratePassword(Credential credential)
{
PasswordGeneratorDialog dialog = new();
if (dialog.ShowDialog() == false) return;
credential.Password = dialog.GeneratedPassword;
}

public void OnNavigatedFrom()
{
Task.Factory.StartNew(() => {
Expand All @@ -169,7 +177,7 @@ public void Initalize()
credential.PropertyChanged += OnCredentialPropertyChanged;
}
}
config.FilteringOptions.PropertyChanged += OnFilteringOptionChanged;
Config.FilteringOptions.PropertyChanged += OnFilteringOptionChanged;
SortList();
}

Expand Down
120 changes: 120 additions & 0 deletions Orderly/Views/Dialogs/PasswordGeneratorDialog.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<ui:FluentWindow
x:Class="Orderly.Views.Dialogs.PasswordGeneratorDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Orderly.Views.Dialogs"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
Width="350"
Height="280"
MinHeight="0"
ui:Design.Background="{DynamicResource ApplicationBackgroundBrush}"
ui:Design.Foreground="{DynamicResource TextFillColorPrimaryBrush}"
ExtendsContentIntoTitleBar="True"
ResizeMode="NoResize"
SizeToContent="Height"
WindowBackdropType="None"
WindowCornerPreference="Round"
WindowStartupLocation="CenterOwner"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Border Padding="25,25,25,10" Background="{DynamicResource CardBackgroundFillColorDefaultBrush}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<TextBlock FontSize="20" Text="Generate new password" />
<ui:PasswordBox
x:Name="pbPassword"
Grid.Row="1"
Margin="0,20,30,20"
IsReadOnly="True" />
<ui:Button
Grid.Row="1"
Margin="-10,0"
Padding="10,5"
HorizontalAlignment="Right"
Background="Transparent"
BorderThickness="0"
Click="CopyPassword">
<ui:SymbolIcon Symbol="Copy24" />
</ui:Button>
<StackPanel Grid.Row="2">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<StackPanel Width="390" HorizontalAlignment="Left">
<TextBlock Text="Length" />
<Slider
x:Name="slLength"
IsSnapToTickEnabled="True"
Maximum="24"
Minimum="4"
TickPlacement="BottomRight"
ValueChanged="slLength_ValueChanged" />
</StackPanel>
<ui:TextBlock
Grid.Column="1"
Margin="0,10"
VerticalAlignment="Bottom"
Text="{Binding ElementName=slLength, Path=Value}" />
</Grid>
<WrapPanel>
<CheckBox
Name="cbUpper"
Checked="UpdatePassword"
Content="Upper case"
IsChecked="True"
Unchecked="UpdatePassword" />
<CheckBox
Name="cbNumbers"
Checked="UpdatePassword"
Content="Numbers"
IsChecked="True"
Unchecked="UpdatePassword" />
<CheckBox
Name="cbSymbols"
Checked="UpdatePassword"
Content="Symbols"
IsChecked="True"
Unchecked="UpdatePassword" />
</WrapPanel>
</StackPanel>
</Grid>
</Border>
<Grid
Grid.Row="1"
Height="65"
HorizontalAlignment="Center"
VerticalAlignment="Bottom">
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".5*" />
<ColumnDefinition Width=".5*" />
</Grid.ColumnDefinitions>
<ui:Button
x:Name="btnConfirm"
Grid.Column="0"
Width="200"
Margin="10,0"
Appearance="Primary"
Click="btnConfirm_Click"
Content="Confirm" />
<ui:Button
x:Name="btnCancel"
Grid.Column="1"
Width="200"
Margin="10,0"
Click="btnCancel_Click"
Content="Cancel" />
</Grid>
</Grid>
</ui:FluentWindow>
67 changes: 67 additions & 0 deletions Orderly/Views/Dialogs/PasswordGeneratorDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Orderly.Modules;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Wpf.Ui.Controls;

namespace Orderly.Views.Dialogs
{
/// <summary>
/// Interaction logic for PasswordGeneratorDialog.xaml
/// </summary>
public partial class PasswordGeneratorDialog : FluentWindow
{
public string GeneratedPassword { get; private set; } = string.Empty;
public PasswordGeneratorDialog()
{
InitializeComponent();
}

private void btnConfirm_Click(object sender, RoutedEventArgs e)
{
GeneratedPassword = pbPassword.Text;
DialogResult = true;
Close();
}

private void btnCancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}

private void CopyPassword(object sender, RoutedEventArgs e)
{
Clipboard.SetText(GeneratedPassword);
pbPassword.Focus();
pbPassword.SelectAll();
}

private void UpdatePassword(object sender, RoutedEventArgs e)
{
if (cbUpper is null || cbSymbols is null || cbNumbers is null || slLength is null || pbPassword is null) return;
bool upperCase = cbUpper.IsChecked!.Value;
bool symbols = cbSymbols.IsChecked!.Value;
bool numbers = cbNumbers.IsChecked!.Value;
int length = (int)slLength.Value;

GeneratedPassword = PasswordGenerator.GenerateSecurePassword(length, upperCase, numbers, symbols);
pbPassword.Password = GeneratedPassword;
}

private void slLength_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
UpdatePassword(this, new());
}
}
}
11 changes: 11 additions & 0 deletions Orderly/Views/Pages/DashboardPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,17 @@
Opacity=".5"
PlaceholderEnabled="True"
PlaceholderText="Password..." />
<Button
Background="Transparent"
BorderThickness="0"
Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.ViewModel.GeneratePasswordCommand}"
CommandParameter="{Binding}"
Cursor="Hand"
ToolTipService.InitialShowDelay="0"
ToolTipService.ToolTip="Generate new password"
Visibility="{Binding IsEditing, Converter={StaticResource BoolToVisibilityConverter}}">
<ui:SymbolIcon Symbol="ShieldAdd24" />
</Button>
<Button
Background="Transparent"
BorderThickness="0"
Expand Down

0 comments on commit 85789f7

Please sign in to comment.