App.axaml.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using Avalonia;
  2. using Avalonia.Controls.ApplicationLifetimes;
  3. using Avalonia.Markup.Xaml;
  4. using Avalonia.Styling;
  5. using FluentAvalonia.Styling;
  6. using Ryujinx.Ava.Ui.Windows;
  7. using Ryujinx.Common;
  8. using Ryujinx.Common.Logging;
  9. using Ryujinx.Ui.Common.Configuration;
  10. using System;
  11. using System.IO;
  12. namespace Ryujinx.Ava
  13. {
  14. public class App : Application
  15. {
  16. public override void Initialize()
  17. {
  18. AvaloniaXamlLoader.Load(this);
  19. }
  20. public override void OnFrameworkInitializationCompleted()
  21. {
  22. if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  23. {
  24. desktop.MainWindow = new MainWindow();
  25. }
  26. base.OnFrameworkInitializationCompleted();
  27. if (Program.PreviewerDetached)
  28. {
  29. ApplyConfiguredTheme();
  30. ConfigurationState.Instance.Ui.BaseStyle.Event += ThemeChanged_Event;
  31. ConfigurationState.Instance.Ui.CustomThemePath.Event += ThemeChanged_Event;
  32. ConfigurationState.Instance.Ui.EnableCustomTheme.Event += CustomThemeChanged_Event;
  33. }
  34. }
  35. private void CustomThemeChanged_Event(object sender, ReactiveEventArgs<bool> e)
  36. {
  37. ApplyConfiguredTheme();
  38. }
  39. private void ShowRestartDialog()
  40. {
  41. // TODO: Implement Restart Dialog when SettingsWindow is implemented.
  42. }
  43. private void ThemeChanged_Event(object sender, ReactiveEventArgs<string> e)
  44. {
  45. ApplyConfiguredTheme();
  46. }
  47. private void ApplyConfiguredTheme()
  48. {
  49. try
  50. {
  51. string baseStyle = ConfigurationState.Instance.Ui.BaseStyle;
  52. string themePath = ConfigurationState.Instance.Ui.CustomThemePath;
  53. bool enableCustomTheme = ConfigurationState.Instance.Ui.EnableCustomTheme;
  54. const string BaseStyleUrl = "avares://Ryujinx.Ava/Assets/Styles/Base{0}.xaml";
  55. if (string.IsNullOrWhiteSpace(baseStyle))
  56. {
  57. ConfigurationState.Instance.Ui.BaseStyle.Value = "Dark";
  58. baseStyle = ConfigurationState.Instance.Ui.BaseStyle;
  59. }
  60. var theme = AvaloniaLocator.Current.GetService<FluentAvaloniaTheme>();
  61. theme.RequestedTheme = baseStyle;
  62. var currentStyles = this.Styles;
  63. // Remove all styles except the base style.
  64. if (currentStyles.Count > 1)
  65. {
  66. currentStyles.RemoveRange(1, currentStyles.Count - 1);
  67. }
  68. IStyle newStyles = null;
  69. // Load requested style, and fallback to Dark theme if loading failed.
  70. try
  71. {
  72. newStyles = (Styles)AvaloniaXamlLoader.Load(new Uri(string.Format(BaseStyleUrl, baseStyle), UriKind.Absolute));
  73. }
  74. catch (XamlLoadException)
  75. {
  76. newStyles = (Styles)AvaloniaXamlLoader.Load(new Uri(string.Format(BaseStyleUrl, "Dark"), UriKind.Absolute));
  77. }
  78. currentStyles.Add(newStyles);
  79. if (enableCustomTheme)
  80. {
  81. if (!string.IsNullOrWhiteSpace(themePath))
  82. {
  83. try
  84. {
  85. var themeContent = File.ReadAllText(themePath);
  86. var customStyle = AvaloniaRuntimeXamlLoader.Parse<IStyle>(themeContent);
  87. currentStyles.Add(customStyle);
  88. }
  89. catch (Exception ex)
  90. {
  91. Logger.Error?.Print(LogClass.Application, $"Failed to Apply Custom Theme. Error: {ex.Message}");
  92. }
  93. }
  94. }
  95. }
  96. catch (Exception)
  97. {
  98. Logger.Warning?.Print(LogClass.Application, "Failed to Apply Theme. A restart is needed to apply the selected theme");
  99. ShowRestartDialog();
  100. }
  101. }
  102. }
  103. }