App.axaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 Ryujinx.Ui.Common.Helper;
  14. using System;
  15. using System.Diagnostics;
  16. using System.IO;
  17. namespace Ryujinx.Ava
  18. {
  19. public class App : Application
  20. {
  21. public override void Initialize()
  22. {
  23. Name = $"Ryujinx {Program.Version}";
  24. AvaloniaXamlLoader.Load(this);
  25. }
  26. public override void OnFrameworkInitializationCompleted()
  27. {
  28. if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  29. {
  30. desktop.MainWindow = new MainWindow();
  31. }
  32. base.OnFrameworkInitializationCompleted();
  33. if (Program.PreviewerDetached)
  34. {
  35. ApplyConfiguredTheme();
  36. ConfigurationState.Instance.Ui.BaseStyle.Event += ThemeChanged_Event;
  37. ConfigurationState.Instance.Ui.CustomThemePath.Event += ThemeChanged_Event;
  38. ConfigurationState.Instance.Ui.EnableCustomTheme.Event += CustomThemeChanged_Event;
  39. }
  40. }
  41. private void CustomThemeChanged_Event(object sender, ReactiveEventArgs<bool> e)
  42. {
  43. ApplyConfiguredTheme();
  44. }
  45. private void ShowRestartDialog()
  46. {
  47. #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
  48. Dispatcher.UIThread.InvokeAsync(async () =>
  49. {
  50. if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  51. {
  52. var result = await ContentDialogHelper.CreateConfirmationDialog(
  53. LocaleManager.Instance["DialogThemeRestartMessage"],
  54. LocaleManager.Instance["DialogThemeRestartSubMessage"],
  55. LocaleManager.Instance["InputDialogYes"],
  56. LocaleManager.Instance["InputDialogNo"],
  57. LocaleManager.Instance["DialogRestartRequiredMessage"]);
  58. if (result == UserResult.Yes)
  59. {
  60. var path = Process.GetCurrentProcess().MainModule.FileName;
  61. var proc = Process.Start(path, CommandLineState.Arguments);
  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. }