App.axaml.cs 4.2 KB

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