Repository navigation
Expand file tree
/
Copy pathMainWindow.axaml.cs
More file actions
5949 lines (5206 loc) · 220 KB
/
Copy pathMainWindow.axaml.cs
File metadata and controls
5949 lines (5206 loc) · 220 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
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using Avalonia.VisualTree;
using GitHubLauncher.Core.Models;
using GitHubLauncher.Core.Services;
using AsyncImageLoader;
using GithubLauncher.Models;
using GithubLauncher.Services;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Text.Json;
using System.Runtime.InteropServices;
using Avalonia.Platform;
using Avalonia.Markup.Xaml.Converters;
#if WINDOWS
using NAudio.Wave;
#endif
namespace GithubLauncher
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private readonly GameManager _gameManager;
public ObservableCollection<GameInfo> Games => _gameManager?.Games ?? new ObservableCollection<GameInfo>();
public AppSettings _settings = new();
public App _app = null!;
public AppSettings Settings => _settings;
private bool isSettingsPanelOpen = false;
public string IconFillStretch = "Uniform";
private string _backgroundImagePath = string.Empty;
private string _backgroundImageUri = string.Empty;
public string BackgroundImagePath
{
get => _backgroundImagePath;
set
{
if (_backgroundImagePath != value)
{
_backgroundImagePath = value;
if (!string.IsNullOrEmpty(value) && File.Exists(value))
{
_backgroundImageUri = new Uri(value).AbsoluteUri;
}
else
{
_backgroundImageUri = string.Empty;
}
OnPropertyChanged(nameof(BackgroundImagePath));
OnPropertyChanged(nameof(BackgroundImageUri));
}
}
}
public string BackgroundImageUri => _backgroundImageUri ?? string.Empty;
public float BackgroundOpacity
{
get => _settings.BackgroundOpacity;
set
{
if (Math.Abs(_settings.BackgroundOpacity - value) > 0.001f)
{
_settings.BackgroundOpacity = value;
OnPropertyChanged(nameof(BackgroundOpacity));
}
}
}
private System.Threading.CancellationTokenSource? _fadeTaskCts;
private const int FADE_DURATION_MS = 500;
#if WINDOWS
private IWavePlayer? _waveOut;
private AudioFileReader? _audioFileReader;
#endif
private Process? _musicProcess;
private bool _musicPausedByDeactivation = false;
private bool _launchedGameOwnsInput;
private bool _trackingLaunchedGameProcess;
private string _launcherMusicPath = string.Empty;
public string LauncherMusicPath
{
get => _launcherMusicPath;
set
{
if (_launcherMusicPath != value)
{
_launcherMusicPath = value;
OnPropertyChanged(nameof(LauncherMusicPath));
}
}
}
private float _musicVolume = 0.2f;
public float MusicVolume
{
get => _musicVolume;
set
{
if (Math.Abs(_musicVolume - value) > 0.001f)
{
_musicVolume = value;
OnPropertyChanged(nameof(MusicVolume));
#if WINDOWS
if (_audioFileReader != null)
{
_audioFileReader.Volume = value;
}
#else
if (!string.IsNullOrEmpty(LauncherMusicPath) && File.Exists(LauncherMusicPath))
{
PlayLauncherMusic(LauncherMusicPath);
}
#endif
}
}
}
public bool IsFullscreen
{
get => _settings.StartFullscreen;
set
{
if (_settings.StartFullscreen != value)
{
_settings.StartFullscreen = value;
OnPropertyChanged(nameof(IsFullscreen));
}
}
}
public bool CloseAfterLaunch
{
get => _settings.CloseAfterLaunch;
set
{
if (_settings.CloseAfterLaunch != value)
{
_settings.CloseAfterLaunch = value;
OnPropertyChanged(nameof(CloseAfterLaunch));
}
}
}
public IBrush WindowBackground
{
get
{
if (_settings?.WindowBorderRounding ?? false)
{
if (IsFullscreen)
{
return this.Resources["ThemeDarker"] as IBrush ?? Brushes.Transparent;
}
if (_settings.ShowOSTopBar)
{
return this.Resources["ThemeDarker"] as IBrush ?? Brushes.Transparent;
}
return Brushes.Transparent;
}
return this.Resources["ThemeDarker"] as IBrush ?? Brushes.Transparent;
}
}
public bool ExtendClientAreaEnabled => !_settings.ShowOSTopBar;
public ExtendClientAreaChromeHints ChromeHints
{
get => _settings.ShowOSTopBar ? ExtendClientAreaChromeHints.PreferSystemChrome : ExtendClientAreaChromeHints.NoChrome;
}
private string _currentSortBy = "Name";
private string _currentVersionString = string.Empty;
public string currentVersionString
{
get => _currentVersionString;
set
{
if (_currentVersionString != value)
{
_currentVersionString = value;
OnPropertyChanged(nameof(currentVersionString));
}
}
}
private string _platformstring = string.Empty;
public string PlatformString
{
get => _platformstring;
set
{
if (_platformstring != value)
{
_platformstring = value;
OnPropertyChanged(nameof(PlatformString));
OnPropertyChanged(nameof(IsLinuxPlatform));
}
}
}
public bool IsLinuxPlatform => PlatformString.Contains("Linux", StringComparison.OrdinalIgnoreCase);
private bool _isContinueVisible;
public bool IsContinueVisible
{
get => _isContinueVisible;
set
{
if (_isContinueVisible != value)
{
_isContinueVisible = value;
OnPropertyChanged(nameof(IsContinueVisible));
}
}
}
private InputService? _inputService;
private bool _isProcessingInput = false;
private bool _hasInitializedFocus = false;
private bool _isChangelogOpen = false;
private GameInfo? _currentChangelogGame;
private GameInfo? _continueGameInfo;
public GameInfo? ContinueGameInfo
{
get => _continueGameInfo;
set
{
if (_continueGameInfo != value)
{
_continueGameInfo = value;
OnPropertyChanged(nameof(ContinueGameInfo));
}
}
}
private bool _isGamesManagerOpen = false;
public string InfoTextLength = "*";
private SolidColorBrush _themeColorBrush = new(Colors.Transparent);
public SolidColorBrush ThemeColorBrush
{
get => _themeColorBrush;
set
{
if (_themeColorBrush != value)
{
_themeColorBrush = value;
OnPropertyChanged(nameof(ThemeColorBrush));
UpdateThemeColors();
}
}
}
private SolidColorBrush _secondaryColorBrush = new(Colors.Transparent);
public SolidColorBrush SecondaryColorBrush
{
get => _secondaryColorBrush;
set
{
if (_secondaryColorBrush != value)
{
_secondaryColorBrush = value;
OnPropertyChanged(nameof(SecondaryColorBrush));
UpdateThemeColors();
}
}
}
public MainWindow()
{
InitializeComponent();
try
{
_settings = AppSettings.Load();
}
catch (Exception ex)
{
_ = ShowMessageBoxAsync($"Failed to load settings: {ex.Message}", "Settings Error");
_settings = new AppSettings();
}
_gameManager = new GameManager();
// Initialize theme
ThemeColorBrush = new SolidColorBrush(Color.Parse(_settings?.PrimaryColor ?? "#18181b"));
SecondaryColorBrush = new SolidColorBrush(Color.Parse(_settings?.SecondaryColor ?? "#404040"));
UpdateThemeColors();
_gameManager.UnhideAllGames();
LoadCurrentVersion();
LoadCurrentPlatform();
UpdateSettingsUI();
// Apply fullscreen from settings immediately
if (_settings.StartFullscreen)
{
WindowState = WindowState.FullScreen;
}
// Initialize background image from settings
BackgroundImagePath = _settings.BackgroundImagePath ?? string.Empty;
// Initialize music from settings
LauncherMusicPath = _settings.LauncherMusicPath ?? string.Empty;
MusicVolume = _settings.MusicVolume;
if (!string.IsNullOrEmpty(LauncherMusicPath) && File.Exists(LauncherMusicPath))
{
PlayLauncherMusic(LauncherMusicPath);
}
_inputService = new InputService(this, _settings);
_inputService.OnConfirm += HandleConfirmAction;
_inputService.OnCancel += HandleCancelAction;
this.KeyDown += MainWindow_KeyDown;
this.KeyUp += MainWindow_KeyUp;
this.Activated += MainWindow_Activated;
this.Deactivated += MainWindow_Deactivated;
_gameManager.PropertyChanged += (s, e) => {
if (e.PropertyName == nameof(GameManager.Games))
{
OnPropertyChanged(nameof(Games));
UpdateContinueButtonState();
Debug.WriteLine($"Games collection changed. Count: {_gameManager.Games?.Count ?? 0}");
foreach (var game in _gameManager.Games ?? new())
{
SubscribeToGameEvents(game);
Debug.WriteLine($"Game: {game.Name}, IconUrl: {game.IconUrl}");
}
}
};
}
// Is Theme Color Light
private bool IsLightColor(Color color)
{
// Calculate perceived brightness using standard formula
double brightness = (0.299 * color.R + 0.587 * color.G + 0.114 * color.B) / 255;
return brightness > 0.5;
}
// Theme Color Shades
private Color GetShadedColor(Color baseColor, double factor)
{
byte r = (byte)Math.Min(255, Math.Max(0, baseColor.R * factor));
byte g = (byte)Math.Min(255, Math.Max(0, baseColor.G * factor));
byte b = (byte)Math.Min(255, Math.Max(0, baseColor.B * factor));
return Color.FromRgb(r, g, b);
}
private void UpdateThemeColors()
{
if (_themeColorBrush == null || _secondaryColorBrush == null) return;
var primaryColor = _themeColorBrush.Color;
var secondaryColor = _secondaryColorBrush.Color;
var themeBase = new SolidColorBrush(primaryColor);
var themeLighter = new SolidColorBrush(GetShadedColor(primaryColor, 1.3));
var themeDarker = new SolidColorBrush(GetShadedColor(primaryColor, 0.7));
var themeBorder = new SolidColorBrush(secondaryColor);
var textColor = CalculateLuminance(primaryColor) > 0.5 ? Colors.Black : Colors.White;
var tintedText = new SolidColorBrush(BlendColors(textColor, secondaryColor, 0.08));
var tintedTextSecondary = new SolidColorBrush(
CalculateLuminance(primaryColor) > 0.5
? BlendColors(Color.FromRgb(70, 70, 70), secondaryColor, 0.15)
: BlendColors(Color.FromRgb(200, 200, 200), secondaryColor, 0.15)
);
Resources["ThemeBase"] = themeBase;
Resources["ThemeLighter"] = themeLighter;
Resources["ThemeDarker"] = themeDarker;
Resources["ThemeBorder"] = themeBorder;
Resources["ThemeText"] = tintedText;
Resources["ThemeTextSecondary"] = tintedTextSecondary;
OnPropertyChanged(nameof(WindowBackground));
}
private double CalculateLuminance(Color color)
{
return (0.299 * color.R + 0.587 * color.G + 0.114 * color.B) / 255;
}
// Color Picker Preset Dialog
private async void ThemeColorPicker_Click(object sender, RoutedEventArgs e)
{
// Simple color presets dialog
var presets = new Dictionary<string, string>
{
{ "Black", "#000000" },
{ "Darker Gray", "#101010" },
{ "Dark Gray (Default)", "#18181b" },
{ "Charcoal Gray", "#2c2c2c" },
{ "Slate Gray", "#36454f" },
{ "Dark Navy Blue", "#1e3a5f" },
{ "Deep Navy", "#0f2b46" },
{ "Deep Indigo", "#2c3e50" },
{ "Dark Grayish Blue", "#45475a" },
{ "Midnight Black", "#19191c" },
{ "Deep Forest Green", "#1a4d2e" },
{ "Darkest Green", "#063204" },
{ "Forest Green", "#228b22" },
{ "Deep Moss Green", "#2c5f2d" },
{ "Deep Forest", "#134411" },
{ "Black Forest Green", "#051D01" },
{ "Dark Olive Green", "#556b2f" },
{ "Dark Olive Drab", "#2a2922" },
{ "Deep Purple", "#2d1b4e" },
{ "Deep Plum", "#4b0082" },
{ "Dark Eggplant", "#614051" },
{ "Dark Burgundy", "#4d1f1f" },
{ "Burgundy", "#800020" },
{ "Deep Maroon", "#5c0b0b" },
{ "Light Gray", "#e5e5e5" },
{ "Silver Gray", "#c0c0c0" },
{ "Pale Gray", "#f0f0f0" },
{ "Soft Blue", "#d4e4f7" },
{ "Sky Blue", "#87ceeb" },
{ "Powder Blue", "#b0e0e6" },
{ "Seafoam Green", "#d4f1e8" },
{ "Mint Green", "#98fb98" },
{ "Bright Sea Foam", "#98ff98" }
};
await ShowColorPresetsDialog(presets);
}
// Secondary Color Picker
private async void SecondaryColorPicker_Click(object sender, RoutedEventArgs e)
{
// Simple color presets dialog
var presets = new Dictionary<string, string>
{
{ "Black", "#000000" },
{ "Dark Gray (Default)", "#404040" },
{ "Gray", "#737373" },
{ "Light Gray", "#d4d4d4" },
{ "White", "#ffffff" },
{ "Red", "#ef4444" },
{ "Orange", "#f97316" },
{ "Yellow", "#eab308" },
{ "Lime", "#84cc16" },
{ "Green", "#10b981" },
{ "Teal", "#14b8a6" },
{ "Cyan", "#06b6d4" },
{ "Sky Blue", "#0ea5e9" },
{ "Blue", "#3b82f6" },
{ "Purple", "#a855f7" },
{ "Violet", "#8b5cf6" },
{ "Indigo", "#6366f1" },
{ "Pink", "#ec4899" }
};
await ShowColorPresetsDialog(presets, true);
}
private Color BlendColors(Color baseColor, Color blendColor, double blendAmount)
{
byte r = (byte)(baseColor.R * (1 - blendAmount) + blendColor.R * blendAmount);
byte g = (byte)(baseColor.G * (1 - blendAmount) + blendColor.G * blendAmount);
byte b = (byte)(baseColor.B * (1 - blendAmount) + blendColor.B * blendAmount);
return Color.FromRgb(r, g, b);
}
private async Task ShowColorPresetsDialog(Dictionary<string, string> presets, bool isSecondary = false)
{
await Dispatcher.UIThread.InvokeAsync(async () =>
{
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop &&
desktop.MainWindow != null)
{
var stackPanel = new StackPanel { Margin = new Thickness(20), Spacing = 10 };
// Add Custom Color button at the top
var customButton = new Button
{
Content = "🎨 Custom Color Picker",
HorizontalAlignment = HorizontalAlignment.Stretch,
Height = 50,
Background = new SolidColorBrush(Color.FromRgb(50, 50, 50)),
Foreground = new SolidColorBrush(Colors.White),
FontWeight = FontWeight.Bold
};
customButton.Click += async (s, e) =>
{
// Close presets dialog
var window = (s as Button)?.GetVisualRoot() as Window;
window?.Close();
// Open custom color picker
await ShowCustomColorPicker(isSecondary);
};
stackPanel.Children.Add(customButton);
// Add separator
stackPanel.Children.Add(new Border
{
Height = 1,
Background = new SolidColorBrush(Color.FromRgb(100, 100, 100)),
Margin = new Thickness(0, 5, 0, 5)
});
foreach (var preset in presets)
{
var button = new Button
{
Content = preset.Key,
HorizontalAlignment = HorizontalAlignment.Stretch,
Height = 40,
Background = new SolidColorBrush(Color.Parse(preset.Value)),
Foreground = new SolidColorBrush(IsLightColor(Color.Parse(preset.Value)) ? Colors.Black : Colors.White),
Tag = preset.Value
};
button.Click += (s, e) =>
{
var colorHex = (s as Button)?.Tag as string;
if (!string.IsNullOrEmpty(colorHex))
{
if (isSecondary)
{
_settings.SecondaryColor = colorHex;
SecondaryColorBrush = new SolidColorBrush(Color.Parse(colorHex));
}
else
{
_settings.PrimaryColor = colorHex;
ThemeColorBrush = new SolidColorBrush(Color.Parse(colorHex));
}
OnSettingChanged();
// Close the dialog after selection
if (s is Button btn && btn.Parent != null)
{
var window = btn.GetVisualRoot() as Window;
window?.Close();
}
}
};
stackPanel.Children.Add(button);
}
var messageBox = new Window
{
Title = isSecondary ? "Select Secondary Color" : "Select Primary Color",
Width = 300,
Height = 1000,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Content = new ScrollViewer { Content = stackPanel }
};
await messageBox.ShowDialog(desktop.MainWindow);
}
});
}
private async Task ShowCustomColorPicker(bool isSecondary = false)
{
await Dispatcher.UIThread.InvokeAsync(async () =>
{
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop &&
desktop.MainWindow != null)
{
var currentColor = isSecondary ? SecondaryColorBrush.Color : ThemeColorBrush.Color;
var (h, s, l) = RgbToHsl(currentColor);
var pickerPanel = new StackPanel { Margin = new Thickness(20), Spacing = 15 };
// Preview box
var previewBorder = new Border
{
Width = 260,
Height = 60,
CornerRadius = new CornerRadius(8),
Background = new SolidColorBrush(currentColor),
BorderBrush = new SolidColorBrush(Colors.White),
BorderThickness = new Thickness(2)
};
pickerPanel.Children.Add(previewBorder);
// HSL Sliders
var hSlider = CreateHslSlider("Hue", h, 0, 360, "°");
var sSlider = CreateHslSlider("Saturation", s, 0, 100, "%");
var lSlider = CreateHslSlider("Lightness", l, 0, 100, "%");
pickerPanel.Children.Add(hSlider.panel);
pickerPanel.Children.Add(sSlider.panel);
pickerPanel.Children.Add(lSlider.panel);
// Update preview on slider change
EventHandler<AvaloniaPropertyChangedEventArgs> updatePreview = (s, e) =>
{
var newColor = HslToRgb(hSlider.slider.Value, sSlider.slider.Value, lSlider.slider.Value);
previewBorder.Background = new SolidColorBrush(newColor);
};
hSlider.slider.PropertyChanged += updatePreview;
sSlider.slider.PropertyChanged += updatePreview;
lSlider.slider.PropertyChanged += updatePreview;
// Hex input
var hexPanel = new StackPanel { Spacing = 5 };
hexPanel.Children.Add(new TextBlock
{
Text = "Hex Color",
Foreground = new SolidColorBrush(Colors.White),
FontSize = 12
});
var hexBox = new TextBox
{
Text = $"#{currentColor.R:X2}{currentColor.G:X2}{currentColor.B:X2}",
Watermark = "#RRGGBB",
Foreground = new SolidColorBrush(Colors.White),
Background = new SolidColorBrush(Color.FromRgb(40, 40, 40))
};
hexBox.TextChanged += (s, e) =>
{
try
{
var text = hexBox.Text?.Trim();
if (!string.IsNullOrEmpty(text) && text.StartsWith("#") && text.Length == 7)
{
var color = Color.Parse(text);
var (hue, sat, light) = RgbToHsl(color);
hSlider.slider.Value = hue;
sSlider.slider.Value = sat;
lSlider.slider.Value = light;
}
}
catch { }
};
// Update hex box when sliders change
EventHandler<AvaloniaPropertyChangedEventArgs> updateHex = (s, e) =>
{
var color = HslToRgb(hSlider.slider.Value, sSlider.slider.Value, lSlider.slider.Value);
hexBox.Text = $"#{color.R:X2}{color.G:X2}{color.B:X2}";
};
hSlider.slider.PropertyChanged += updateHex;
sSlider.slider.PropertyChanged += updateHex;
lSlider.slider.PropertyChanged += updateHex;
hexPanel.Children.Add(hexBox);
pickerPanel.Children.Add(hexPanel);
// Buttons
var buttonPanel = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 10, Margin = new Thickness(0, 10, 0, 0) };
var applyButton = new Button
{
Content = "Apply",
Width = 120,
Height = 35,
Background = new SolidColorBrush(Color.FromRgb(34, 197, 94)),
Foreground = new SolidColorBrush(Colors.White)
};
var cancelButton = new Button
{
Content = "Cancel",
Width = 120,
Height = 35,
Background = new SolidColorBrush(Color.FromRgb(100, 100, 100)),
Foreground = new SolidColorBrush(Colors.White)
};
buttonPanel.Children.Add(applyButton);
buttonPanel.Children.Add(cancelButton);
pickerPanel.Children.Add(buttonPanel);
var pickerWindow = new Window
{
Title = isSecondary ? "Custom Secondary Color" : "Custom Primary Color",
Width = 320,
Height = 480,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Background = new SolidColorBrush(Color.FromRgb(30, 30, 30)),
Content = pickerPanel,
CanResize = false
};
applyButton.Click += (s, e) =>
{
var finalColor = HslToRgb(hSlider.slider.Value, sSlider.slider.Value, lSlider.slider.Value);
var hexColor = $"#{finalColor.R:X2}{finalColor.G:X2}{finalColor.B:X2}";
if (isSecondary)
{
_settings.SecondaryColor = hexColor;
SecondaryColorBrush = new SolidColorBrush(finalColor);
}
else
{
_settings.PrimaryColor = hexColor;
ThemeColorBrush = new SolidColorBrush(finalColor);
}
OnSettingChanged();
pickerWindow.Close();
};
cancelButton.Click += (s, e) => pickerWindow.Close();
await pickerWindow.ShowDialog(desktop.MainWindow);
}
});
}
private (StackPanel panel, Slider slider) CreateHslSlider(string label, double initialValue, double min, double max, string unit)
{
var panel = new StackPanel { Spacing = 5 };
var headerPanel = new StackPanel { Orientation = Orientation.Horizontal };
headerPanel.Children.Add(new TextBlock
{
Text = label,
Foreground = new SolidColorBrush(Colors.White),
FontSize = 12,
Width = 80
});
var valueText = new TextBlock
{
Text = $"{(int)initialValue}{unit}",
Foreground = new SolidColorBrush(Colors.White),
FontSize = 12,
Width = 50,
TextAlignment = TextAlignment.Right
};
headerPanel.Children.Add(valueText);
panel.Children.Add(headerPanel);
var slider = new Slider
{
Minimum = min,
Maximum = max,
Value = initialValue,
Width = 260,
TickFrequency = 1,
IsSnapToTickEnabled = true
};
slider.PropertyChanged += (s, e) =>
{
if (e.Property.Name == "Value")
{
valueText.Text = $"{(int)slider.Value}{unit}";
}
};
panel.Children.Add(slider);
return (panel, slider);
}
// Convert RGB to HSL
private (double h, double s, double l) RgbToHsl(Color color)
{
double r = color.R / 255.0;
double g = color.G / 255.0;
double b = color.B / 255.0;
double max = Math.Max(r, Math.Max(g, b));
double min = Math.Min(r, Math.Min(g, b));
double delta = max - min;
double h = 0;
double s = 0;
double l = (max + min) / 2.0;
if (delta != 0)
{
s = l > 0.5 ? delta / (2.0 - max - min) : delta / (max + min);
if (max == r)
h = ((g - b) / delta + (g < b ? 6 : 0)) / 6.0;
else if (max == g)
h = ((b - r) / delta + 2) / 6.0;
else
h = ((r - g) / delta + 4) / 6.0;
}
return (h * 360, s * 100, l * 100);
}
// Convert HSL to RGB
private Color HslToRgb(double h, double s, double l)
{
h = h / 360.0;
s = s / 100.0;
l = l / 100.0;
double r, g, b;
if (s == 0)
{
r = g = b = l;
}
else
{
double q = l < 0.5 ? l * (1 + s) : l + s - l * s;
double p = 2 * l - q;
r = HueToRgb(p, q, h + 1.0 / 3.0);
g = HueToRgb(p, q, h);
b = HueToRgb(p, q, h - 1.0 / 3.0);
}
return Color.FromRgb(
(byte)Math.Round(r * 255),
(byte)Math.Round(g * 255),
(byte)Math.Round(b * 255)
);
}
private double HueToRgb(double p, double q, double t)
{
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1.0 / 6.0) return p + (q - p) * 6 * t;
if (t < 1.0 / 2.0) return q;
if (t < 2.0 / 3.0) return p + (q - p) * (2.0 / 3.0 - t) * 6;
return p;
}
private void UpdateContinueButtonState()
{
ContinueGameInfo = _gameManager.GetLatestPlayedInstalledGame();
IsContinueVisible = ContinueGameInfo != null;
}
private void TopBar_PointerPressed(object? sender, PointerPressedEventArgs e)
{
// Skip if Hovering ComboBox or if system decorations are enabled
var source = e.Source as Control;
if (IsDescendantOf(source, SortByComboBox))
{
return;
}
if (_settings.ShowOSTopBar)
{
return;
}
var point = e.GetCurrentPoint(this);
if (point.Properties.IsLeftButtonPressed)
{
if (e.ClickCount == 2)
{
// Double-click to maximize/restore
WindowState = WindowState == WindowState.Maximized
? WindowState.Normal
: WindowState.Maximized;
}
else
{
// Single-click drag to move
BeginMoveDrag(e);
}
}
}
private bool IsDescendantOf(Control? control, Control? parent)
{
if (control == null || parent == null)
return false;
while (control != null)
{
if (control == parent)
return true;
control = control.Parent as Control;
}
return false;
}
private void LoadCurrentPlatform()
{
if (_settings != null)
{
PlatformString = _settings.Platform switch
{
TargetOS.Auto => "Automatic",
TargetOS.Windows => "Windows",
TargetOS.MacOS => "macOS",
TargetOS.LinuxX64 => "Linux x64",
TargetOS.LinuxARM64 => "Linux ARM64",
_ => "Unknown"
};
}
else
{
PlatformString = "Unknown";
}
}
private void LoadCurrentVersion()
{
try
{
string currentAppDirectory = AppDomain.CurrentDomain.BaseDirectory;
string updateCheckFilePath = Path.Combine(currentAppDirectory, "update_check.json");
if (File.Exists(updateCheckFilePath))
{
try
{
var json = File.ReadAllText(updateCheckFilePath);
var updateInfo = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(json);
if (updateInfo != null && updateInfo.TryGetValue("CurrentVersion", out var versionElement))
{
currentVersionString = versionElement.GetString() ?? "v0.0";
return;
}
}
catch (Exception ex)
{
Debug.WriteLine($"Failed to parse update_check.json: {ex.Message}");
// Fall through to version.txt check
}
}
// Fallback to version.txt
string versionFilePath = Path.Combine(currentAppDirectory, "version.txt");
if (File.Exists(versionFilePath))
{
currentVersionString = File.ReadAllText(versionFilePath).Trim();
}
else
{
currentVersionString = "v0.0";
}
}
catch (Exception ex)
{
currentVersionString = "Unknown";
Debug.WriteLine($"Failed to load version: {ex.Message}");
}
}
protected override void OnOpened(EventArgs e)
{
base.OnOpened(e);
ApplyRoundedCorners();
_ = InitializeGamesAsync();
}
private async Task InitializeGamesAsync()
{
try
{
await _gameManager.LoadGamesAsync();
ApplySorting();
await Dispatcher.UIThread.InvokeAsync(() =>
{
DataContext = this;
UpdateContinueButtonState();
UpdateEmptyStateVisibility();
if (!_hasInitializedFocus)
{
SetInitialFocus();
_hasInitializedFocus = true;
}
});
}
catch (Exception ex)
{
await Dispatcher.UIThread.InvokeAsync(() =>
_ = ShowMessageBoxAsync($"Failed to load apps: {ex.Message}", "Load Error"));
}
}
private void SetInitialFocus()
{
// Small delay to ensure UI is fully rendered
Dispatcher.UIThread.Post(() =>
{