App.axaml.cs 5.5 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 FluentAvalonia.UI.Windowing;
  8. using Gommon;
  9. using Ryujinx.Ava.Common;
  10. using Ryujinx.Ava.Common.Locale;
  11. using Ryujinx.Ava.UI.Helpers;
  12. using Ryujinx.Ava.UI.Windows;
  13. using Ryujinx.Common;
  14. using Ryujinx.Common.Logging;
  15. using Ryujinx.UI.Common.Configuration;
  16. using Ryujinx.UI.Common.Helper;
  17. using System;
  18. using System.Diagnostics;
  19. namespace Ryujinx.Ava
  20. {
  21. public class App : Application
  22. {
  23. internal static string FormatTitle(LocaleKeys? windowTitleKey = null)
  24. => windowTitleKey is null
  25. ? $"Ryujinx {Program.Version}"
  26. : $"Ryujinx {Program.Version} - {LocaleManager.Instance[windowTitleKey.Value]}";
  27. public static MainWindow MainWindow => Current!
  28. .ApplicationLifetime.Cast<IClassicDesktopStyleApplicationLifetime>()
  29. .MainWindow.Cast<MainWindow>();
  30. public static void SetTaskbarProgress(TaskBarProgressBarState state) => MainWindow.PlatformFeatures.SetTaskBarProgressBarState(state);
  31. public static void SetTaskbarProgressValue(ulong current, ulong total) => MainWindow.PlatformFeatures.SetTaskBarProgressBarValue(current, total);
  32. public static void SetTaskbarProgressValue(long current, long total) => SetTaskbarProgressValue(Convert.ToUInt64(current), Convert.ToUInt64(total));
  33. public override void Initialize()
  34. {
  35. Name = FormatTitle();
  36. AvaloniaXamlLoader.Load(this);
  37. if (OperatingSystem.IsMacOS())
  38. {
  39. Process.Start("/usr/bin/defaults", "write org.ryujinx.Ryujinx ApplePressAndHoldEnabled -bool false");
  40. }
  41. }
  42. public override void OnFrameworkInitializationCompleted()
  43. {
  44. if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  45. {
  46. desktop.MainWindow = new MainWindow();
  47. }
  48. base.OnFrameworkInitializationCompleted();
  49. if (Program.PreviewerDetached)
  50. {
  51. ApplyConfiguredTheme();
  52. ConfigurationState.Instance.UI.BaseStyle.Event += ThemeChanged_Event;
  53. ConfigurationState.Instance.UI.CustomThemePath.Event += ThemeChanged_Event;
  54. ConfigurationState.Instance.UI.EnableCustomTheme.Event += CustomThemeChanged_Event;
  55. }
  56. }
  57. private void ShowRestartDialog()
  58. {
  59. _ = Dispatcher.UIThread.InvokeAsync(async () =>
  60. {
  61. if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  62. {
  63. var result = await ContentDialogHelper.CreateConfirmationDialog(
  64. LocaleManager.Instance[LocaleKeys.DialogThemeRestartMessage],
  65. LocaleManager.Instance[LocaleKeys.DialogThemeRestartSubMessage],
  66. LocaleManager.Instance[LocaleKeys.InputDialogYes],
  67. LocaleManager.Instance[LocaleKeys.InputDialogNo],
  68. LocaleManager.Instance[LocaleKeys.DialogRestartRequiredMessage]);
  69. if (result == UserResult.Yes)
  70. {
  71. _ = Process.Start(Environment.ProcessPath!, CommandLineState.Arguments);
  72. desktop.Shutdown();
  73. Environment.Exit(0);
  74. }
  75. }
  76. });
  77. }
  78. private void CustomThemeChanged_Event(object _, ReactiveEventArgs<bool> __) => ApplyConfiguredTheme();
  79. private void ThemeChanged_Event(object _, ReactiveEventArgs<string> __) => ApplyConfiguredTheme();
  80. public void ApplyConfiguredTheme()
  81. {
  82. try
  83. {
  84. string baseStyle = ConfigurationState.Instance.UI.BaseStyle;
  85. if (string.IsNullOrWhiteSpace(baseStyle))
  86. {
  87. ConfigurationState.Instance.UI.BaseStyle.Value = "Auto";
  88. baseStyle = ConfigurationState.Instance.UI.BaseStyle;
  89. }
  90. ThemeVariant systemTheme = DetectSystemTheme();
  91. ThemeManager.OnThemeChanged();
  92. RequestedThemeVariant = baseStyle switch
  93. {
  94. "Auto" => systemTheme,
  95. "Light" => ThemeVariant.Light,
  96. "Dark" => ThemeVariant.Dark,
  97. _ => ThemeVariant.Default,
  98. };
  99. }
  100. catch (Exception)
  101. {
  102. Logger.Warning?.Print(LogClass.Application, "Failed to apply theme. A restart is needed to apply the selected theme.");
  103. ShowRestartDialog();
  104. }
  105. }
  106. /// <summary>
  107. /// Converts a PlatformThemeVariant value to the corresponding ThemeVariant value.
  108. /// </summary>
  109. public static ThemeVariant ConvertThemeVariant(PlatformThemeVariant platformThemeVariant) =>
  110. platformThemeVariant switch
  111. {
  112. PlatformThemeVariant.Dark => ThemeVariant.Dark,
  113. PlatformThemeVariant.Light => ThemeVariant.Light,
  114. _ => ThemeVariant.Default,
  115. };
  116. public static ThemeVariant DetectSystemTheme() =>
  117. Current is App { PlatformSettings: not null } app
  118. ? ConvertThemeVariant(app.PlatformSettings.GetColorValues().ThemeVariant)
  119. : ThemeVariant.Default;
  120. }
  121. }