-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContainerSettings.xaml.cs
245 lines (207 loc) · 8.72 KB
/
ContainerSettings.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Desktop_Container
{
/// <summary>
/// Logique d'interaction pour ContainerSettings.xaml
/// </summary>
public partial class ContainerSettings : Window
{
long ownerTimestamp;
List<string> positionsContainers = new();
readonly string saveDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\DesktopContainer";
public ContainerSettings(long ownerTimestampPassed)
{
InitializeComponent();
Settings_Version.Text = "v" + Assembly.GetExecutingAssembly().GetName().Version.ToString();
ownerTimestamp = ownerTimestampPassed;
// Récupération des fichiers Container pour le dropdown
string saveDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\DesktopContainer";
if (!Directory.Exists(saveDirectory))
Directory.CreateDirectory(saveDirectory);
List<string> savedContainer = Directory.GetFiles(saveDirectory, "*.json").ToList();
List<string> backedUpContainer = Directory.GetFiles(saveDirectory, "*.json.backup").ToList();
List<string> activeContainerTS = new();
foreach (string container in savedContainer)
{
StreamReader r = new(container);
string json = r.ReadToEnd();
if (json != "")
{
List<List<string>> save_datas = JsonSerializer.Deserialize<List<List<string>>>(json) ?? [];
r.Close();
string name = save_datas[0][0];
positionsContainers.Add(save_datas[0][5]);
string timestampText = container.Split("\\")[^1].Split("container")[1].Split(".")[0];
CmbBox_ContainersList.Items.Add(name + " [" + timestampText + "]");
activeContainerTS.Add(timestampText);
}
}
foreach (string container in backedUpContainer)
{
StreamReader r = new(container);
string json = r.ReadToEnd();
if (json != "")
{
List<List<string>> save_datas = JsonSerializer.Deserialize<List<List<string>>>(json) ?? [];
r.Close();
string name = save_datas[0][0];
string timestampText = container.Split("\\")[^1].Split("container")[1].Split(".")[0];
if (!activeContainerTS.Contains(timestampText))
{
ComboBoxItem item = new()
{
Foreground = Brushes.Firebrick,
Content = "(BACKUP) " + name + " [" + timestampText + "]"
};
CmbBox_ContainersList.Items.Add(item);
}
}
}
}
private void BtnCloseAll_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void Btn_CloseSettings_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void BtnReload_Click(object sender, RoutedEventArgs e)
{
Process.Start(Process.GetCurrentProcess().MainModule.FileName);
Application.Current.Shutdown();
}
int containerIndex;
string selectedTimestamp = "";
private void CmbBox_ContainersList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string selectedValue = CmbBox_ContainersList.SelectedItem.ToString() ?? "";
selectedTimestamp = selectedValue.Split("[")[1].Replace("]","");
if (selectedValue.Contains("(BACKUP)"))
{
btnBackup.IsEnabled = false;
}
else
{
btnBackup.IsEnabled = true;
}
if (File.Exists(saveDirectory + @"\container" + selectedTimestamp + ".json.backup"))
{
btnRestore.IsEnabled = true;
}
else
{
btnRestore.IsEnabled = false;
}
containerIndex = CmbBox_ContainersList.SelectedIndex;
Update_Checkboxes();
}
private void BtnBackup_Click(object sender, RoutedEventArgs e)
{
File.Copy(saveDirectory + @"\container" + selectedTimestamp + ".json", saveDirectory + @"\container" + selectedTimestamp + ".json.backup", true);
}
private void BtnOpenSaveLocation_Click(object sender, RoutedEventArgs e)
{
Process.Start("explorer.exe", saveDirectory);
}
private void BtnRestore_Click(object sender, RoutedEventArgs e)
{
File.Delete(saveDirectory + @"\container" + selectedTimestamp + ".json");
File.Copy(saveDirectory + @"\container" + selectedTimestamp + ".json.backup", saveDirectory + @"\container" + selectedTimestamp + ".json", true);
btnReload.Background = Brushes.LightGreen;
}
private void Radio_AnchorTop_Checked(object sender, RoutedEventArgs e)
{
PositionAnchorEdit();
}
private void Radio_AnchorLeft_Checked(object sender, RoutedEventArgs e)
{
PositionAnchorEdit();
}
private void Radio_AnchorRight_Checked(object sender, RoutedEventArgs e)
{
PositionAnchorEdit();
}
private void Radio_AnchorBottom_Checked(object sender, RoutedEventArgs e)
{
PositionAnchorEdit();
}
private void PositionAnchorEdit()
{
int positionHori = 0;
int positionVert = 0;
if (Radio_AnchorLeft.IsChecked == true)
{
positionHori = (int)(Owner as MainWindow).PointToScreen(new Point(0, 0)).X;
}
else if (Radio_AnchorRight.IsChecked == true)
{
positionHori = -(int)(SystemParameters.PrimaryScreenWidth - (Owner as MainWindow).PointToScreen(new Point((Owner as MainWindow).Width, 0)).X);
}
if (Radio_AnchorTop.IsChecked == true)
{
positionVert = (int)(Owner as MainWindow).PointToScreen(new Point(0, 0)).Y;
}
else if (Radio_AnchorBottom.IsChecked == true)
{
positionVert = -(int)(SystemParameters.PrimaryScreenHeight - (Owner as MainWindow).PointToScreen(new Point(0, (Owner as MainWindow).Height)).Y);
}
string position = positionHori + "," + positionVert;
StreamReader r = new(saveDirectory + @"\container" + selectedTimestamp + ".json");
string json = r.ReadToEnd();
if (json != "")
{
List<List<string>> save_datas = JsonSerializer.Deserialize<List<List<string>>>(json) ?? [];
r.Close();
save_datas[0][5] = position;
var jsonNew = JsonSerializer.Serialize(save_datas);
File.WriteAllText(saveDirectory + @"\container" + selectedTimestamp + ".json", jsonNew);
}
}
private void Window_Activated(object sender, EventArgs e)
{
ItemCollection listContainer = CmbBox_ContainersList.Items;
foreach (string container in listContainer)
{
if (container.Contains(ownerTimestamp.ToString()))
{
CmbBox_ContainersList.SelectedItem = container;
break;
}
}
Update_Checkboxes();
}
private void Update_Checkboxes()
{
if(CmbBox_ContainersList.SelectedIndex != -1)
{
string posX = positionsContainers[containerIndex].Split(",")[0];
string posY = positionsContainers[containerIndex].Split(",")[1];
if (posX[0] == '-')
Radio_AnchorRight.IsChecked = true;
else
Radio_AnchorLeft.IsChecked = true;
if (posY[0] == '-')
Radio_AnchorBottom.IsChecked = true;
else
Radio_AnchorTop.IsChecked = true;
}
else
{
Radio_AnchorTop.IsChecked = false;
Radio_AnchorLeft.IsChecked = false;
Radio_AnchorRight.IsChecked = false;
Radio_AnchorBottom.IsChecked = false;
}
}
}
}