MainView.xaml.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections;
  3. using Avalonia;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.ApplicationLifetimes;
  6. using Avalonia.LogicalTree;
  7. using Avalonia.Markup.Xaml;
  8. using Avalonia.Media;
  9. using Avalonia.Media.Immutable;
  10. using Avalonia.Platform;
  11. using Avalonia.Styling;
  12. using Avalonia.VisualTree;
  13. using ControlCatalog.Models;
  14. using ControlCatalog.Pages;
  15. namespace ControlCatalog
  16. {
  17. public class MainView : UserControl
  18. {
  19. private readonly IPlatformSettings _platformSettings;
  20. public MainView()
  21. {
  22. AvaloniaXamlLoader.Load(this);
  23. _platformSettings = AvaloniaLocator.Current.GetRequiredService<IPlatformSettings>();
  24. PlatformSettingsOnColorValuesChanged(_platformSettings, _platformSettings.GetColorValues());
  25. var sideBar = this.Get<TabControl>("Sidebar");
  26. if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime)
  27. {
  28. var tabItems = (sideBar.Items as IList);
  29. tabItems?.Add(new TabItem()
  30. {
  31. Header = "Screens",
  32. Content = new ScreenPage()
  33. });
  34. }
  35. var themes = this.Get<ComboBox>("Themes");
  36. themes.SelectedItem = App.CurrentTheme;
  37. themes.SelectionChanged += (sender, e) =>
  38. {
  39. if (themes.SelectedItem is CatalogTheme theme)
  40. {
  41. App.SetThemeVariant(theme);
  42. ((TopLevel?)this.GetVisualRoot())?.PlatformImpl?.SetFrameThemeVariant(theme switch
  43. {
  44. CatalogTheme.FluentLight => PlatformThemeVariant.Light,
  45. CatalogTheme.FluentDark => PlatformThemeVariant.Dark,
  46. CatalogTheme.SimpleLight => PlatformThemeVariant.Light,
  47. CatalogTheme.SimpleDark => PlatformThemeVariant.Dark,
  48. _ => throw new ArgumentOutOfRangeException()
  49. });
  50. }
  51. };
  52. var flowDirections = this.Get<ComboBox>("FlowDirection");
  53. flowDirections.SelectionChanged += (sender, e) =>
  54. {
  55. if (flowDirections.SelectedItem is FlowDirection flowDirection)
  56. {
  57. this.FlowDirection = flowDirection;
  58. }
  59. };
  60. var decorations = this.Get<ComboBox>("Decorations");
  61. decorations.SelectionChanged += (sender, e) =>
  62. {
  63. if (VisualRoot is Window window
  64. && decorations.SelectedItem is SystemDecorations systemDecorations)
  65. {
  66. window.SystemDecorations = systemDecorations;
  67. }
  68. };
  69. var transparencyLevels = this.Get<ComboBox>("TransparencyLevels");
  70. IDisposable? topLevelBackgroundSideSetter = null, sideBarBackgroundSetter = null, paneBackgroundSetter = null;
  71. transparencyLevels.SelectionChanged += (sender, e) =>
  72. {
  73. topLevelBackgroundSideSetter?.Dispose();
  74. sideBarBackgroundSetter?.Dispose();
  75. paneBackgroundSetter?.Dispose();
  76. if (transparencyLevels.SelectedItem is WindowTransparencyLevel selected)
  77. {
  78. var topLevel = (TopLevel)this.GetVisualRoot()!;
  79. topLevel.TransparencyLevelHint = selected;
  80. if (selected != WindowTransparencyLevel.None)
  81. {
  82. var transparentBrush = new ImmutableSolidColorBrush(Colors.White, 0);
  83. var semiTransparentBrush = new ImmutableSolidColorBrush(Colors.Gray, 0.2);
  84. topLevelBackgroundSideSetter = topLevel.SetValue(BackgroundProperty, transparentBrush, Avalonia.Data.BindingPriority.Style);
  85. sideBarBackgroundSetter = sideBar.SetValue(BackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
  86. paneBackgroundSetter = sideBar.SetValue(SplitView.PaneBackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
  87. }
  88. }
  89. };
  90. }
  91. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  92. {
  93. base.OnAttachedToVisualTree(e);
  94. var decorations = this.Get<ComboBox>("Decorations");
  95. if (VisualRoot is Window window)
  96. decorations.SelectedIndex = (int)window.SystemDecorations;
  97. _platformSettings.ColorValuesChanged += PlatformSettingsOnColorValuesChanged;
  98. }
  99. protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
  100. {
  101. base.OnDetachedFromLogicalTree(e);
  102. _platformSettings.ColorValuesChanged -= PlatformSettingsOnColorValuesChanged;
  103. }
  104. private void PlatformSettingsOnColorValuesChanged(object? sender, PlatformColorValues e)
  105. {
  106. var themes = this.Get<ComboBox>("Themes");
  107. var currentTheme = (CatalogTheme?)themes.SelectedItem ?? CatalogTheme.FluentLight;
  108. var newTheme = (currentTheme, e.ThemeVariant) switch
  109. {
  110. (CatalogTheme.FluentDark, PlatformThemeVariant.Light) => CatalogTheme.FluentLight,
  111. (CatalogTheme.FluentLight, PlatformThemeVariant.Dark) => CatalogTheme.FluentDark,
  112. (CatalogTheme.SimpleDark, PlatformThemeVariant.Light) => CatalogTheme.SimpleLight,
  113. (CatalogTheme.SimpleLight, PlatformThemeVariant.Dark) => CatalogTheme.SimpleDark,
  114. _ => currentTheme
  115. };
  116. themes.SelectedItem = newTheme;
  117. Application.Current!.Resources["SystemAccentColor"] = e.AccentColor1;
  118. Application.Current.Resources["SystemAccentColorDark1"] = ChangeColorLuminosity(e.AccentColor1, -0.3);
  119. Application.Current.Resources["SystemAccentColorDark2"] = ChangeColorLuminosity(e.AccentColor1, -0.5);
  120. Application.Current.Resources["SystemAccentColorDark3"] = ChangeColorLuminosity(e.AccentColor1, -0.7);
  121. Application.Current.Resources["SystemAccentColorLight1"] = ChangeColorLuminosity(e.AccentColor1, -0.3);
  122. Application.Current.Resources["SystemAccentColorLight2"] = ChangeColorLuminosity(e.AccentColor1, -0.5);
  123. Application.Current.Resources["SystemAccentColorLight3"] = ChangeColorLuminosity(e.AccentColor1, -0.7);
  124. static Color ChangeColorLuminosity(Color color, double luminosityFactor)
  125. {
  126. var red = (double)color.R;
  127. var green = (double)color.G;
  128. var blue = (double)color.B;
  129. if (luminosityFactor < 0)
  130. {
  131. luminosityFactor = 1 + luminosityFactor;
  132. red *= luminosityFactor;
  133. green *= luminosityFactor;
  134. blue *= luminosityFactor;
  135. }
  136. else if (luminosityFactor >= 0)
  137. {
  138. red = (255 - red) * luminosityFactor + red;
  139. green = (255 - green) * luminosityFactor + green;
  140. blue = (255 - blue) * luminosityFactor + blue;
  141. }
  142. return new Color(color.A, (byte)red, (byte)green, (byte)blue);
  143. }
  144. }
  145. }
  146. }