This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
414 lines (362 loc) · 12.5 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
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
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Web;
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 Art_queue_maker
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public partial class MainWindow : Window
{
private static readonly Stack<string> undobuffer = new();
private static string filename = "Untitled";
private static bool saved = true;
private static string ticker = "";
public static string WindowTitle { get { return filename + ticker + " - Art Queue Maker"; } } //filename + ticker + " - Art Queue Maker"
public MainWindow()
{
InitializeComponent();
DataContext = this;
saved = true;
}
private void NewMenu_Click(object sender, RoutedEventArgs e)
{
ClearList();
}
private void ClearList()
{
if (ConfirmSaved())
{
filename = "Untitled";
Title = WindowTitle;
while (LinkList.Items.Count > 0)
{
LinkList.Items.Remove(0);
}
while (undobuffer.Count > 0)
{
_ = undobuffer.Pop();
}
}
}
private void OpenMenu_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new()
{
// Set filter for file extension and default file extension
DefaultExt = ".txt",
Filter = "Text files (*.txt)|*.txt"
};
// Display OpenFileDialog by calling ShowDialog method
bool? result = dlg.ShowDialog();
if (result == true)
{
if (ConfirmSaved())
{
ClearList();
filename = dlg.FileName;
Title = WindowTitle;
string[] lines = File.ReadAllLines(filename);
foreach (var line in lines)
LinkList.Items.Add(line);
}
}
}
private void SaveMenu_Click(object sender, RoutedEventArgs e)
{
Save();
}
private void SaveAsMenu_Click(object sender, RoutedEventArgs e)
{
SaveDialog();
}
private void SaveDialog()
{
SaveFileDialog dlg = new()
{
Filter = "Text file (*.txt)|*.txt"
};
if (dlg.ShowDialog() == true)
{
filename = dlg.FileName;
SaveData();
}
}
private void Save()
{
if (filename == "Untitled")
SaveDialog();
else
SaveData();
}
private void SaveData()
{
//string savedata = JsonSerializer.Serialize(links);
//Debug.WriteLine(savedata);
// File.WriteAllText(filename, savedata);
File.WriteAllText(filename, string.Empty);
StreamWriter SaveFile = new(filename);
foreach (var item in LinkList.Items)
{
SaveFile.WriteLine(item.ToString());
}
SaveFile.Close();
saved = true;
ticker = "";
Title = WindowTitle;
}
private void InstructMenu_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("\"Pop to Clipboard\" will move the top item of the list to the entry box and copy it to the clipboard." +
" When the archive.today mode is selected, " +
"the pop button will open up a tab in your browser that saves the link to archive.today, instead of copying to the clipboard.\n" +
"Buttons on the right:\n" +
"+: Same thing as pressing enter on the entry box. Adds the contents of the entry box to the top of the list and clears the entry box\n" +
"-: Removes the selected item from the list\n" +
"^^: Moves the selected item to the top of the list\n" +
"^: Moves the selected item up one space\n" +
"v: Moves the selected item down one space\n" +
"v v: Moves the selected item to the bottom of the list");
}
private bool ConfirmSaved()
{
Title = WindowTitle;
if (saved) return true;
if (ticker == "") return true;
MessageBoxResult result = MessageBox.Show("You have unsaved changes. Do you want to save them before leaving them?", "Save changes", MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.Cancel);
switch (result)
{
case MessageBoxResult.Cancel:
return false;
case MessageBoxResult.Yes:
Save();
return true;
case MessageBoxResult.No:
saved = true;
ticker = "";
Title = WindowTitle;
return true;
default:
return false;
}
}
private void AboutMenu_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("By AceOfSpadesProduc100\nLicensed under the GNU GPL v3 or later");
}
private void UndoButton_Click(object sender, RoutedEventArgs e)
{
Undelete();
}
private void Grid_PreviewKeyDown(object sender, KeyEventArgs e)
{
// Fetch the real key.
var key = e.Key == Key.System ? e.SystemKey : e.Key;
if ((Keyboard.IsKeyDown(Key.LeftCtrl))
&& key == Key.Z)
{
Undelete();
}
if ((Keyboard.IsKeyDown(Key.LeftCtrl))
&& key == Key.S)
{
Save();
}
}
private void Undelete()
{
if (undobuffer.Count == 0) return;
LinkList.Items.Add(undobuffer.Pop());
Unsave();
}
private void RemoveButton_Click(object sender, RoutedEventArgs e)
{
undobuffer.Push((string)LinkList.SelectedValue);
LinkList.Items.RemoveAt(LinkList.SelectedIndex);
Unsave();
if (LinkList.Items.Count == 0)
DisableButtons();
}
private void Entry_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
AddItem();
}
var key = e.Key == Key.System ? e.SystemKey : e.Key;
if ((Keyboard.IsKeyDown(Key.LeftCtrl))
&& key == Key.S)
{
Entry.IsReadOnly = true;
}
if ((Keyboard.IsKeyDown(Key.LeftCtrl))
&& key == Key.Z)
{
Entry.IsReadOnly = true;
}
}
private void Entry_KeyUp(object sender, KeyEventArgs e)
{
var key = e.Key == Key.System ? e.SystemKey : e.Key;
if ((Keyboard.IsKeyDown(Key.LeftCtrl))
&& key == Key.S)
{
Entry.IsReadOnly = false;
}
if ((Keyboard.IsKeyDown(Key.LeftCtrl))
&& key == Key.Z)
{
Entry.IsReadOnly = false;
}
}
private void AddItem()
{
if (Entry.Text.Length > 0)
{
EnableButtons();
Unsave();
LinkList.Items.Add(Entry.Text);
Entry.Clear();
}
}
private void EnableButtons()
{
RemoveButton.IsEnabled = true;
TopButton.IsEnabled = true;
UpButton.IsEnabled = true;
DownButton.IsEnabled = true;
BottomButton.IsEnabled = true;
}
private void DisableButtons()
{
RemoveButton.IsEnabled = false;
TopButton.IsEnabled = false;
UpButton.IsEnabled = false;
DownButton.IsEnabled = false;
BottomButton.IsEnabled = false;
}
private void Unsave()
{
ticker = "*";
saved = false;
Title = WindowTitle;
}
private void AddButton_Click(object sender, RoutedEventArgs e)
{
AddItem();
}
private void TopButton_Click(object sender, RoutedEventArgs e)
{
Unsave();
var selectedIndex = LinkList.SelectedIndex;
if (selectedIndex > 0)
{
var itemToMoveUp = LinkList.Items[selectedIndex];
LinkList.Items.RemoveAt(selectedIndex);
LinkList.Items.Insert(0, itemToMoveUp);
LinkList.SelectedIndex = 0;
}
}
private void UpButton_Click(object sender, RoutedEventArgs e)
{
Unsave();
var selectedIndex = LinkList.SelectedIndex;
if (selectedIndex > 0)
{
var itemToMoveUp = LinkList.Items[selectedIndex];
LinkList.Items.RemoveAt(selectedIndex);
LinkList.Items.Insert(selectedIndex - 1, itemToMoveUp);
LinkList.SelectedIndex = selectedIndex - 1;
}
}
private void DownButton_Click(object sender, RoutedEventArgs e)
{
Unsave();
var selectedIndex = LinkList.SelectedIndex;
if (selectedIndex + 1 < LinkList.Items.Count)
{
var itemToMoveDown = LinkList.Items[selectedIndex];
LinkList.Items.RemoveAt(selectedIndex);
LinkList.Items.Insert(selectedIndex + 1, itemToMoveDown);
LinkList.SelectedIndex = selectedIndex + 1;
}
}
private void BottomButton_Click(object sender, RoutedEventArgs e)
{
Unsave();
var selectedIndex = LinkList.SelectedIndex;
if (selectedIndex > 0)
{
var itemToMoveUp = LinkList.Items[selectedIndex];
LinkList.Items.RemoveAt(selectedIndex);
LinkList.Items.Insert(LinkList.Items.Count, itemToMoveUp);
LinkList.SelectedIndex = LinkList.Items.Count;
}
}
private void ClipboardRadio_Checked(object sender, RoutedEventArgs e)
{
PopButton.Content = "Pop to Clipboard";
}
private void ArchiveRadio_Checked(object sender, RoutedEventArgs e)
{
PopButton.Content = "Send to Archive.today";
}
private void PopButton_Click(object sender, RoutedEventArgs e)
{
Entry.Text = LinkList.Items[0].ToString();
LinkList.Items.RemoveAt(0);
if (ClipboardRadio.IsChecked == true)
Clipboard.SetText(Entry.Text);
else
OpenUrl("https://archive.today/?run=1&url=" + HttpUtility.UrlEncode(Entry.Text));
Unsave();
}
private void OpenUrl(string url)
{
try
{
Process.Start(url);
}
catch
{
// hack because of this: https://github.com/dotnet/corefx/issues/10361
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
url = url.Replace("&", "&");
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Process.Start("xdg-open", url);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Process.Start("open", url);
}
else
{
throw;
}
}
}
}
}