Add logo and improve error handling

This commit is contained in:
Stone_Red
2023-03-17 15:02:14 +01:00
parent 71451f22b2
commit e23ae0a47a
4 changed files with 110 additions and 32 deletions
+29 -16
View File
@@ -1,20 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework> <TargetFramework>net7.0-windows</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
<AssemblyVersion>0.0.0.2</AssemblyVersion> <UseWindowsForms>true</UseWindowsForms>
<FileVersion>0.0.0.2</FileVersion> <AssemblyVersion>0.0.0.2</AssemblyVersion>
<Version>0.0.0.2</Version> <FileVersion>0.0.0.2</FileVersion>
</PropertyGroup> <Version>0.0.0.2</Version>
<ApplicationIcon>logo.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="InputSimulator" Version="1.0.4" /> <Content Include="logo.ico">
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.0.0" /> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
<PackageReference Include="System.Management.Automation" Version="7.0.0" /> </Content>
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" /> </ItemGroup>
</ItemGroup>
</Project> <ItemGroup>
<PackageReference Include="InputSimulator" Version="1.0.4" />
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.0.0" />
<PackageReference Include="System.Management.Automation" Version="7.0.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<None Update="logo.ico">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
+12 -6
View File
@@ -5,15 +5,16 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ClipCMD" xmlns:local="clr-namespace:ClipCMD"
mc:Ignorable="d" mc:Ignorable="d"
Title="ClipCMD" Height="450" Width="800"> Title="ClipCMD" Height="450" Width="800"
Closing="OnClose" StateChanged="OnStateChanged" Icon="logo.ico">
<DockPanel> <DockPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal"> <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<Label Content="Prefix:" Width="45" /> <Label Content="Prefix:" Width="45" />
<TextBox x:Name="prefixTextBox" KeyUp="FixTextBox_TextChanged" Text="01" /> <TextBox x:Name="prefixTextBox" KeyUp="FixTextBox_TextChanged" Text="_" />
</StackPanel> </StackPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal"> <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<Label Content="Suffix:" Width="45" /> <Label Content="Suffix:" Width="45" />
<TextBox x:Name="suffixTextBox" KeyUp="FixTextBox_TextChanged" Text="01" /> <TextBox x:Name="suffixTextBox" KeyUp="FixTextBox_TextChanged" Text="_" />
</StackPanel> </StackPanel>
<Grid DockPanel.Dock="Top" VerticalAlignment="Stretch"> <Grid DockPanel.Dock="Top" VerticalAlignment="Stretch">
@@ -23,14 +24,19 @@
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<DockPanel Grid.Column="0"> <DockPanel Grid.Column="0">
<Label DockPanel.Dock="Top" Content="Static Commands:" /> <Label DockPanel.Dock="Top" Content="Commands:" />
<TextBox DockPanel.Dock="Top" x:Name="commandsTextBox" TextChanged="StaticCommandsTextBox_TextChanged" TextWrapping="Wrap" AcceptsReturn="True" VerticalAlignment="Stretch" /> <TextBox DockPanel.Dock="Top" x:Name="commandsTextBox" HorizontalScrollBarVisibility="Visible" TextChanged="StaticCommandsTextBox_TextChanged" AcceptsReturn="True" VerticalAlignment="Stretch" />
</DockPanel> </DockPanel>
<DockPanel Grid.Column="1"> <DockPanel x:Name="logPanel" Grid.Column="1">
<Label DockPanel.Dock="Top" Content="History:" /> <Label DockPanel.Dock="Top" Content="History:" />
<ListBox DockPanel.Dock="Top" x:Name="logListBox" VerticalAlignment="Stretch" /> <ListBox DockPanel.Dock="Top" x:Name="logListBox" VerticalAlignment="Stretch" />
</DockPanel> </DockPanel>
<DockPanel Visibility="Collapsed" x:Name="errorPanel" Grid.Column="1">
<Label DockPanel.Dock="Top" Content="Errors:" />
<ListBox DockPanel.Dock="Top" x:Name="errorListBox" Foreground="Red" VerticalAlignment="Stretch" />
</DockPanel>
</Grid> </Grid>
</DockPanel> </DockPanel>
</Window> </Window>
+69 -10
View File
@@ -1,12 +1,13 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Management.Automation; using System.Management.Automation;
using System.Management.Automation.Language;
using System.Text; using System.Text;
using System.Windows; using System.Windows;
using System.Windows.Media;
using WindowsInput; using WindowsInput;
using WindowsInput.Native; using WindowsInput.Native;
@@ -20,11 +21,25 @@ public partial class MainWindow : Window
{ {
private readonly Dictionary<string, string> commands = new Dictionary<string, string>(); private readonly Dictionary<string, string> commands = new Dictionary<string, string>();
private readonly System.Windows.Forms.NotifyIcon notifyIcon;
private WindowState storedWindowState = WindowState.Normal;
private readonly string cmdPath; private readonly string cmdPath;
private readonly string fixPath; private readonly string fixPath;
public MainWindow() public MainWindow()
{ {
notifyIcon = new System.Windows.Forms.NotifyIcon
{
BalloonTipText = "ClipCMD has been minimized. Click the tray icon to show.",
BalloonTipTitle = "ClipCMD",
Text = "ClipCMD",
Icon = new System.Drawing.Icon("logo.ico"),
Visible = true
};
notifyIcon.Click += NotifyIcon_Click;
InitializeComponent(); InitializeComponent();
string filePath = System.Reflection.Assembly.GetExecutingAssembly().Location; string filePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
@@ -62,6 +77,30 @@ public partial class MainWindow : Window
} }
} }
private void OnClose(object? sender, CancelEventArgs args)
{
notifyIcon.Dispose();
}
private void OnStateChanged(object? sender, EventArgs args)
{
if (WindowState == WindowState.Minimized)
{
Hide();
notifyIcon?.ShowBalloonTip(2000);
}
else
{
storedWindowState = WindowState;
}
}
private void NotifyIcon_Click(object? sender, EventArgs e)
{
Show();
WindowState = storedWindowState;
}
private IDataObject? oldData; private IDataObject? oldData;
private void ClipboardManager_ClipboardChanged(object? sender, EventArgs e) private void ClipboardManager_ClipboardChanged(object? sender, EventArgs e)
@@ -120,13 +159,14 @@ public partial class MainWindow : Window
private void StaticCommandsTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e) private void StaticCommandsTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{ {
//TODO parse commands
string[] lines = commandsTextBox.Text.Split('\n').Append("[#END#]").ToArray(); string[] lines = commandsTextBox.Text.Split('\n').Append("[#END#]").ToArray();
string commandNames = string.Empty; string commandNames = string.Empty;
StringBuilder script = new StringBuilder(); StringBuilder script = new StringBuilder();
commandsTextBox.Foreground = Brushes.Black; commandsTextBox.Foreground = System.Windows.Media.Brushes.Black;
errorPanel.Visibility = Visibility.Collapsed;
logPanel.Visibility = Visibility.Visible;
errorListBox.Items.Clear();
commands.Clear(); commands.Clear();
foreach (string l in lines) foreach (string l in lines)
@@ -137,16 +177,25 @@ public partial class MainWindow : Window
{ {
if (!string.IsNullOrEmpty(commandNames)) if (!string.IsNullOrEmpty(commandNames))
{ {
_ = Parser.ParseInput(script.ToString(), out _, out ParseError[] errors);
if (errors.Length > 0)
{
foreach (ParseError error in errors)
{
AddError($"[{commandNames}]\n{error}");
}
}
foreach (string commandName in commandNames.Split(',')) foreach (string commandName in commandNames.Split(','))
{ {
if (!string.IsNullOrWhiteSpace(commandName) && commands.TryAdd(commandName.Trim(), script.ToString())) if (string.IsNullOrWhiteSpace(commandName))
{ {
commandsTextBox.Foreground = Brushes.Black; AddError($"[{commandNames}]\nCommand \"{commandName}\" is empty!");
} }
else else if (!commands.TryAdd(commandName.Trim(), script.ToString()))
{ {
commandsTextBox.Foreground = Brushes.Red; AddError($"[{commandNames}]\nCommand \"{commandName}\" already exists!");
return;
} }
} }
} }
@@ -163,14 +212,24 @@ public partial class MainWindow : Window
} }
else else
{ {
commandsTextBox.Foreground = Brushes.Red; AddError($"[{commandNames}]\nInput has to start with [<Command Name>]!");
return; return;
} }
} }
_ = commands.TryAdd("list", $"\"{string.Join(", ", commands.Keys)}\"");
File.WriteAllText(cmdPath, commandsTextBox.Text); File.WriteAllText(cmdPath, commandsTextBox.Text);
} }
private void AddError(string error)
{
errorPanel.Visibility = Visibility.Visible;
logPanel.Visibility = Visibility.Collapsed;
commandsTextBox.Foreground = System.Windows.Media.Brushes.Red;
_ = errorListBox.Items.Add(error);
}
private void FixTextBox_TextChanged(object sender, System.Windows.Input.KeyEventArgs e) private void FixTextBox_TextChanged(object sender, System.Windows.Input.KeyEventArgs e)
{ {
if (prefixTextBox is null || suffixTextBox is null) if (prefixTextBox is null || suffixTextBox is null)
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 101 KiB