Skip to content

Commit

Permalink
Bug fixes and upgrade to version 1.1
Browse files Browse the repository at this point in the history
- Fixed a Heisenbug due to using unsafe code which was responsible for getting the icons of selected files and folders.
- Code cleanup and refactoring here and there
  • Loading branch information
hmz777 committed Jul 7, 2020
1 parent 029d06c commit 0ecfad4
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 76 deletions.
24 changes: 12 additions & 12 deletions App.config
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="Z_Toolbar.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<section name="Z_Toolbar.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
Expand All @@ -38,4 +38,4 @@
</setting>
</Z_Toolbar.Properties.Settings>
</userSettings>
</configuration>
</configuration>
3 changes: 1 addition & 2 deletions MainLogic/ApplicationItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public class ApplicationItem
public string Path { get; set; }
public bool IsFolder { get; set; }

[JsonIgnore]
public BitmapSource Icon { get; set; }
public byte[] IconBytes { get; set; }

public ICommand delCommand;
public ICommand runCommand;
Expand Down
12 changes: 11 additions & 1 deletion MainLogic/Serializer.cs → MainLogic/DataLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Z_Toolbar.MainLogic
{
public static class Serializer
public static class DataLogic
{
public async static Task Serialize(ObservableCollection<ApplicationItem> t)
{
Expand All @@ -33,5 +33,15 @@ public async static Task<ObservableCollection<ApplicationItem>> Deserialize()

return loa;
}

public static async Task SaveDataAsync(ObservableCollection<ApplicationItem> dataCollection)
{
await Serialize(dataCollection);
}

public static async Task<ObservableCollection<ApplicationItem>> LoadDataAsync()
{
return await Deserialize();
}
}
}
11 changes: 7 additions & 4 deletions MainLogic/DelCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -22,10 +23,12 @@ public bool CanExecute(object parameter)
public async void Execute(object parameter)
{
string tag = parameter as string;
var app = MainWindow.loa.Where(x => x.Path == tag).FirstOrDefault();
if (app == null) return;
MainWindow.loa.Remove(app);
await Serializer.Serialize(MainWindow.loa).ConfigureAwait(false);
var item = MainWindow.loa.Where(x => x.Path == tag).FirstOrDefault();
if (item == null) return;

MainWindow.loa.Remove(item);

await DataLogic.SaveDataAsync(MainWindow.loa).ConfigureAwait(false);
}
}
}
1 change: 1 addition & 0 deletions MainLogic/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ internal struct IMAGEINFO
internal int Unused2;
internal RECT rcImage;
}

[ComImportAttribute()]
[GuidAttribute("46EB5926-582E-4017-9FDF-E8998DAA0950")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
Expand Down
46 changes: 27 additions & 19 deletions MainLogic/UITools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using static Z_Toolbar.MainLogic.NativeMethods;
Expand Down Expand Up @@ -50,36 +52,35 @@ public static ObservableCollection<ApplicationItem> PopulateIcons(ObservableColl
{
foreach (var item in loa)
{
if (item.IsFolder)
{
item.Icon = (BitmapSource)GetIcon(item.Path, true);
}
else
{
item.Icon = (BitmapSource)GetIcon(item.Path, false);
}
item.IconBytes = GetIcon(item.Path, item.IsFolder, item.IconBytes);
}

return loa;
}

public static BitmapSource GetIcon(string path,bool isDirectory)
public static byte[] GetIcon(string path, bool isDirectory, byte[] iconBytes = null)
{
IntPtr hIcon = GetJumboIcon(GetIconIndex(path,isDirectory));
BitmapSource icon = null;
if (iconBytes?.Length > 0)
return iconBytes;

IntPtr hIcon = GetJumboIcon(GetIconIndex(path, isDirectory));
byte[] IconBytes;

using (Icon ico = (Icon)System.Drawing.Icon.FromHandle(hIcon).Clone())
{

icon = ico.ToBitmap().ToBitmapSource();
using (var stream = new MemoryStream())
{
ico.ToBitmap().Save(stream, ImageFormat.Png);
IconBytes = stream.ToArray();
}
}

DestroyIcon(hIcon);
_ = DestroyIcon(hIcon);

return icon;
return IconBytes;
}

internal static int GetIconIndex(string pszFile,bool isDirectory)
internal static int GetIconIndex(string pszFile, bool isDirectory)
{
uint attributes = FILE_ATTRIBUTE_NORMAL;
if (isDirectory)
Expand All @@ -100,12 +101,19 @@ internal static IntPtr GetJumboIcon(int iImage)
IImageList spiml = null;
Guid guil = new Guid(IID_IImageList); //or IID_IImageList2

SHGetImageList(SHIL_EXTRALARGE, ref guil, ref spiml);
_ = SHGetImageList(SHIL_EXTRALARGE, ref guil, ref spiml);
IntPtr hIcon = IntPtr.Zero;
spiml.GetIcon(iImage, ILD_TRANSPARENT | ILD_IMAGE, ref hIcon); //

_ = spiml.GetIcon(iImage, ILD_TRANSPARENT | ILD_IMAGE, ref hIcon); //
return hIcon;
}

internal static byte[] GetByteArrayFromImage(string imagePath)
{
using (var image = Image.FromFile(imagePath))
{
var converter = new ImageConverter();
return (byte[])converter.ConvertTo(image, typeof(byte[]));
}
}
}
}
2 changes: 1 addition & 1 deletion MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
<ColumnDefinition Width="75"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="1" Text="{Binding Name}" TextTrimming="CharacterEllipsis" TextOptions.TextFormattingMode="Ideal" TextOptions.TextRenderingMode="ClearType" Foreground="White" DockPanel.Dock="Bottom" HorizontalAlignment="Center" FontFamily="Roboto Light" FontSize="9"></TextBlock>
<Image RenderOptions.BitmapScalingMode="NearestNeighbor" Source="{Binding Icon}"/>
<Image RenderOptions.BitmapScalingMode="NearestNeighbor" Source="{Binding IconBytes}"/>
<Button Name="DelButton" Command="{Binding DelCommand}" CommandParameter="{Binding Path}" IsEnabled="False" Opacity="0" Height="25" Width="25" Style="{StaticResource DelButton}" xf:Animations.Primary="{xf:Animate BasedOn={StaticResource Grow}, Event=PointerOver}">
<Image Source="/Resources/del.png" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RenderOptions.BitmapScalingMode="HighQuality"/>
</Button>
Expand Down
27 changes: 19 additions & 8 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
using System.Windows.Media;
using Hardcodet.Wpf.TaskbarNotification;
using Microsoft.WindowsAPICodePack.Dialogs;
using System.Threading.Tasks;
using Z_Toolbar.UiComponents;

namespace Z_Toolbar
{
Expand Down Expand Up @@ -70,7 +72,7 @@ private async void MenuItem_Click_plus(object sender, RoutedEventArgs e)
{
Name = item.Remove(0, item.LastIndexOf('\\') + 1),
Path = item,
Icon = (BitmapSource)UITools.GetIcon(item,false),
IconBytes = UITools.GetIcon(item, false),
IsFolder = false
};

Expand All @@ -82,19 +84,27 @@ private async void MenuItem_Click_plus(object sender, RoutedEventArgs e)

lv.ItemsSource = loa;

await Serializer.Serialize(loa).ConfigureAwait(false); //Save apps to Apps.json
await DataLogic.SaveDataAsync(loa).ConfigureAwait(false); //Save apps to Apps.json
}
}

private async void window_Initialized(object sender, EventArgs e)
{
if (File.Exists(ConfigLocation))
{
loa = await Serializer.Deserialize(); //Get apps from Apps.json
loa = UITools.PopulateIcons(loa);
lv.ItemsSource = loa;
try
{
loa = await DataLogic.LoadDataAsync(); //Get apps from Apps.json
loa = UITools.PopulateIcons(loa);
lv.ItemsSource = loa;
await DataLogic.SaveDataAsync(loa).ConfigureAwait(false);
}
catch (Exception)
{
CustomMessageBox cmb = new CustomMessageBox("It Appears that app data is either missing or corrupt, if the \"Apps.json\" file is still present try deleting it and restarting the program.", MessageBoxIcon.Error);
cmb.ShowDialog();
}
}

}

private void window_MouseEnter(object sender, MouseEventArgs e)
Expand Down Expand Up @@ -176,11 +186,12 @@ private async void MenuItem_Click_Folder(object sender, RoutedEventArgs e)
{
foreach (var item in cofd.FileNames)
{

var app = new ApplicationItem()
{
Name = item.Remove(0, item.LastIndexOf('\\') + 1),
Path = item,
Icon = (BitmapSource)UITools.GetIcon(item,true),
IconBytes = UITools.GetIcon(item, true),
IsFolder = true
};

Expand All @@ -192,7 +203,7 @@ private async void MenuItem_Click_Folder(object sender, RoutedEventArgs e)

lv.ItemsSource = loa;

await Serializer.Serialize(loa).ConfigureAwait(false); //Save apps to Apps.json
await DataLogic.SaveDataAsync(loa).ConfigureAwait(false); //Save apps to Apps.json
}

cofd.Dispose();
Expand Down
14 changes: 7 additions & 7 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Z Toolbar")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyTitle("Z-Toolbar")]
[assembly: AssemblyDescription("A Simple productivity toolbar")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Z Toolbar")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyCompany("HMZ-Software")]
[assembly: AssemblyProduct("Z-Toolbar")]
[assembly: AssemblyCopyright("HMZ-Software Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand Down Expand Up @@ -51,5 +51,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
2 changes: 1 addition & 1 deletion Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Tools/HelperMethods.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down
10 changes: 0 additions & 10 deletions UiComponents/CustomMessageBox.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,6 @@ private void window_Loaded(object sender, RoutedEventArgs e)
break;
}
}

private void window_Initialized(object sender, EventArgs e)
{

}

private void MessageTextBlock_Initialized(object sender, EventArgs e)
{

}
}

public enum MessageBoxIcon : int
Expand Down
6 changes: 3 additions & 3 deletions UiComponents/Settings.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Z_Toolbar.UiComponents"
mc:Ignorable="d"
Title="Settings" BorderBrush="white" BorderThickness="1" Height="324" Width="311" Padding="0" Margin="0" WindowStyle="None" Background="Black" FontFamily="Roboto" FontSize="14" ResizeMode="NoResize" Topmost="True" WindowStartupLocation="CenterScreen">
Title="Settings" BorderBrush="white" BorderThickness="1" Height="324" Width="311" Padding="0" Margin="0" WindowStyle="None" Background="Black" FontFamily="Roboto" FontSize="14" ResizeMode="NoResize" Topmost="True" WindowStartupLocation="CenterScreen" Initialized="window_Initialized_1">
<Grid x:Name="grid" Margin="0" Width="{Binding ActualWidth, ElementName=window}" Height="{Binding ActualHeight, ElementName=window}">
<Grid.Resources>
<ResourceDictionary Source="SettingsResourceBundle.xaml"/>
</Grid.Resources>
<TextBlock Foreground="White" HorizontalAlignment="Left" Margin="22,20,0,0" TextWrapping="Wrap" Text="Settings" VerticalAlignment="Top" FontFamily="Roboto" FontSize="22" TextDecorations="{x:Null}"/>
<Button Click="Button_Click" Style="{DynamicResource MessageboxButtonStyle}" Foreground="White" Content="Save" HorizontalAlignment="Left" Margin="205,270,0,0" VerticalAlignment="Top" Width="80" Height="29.999"/>
<CheckBox IsThreeState="False" Foreground="White" HorizontalAlignment="Left" Margin="22,80,0,0" VerticalAlignment="Top" Height="19.999" Width="163" Click="CheckBox_Click">
<CheckBox x:FieldModifier="public" x:Name="startupCheckbox" IsThreeState="False" Foreground="White" HorizontalAlignment="Left" Margin="22,80,0,0" VerticalAlignment="Top" Height="19.999" Width="163" Click="CheckBox_Click">
<TextBlock Margin="5,0,0,0">Run at startup</TextBlock>
</CheckBox>
<ComboBox SelectedIndex="2" IsEnabled="False" IsReadOnly="True" HorizontalAlignment="Left" Foreground="White" Margin="65,123.5,0,0" VerticalAlignment="Top" Width="120" Height="25" Style="{DynamicResource CustomComboBox}" SelectionChanged="ComboBox_SelectionChanged">
<ComboBox SelectedIndex="2" IsEnabled="False" IsReadOnly="True" HorizontalAlignment="Left" Foreground="White" Margin="65,123.5,0,0" VerticalAlignment="Top" Width="120" Height="25" Style="{DynamicResource CustomComboBox}">
<ComboBoxItem Template="{DynamicResource CustomComboBoxItem}">Right</ComboBoxItem>
<ComboBoxItem Template="{DynamicResource CustomComboBoxItem}">Left</ComboBoxItem>
<ComboBoxItem Template="{DynamicResource CustomComboBoxItem}">Top</ComboBoxItem>
Expand Down
14 changes: 10 additions & 4 deletions UiComponents/Settings.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ private void Button_Click(object sender, RoutedEventArgs e)
this.DialogResult = true;
}

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

}

private void CheckBox_Click(object sender, RoutedEventArgs e)
{
Expand All @@ -53,5 +49,15 @@ private void CheckBox_Click(object sender, RoutedEventArgs e)
rKey.Close();

}

private void window_Initialized_1(object sender, EventArgs e)
{
RegistryKey rKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", false);

if (rKey.GetValue("ZToolbar") != null)
startupCheckbox.IsChecked = true;

rKey.Close();
}
}
}
Loading

0 comments on commit 0ecfad4

Please sign in to comment.