App.axaml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using Avalonia;
  2. using Avalonia.Controls.ApplicationLifetimes;
  3. using Avalonia.Markup.Xaml;
  4. using Avalonia.Platform;
  5. using Avalonia.Styling;
  6. using Avalonia.Threading;
  7. using Ryujinx.Ava.Common;
  8. using Ryujinx.Ava.Common.Locale;
  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. 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. if (OperatingSystem.IsMacOS())
  26. {
  27. Process.Start("/usr/bin/defaults", "write org.ryujinx.Ryujinx ApplePressAndHoldEnabled -bool false");
  28. }
  29. }
  30. public override void OnFrameworkInitializationCompleted()
  31. {
  32. if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  33. {
  34. desktop.MainWindow = new MainWindow();
  35. }
  36. base.OnFrameworkInitializationCompleted();
  37. if (Program.PreviewerDetached)
  38. {
  39. ApplyConfiguredTheme();
  40. ConfigurationState.Instance.UI.BaseStyle.Event += ThemeChanged_Event;
  41. ConfigurationState.Instance.UI.CustomThemePath.Event += ThemeChanged_Event;
  42. ConfigurationState.Instance.UI.EnableCustomTheme.Event += CustomThemeChanged_Event;
  43. }
  44. }
  45. private void CustomThemeChanged_Event(object sender, ReactiveEventArgs<bool> e)
  46. {
  47. ApplyConfiguredTheme();
  48. }
  49. private void ShowRestartDialog()
  50. {
  51. #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
  52. Dispatcher.UIThread.InvokeAsync(async () =>
  53. {
  54. if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  55. {
  56. var result = await ContentDialogHelper.CreateConfirmationDialog(
  57. LocaleManager.Instance[LocaleKeys.DialogThemeRestartMessage],
  58. LocaleManager.Instance[LocaleKeys.DialogThemeRestartSubMessage],
  59. LocaleManager.Instance[LocaleKeys.InputDialogYes],
  60. LocaleManager.Instance[LocaleKeys.InputDialogNo],
  61. LocaleManager.Instance[LocaleKeys.DialogRestartRequiredMessage]);
  62. if (result == UserResult.Yes)
  63. {
  64. var path = Environment.ProcessPath;
  65. var proc = Process.Start(path, CommandLineState.Arguments);
  66. desktop.Shutdown();
  67. Environment.Exit(0);
  68. }
  69. }
  70. });
  71. #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
  72. }
  73. private void ThemeChanged_Event(object sender, ReactiveEventArgs<string> e)
  74. {
  75. ApplyConfiguredTheme();
  76. }
  77. public void ApplyConfiguredTheme()
  78. {
  79. try
  80. {
  81. string baseStyle = ConfigurationState.Instance.UI.BaseStyle;
  82. if (string.IsNullOrWhiteSpace(baseStyle))
  83. {
  84. ConfigurationState.Instance.UI.BaseStyle.Value = "Auto";
  85. baseStyle = ConfigurationState.Instance.UI.BaseStyle;
  86. }
  87. ThemeVariant systemTheme = DetectSystemTheme();
  88. ThemeManager.OnThemeChanged();
  89. RequestedThemeVariant = baseStyle switch
  90. {
  91. "Auto" => systemTheme,
  92. "Light" => ThemeVariant.Light,
  93. "Dark" => ThemeVariant.Dark,
  94. _ => ThemeVariant.Default,
  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. /// <summary>
  104. /// Converts a PlatformThemeVariant value to the corresponding ThemeVariant value.
  105. /// </summary>
  106. public static ThemeVariant ConvertThemeVariant(PlatformThemeVariant platformThemeVariant) =>
  107. platformThemeVariant switch
  108. {
  109. PlatformThemeVariant.Dark => ThemeVariant.Dark,
  110. PlatformThemeVariant.Light => ThemeVariant.Light,
  111. _ => ThemeVariant.Default,
  112. };
  113. public static ThemeVariant DetectSystemTheme()
  114. {
  115. if (Application.Current is App app)
  116. {
  117. var colorValues = app.PlatformSettings.GetColorValues();
  118. return ConvertThemeVariant(colorValues.ThemeVariant);
  119. }
  120. return ThemeVariant.Default;
  121. }
  122. }
  123. }