Skip to content

Commit 5a83900

Browse files
author
Carl Chang
committed
reorganize files;
add setting window; add setting class;
1 parent 35342ed commit 5a83900

20 files changed

+470
-130
lines changed

App.xaml.cs

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,71 +6,35 @@
66
using System.Linq;
77
using System.Threading.Tasks;
88
using System.Windows;
9-
using IniParser;
10-
using IniParser.Model;
11-
using SevenZip;
12-
using Size = System.Drawing.Size;
9+
10+
1311

1412
namespace ZipImageViewer
1513
{
1614
public partial class App : Application {
1715
public static MainWindow MainWin;
18-
public static IniData Config;
19-
2016
public static readonly HashSet<string> ImageExtensions =
2117
new HashSet<string>(new[] {
2218
"jpg", "jpeg", "png", "gif", "tiff", "bmp",
2319
".jpg", ".jpeg", ".png", ".gif", ".tiff", ".bmp",
2420
});
25-
2621
public static readonly HashSet<string> ZipExtensions =
2722
new HashSet<string>(new[] {
2823
"zip", "rar", "7z",
2924
".zip", ".rar", ".7z",
3025
});
3126

32-
public static Size ThumbnailSize { get; set; } = new Size(300, 200);
3327

3428
private void App_Startup(object sender, StartupEventArgs e) {
35-
//load config
36-
if (!File.Exists("config.ini")) File.WriteAllText("config.ini",
37-
@"
38-
[App Config]
39-
SevenZipDllPath=C:\Program Files\7-Zip\7z.dll
40-
ThumbnailSize=300x200
41-
42-
;Saved passwords for zipped files. Format:
43-
;password=file_full_path
44-
;leaving file_full_path empty indicates a password to try on all files when there is no matched path.
45-
[Saved Passwords]
46-
");
47-
var parser = new FileIniDataParser();
48-
Config = parser.ReadFile("config.ini");
49-
50-
//dll path
51-
var sevenZipDllPath = Config["App Config"]["SevenZipDllPath"];
52-
if (string.IsNullOrEmpty(sevenZipDllPath)) {
53-
MessageBox.Show("7z.dll path is missing from the configuration.", "", MessageBoxButton.OK,
54-
MessageBoxImage.Error);
55-
Current.Shutdown();
56-
return;
57-
}
58-
SevenZipBase.SetLibraryPath(sevenZipDllPath);
59-
60-
//thumb size
61-
var thumbsize = Config["App Config"]["ThumbnailSize"]?.Split('x', '*', ',');
62-
if (thumbsize?.Length == 2)
63-
ThumbnailSize = new Size(int.Parse(thumbsize[0]), int.Parse(thumbsize[1]));
64-
29+
Setting.LoadConfigFromFile();
6530
//show mainwindow
6631
MainWin = new MainWindow();
6732
MainWin.Show();
6833
}
6934

7035

7136
private void App_Exit(object sender, ExitEventArgs e) {
72-
var parser = new FileIniDataParser();
73-
parser.WriteFile("config.ini", Config);
37+
Setting.SaveConfigToFile();
7438
}
7539
}
7640
}

Converters.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Globalization;
33
using System.Windows;
44
using System.Windows.Data;
5+
using System.Windows.Media;
56

67
namespace ZipImageViewer
78
{
@@ -17,4 +18,21 @@ public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
1718
throw new NotSupportedException();
1819
}
1920
}
21+
22+
public class FlagToBrushConverter : IValueConverter
23+
{
24+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
25+
var brush = Brushes.DimGray;
26+
if (value is FileFlags flags) {
27+
if (flags.HasFlag(FileFlags.Directory)) brush = Brushes.Goldenrod;
28+
else if (flags.HasFlag(FileFlags.Archive)) brush = Brushes.DarkRed;
29+
else if (flags.HasFlag(FileFlags.Image)) brush = Brushes.Transparent;
30+
}
31+
return brush;
32+
}
33+
34+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
35+
throw new NotImplementedException();
36+
}
37+
}
2038
}

Helpers.cs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Windows.Media;
88
using System.Windows.Media.Animation;
99
using System.Windows.Media.Imaging;
10+
using Microsoft.WindowsAPICodePack.Dialogs;
1011

1112
namespace ZipImageViewer
1213
{
@@ -152,14 +153,14 @@ private void OnLoaded(object sender, RoutedEventArgs e)
152153

153154
[Flags]
154155
public enum FileFlags {
155-
Unknown,
156-
Image,
157-
Archive,
158-
Directory,
156+
Unknown = 0,
157+
Image = 1,
158+
Archive = 2,
159+
Directory = 4,
159160
/// <summary>
160161
/// Indicate to load all archive content instead of a single image
161162
/// </summary>
162-
Archive_OpenSelf
163+
Archive_OpenSelf = 8
163164
}
164165

165166
public class ImageInfo
@@ -170,7 +171,9 @@ public class ImageInfo
170171
public string FileName { get; set; }
171172

172173
/// <summary>
173-
/// For archives, full path to the archive file. Otherwise full path to the image file.
174+
/// For archives, full path to the archive file.
175+
/// For directories, full path to the directory.
176+
/// Otherwise full path to the image file.
174177
/// </summary>
175178
public string FilePath { get; set; }
176179

@@ -193,7 +196,12 @@ public string ImageRealPath {
193196
return FilePath;
194197
return null;
195198
}
196-
}
199+
}
200+
201+
/// <summary>
202+
/// For Flags.Directory, the parent folder name of FilePath. Otherwise FileName.
203+
/// </summary>
204+
public string DisplayName => Flags.HasFlag(FileFlags.Directory) ? Path.GetDirectoryName(FilePath) : FileName;
197205

198206
// public HashSet<string> ImageSet { get; set; }
199207
// public string Password { get; set; }
@@ -225,11 +233,11 @@ public LoadOptions(int decodeWidth = 0, string password = null,
225233
public class Helpers
226234
{
227235
/// <summary>
228-
/// Get file type based on extension.
236+
/// Get file type based on extension. Assumes fileName points to a file.
229237
/// </summary>
230238
/// <param name="fileName">A full or not full path of the file.</param>
231239
/// <returns></returns>
232-
public static FileFlags GetFileType(string fileName)
240+
public static FileFlags GetPathType(string fileName)
233241
{
234242
var ft = FileFlags.Unknown;
235243
var extension = Path.GetExtension(fileName)?.TrimStart('.').ToLowerInvariant();
@@ -240,7 +248,7 @@ public static FileFlags GetFileType(string fileName)
240248
return ft;
241249
}
242250

243-
public static FileFlags GetFileType(FileSystemInfo fsInfo) {
251+
public static FileFlags GetPathType(FileSystemInfo fsInfo) {
244252
if (fsInfo.Attributes.HasFlag(FileAttributes.Directory))
245253
return FileFlags.Directory;
246254
if (App.ZipExtensions.Contains(fsInfo.Extension))
@@ -361,5 +369,14 @@ public static Size UniformScaleDown(double oldW, double oldH, double minW, doubl
361369

362370
return new Size(oldW, oldH);
363371
}
372+
373+
public static string OpenFolderDialog(Window owner)
374+
{
375+
var cofd = new CommonOpenFileDialog() {
376+
IsFolderPicker = true
377+
};
378+
379+
return cofd.ShowDialog(owner) == CommonFileDialogResult.Ok ? cofd.FileName : null;
380+
}
364381
}
365382
}

MainWindow.xaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:local="clr-namespace:ZipImageViewer"
7-
mc:Ignorable="d" Background="#FF1F1F1F" UseLayoutRounding="True"
8-
Title="ZipImageViewer" Height="640" Width="1110" Drop="MainWin_Drop" AllowDrop="True" Loaded="MainWin_Loaded">
7+
mc:Ignorable="d" Background="#FF1F1F1F" UseLayoutRounding="True" Foreground="LightGray"
8+
Title="ZipImageViewer" Height="640" Width="1200" Drop="MainWin_Drop" AllowDrop="True" Loaded="MainWin_Loaded">
99
<ScrollViewer>
1010
<ItemsControl x:Name="LB1" ItemsSource="{Binding ElementName=MainWin, Path=ImageList}">
1111
<ItemsControl.ItemsPanel>
@@ -19,7 +19,7 @@
1919
UseLayoutRounding="True">
2020
<local:Thumbnail x:Name="TN1" Tag="False" RenderTransformOrigin="0.5 0.5" Panel.ZIndex="1"
2121
ImageInfo="{Binding}" Margin="10"
22-
PreviewMouseLeftButtonUp="TN1_PreviewMouseLeftButtonUp">
22+
PreviewMouseUp="TN1_PreviewMouseUp">
2323
<!-- Width="{Binding Source={x:Static Application.Current}, Path=ThumbnailSize.Width}" -->
2424
<!-- Height="{Binding Source={x:Static Application.Current}, Path=ThumbnailSize.Height}" -->
2525
<local:Thumbnail.Effect>
@@ -44,7 +44,7 @@
4444
</local:Thumbnail.Style>
4545
</local:Thumbnail>
4646

47-
<TextBlock Text="{Binding FileName}" FontSize="10" Foreground="Gray"
47+
<TextBlock Text="{Binding DisplayName}" FontSize="10" Foreground="Gray"
4848
HorizontalAlignment="Center" Panel.ZIndex="0"/>
4949
</StackPanel>
5050
<DataTemplate.Triggers>

0 commit comments

Comments
 (0)