-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
727 lines (645 loc) · 23.6 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
using UnicodeMAP.Logic;
using UnicodeMAP.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
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.IO;
using System.Windows.Threading;
namespace UnicodeMAP
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private const string BaseTitle = "UnicodeMAP ";
private bool IsStarting = true;
private bool IsDoubleClick = false;
private object SelectedTile = null;
private bool IsCtrlKeyPressed = false;
private EmojiParser EmojiParser = new EmojiParser();
private UnicodeParser UnicodeParser = new UnicodeParser();
//public List<EmojiCharacter> Emojis { get; set; }
//public List<UnicodeCharacter> Characters { get; set; }
public string Status
{
get => tbStatus.Text;
set => tbStatus.Text = value;
}
public DispatcherTimer ClickWaitTimer { get; set; }
// CONSTRUCTOR
public MainWindow()
{
InitializeComponent();
InitializeClickWaitTimer();
// Startup in Rendered() funtion
}
// METHODS
private void InitializeClickWaitTimer()
{
ClickWaitTimer = new DispatcherTimer(DispatcherPriority.Background);
ClickWaitTimer.Interval = new TimeSpan(0, 0, 0, 0, 300);
ClickWaitTimer.Tick += ClickWaitTimer_Tick;
}
private async void Rendered()
{
IsStarting = false;
// Startup
UIHelper.SetBusyState();
if (!CheckParserFilesExist())
{
ShowInfo("Required Unicode Data files are not present." + Environment.NewLine +
"Trying to download...");
Clear();
try
{
ShowOverlay();
Status = $"Downloading fresh Unicode files... Please wait.";
Debug.WriteLine($"Downloading fresh Unicode files... Please wait.");
if (await DownloadUnicodeFiles())
{
Status = "Download successful. :)";
ShowInfo("Download successful. Click OK to reload...");
LoadParserFiles();
TxSearch.Focus();
RbSelectUnicode.IsChecked = true;
Run();
}
else
{
Status = "Download failed. Unicode Data files still missing... :(";
ShowInfo("- Download unsuccessful -" + Environment.NewLine + Environment.NewLine +
"An Exception occurred during downloading!" + Environment.NewLine + Environment.NewLine +
"Please check your firewall or internet connection " + Environment.NewLine +
"and try updating again.");
}
}
catch (Exception ex)
{
Debug.WriteLine("Exception thrown: 'Download failed'");
Debug.WriteLine(ex);
}
finally
{
ShowOverlay(false);
}
}
else
{
LoadParserFiles();
TxSearch.Focus();
RbSelectUnicode.IsChecked = true;
Run();
}
}
private void ShowOverlay(bool show = true)
{
if (show)
Overlay.Visibility = Visibility.Visible;
else
Overlay.Visibility = Visibility.Collapsed;
}
private bool CheckParserFilesExist()
{
bool parserFilesExisting = true;
if (!UnicodeParser.CheckFilesExist())
{
parserFilesExisting = false;
}
if (!EmojiParser.CheckFilesExist())
{
parserFilesExisting = false;
}
return parserFilesExisting;
}
private void LoadParserFiles()
{
// Check and try to download if Unicode files are not existing
if (CheckParserFilesExist())
{
UnicodeParser.LoadData();
EmojiParser.LoadData();
GetUnicodeVersion();
}
else
{
ShowInfo("Required Unicode Data files are not present!" + Environment.NewLine
+ "Please click the Update button to try again downloading.");
}
}
private void GetUnicodeVersion()
{
var versionBlocksArray = UnicodeParser.UnicodeBlocks[0]
.Replace("# ", null)
.Replace(Path.GetFileNameWithoutExtension(UnicodeParser.UnicodeBlockFile.FilePath), null)
.Replace("-", null)
.Split('.');
var versionBlocks = new Version(int.Parse(versionBlocksArray[0]), int.Parse(versionBlocksArray[1]));
var versionPropertiesArray = UnicodeParser.UnicodeProperties[0]
.Replace("# ", null)
.Replace(Path.GetFileNameWithoutExtension(UnicodeParser.UnicodePropertiesFile.FilePath), null)
.Replace("-", null)
.Split('.');
var versionProperties = new Version(int.Parse(versionPropertiesArray[0]), int.Parse(versionPropertiesArray[1]));
var versionEmojiArray = EmojiParser.EmojiUnicode[7]
.Replace("# Version: ", null)
.Split('.');
var versionEmoji = new Version(int.Parse(versionPropertiesArray[0]), int.Parse(versionPropertiesArray[1]));
if (versionBlocks != versionProperties ||
versionBlocks != versionEmoji ||
versionProperties != versionEmoji)
{
throw new Exception($"GetUnicodeversion: Versions mismatch! " +
$"(Blocks:{versionBlocks.ToString()}, " +
$"Properties:{versionProperties.ToString()}, " +
$"Emoji:{versionEmoji.ToString()})");
}
Title = BaseTitle + $"[Unicode Version v{versionProperties.Major}.{versionProperties.Minor}]";
}
private async Task<bool> DownloadUnicodeFiles()
{
if (!Directory.Exists("Data"))
Directory.CreateDirectory("Data");
try
{
await Task.Run(() =>
{
// Rename existing (as fallback)
if (UnicodeParser.CheckFilesExist())
UnicodeParser.BackupFiles();
if (EmojiParser.CheckFilesExist())
EmojiParser.BackupFiles();
// Try to download
UnicodeParser.DownloadData();
EmojiParser.DownloadData();
});
return true;
}
catch (Exception)
{
return false;
}
finally
{
if (UnicodeParser.CheckFilesExist())
UnicodeParser.RemoveBackupFiles();
else
UnicodeParser.BackupFiles(true);
if (EmojiParser.CheckFilesExist())
EmojiParser.RemoveBackupFiles();
else
EmojiParser.BackupFiles(true);
}
}
private void Run()
{
try
{
Debug.WriteLine("Run()");
//if (CheckParserFilesExist())
//{
UIHelper.SetBusyState();
Clear();
if (RbSelectEmoji.IsChecked.Value)
{
//EmojiParser.Clear();
//EmojiParser.LoadData();
if (EmojiParser.EmojiUnicode != null)
{
EmojiParser.ParseEmojis();
PopulateEmojiGroups();
PopulareEmojiSubgroups();
PopulateEmojis();
}
}
else if (RbSelectUnicode.IsChecked.Value)
{
//EmojiParser.Clear();
//UnicodeParser.LoadData();
if (UnicodeParser.UnicodeData != null && UnicodeParser.UnicodeBlocks != null && UnicodeParser.UnicodeProperties != null)
{
UnicodeParser.ParseBlocks();
UnicodeParser.ParseProperties();
UnicodeParser.ParseCharacters();
PopulateUnicodeBlocks();
PopulateUnicodeGroups();
PopulareUnicodeSubgroups();
PopulateCharacters();
}
}
//}
//else
//{
// Status = "Required Unicode Data files are missing! :(";
//}
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void ShowInfo(string msg, MessageBoxImage image = MessageBoxImage.Information)
{
MessageBox.Show(msg, "Information", MessageBoxButton.OK, image);
}
private void ShowError(Exception ex)
{
MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
private void Clear()
{
Debug.WriteLine("Clear()");
CbBlocks.Items.Clear();
CbGroups.Items.Clear();
CbGroups.SelectedItem = null;
CbSubgroups.Items.Clear();
CbSubgroups.SelectedItem = null;
PanelMapTiles.ItemsSource = null;
//Emojis = null;
//Characters = null;
}
private void Search(string text)
{
if (RbSelectEmoji.IsChecked.Value)
{
var emojis = EmojiParser.Emojis.Where(c => c.Name.ToLower().Contains(text.ToLower())).ToList();
CbGroups.SelectedItem = null;
CbSubgroups.SelectedItem = null;
PopulateEmojis(emojis);
}
else if (RbSelectUnicode.IsChecked.Value)
{
var characters = UnicodeParser.Characters.Where(c => c.Name.ToLower().Contains(text.ToLower())).ToList();
CbBlocks.SelectedItem = null;
CbGroups.SelectedItem = null;
CbSubgroups.SelectedItem = null;
PopulateCharacters(characters);
}
}
private void ResetSearchFilter()
{
TxSearch.Text = null;
CbBlocks.SelectedItem = null;
CbGroups.SelectedItem = null;
CbSubgroups.SelectedItem = null;
if (RbSelectEmoji.IsChecked.Value)
{
PopulateEmojis();
}
else if (RbSelectUnicode.IsChecked.Value)
{
PopulateCharacters();
}
TxSearch.Focus();
}
private void CopyToClipboard(string text = null)
{
IsCtrlKeyPressed = Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl);
//Unicode-Nummer: U + 0033
//3
Clipboard.Clear();
if (text != null)
{
Clipboard.SetDataObject(text);
return;
}
if (RbSelectEmoji.IsChecked.Value && EmojiParser != null)
{
var selectedItem = (EmojiCharacter)SelectedTile;
if (IsCtrlKeyPressed)
{
Clipboard.SetDataObject(selectedItem.Code);
}
else
{
Clipboard.SetDataObject(selectedItem.Icon);
}
}
else if (RbSelectUnicode.IsChecked.Value && UnicodeParser != null)
{
var selectedItem = (UnicodeCharacter)SelectedTile;
if (IsCtrlKeyPressed)
{
Clipboard.SetDataObject(selectedItem.Code);
}
else
{
Clipboard.SetDataObject(selectedItem.Icon);
}
}
SelectedTile = null;
PanelMapTiles.SelectedItem = null;
}
// EMOJIs
private void PopulateEmojiGroups()
{
foreach (var group in EmojiParser.Groups)
{
CbGroups.Items.Add(group);
}
}
private void PopulareEmojiSubgroups(EmojiGroup group = null)
{
CbSubgroups.Items.Clear();
if (group != null)
{
var subgroups = EmojiParser.Subgroups.Where(s => s.Group.Name == group.Name).ToList();
foreach (var subgroup in subgroups)
{
CbSubgroups.Items.Add(subgroup);
}
}
else
{
foreach (var subgroup in EmojiParser.Subgroups)
{
CbSubgroups.Items.Add(subgroup);
}
}
}
private void PopulateEmojis(List<EmojiCharacter> emojis = null)
{
Debug.WriteLine($"PopulateEmojis({emojis})");
UIHelper.SetBusyState();
if (emojis == null)
{
if (!string.IsNullOrWhiteSpace(TxSearch.Text))
{
Search(TxSearch.Text);
return;
}
else
{
emojis = EmojiParser.Emojis.ToList();
}
}
PanelMapTiles.ItemsSource = emojis;
Status = $"{emojis.Count()} Emojies listed.";
}
private void PopulateEmojiStatus()
{
}
// UNICODEs
private void PopulateUnicodeBlocks()
{
foreach (var block in UnicodeParser.Blocks.OrderBy(p => p.Name).ToList())
{
CbBlocks.Items.Add(block);
}
}
private void PopulateUnicodeGroups()
{
foreach (var property in UnicodeParser.Properties.Where(p => p.IsMainProperty).OrderBy(p => p.Name).ToList())
{
CbGroups.Items.Add(property);
}
}
private void PopulareUnicodeSubgroups(UnicodeProperty mainProperty = null)
{
if (mainProperty != null && !mainProperty.IsMainProperty)
throw new Exception("Main UnicodeProperty is not marked as IsMainPropery!");
CbSubgroups.Items.Clear();
if (mainProperty != null)
{
var subgroups = UnicodeParser.Properties.Where(
p => !p.IsMainProperty && mainProperty.LinkedProperties.Contains(p.Abbreviation)
).ToList();
foreach (var subgroup in subgroups)
{
CbSubgroups.Items.Add(subgroup);
}
}
else
{
var subgroups = UnicodeParser.Properties.Where(p => !p.IsMainProperty).OrderBy(p => p.Name).ToList();
foreach (var subgroup in subgroups)
{
CbSubgroups.Items.Add(subgroup);
}
}
}
private void PopulateCharacters(List<UnicodeCharacter> characters = null)
{
Debug.WriteLine($"PopulateCharacters({characters})");
UIHelper.SetBusyState();
if (characters == null)
{
if (!string.IsNullOrWhiteSpace(TxSearch.Text))
{
Search(TxSearch.Text);
return;
}
else
{
//characters = UnicodeParser.Characters.OrderBy(c => c.Name).ToList();
characters = UnicodeParser.Characters.ToList();
}
}
PanelMapTiles.ItemsSource = characters;
Status = $"{characters.Count()} Characters listed.";
}
// EVENTS
private void Window_ContentRendered(object sender, EventArgs e)
{
Rendered();
}
private void BtnReset_Click(object sender, RoutedEventArgs e)
{
ResetSearchFilter();
}
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
ResetSearchFilter();
}
}
private void TxSearch_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
UIHelper.SetBusyState();
Search(TxSearch.Text);
}
}
private void FooterLink_Click(object sender, RoutedEventArgs e)
{
var hyperlink = (Hyperlink)sender;
Process.Start(hyperlink.NavigateUri.AbsoluteUri);
}
private void RbSelectEmoji_Checked(object sender, RoutedEventArgs e)
{
RbSelectUnicode.IsChecked = false;
RbSelectUnicode.IsEnabled = true;
RbSelectEmoji.IsEnabled = false;
GridBlocks.Visibility = Visibility.Collapsed;
if (!IsStarting)
{
//Clear();
Run();
}
}
private void RbSelectUnicode_Checked(object sender, RoutedEventArgs e)
{
RbSelectEmoji.IsChecked = false;
RbSelectEmoji.IsEnabled = true;
RbSelectUnicode.IsEnabled = false;
GridBlocks.Visibility = Visibility.Visible;
if (!IsStarting)
{
//Clear();
Run();
}
}
private void CbBlocks_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Only visible at Unicode mode
if (RbSelectUnicode.IsChecked.Value && UnicodeParser != null)
{
var block = (UnicodeBlock)((ComboBox)sender).SelectedItem;
if (block != null)
{
var characters = UnicodeParser.Characters.Where(c => block.CodePointRange.Contains(Helper.HexToInt(c.Code))).ToList();
PopulateCharacters(characters);
CbGroups.SelectedItem = null;
CbSubgroups.SelectedItem = null;
TxSearch.Text = null;
}
}
}
private void CbGroups_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (RbSelectEmoji.IsChecked.Value && EmojiParser != null)
{
var group = (EmojiGroup)((ComboBox)sender).SelectedItem;
if (group != null)
{
PopulareEmojiSubgroups(group);
var emojis = EmojiParser.Emojis.Where(c => c.Group == group).ToList();
PopulateEmojis(emojis);
TxSearch.Text = null;
}
}
else if (RbSelectUnicode.IsChecked.Value && UnicodeParser != null)
{
var property = (UnicodeProperty)((ComboBox)sender).SelectedItem;
if (property != null)
{
CbBlocks.SelectedItem = null;
PopulareUnicodeSubgroups(property);
var characters = UnicodeParser.Characters.Where(c => property.LinkedProperties.Contains(c.Property.Abbreviation)).ToList();
PopulateCharacters(characters);
TxSearch.Text = null;
}
}
}
private void CbSubgroups_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (RbSelectEmoji.IsChecked.Value && EmojiParser != null)
{
var subgroup = (EmojiSubgroup)((ComboBox)sender).SelectedItem;
if (subgroup != null)
{
var emojis = EmojiParser.Emojis.Where(c => c.Subgroup == subgroup).ToList();
PopulateEmojis(emojis);
TxSearch.Text = null;
}
}
else if (RbSelectUnicode.IsChecked.Value && UnicodeParser != null)
{
CbBlocks.SelectedItem = null;
var subgroup = (UnicodeProperty)((ComboBox)sender).SelectedItem;
if (subgroup != null)
{
var characters = UnicodeParser.Characters.Where(c => c.Property == subgroup).ToList();
PopulateCharacters(characters);
TxSearch.Text = null;
}
}
}
private async void BtnUpdate_Click(object sender, RoutedEventArgs e)
{
try
{
ShowOverlay();
Status = "Downloading fresh Unicode files... Please wait.";
Debug.WriteLine($"Downloading fresh Unicode files... Please wait.");
Clear();
//Characters = null;
//Emojis = null;
await DownloadUnicodeFiles();
LoadParserFiles();
Status = "Download successful.";
ShowInfo("Download successful. Click OK to reload...");
Run();
}
catch (Exception ex)
{
ShowError(ex);
}
finally
{
ShowOverlay(false);
}
}
private void PanelMapTiles_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (!IsDoubleClick)
{
SelectedTile = PanelMapTiles.SelectedItem;
ClickWaitTimer.Start();
}
}
private void PanelMapTiles_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
Debug.WriteLine("CopyToTextbox()");
IsDoubleClick = true;
TxText.Text += ((Character)SelectedTile).Icon;
}
private void ClickWaitTimer_Tick(object sender, EventArgs e)
{
ClickWaitTimer.Stop();
if (!IsDoubleClick)
{
Debug.WriteLine("CopyToClipboard()");
CopyToClipboard();
}
IsCtrlKeyPressed = false;
IsDoubleClick = false;
}
private void GridTile_MouseEnter(object sender, MouseEventArgs e)
{
var gridTile = (Grid)sender;
Preview.Visibility = Visibility.Visible;
Preview.DataContext = gridTile.DataContext;
}
private void GridTile_MouseLeave(object sender, MouseEventArgs e)
{
var gridTile = (Grid)sender;
Preview.Visibility = Visibility.Collapsed;
Preview.DataContext = null;
}
private void BtnTextReset_Click(object sender, RoutedEventArgs e)
{
TxText.Text = null;
}
private void BtnTextCopy_Click(object sender, RoutedEventArgs e)
{
CopyToClipboard(TxText.Text);
}
}
}