-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
252 lines (212 loc) · 6.29 KB
/
MainWindow.xaml.cs
File metadata and controls
252 lines (212 loc) · 6.29 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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WallpaperEngineFileUnistaller
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private string workingDirectoryPath;
private string workingWallpaperDirectoryPath;
public MainWindow()
{
InitializeComponent();
WallpaperEngine_WallpaperBrowser.DisplayMemberPath = "Name";
WallpaperEngine_WallpaperBrowser.SelectionChanged += WallpaperEngine_WallpaperBrowser_SelectionChanged;
}
private void WallpaperEngine_WallpaperBrowser_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
WallpaperItem wItem = (WallpaperItem)WallpaperEngine_WallpaperBrowser.SelectedItem;
if (wItem != null)
{
string[] dataFromWallpapaer = Directory.GetFiles(wItem.DirectoryPath);
foreach (string wallpaperFile in dataFromWallpapaer)
{
FileInfo file = new FileInfo(wallpaperFile);
workingWallpaperDirectoryPath = file.Directory.FullName;
if (file.Name.Split(".")[0] == "preview")
{
if (!string.IsNullOrWhiteSpace(wallpaperFile))
{
try
{
using (var stream = File.Open(wallpaperFile, FileMode.Open))
{
using (System.Drawing.Image image = System.Drawing.Image.FromStream(stream))
{
Bitmap bmp = (Bitmap)image;
using (var memory = new MemoryStream())
{
bmp.Save(memory, ImageFormat.Png);
memory.Position = 0;
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();
WallpaperEngine_WallpaperPreview.Source = bitmapImage;
}
}
}
}
catch (Exception) { }
try
{
string fileJson = File.ReadAllText(System.IO.Path.Combine(file.DirectoryName, "project.json"));
WallpaperEngine_JsonBox.Text = fileJson;
}
catch (Exception exception)
{
WallpaperEngine_JsonBox.Text = "Couldn't parse this wallpaper project.json\n\nException: " + exception.Message;
}
break;
}
else
{
WallpaperEngine_WallpaperPreview.Source = null;
try
{
SetImageIfError();
}
catch (Exception)
{ }
}
}
else
{
WallpaperEngine_WallpaperPreview.Source = null;
try
{
SetImageIfError();
}
catch (Exception)
{ }
}
}
}
}
catch (Exception) {}
}
private void SetImageIfError()
{
WallpaperEngine_WallpaperPreview.Source = GenerateRandomImage();
}
private void WallpaperEngine_SelectWorkFolder(object sender, RoutedEventArgs e)
{
var dialog = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog();
if (dialog.ShowDialog(this).GetValueOrDefault())
{
workingDirectoryPath = dialog.SelectedPath;
ParseFilesToBrowser();
}
}
private void ParseFilesToBrowser()
{
try
{
string[] foldersInWorkshopContent = Directory.GetDirectories(System.IO.Path.Combine(workingDirectoryPath, "content"));
WallpaperEngine_WallpaperBrowser.Items.Clear();
foreach (string folderFromWorkshopContent in foldersInWorkshopContent)
{
string[] foldersInWorkshopContentDirectory = Directory.GetDirectories(folderFromWorkshopContent);
foreach (string wallpaperFolder in foldersInWorkshopContentDirectory)
{
string displayName = new DirectoryInfo(wallpaperFolder).Name;
try
{
string json = File.ReadAllText(System.IO.Path.Combine(wallpaperFolder, "project.json"));
dynamic data = JsonConvert.DeserializeObject(json);
displayName += $": {data.title}";
}
catch (Exception exception) { displayName += $": \t\t\t\t(File project.json not found)"; }
WallpaperEngine_WallpaperBrowser.Items.Add(new WallpaperItem(displayName, wallpaperFolder));
}
}
}
catch (Exception)
{
}
}
class WallpaperItem
{
public string Name { get; set; }
public string DirectoryPath { get; set; }
public WallpaperItem(string name, string path)
{
Name = name;
DirectoryPath = path;
}
}
private void WallpaperEngine_OpenWallpaperInExplorer(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(workingWallpaperDirectoryPath)) Process.Start("explorer.exe", workingWallpaperDirectoryPath);
}
private BitmapImage GenerateRandomImage()
{
int width = 200, height = 200;
//bitmap
Bitmap bmp = new Bitmap(width, height);
//random number
Random rand = new Random();
//create random pixels
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
//generate random ARGB value
int a = rand.Next(256);
int r = rand.Next(256);
int g = rand.Next(256);
int b = rand.Next(256);
//set ARGB value
bmp.SetPixel(x, y, System.Drawing.Color.FromArgb(a, r, g, b));
}
}
using (var memory = new MemoryStream())
{
bmp.Save(memory, ImageFormat.Png);
memory.Position = 0;
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();
return bitmapImage;
}
}
private void WallpaperEngine_DeleteWallpaper(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(workingWallpaperDirectoryPath))
{
DirectoryInfo dir = new DirectoryInfo(workingWallpaperDirectoryPath);
WallpaperEngine_WallpaperPreview.Source = GenerateRandomImage();
WallpaperEngine_JsonBox.Text = string.Empty;
dir.Delete(true);
ParseFilesToBrowser();
}
}
}
}