-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
637 lines (556 loc) · 27.1 KB
/
MainWindow.xaml.cs
File metadata and controls
637 lines (556 loc) · 27.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Linq;
using SevenZip;
using System.ComponentModel;
using System.Threading;
using System.Windows.Media;
using System.Windows.Data;
using System.Windows.Threading;
using System.Data;
using Path = System.IO.Path;
using SizeInt = System.Drawing.Size;
using static ZipImageViewer.TableHelper;
namespace ZipImageViewer
{
public partial class MainWindow : BorderlessWindow, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ObservableKeyedCollection<string, ObjectInfo> ObjectList { get; } = new ObservableKeyedCollection<string, ObjectInfo>(o => o.VirtualPath);
private DpiScale DpiScale;
public double ThumbRealWidth => Setting.ThumbnailSize.Item1 / DpiScale.DpiScaleX;
public double ThumbRealHeight => Setting.ThumbnailSize.Item2 / DpiScale.DpiScaleY;
private string currentPath = "";
public string CurrentPath {
get { return currentPath; }
set {
if (currentPath == value) return;
currentPath = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentPath)));
}
}
public int ThumbChangeDelay => Convert.ToInt32((virWrapPanel.VisualChildrenCount * 200 + App.Random.Next(2, 7) * 1000) * Setting.ThumbSwapDelayMultiplier);
internal CancellationTokenSource tknSrc_LoadThumb;
private readonly object lock_LoadThumb = new object();
public string InitialPath;
private VirtualizingWrapPanel virWrapPanel;
private Rect lastWindowRect;
internal Rect lastViewWindowRect;
public MainWindow()
{
InitializeComponent();
}
#region MainWindow Event Handlers
private void MainWin_Loaded(object sender, RoutedEventArgs e)
{
if (App.ContextMenuWin == null)
App.ContextMenuWin = new ContextMenuWindow();
virWrapPanel = Helpers.GetVisualChild<VirtualizingWrapPanel>(TV1);
DpiScale = VisualTreeHelper.GetDpi(this);
Setting.ThumbnailSize.PropertyChanged += ThumbnailSizeChanged;
var view = (ListCollectionView)((CollectionViewSource)FindResource("ObjectListViewSource")).View;
view.CustomSort = new FolderSorter();
//load last path or open dialog
if (InitialPath?.Length > 0)
Task.Run(() => LoadPath(InitialPath));
else if (Setting.LastPath?.Length > 0)
Task.Run(() => LoadPath(Setting.LastPath));
else
openFolderPrompt();
}
private void MainWin_Unloaded(object sender, RoutedEventArgs e) {
if (Application.Current.Windows.Cast<Window>().Count(w => w is MainWindow) == 0)
Application.Current.Shutdown();
}
private bool reallyClose = false;
private async void MainWin_Closing(object sender, CancelEventArgs e) {
if (reallyClose) return;
//start cleaning up
e.Cancel = true;
Setting.ThumbnailSize.PropertyChanged -= ThumbnailSizeChanged;
tknSrc_LoadThumb?.Cancel();
while (tknSrc_LoadThumb != null) { await Task.Delay(100); }
Dispatcher.Invoke(() => ObjectList.Clear());
Setting.LastPath = CurrentPath;
if (WindowState != WindowState.Maximized) {
Setting.LastWindowSize = new Size(Width, Height);
}
//now really close
reallyClose = true;
Dispatcher.BeginInvoke(new Action(() => Close()));
}
private void ThumbnailSizeChanged(object sender, PropertyChangedEventArgs e) {
if (PropertyChanged == null) return;
if (e.PropertyName == nameof(Setting.ThumbnailSize.Item1))
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(ThumbRealWidth)));
if (e.PropertyName == nameof(Setting.ThumbnailSize.Item2))
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(ThumbRealHeight)));
}
private void MainWin_DpiChanged(object sender, DpiChangedEventArgs e) {
DpiScale = VisualTreeHelper.GetDpi(this);
if (PropertyChanged == null) return;
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(ThumbRealWidth)));
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(ThumbRealHeight)));
}
private void MainWin_Drop(object sender, DragEventArgs e)
{
var paths = (string[])e.Data.GetData(DataFormats.FileDrop);
if (paths == null || paths.Length == 0) return;
Task.Run(() => LoadPath(paths[0]));
}
private void TV1_MouseDown(object sender, MouseButtonEventArgs e) {
if (!e.Source.Equals(sender)) return;
if (e.ClickCount == 1 && e.ChangedButton == MouseButton.Right) {
Nav_Up(null, null);
e.Handled = true;
}
else if (e.ClickCount == 2 && e.ChangedButton == MouseButton.Left) {
openFolderPrompt();
e.Handled = true;
}
}
#endregion
#region Private Helper Methods
private void openFolderPrompt() {
Helpers.OpenFolderDialog(this, path => Task.Run(() => LoadPath(path)));
}
private void Callback_AddToImageList(ObjectInfo objInfo) {
//var add = true;
//Helpers.UpdateSourcePaths(objInfo);
//Dispatcher.Invoke(() => {
// if (AuxVisibility == Visibility.Collapsed && (objInfo.SourcePaths == null || objInfo.SourcePaths.Length == 0))
// add = false;
//});
//if (add) ObjectList.Add(objInfo);
if (Setting.ImmersionMode && objInfo.SourcePaths == null) {
Helpers.UpdateSourcePaths(objInfo);//update needed to exclude items that do not have thumbs
if (objInfo.SourcePaths == null || objInfo.SourcePaths.Length == 0)
return;
}
ObjectList.Add(objInfo);
}
private void clearObjectList() {
foreach (var objInfo in ObjectList) {
objInfo.SourcePaths = null;
objInfo.ImageSource = null;
}
ObjectList.Clear();
Dispatcher.Invoke(() => virWrapPanel.ScrollOwner.ScrollToTop());
}
#endregion
#region Load Methods
/// <summary>
/// Display file system objects in the path as thumbnails, or open viewer depending on the file type and parameters.
/// Main open logic is set here.
/// Support cancellation. Used in Task.
/// Flags are inferred from path. Not for opening image in an archive.
/// </summary>
internal void LoadPath(string path, ViewWindow viewWin = null) {
LoadPath(new ObjectInfo(path), viewWin);
}
/// <summary>
/// Display file system objects in the path as thumbnails, or open viewer depending on the file type and parameters.
/// Main open logic is set here.
/// Support cancellation. Used in Task.
/// </summary>
internal void LoadPath(ObjectInfo objInfo, ViewWindow viewWin = null) {
//infer path type (flags)
if (objInfo.Flags == FileFlags.Unknown)
objInfo.Flags = Helpers.GetPathType(new DirectoryInfo(objInfo.FileSystemPath));
// action based on flags
if (objInfo.Flags.HasFlag(FileFlags.Directory)) {
//directory -> load thumbs
try {
tknSrc_LoadThumb?.Cancel();
Monitor.Enter(lock_LoadThumb);
tknSrc_LoadThumb = new CancellationTokenSource();
CurrentPath = objInfo.FileSystemPath;
clearObjectList();
LoadFolder(new DirectoryInfo(objInfo.FileSystemPath), tknSrc: tknSrc_LoadThumb);
}
finally {
tknSrc_LoadThumb = null;
Monitor.Exit(lock_LoadThumb);
}
}
else if (objInfo.Flags.HasFlag(FileFlags.Archive)) {
if (objInfo.Flags.HasFlag(FileFlags.Image)) {
//image inside archive -> open viewer
LoadFile(new LoadOptions(objInfo.FileSystemPath) {
Flags = objInfo.Flags,
LoadImage = true,
FileNames = new[] { objInfo.FileName },
CldInfoCallback = oi => Dispatcher.Invoke(() => {
if (viewWin == null) new ViewWindow(this) { ObjectInfo = oi }.Show();
else viewWin.ObjectInfo = oi;
}),
});
}
else {
//archive itself -> extract and load thumbs
try {
tknSrc_LoadThumb?.Cancel();
Monitor.Enter(lock_LoadThumb);
tknSrc_LoadThumb = new CancellationTokenSource();
CurrentPath = objInfo.FileSystemPath;
clearObjectList();
LoadFile(new LoadOptions(objInfo.FileSystemPath) {
Flags = objInfo.Flags,
LoadImage = true,
DecodeSize = (SizeInt)Setting.ThumbnailSize,
CldInfoCallback = Callback_AddToImageList,
}, tknSrc_LoadThumb);
}
finally {
tknSrc_LoadThumb = null;
Monitor.Exit(lock_LoadThumb);
}
}
}
else if (objInfo.Flags.HasFlag(FileFlags.Image)) {
//plain image file -> open viewer
LoadFile(new LoadOptions(objInfo.FileSystemPath) {
Flags = objInfo.Flags,
LoadImage = true,
ObjInfoCallback = oi => Dispatcher.Invoke(() => {
if (viewWin == null) new ViewWindow(this) { ObjectInfo = oi }.Show();
else viewWin.ObjectInfo = oi;
}),
});
}
}
/// <summary>
/// Load thumbnails in folder.
/// </summary>
private void LoadFolder(DirectoryInfo dirInfo, CancellationTokenSource tknSrc = null) {
CurrentPath = dirInfo.FullName;
foreach (var childInfo in dirInfo.EnumerateFileSystemInfos()) {
if (tknSrc?.IsCancellationRequested == true) return;
var flag = Helpers.GetPathType(childInfo);
Callback_AddToImageList(new ObjectInfo(childInfo.FullName, flag) {
FileName = childInfo.Name,
});
//throttle the load
if (flag != FileFlags.Unknown) Thread.Sleep(50);
//how to sleep only when childinfo thumbnail is loading images?
}
}
/// <summary>
/// Load image based on the type of file and try passwords when possible.
/// <para>
/// If filePath points to an archive, ObjectInfo.Flags in ObjInfoCallback will contain FileFlag.Error when extraction fails.
/// ObjectInfo.SourcePaths in ObjInfoCallback contains the file list inside archive.
/// ObjectInfo in CldInfoCallback contains information for files inside archive.
/// </para>
/// Should be called from a background thread.
/// Callback can be used to manipulate the loaded images. For e.g. display it in the ViewWindow, or add to ObjectList as thumbnails.
/// Callback is called for each image loaded.
/// Use Dispatcher if callback needs to access the UI thread.
/// <param name="flags">Only checks for Image and Archive.</param>
/// </summary>
internal static void LoadFile(LoadOptions options, CancellationTokenSource tknSrc = null)
{
if (tknSrc?.IsCancellationRequested == true) return;
//objInfo to be returned
var objInfo = new ObjectInfo(options.FilePath, options.Flags) {
FileName = Path.GetFileName(options.FilePath)
};
//when file is an image
if (options.Flags.HasFlag(FileFlags.Image) && !options.Flags.HasFlag(FileFlags.Archive)) {
objInfo.SourcePaths = new[] { options.FilePath };
if (options.LoadImage)
objInfo.ImageSource = Helpers.GetImageSource(options.FilePath, options.DecodeSize);
}
//when file is an archive
else if (options.Flags.HasFlag(FileFlags.Archive)) {
//some files may get loaded from cache therefore unaware of whether password is correct
//the HashSet records processed files through retries
var done = new HashSet<string>();
for (int caseIdx = 0; caseIdx < 4; caseIdx++) {
if (tknSrc?.IsCancellationRequested == true) break;
var success = false;
switch (caseIdx) {
//first check if there is a match in saved passwords
case 0 when Setting.MappedPasswords.Rows.Find(options.FilePath) is DataRow row:
options.Password = (string)row[nameof(Column.Password)];
success = ExtractZip(options, objInfo, done, tknSrc);
break;
//then try no password
case 1:
options.Password = null;
success = ExtractZip(options, objInfo, done, tknSrc);
break;
//then try all saved passwords with no filename
case 2:
foreach (var fp in Setting.FallbackPasswords) {
options.Password = fp;
success = ExtractZip(options, objInfo, done, tknSrc);
if (success) break;
}
break;
case 3:
//if all fails, prompt for password then extract with it
if (options.LoadImage &&
(options.FileNames == null || options.DecodeSize == default)) {
//ask for password when opening explicitly the archive or opening viewer for images inside archive
while (!success) {
string pwd = null;
bool isFb = true;
Application.Current.Dispatcher.Invoke(() => {
var win = new InputWindow();
if (win.ShowDialog() == true) {
pwd = win.TB_Password.Text;
isFb = win.CB_Fallback.IsChecked == true;
}
win.Close();
});
if (!string.IsNullOrEmpty(pwd)) {
options.Password = pwd;
success = ExtractZip(options, objInfo, done, tknSrc);
if (success) {
//make sure the password is saved when task is cancelled
Setting.MappedPasswords.UpdateDataTable(options.FilePath, nameof(Column.Password), pwd);
if (isFb) {
Setting.FallbackPasswords[pwd] = new Observable<string>(pwd);
}
break;
}
}
else break;
}
}
if (!success) {
objInfo.Flags |= FileFlags.Error;
objInfo.Comments = $"Extraction failed. Bad password or not supported image formats.";
}
break;
}
if (success) break;
}
}
if (tknSrc?.IsCancellationRequested == true) return;
options.ObjInfoCallback?.Invoke(objInfo);
}
//private static bool ExtractZip(LoadOptions options, ObjectInfo objInfo, CancellationTokenSource tknSrc = null)
//{
// if (tknSrc?.IsCancellationRequested == true) return false;
// using (var fs = new FileStream(options.FilePath, FileMode.Open, FileAccess.Read)) {
// return ExtractZip(fs, options, objInfo, tknSrc);
// }
//}
/// <summary>
/// Returns true or false based on whether extraction succeeds.
/// </summary>
private static bool ExtractZip(LoadOptions options, ObjectInfo objInfo, HashSet<string> done, CancellationTokenSource tknSrc = null)
{
var success = false;
if (tknSrc?.IsCancellationRequested == true) return success;
SevenZipExtractor ext = null;
try
{
ext = options.Password?.Length > 0 ? new SevenZipExtractor(options.FilePath, options.Password) :
new SevenZipExtractor(options.FilePath);
var isThumb = options.DecodeSize.Width + options.DecodeSize.Height > 0;
bool fromDisk = false;
//get files in archive to extract
string[] toDo;
if (options.FileNames?.Length > 0)
toDo = options.FileNames;
else
toDo = ext.ArchiveFileData
.Where(d => !d.IsDirectory && Helpers.GetPathType(d.FileName) == FileFlags.Image)
.Select(d => d.FileName).ToArray();
foreach (var fileName in toDo) {
if (tknSrc?.IsCancellationRequested == true) break;
//skip if already done
if (done.Contains(fileName)) continue;
ImageSource source = null;
if (options.LoadImage) {
var thumbPathInDb = Path.Combine(options.FilePath, fileName);
if (isThumb) {
//try load from cache
source = SQLiteHelper.GetFromThumbDB(thumbPathInDb, options.DecodeSize);
}
if (source == null) {
#if DEBUG
Console.WriteLine("Extracting " + fileName);
#endif
fromDisk = true;
//load from disk
using (var ms = new MemoryStream()) {
ext.ExtractFile(fileName, ms);
success = true; //if the task is cancelled, success info is still returned correctly.
source = Helpers.GetImageSource(ms, options.DecodeSize);
}
if (isThumb && source != null) SQLiteHelper.AddToThumbDB(source, thumbPathInDb, options.DecodeSize);
}
}
if (options.CldInfoCallback != null) {
var cldInfo = new ObjectInfo(options.FilePath, FileFlags.Image | FileFlags.Archive) {
FileName = fileName,
SourcePaths = new[] { fileName },
ImageSource = source,
};
options.CldInfoCallback.Invoke(cldInfo);
}
done.Add(fileName);
}
//update objInfo
objInfo.SourcePaths = toDo;
//save password for the future
if (fromDisk && options.Password?.Length > 0) {
Setting.MappedPasswords.UpdateDataTable(options.FilePath, nameof(Column.Password), options.Password);
}
return true; //it is considered successful if the code reaches here
}
catch (Exception ex)
{
if (ex is ExtractionFailedException ||
ex is SevenZipArchiveException ||
ex is NotSupportedException) return false;
if (ext != null) {
ext.Dispose();
ext = null;
}
throw;
}
finally
{
if (ext != null) {
ext.Dispose();
ext = null;
}
}
}
#endregion
#region Misc Event Handlers
private void TN1_Click(object sender, MouseButtonEventArgs e) {
if (e.Source.Equals(sender)) {
e.Handled = true;
var tn = (Thumbnail)sender;
var objInfo = tn.ObjectInfo;
if (e.ClickCount != 1) return;
switch (e.ChangedButton) {
case MouseButton.Left:
Task.Run(() => LoadPath(objInfo));
break;
case MouseButton.Right:
App.ContextMenuWin.Owner = this;
App.ContextMenuWin.ObjectInfo = objInfo;
App.ContextMenuWin.FadeIn();
break;
}
}
}
private void Nav_Up(object sender, RoutedEventArgs e) {
Task.Run(() => LoadPath(Path.GetDirectoryName(CurrentPath)));
}
private void Sidebar_Click(object sender, RoutedEventArgs e) {
switch (((FrameworkContentElement)sender).Name) {
case nameof(HY_Open):
openFolderPrompt();
break;
case nameof(HY_Options):
var win = new SettingsWindow(this);
win.ShowDialog();
win.Close();
break;
case nameof(HY_ImmersionMode):
Setting.ImmersionMode = !Setting.ImmersionMode;
if (Setting.ImmersionMode)
{
virWrapPanel.ScrollOwner.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
if (WindowState == WindowState.Maximized) {
//go fullscreen if alreayd maximized
WindowState = WindowState.Normal;
lastWindowRect = new Rect(Left, Top, Width, Height);
var info = NativeHelpers.GetMonitorFromWindow(this);
Top = info.Top;
Left = info.Left;
Width = info.Width;
Height = info.Height;
}
}
else
{
virWrapPanel.ScrollOwner.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
if (lastWindowRect.Size.Width + lastWindowRect.Size.Height > 0) {
Top = lastWindowRect.Top;
Left = lastWindowRect.Left;
Width = lastWindowRect.Width;
Height = lastWindowRect.Height;
}
}
break;
case nameof(HY_Close):
Close();
break;
}
}
protected override void OnStateChanged(EventArgs e) {
//override state behavior to enable fullscreen in immersion mode
if (WindowState == WindowState.Maximized && Setting.ImmersionMode) {
WindowState = WindowState.Normal;
var info = NativeHelpers.GetMonitorFromWindow(this);
if (Top == info.Top && Left == info.Left && Width == info.Width && Height == info.Height) {
//restore
Top = lastWindowRect.Top;
Left = lastWindowRect.Left;
Width = lastWindowRect.Width;
Height = lastWindowRect.Height;
}
else {
//fullscreen
lastWindowRect = new Rect(Left, Top, Width, Height);
Top = info.Top;
Left = info.Left;
Width = info.Width;
Height = info.Height;
}
}
}
//private void CTM_Click(object sender, RoutedEventArgs e) {
// var mi = (MenuItem)sender;
// var ctm = (ContextMenu)mi.CommandParameter;
// switch (mi.DataContext) {
// case ObjectInfo oi:
// switch (mi.Header) {
// case "View in Explorer":
// Helpers.Run("explorer", $"/select, \"{oi.FileSystemPath}\"");
// break;
// case "Open in New Window":
// if (oi.Flags.HasFlag(FileFlags.Image)) {
// LoadPath(oi);
// }
// else if (oi.Flags.HasFlag(FileFlags.Directory) ||
// oi.Flags.HasFlag(FileFlags.Archive)) {
// var win = new MainWindow {
// InitialPath = oi.FileSystemPath
// };
// win.Show();
// }
// break;
// }
// break;
// //case ObservablePair<string, string> op:
// // Helpers.Run(op.Item1, Helpers.CustomCmdArgsReplace(op.Item2, tn.ObjectInfo));
// // break;
// }
//}
//private void CTM_Opened(object sender, RoutedEventArgs e) {
// //var ctm = (ContextMenu)sender;
// //var tn = (Thumbnail)ctm.PlacementTarget;
// //ctm.Tag = tn.ObjectInfo.FileSystemPath;
// //foreach (MenuItem item in mi.Items) {
// // var op = (ObservablePair<string, string>)item.DataContext;
// // item.DataContext = new ObservablePair<string, string>(op.Item1, op.Item2.Replace(@"%FileSystemPath%", tn.ObjectInfo.FileSystemPath));
// //}
//}
#endregion
}
}