Skip to content
This repository has been archived by the owner on Jul 27, 2024. It is now read-only.

Commit

Permalink
v0.4.0: First release, input validation, icon added.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-ihnatiuk committed May 24, 2023
1 parent f228a7d commit 137a316
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 14 deletions.
Binary file added QRes.exe
Binary file not shown.
50 changes: 41 additions & 9 deletions ScriptRes/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,64 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ScriptRes"
mc:Ignorable="d"
Title="MainWindow" Height="550" Width="350" ResizeMode="CanMinimize" UseLayoutRounding="False" Loaded="Window_Loaded">
Title="ScriptRes by Andrii Ihnatiuk" Height="550" Width="350" Loaded="Window_Loaded" MinWidth="310" MinHeight="550" ScrollViewer.CanContentScroll="True" MaxHeight="550">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="80"></RowDefinition>
<RowDefinition Height="1*"></RowDefinition>
</Grid.RowDefinitions>
<GroupBox Header="Display Settings" Grid.Row="0" UseLayoutRounding="False" Padding="5">
<GroupBox Header="Display Settings" Grid.Row="0" Padding="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*"></RowDefinition>
<RowDefinition Height="1*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width="60"></ColumnDefinition>
<ColumnDefinition Width="60"></ColumnDefinition>
<ColumnDefinition Width="70"></ColumnDefinition>
<ColumnDefinition Width="70"></ColumnDefinition>
</Grid.ColumnDefinitions>

<TextBlock Text="Initial resolution:" Padding="0" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<TextBox Name="tBoxInX" Text="1920" HorizontalAlignment="Right" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" TextAlignment="Center" MaxLength="4" VerticalAlignment="Center" Width="50" Grid.Column="1"/>
<TextBox Name="tBoxInY" Text="1080" HorizontalAlignment="Right" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" TextAlignment="Center" MaxLength="4" VerticalAlignment="Center" Width="50" Grid.Column="2"/>
<Grid Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>

<TextBlock Text="X:" LineHeight="18" VerticalAlignment="Center" TextAlignment="Right" Margin="0,0,4,0"/>
<TextBox Name="tBoxInX" HorizontalAlignment="Right" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" TextAlignment="Center" MaxLength="4" VerticalAlignment="Center" Width="50" Grid.Column="1" Padding="1.02" PreviewTextInput="TBox_AllowNumbersOnly"/>
</Grid>
<Grid Grid.Column="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>

<TextBlock Text="Y:" LineHeight="18" VerticalAlignment="Center" TextAlignment="Right" Margin="0,0,4,0"/>
<TextBox Name="tBoxInY" HorizontalAlignment="Right" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" TextAlignment="Center" MaxLength="4" VerticalAlignment="Center" Width="50" Grid.Column="2" Padding="1.02" PreviewTextInput="TBox_AllowNumbersOnly"/>
</Grid>

<TextBlock Text="Preferred resolution:" Padding="0" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="1"/>
<TextBox Name="tBoxOutX" Text="1280" HorizontalAlignment="Right" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" TextAlignment="Center" MaxLength="4" VerticalAlignment="Center" Width="50" Grid.Column="1" Grid.Row="1"/>
<TextBox Name="tBoxOutY" Text="720" HorizontalAlignment="Right" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" TextAlignment="Center" MaxLength="4" VerticalAlignment="Center" Width="50" Grid.Column="2" Grid.Row="1"/>
<Grid Grid.Column="1" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>

<TextBlock Text="X:" LineHeight="18" VerticalAlignment="Center" TextAlignment="Right" Margin="0,0,4,0"/>
<TextBox Name="tBoxOutX" HorizontalAlignment="Right" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" TextAlignment="Center" MaxLength="4" VerticalAlignment="Center" Width="50" Grid.Column="1" Grid.Row="1" Padding="1.02" PreviewTextInput="TBox_AllowNumbersOnly"/>
</Grid>
<Grid Grid.Column="2" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>

<TextBlock Text="Y:" LineHeight="18" VerticalAlignment="Center" TextAlignment="Right" Margin="0,0,4,0"/>
<TextBox Name="tBoxOutY" HorizontalAlignment="Right" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" TextAlignment="Center" MaxLength="4" VerticalAlignment="Center" Width="50" Grid.Column="2" Grid.Row="1" Padding="1.02" PreviewTextInput="TBox_AllowNumbersOnly"/>
</Grid>
</Grid>
</GroupBox>

Expand Down Expand Up @@ -89,7 +121,7 @@
<Button Name="btnCustomIcon" Click="BtnBrowse_Click" Grid.Row="3" Grid.Column="1" Margin="0,2" Visibility="Hidden" Height="30" Background="White" Content="Custom" VerticalAlignment="Top"/>

<TextBlock Grid.Row="4" Text="Shortcut name" Height="16" VerticalAlignment="Bottom" Margin="0,20,0,4"></TextBlock>
<TextBox Name="tBoxShortcutName" IsEnabled="False" Grid.Row="5" Grid.ColumnSpan="2" Padding="6.02" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" VerticalAlignment="Center" Foreground="DimGray"/>
<TextBox Name="tBoxShortcutName" IsEnabled="False" Grid.Row="5" Grid.ColumnSpan="2" Padding="6.02" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" VerticalAlignment="Center" Foreground="DimGray" PreviewTextInput="TBoxShortcutName_ValidateShortcutName" MaxLength="50" />

<Button Click="BtnCreate_Click" Grid.Row="6" Grid.ColumnSpan="2" Content="Create a shortcut" Height="30" VerticalAlignment="Bottom"></Button>
</Grid>
Expand Down
46 changes: 41 additions & 5 deletions ScriptRes/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
Expand All @@ -26,6 +27,9 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
chBoxQresPath.IsChecked = true;
chBoxQresPath.Checked += CheckBox_Checked;
chBoxQresPath.Unchecked += CheckBox_Checked;

tBoxInX.Text = SystemParameters.PrimaryScreenWidth.ToString();
tBoxInY.Text = SystemParameters.PrimaryScreenHeight.ToString();
}

// Handler for buttons to select file locations
Expand Down Expand Up @@ -58,12 +62,12 @@ private void BtnBrowse_Click(object sender, RoutedEventArgs e)
{
extractedIcons = IconUtil.ExtractAllIcons(_iconsSource);
}
catch (FileNotFoundException exc)
catch (FileNotFoundException exc)
{
MessageBox.Show($"An error occured while extracting icons from the file.\nDetails: {exc.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}

// Display all icons in ListBox
var itemsSource = new List<IconListItem>();
for (int i = 0; i < extractedIcons.Length; i++)
Expand Down Expand Up @@ -101,6 +105,7 @@ private void BtnCreate_Click(object sender, RoutedEventArgs e)

if (string.IsNullOrEmpty(inX) || string.IsNullOrEmpty(inY) || string.IsNullOrEmpty(outX) || string.IsNullOrEmpty(outY) || string.IsNullOrEmpty(qresPath))
{
MessageBox.Show("Not all fields are filled in!", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}

Expand Down Expand Up @@ -160,7 +165,7 @@ private void BtnCreate_Click(object sender, RoutedEventArgs e)
File.WriteAllText(batchLocation, script);

string shortcutName = string.IsNullOrWhiteSpace(tBoxShortcutName.Text) ? execNameNoExt : tBoxShortcutName.Text;

CreateShortcut(desktopPath, batchLocation, _iconsSource, shortcutName);
MessageBox.Show($"The shortcut was created: \n{desktopPath}\\{shortcutName}.lnk", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
}
Expand All @@ -170,7 +175,7 @@ private void BtnCreate_Click(object sender, RoutedEventArgs e)
}
}

private void CheckBox_Checked(object sender, EventArgs e)
private void CheckBox_Checked(object sender, EventArgs e)
{
if (sender is not CheckBox box) return;

Expand Down Expand Up @@ -198,5 +203,36 @@ private void CreateShortcut(string shortcutPath, string targetPath, string? icon
}
shortcut.Save();
}

// PreviewTextInput: allow only numbers in the textbox
private void TBox_AllowNumbersOnly(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
if (sender is not TextBox tBox) return;
// If selection starts from the first character - forbid to enter 0
if (tBox.SelectionStart == 0)
{
e.Handled = !NumbersMatch().IsMatch(e.Text);
}
else
{
e.Handled = !NumbersMatch().IsMatch(tBox.Text + e.Text);
}
}

// PreviewTextInput: forbid special characters in shortcut name
private void TBoxShortcutName_ValidateShortcutName(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
if (sender is not TextBox) return;
e.Handled = !InvalidShortcutCharactersMatch().IsMatch(e.Text);
}

// Compile time regular expressions

[GeneratedRegex(@"^[1-9]\d*$")]
private static partial Regex NumbersMatch();

[GeneratedRegex(@"^[^\\/:\*\?""<>|]$")]
private static partial Regex InvalidShortcutCharactersMatch();

}
}
}
5 changes: 5 additions & 0 deletions ScriptRes/ScriptRes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFramework>net7.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<ApplicationIcon>icons8-resolution-96.ico</ApplicationIcon>
</PropertyGroup>

<ItemGroup>
Expand All @@ -19,6 +20,10 @@
</COMReference>
</ItemGroup>

<ItemGroup>
<Content Include="icons8-resolution-96.ico" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
</ItemGroup>
Expand Down
Binary file added ScriptRes/icons8-resolution-96.ico
Binary file not shown.

0 comments on commit 137a316

Please sign in to comment.