App.axaml.cs 5.6 KB

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