App.axaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Avalonia;
  2. using Avalonia.Controls.ApplicationLifetimes;
  3. using Avalonia.Markup.Xaml;
  4. using Avalonia.Styling;
  5. using Avalonia.Threading;
  6. using FluentAvalonia.Styling;
  7. using Ryujinx.Ava.Common.Locale;
  8. using Ryujinx.Ava.Ui.Controls;
  9. using Ryujinx.Ava.Ui.Windows;
  10. using Ryujinx.Common;
  11. using Ryujinx.Common.Logging;
  12. using Ryujinx.Ui.Common.Configuration;
  13. using System;
  14. using System.Diagnostics;
  15. using System.IO;
  16. namespace Ryujinx.Ava
  17. {
  18. public class App : Application
  19. {
  20. public override void Initialize()
  21. {
  22. AvaloniaXamlLoader.Load(this);
  23. }
  24. public override void OnFrameworkInitializationCompleted()
  25. {
  26. if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  27. {
  28. desktop.MainWindow = new MainWindow();
  29. }
  30. base.OnFrameworkInitializationCompleted();
  31. if (Program.PreviewerDetached)
  32. {
  33. ApplyConfiguredTheme();
  34. ConfigurationState.Instance.Ui.BaseStyle.Event += ThemeChanged_Event;
  35. ConfigurationState.Instance.Ui.CustomThemePath.Event += ThemeChanged_Event;
  36. ConfigurationState.Instance.Ui.EnableCustomTheme.Event += CustomThemeChanged_Event;
  37. }
  38. }
  39. private void CustomThemeChanged_Event(object sender, ReactiveEventArgs<bool> e)
  40. {
  41. ApplyConfiguredTheme();
  42. }
  43. private void ShowRestartDialog()
  44. {
  45. #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
  46. Dispatcher.UIThread.InvokeAsync(async () =>
  47. {
  48. if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  49. {
  50. var result = await ContentDialogHelper.CreateConfirmationDialog(
  51. LocaleManager.Instance["DialogThemeRestartMessage"],
  52. LocaleManager.Instance["DialogThemeRestartSubMessage"],
  53. LocaleManager.Instance["InputDialogYes"],
  54. LocaleManager.Instance["InputDialogNo"],
  55. LocaleManager.Instance["DialogRestartRequiredMessage"]);
  56. if (result == UserResult.Yes)
  57. {
  58. var path = Process.GetCurrentProcess().MainModule.FileName;
  59. var info = new ProcessStartInfo() { FileName = path, UseShellExecute = false };
  60. var proc = Process.Start(info);
  61. desktop.Shutdown();
  62. Environment.Exit(0);
  63. }
  64. }
  65. });
  66. #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
  67. }
  68. private void ThemeChanged_Event(object sender, ReactiveEventArgs<string> e)
  69. {
  70. ApplyConfiguredTheme();
  71. }
  72. private void ApplyConfiguredTheme()
  73. {
  74. try
  75. {
  76. string baseStyle = ConfigurationState.Instance.Ui.BaseStyle;
  77. string themePath = ConfigurationState.Instance.Ui.CustomThemePath;
  78. bool enableCustomTheme = ConfigurationState.Instance.Ui.EnableCustomTheme;
  79. const string BaseStyleUrl = "avares://Ryujinx.Ava/Assets/Styles/Base{0}.xaml";
  80. if (string.IsNullOrWhiteSpace(baseStyle))
  81. {
  82. ConfigurationState.Instance.Ui.BaseStyle.Value = "Dark";
  83. baseStyle = ConfigurationState.Instance.Ui.BaseStyle;
  84. }
  85. var theme = AvaloniaLocator.Current.GetService<FluentAvaloniaTheme>();
  86. theme.RequestedTheme = baseStyle;
  87. var currentStyles = this.Styles;
  88. // Remove all styles except the base style.
  89. if (currentStyles.Count > 1)
  90. {
  91. currentStyles.RemoveRange(1, currentStyles.Count - 1);
  92. }
  93. IStyle newStyles = null;
  94. // Load requested style, and fallback to Dark theme if loading failed.
  95. try
  96. {
  97. newStyles = (Styles)AvaloniaXamlLoader.Load(new Uri(string.Format(BaseStyleUrl, baseStyle), UriKind.Absolute));
  98. }
  99. catch (XamlLoadException)
  100. {
  101. newStyles = (Styles)AvaloniaXamlLoader.Load(new Uri(string.Format(BaseStyleUrl, "Dark"), UriKind.Absolute));
  102. }
  103. currentStyles.Add(newStyles);
  104. if (enableCustomTheme)
  105. {
  106. if (!string.IsNullOrWhiteSpace(themePath))
  107. {
  108. try
  109. {
  110. var themeContent = File.ReadAllText(themePath);
  111. var customStyle = AvaloniaRuntimeXamlLoader.Parse<IStyle>(themeContent);
  112. currentStyles.Add(customStyle);
  113. }
  114. catch (Exception ex)
  115. {
  116. Logger.Error?.Print(LogClass.Application, $"Failed to Apply Custom Theme. Error: {ex.Message}");
  117. }
  118. }
  119. }
  120. }
  121. catch (Exception)
  122. {
  123. Logger.Warning?.Print(LogClass.Application, "Failed to Apply Theme. A restart is needed to apply the selected theme");
  124. ShowRestartDialog();
  125. }
  126. }
  127. }
  128. }