-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
248 lines (214 loc) · 9.06 KB
/
MainWindow.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
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
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;
using Microsoft.Win32;
using Octokit;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace BetterDesktop
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
string owner = "ElectroGamesYT";
string repo = "BetterDesktopImages";
string branch = "main";
bool started = false;
string settingsPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), ".settings");
public MainWindow()
{
InitializeComponent();
CreateSettings();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
string? category = GetSetting("Category").ToString();
if (category != null)
{
Button? button = FindName(category.ToLower() + "Button") as Button;
if (button != null && category != null && category != "None")
{
HighlightSelectedCategory(button, true);
}
}
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed) DragMove();
}
private void homeBtnMinimize_Click(object sender, RoutedEventArgs e)
{
WindowState = WindowState.Minimized;
}
private void homeBtnClose_Click(object sender, RoutedEventArgs e)
{
System.Windows.Application.Current.Shutdown();
}
private void btnStartStop_Click(object sender, RoutedEventArgs e)
{
started = !started;
if (started)
{
// popup messaging saying "Downloading images". When done say "You may now exit Better Desktop"
//GithubDownloadImages(1);
}
else
{
// Popup button saying "Click here to reset desktop background"
}
}
private void btnAnimalsCategory_Click(object sender, RoutedEventArgs e)
{
HighlightSelectedCategory(animalsButton);
SetSetting("Category", "Animals");
}
private void btnNatureCategory_Click(object sender, RoutedEventArgs e)
{
HighlightSelectedCategory(natureButton);
SetSetting("Category", "Nature");
}
private void btnTravelCategory_Click(object sender, RoutedEventArgs e)
{
HighlightSelectedCategory(travelButton);
SetSetting("Category", "Travel");
}
private void btnAbstractCategory_Click(object sender, RoutedEventArgs e)
{
HighlightSelectedCategory(abstractButton);
SetSetting("Category", "Abstract");
}
private void btnTechnologyCategory_Click(object sender, RoutedEventArgs e)
{
HighlightSelectedCategory(technologyButton);
SetSetting("Category", "Technology");
}
private void btnMinimalCategory_Click(object sender, RoutedEventArgs e)
{
HighlightSelectedCategory(minimalButton);
SetSetting("Category", "Minimal");
}
private void btnArtCategory_Click(object sender, RoutedEventArgs e)
{
HighlightSelectedCategory(artButton);
SetSetting("Category", "Art");
}
private void btnInspirationalCategory_Click(object sender, RoutedEventArgs e)
{
HighlightSelectedCategory(inspirationalButton);
SetSetting("Category", "Inspirational");
}
private void btnCustomCategory_Click(object sender, RoutedEventArgs e)
{
HighlightSelectedCategory(customButton);
SetSetting("Category", "Custom");
}
private async void GithubDownloadImages(string type, int amount)
{
GitHubClient client = new GitHubClient(new ProductHeaderValue("BetterDesktop"));
IReadOnlyList<RepositoryContent> contents = await client.Repository.Content.GetAllContents(owner, repo, type);
string backgroundsPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Backgrounds");
if (contents != null)
{
Random random = new Random();
List<int> allNumbers = Enumerable.Range(0, contents.Count).ToList();
List<int> uniqueNumbers = new List<int>();
for (int i = 0; i < amount; i++)
{
int index = random.Next(0, allNumbers.Count);
allNumbers.RemoveAt(index);
uniqueNumbers.Add(allNumbers[index]);
}
foreach (int number in uniqueNumbers)
{
byte[] imageContent = await client.Repository.Content.GetRawContent(owner, repo, contents[number].Path);
if (!Directory.Exists(backgroundsPath)) Directory.CreateDirectory(backgroundsPath);
File.WriteAllBytes(System.IO.Path.Combine(backgroundsPath, contents[number].Name), imageContent);
}
}
else
{
// Category cannot be found or there is an issue with the internet connection
}
}
private void HighlightSelectedCategory(Button button, bool highlightingCurrent = false)
{
string? currentCategory = GetSetting("Category").ToString();
if (!highlightingCurrent && currentCategory != null && currentCategory != "None")
{
Button? btn = FindName(currentCategory.ToLower() + "Button") as Button;
if (btn != null)
{
Ellipse? eli = btn.Template.FindName("ellipse", btn) as Ellipse;
if (eli != null)
{
eli.Stroke = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF962484"));
eli.StrokeThickness = 1.5f;
}
}
}
Ellipse? ellipse = button.Template.FindName("ellipse", button) as Ellipse;
if (ellipse != null)
{
ellipse.Stroke = new SolidColorBrush(Colors.Yellow);
ellipse.StrokeThickness = 2.5f;
}
}
private object? GetSetting(string key)
{
CreateSettings();
Dictionary<string, object> deserializedData = new DeserializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance).Build().Deserialize<Dictionary<string, object>>(File.ReadAllText(settingsPath));
if (deserializedData.TryGetValue(key, out object? value)) return value;
else return null;
}
private void SetSetting(string key, object value)
{
CreateSettings();
Dictionary<string, object> deserializedData = new DeserializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance).Build().Deserialize<Dictionary<string, object>>(File.ReadAllText(settingsPath));
deserializedData[key] = value;
string yaml = new SerializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance).Build().Serialize(deserializedData);
File.WriteAllText(settingsPath, yaml);
}
private void CreateSettings()
{
if (File.Exists(settingsPath)) return;
//File.Create(settingsPath);
Dictionary<string, object> settingsData = new Dictionary<string, object>
{
{ "OldBackgroundPath", "None" },
{ "Category", "None" },
{ "CustomPath", "None" },
{ "Started", false },
{ "UpdateType", 0 }
};
string yaml = new SerializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance).Build().Serialize(settingsData);
File.WriteAllText(settingsPath, yaml);
}
private void SaveCurrentBackground()
{
}
private string? GetCurrentBackground()
{
return Registry.GetValue(@"HKEY_CURRENT_USER\Control Panel\Desktop", "Wallpaper", null) as string;
}
private string? SetBackground()
{
return Registry.GetValue(@"HKEY_CURRENT_USER\Control Panel\Desktop", "Wallpaper", null) as string;
}
}
}