-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathContextMenuWindow.xaml.cs
139 lines (125 loc) · 5.24 KB
/
ContextMenuWindow.xaml.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using static ZipImageViewer.LoadHelper;
using static ZipImageViewer.Helpers;
namespace ZipImageViewer
{
public partial class ContextMenuWindow : RoundedWindow, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public MainWindow MainWin { get; set; }
public ContextMenuWindow() {
InitializeComponent();
}
private ObjectInfo objectInfo;
public ObjectInfo ObjectInfo {
get => objectInfo;
set {
if (objectInfo == value) return;
objectInfo = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ObjectInfo)));
Task.Run(() => {
var imgInfo = getImageInfo(ObjectInfo);
Dispatcher.Invoke(() => ImageInfo = imgInfo);
});
}
}
public ImageInfo ImageInfo {
get { return (ImageInfo)GetValue(ImageInfoProperty); }
set { SetValue(ImageInfoProperty, value); }
}
public static readonly DependencyProperty ImageInfoProperty =
DependencyProperty.Register("ImageInfo", typeof(ImageInfo), typeof(ContextMenuWindow), new PropertyMetadata(null));
private static ImageInfo getImageInfo(ObjectInfo objInfo) {
if (objInfo == null) return null;
var imgInfo = new ImageInfo();
try {
if (objInfo.Flags == (FileFlags.Archive | FileFlags.Image)) {
ExtractFile(objInfo.FileSystemPath, objInfo.FileName, (fileInfo, stream) => {
imgInfo.Created = fileInfo.CreationTime;
imgInfo.Modified = fileInfo.LastWriteTime;
imgInfo.FileSize = (long)fileInfo.Size;
UpdateImageInfo(stream, imgInfo);
});
}
else {
var fileInfo = new FileInfo(objInfo.FileSystemPath);
imgInfo.Created = fileInfo.CreationTime;
imgInfo.Modified = fileInfo.LastWriteTime;
if (!fileInfo.Attributes.HasFlag(FileAttributes.Directory)) {
imgInfo.FileSize = fileInfo.Length;
if (objInfo.Flags.HasFlag(FileFlags.Image)) {
using (var stream = new FileStream(objInfo.FileSystemPath, FileMode.Open, FileAccess.Read)) {
UpdateImageInfo(stream, imgInfo);
}
}
}
}
}
catch { }
return imgInfo;
}
private void Menu_PreviewMouseDown(object sender, MouseButtonEventArgs e) {
if (ObjectInfo == null) return;
if (sender is Border border) {
switch (border.Name) {
case nameof(B_OpenWithDefaultApp):
Cmd_OpenWithDefaultApp(ObjectInfo.FileSystemPath);
break;
case nameof(B_OpenInExplorer):
Cmd_OpenInExplorer(ObjectInfo.FileSystemPath);
break;
case nameof(B_OpenInNewWindow):
Cmd_OpenInNewWindow(ObjectInfo, MainWin);
break;
case nameof(B_CacheFirst):
CacheHelper.CachePath(ObjectInfo.ContainerPath, true);
break;
case nameof(B_CacheAll):
CacheHelper.CachePath(ObjectInfo.ContainerPath, false);
break;
case nameof(B_Slideshow):
new SlideshowWindow(ObjectInfo.ContainerPath).Show();
break;
}
}
else if (sender is ContentPresenter cp && cp.Content is ObservableObj obsObj) {
Run(obsObj.Str2, CustomCmdArgsReplace(obsObj.Str3, ObjectInfo));
}
Close();
e.Handled = true;
}
private void CTMWin_FadedOut(object sender, RoutedEventArgs e) {
ObjectInfo = null;
ImageInfo = null;
MainWin = null;
}
#region Context Menu Commands
internal static void Cmd_OpenWithDefaultApp(string fsPath) {
Run("explorer", fsPath);
}
internal static void Cmd_OpenInExplorer(string fsPath) {
Run("explorer", $"/select, \"{fsPath}\"");
}
internal static void Cmd_OpenInNewWindow(ObjectInfo objInfo, MainWindow mainWin = null) {
if (objInfo.Flags.HasFlag(FileFlags.Image)) {
mainWin?.LoadPath(objInfo);
}
else if (objInfo.Flags.HasFlag(FileFlags.Directory) ||
objInfo.Flags.HasFlag(FileFlags.Archive)) {
var win = new MainWindow {
InitialPath = objInfo.FileSystemPath
};
win.Show();
}
}
#endregion
}
}