App.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.ApplicationLifetimes;
  5. using Avalonia.Markup.Xaml;
  6. using Avalonia.Styling;
  7. using Avalonia.Themes.Simple;
  8. using Avalonia.Themes.Fluent;
  9. using ControlCatalog.Models;
  10. using ControlCatalog.ViewModels;
  11. namespace ControlCatalog
  12. {
  13. public class App : Application
  14. {
  15. private readonly Styles _themeStylesContainer = new();
  16. private FluentTheme? _fluentTheme;
  17. private SimpleTheme? _simpleTheme;
  18. private IResourceDictionary? _fluentBaseLightColors, _fluentBaseDarkColors;
  19. private IStyle? _colorPickerFluent, _colorPickerSimple;
  20. private IStyle? _dataGridFluent, _dataGridSimple;
  21. public App()
  22. {
  23. DataContext = new ApplicationViewModel();
  24. }
  25. public override void Initialize()
  26. {
  27. Styles.Add(_themeStylesContainer);
  28. AvaloniaXamlLoader.Load(this);
  29. _fluentTheme = new FluentTheme();
  30. _simpleTheme = new SimpleTheme();
  31. _simpleTheme.Resources.MergedDictionaries.Add((IResourceDictionary)Resources["FluentAccentColors"]!);
  32. _simpleTheme.Resources.MergedDictionaries.Add((IResourceDictionary)Resources["FluentBaseColors"]!);
  33. _colorPickerFluent = (IStyle)Resources["ColorPickerFluent"]!;
  34. _colorPickerSimple = (IStyle)Resources["ColorPickerSimple"]!;
  35. _dataGridFluent = (IStyle)Resources["DataGridFluent"]!;
  36. _dataGridSimple = (IStyle)Resources["DataGridSimple"]!;
  37. _fluentBaseLightColors = (IResourceDictionary)Resources["FluentBaseLightColors"]!;
  38. _fluentBaseDarkColors = (IResourceDictionary)Resources["FluentBaseDarkColors"]!;
  39. SetThemeVariant(CatalogTheme.FluentLight);
  40. }
  41. public override void OnFrameworkInitializationCompleted()
  42. {
  43. if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
  44. {
  45. desktopLifetime.MainWindow = new MainWindow();
  46. }
  47. else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewLifetime)
  48. {
  49. singleViewLifetime.MainView = new MainView();
  50. }
  51. base.OnFrameworkInitializationCompleted();
  52. }
  53. private CatalogTheme _prevTheme;
  54. public static CatalogTheme CurrentTheme => ((App)Current!)._prevTheme;
  55. public static void SetThemeVariant(CatalogTheme theme)
  56. {
  57. var app = (App)Current!;
  58. var prevTheme = app._prevTheme;
  59. app._prevTheme = theme;
  60. var shouldReopenWindow = theme switch
  61. {
  62. CatalogTheme.FluentLight => prevTheme is CatalogTheme.SimpleDark or CatalogTheme.SimpleLight,
  63. CatalogTheme.FluentDark => prevTheme is CatalogTheme.SimpleDark or CatalogTheme.SimpleLight,
  64. CatalogTheme.SimpleLight => prevTheme is CatalogTheme.FluentDark or CatalogTheme.FluentLight,
  65. CatalogTheme.SimpleDark => prevTheme is CatalogTheme.FluentDark or CatalogTheme.FluentLight,
  66. _ => throw new ArgumentOutOfRangeException(nameof(theme), theme, null)
  67. };
  68. if (app._themeStylesContainer.Count == 0)
  69. {
  70. app._themeStylesContainer.Add(new Style());
  71. app._themeStylesContainer.Add(new Style());
  72. app._themeStylesContainer.Add(new Style());
  73. }
  74. if (theme == CatalogTheme.FluentLight)
  75. {
  76. app._fluentTheme!.Mode = FluentThemeMode.Light;
  77. app._themeStylesContainer[0] = app._fluentTheme;
  78. app._themeStylesContainer[1] = app._colorPickerFluent!;
  79. app._themeStylesContainer[2] = app._dataGridFluent!;
  80. }
  81. else if (theme == CatalogTheme.FluentDark)
  82. {
  83. app._fluentTheme!.Mode = FluentThemeMode.Dark;
  84. app._themeStylesContainer[0] = app._fluentTheme;
  85. app._themeStylesContainer[1] = app._colorPickerFluent!;
  86. app._themeStylesContainer[2] = app._dataGridFluent!;
  87. }
  88. else if (theme == CatalogTheme.SimpleLight)
  89. {
  90. app._simpleTheme!.Mode = SimpleThemeMode.Light;
  91. app._simpleTheme.Resources.MergedDictionaries.Remove(app._fluentBaseDarkColors!);
  92. app._simpleTheme.Resources.MergedDictionaries.Add(app._fluentBaseLightColors!);
  93. app._themeStylesContainer[0] = app._simpleTheme;
  94. app._themeStylesContainer[1] = app._colorPickerSimple!;
  95. app._themeStylesContainer[2] = app._dataGridSimple!;
  96. }
  97. else if (theme == CatalogTheme.SimpleDark)
  98. {
  99. app._simpleTheme!.Mode = SimpleThemeMode.Dark;
  100. app._simpleTheme.Resources.MergedDictionaries.Remove(app._fluentBaseLightColors!);
  101. app._simpleTheme.Resources.MergedDictionaries.Add(app._fluentBaseDarkColors!);
  102. app._themeStylesContainer[0] = app._simpleTheme;
  103. app._themeStylesContainer[1] = app._colorPickerSimple!;
  104. app._themeStylesContainer[2] = app._dataGridSimple!;
  105. }
  106. if (shouldReopenWindow)
  107. {
  108. if (app.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
  109. {
  110. var oldWindow = desktopLifetime.MainWindow;
  111. var newWindow = new MainWindow();
  112. desktopLifetime.MainWindow = newWindow;
  113. newWindow.Show();
  114. oldWindow?.Close();
  115. }
  116. else if (app.ApplicationLifetime is ISingleViewApplicationLifetime singleViewLifetime)
  117. {
  118. singleViewLifetime.MainView = new MainView();
  119. }
  120. }
  121. }
  122. }
  123. }