App.axaml.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. (desktop.MainWindow as MainWindow).SettingsWindow,
  52. LocaleManager.Instance["DialogThemeRestartMessage"],
  53. LocaleManager.Instance["DialogThemeRestartSubMessage"],
  54. LocaleManager.Instance["InputDialogYes"],
  55. LocaleManager.Instance["InputDialogNo"],
  56. LocaleManager.Instance["DialogRestartRequiredMessage"]);
  57. if (result == UserResult.Yes)
  58. {
  59. var path = Process.GetCurrentProcess().MainModule.FileName;
  60. var info = new ProcessStartInfo() { FileName = path, UseShellExecute = false };
  61. var proc = Process.Start(info);
  62. desktop.Shutdown();
  63. Environment.Exit(0);
  64. }
  65. }
  66. });
  67. #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
  68. }
  69. private void ThemeChanged_Event(object sender, ReactiveEventArgs<string> e)
  70. {
  71. ApplyConfiguredTheme();
  72. }
  73. private void ApplyConfiguredTheme()
  74. {
  75. try
  76. {
  77. string baseStyle = ConfigurationState.Instance.Ui.BaseStyle;
  78. string themePath = ConfigurationState.Instance.Ui.CustomThemePath;
  79. bool enableCustomTheme = ConfigurationState.Instance.Ui.EnableCustomTheme;
  80. const string BaseStyleUrl = "avares://Ryujinx.Ava/Assets/Styles/Base{0}.xaml";
  81. if (string.IsNullOrWhiteSpace(baseStyle))
  82. {
  83. ConfigurationState.Instance.Ui.BaseStyle.Value = "Dark";
  84. baseStyle = ConfigurationState.Instance.Ui.BaseStyle;
  85. }
  86. var theme = AvaloniaLocator.Current.GetService<FluentAvaloniaTheme>();
  87. theme.RequestedTheme = baseStyle;
  88. var currentStyles = this.Styles;
  89. // Remove all styles except the base style.
  90. if (currentStyles.Count > 1)
  91. {
  92. currentStyles.RemoveRange(1, currentStyles.Count - 1);
  93. }
  94. IStyle newStyles = null;
  95. // Load requested style, and fallback to Dark theme if loading failed.
  96. try
  97. {
  98. newStyles = (Styles)AvaloniaXamlLoader.Load(new Uri(string.Format(BaseStyleUrl, baseStyle), UriKind.Absolute));
  99. }
  100. catch (XamlLoadException)
  101. {
  102. newStyles = (Styles)AvaloniaXamlLoader.Load(new Uri(string.Format(BaseStyleUrl, "Dark"), UriKind.Absolute));
  103. }
  104. currentStyles.Add(newStyles);
  105. if (enableCustomTheme)
  106. {
  107. if (!string.IsNullOrWhiteSpace(themePath))
  108. {
  109. try
  110. {
  111. var themeContent = File.ReadAllText(themePath);
  112. var customStyle = AvaloniaRuntimeXamlLoader.Parse<IStyle>(themeContent);
  113. currentStyles.Add(customStyle);
  114. }
  115. catch (Exception ex)
  116. {
  117. Logger.Error?.Print(LogClass.Application, $"Failed to Apply Custom Theme. Error: {ex.Message}");
  118. }
  119. }
  120. }
  121. }
  122. catch (Exception)
  123. {
  124. Logger.Warning?.Print(LogClass.Application, "Failed to Apply Theme. A restart is needed to apply the selected theme");
  125. ShowRestartDialog();
  126. }
  127. }
  128. }
  129. }