MainView.xaml.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.VisualTree;
  12. using Avalonia.Styling;
  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.SetCatalogThemes(theme);
  42. }
  43. };
  44. var themeVariants = this.Get<ComboBox>("ThemeVariants");
  45. themeVariants.SelectedItem = Application.Current!.RequestedThemeVariant;
  46. themeVariants.SelectionChanged += (sender, e) =>
  47. {
  48. if (themeVariants.SelectedItem is ThemeVariant themeVariant)
  49. {
  50. Application.Current!.RequestedThemeVariant = themeVariant;
  51. }
  52. };
  53. var flowDirections = this.Get<ComboBox>("FlowDirection");
  54. flowDirections.SelectionChanged += (sender, e) =>
  55. {
  56. if (flowDirections.SelectedItem is FlowDirection flowDirection)
  57. {
  58. TopLevel.GetTopLevel(this).FlowDirection = flowDirection;
  59. }
  60. };
  61. var decorations = this.Get<ComboBox>("Decorations");
  62. decorations.SelectionChanged += (sender, e) =>
  63. {
  64. if (VisualRoot is Window window
  65. && decorations.SelectedItem is SystemDecorations systemDecorations)
  66. {
  67. window.SystemDecorations = systemDecorations;
  68. }
  69. };
  70. var transparencyLevels = this.Get<ComboBox>("TransparencyLevels");
  71. IDisposable? topLevelBackgroundSideSetter = null, sideBarBackgroundSetter = null, paneBackgroundSetter = null;
  72. transparencyLevels.SelectionChanged += (sender, e) =>
  73. {
  74. topLevelBackgroundSideSetter?.Dispose();
  75. sideBarBackgroundSetter?.Dispose();
  76. paneBackgroundSetter?.Dispose();
  77. if (transparencyLevels.SelectedItem is WindowTransparencyLevel selected)
  78. {
  79. var topLevel = (TopLevel)this.GetVisualRoot()!;
  80. topLevel.TransparencyLevelHint = selected;
  81. if (selected != WindowTransparencyLevel.None)
  82. {
  83. var transparentBrush = new ImmutableSolidColorBrush(Colors.White, 0);
  84. var semiTransparentBrush = new ImmutableSolidColorBrush(Colors.Gray, 0.2);
  85. topLevelBackgroundSideSetter = topLevel.SetValue(BackgroundProperty, transparentBrush, Avalonia.Data.BindingPriority.Style);
  86. sideBarBackgroundSetter = sideBar.SetValue(BackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
  87. paneBackgroundSetter = sideBar.SetValue(SplitView.PaneBackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
  88. }
  89. }
  90. };
  91. }
  92. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  93. {
  94. base.OnAttachedToVisualTree(e);
  95. var decorations = this.Get<ComboBox>("Decorations");
  96. if (VisualRoot is Window window)
  97. decorations.SelectedIndex = (int)window.SystemDecorations;
  98. _platformSettings.ColorValuesChanged += PlatformSettingsOnColorValuesChanged;
  99. PlatformSettingsOnColorValuesChanged(_platformSettings, _platformSettings.GetColorValues());
  100. }
  101. protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
  102. {
  103. base.OnDetachedFromLogicalTree(e);
  104. _platformSettings.ColorValuesChanged -= PlatformSettingsOnColorValuesChanged;
  105. }
  106. private void PlatformSettingsOnColorValuesChanged(object? sender, PlatformColorValues e)
  107. {
  108. Application.Current!.Resources["SystemAccentColor"] = e.AccentColor1;
  109. Application.Current.Resources["SystemAccentColorDark1"] = ChangeColorLuminosity(e.AccentColor1, -0.3);
  110. Application.Current.Resources["SystemAccentColorDark2"] = ChangeColorLuminosity(e.AccentColor1, -0.5);
  111. Application.Current.Resources["SystemAccentColorDark3"] = ChangeColorLuminosity(e.AccentColor1, -0.7);
  112. Application.Current.Resources["SystemAccentColorLight1"] = ChangeColorLuminosity(e.AccentColor1, 0.3);
  113. Application.Current.Resources["SystemAccentColorLight2"] = ChangeColorLuminosity(e.AccentColor1, 0.5);
  114. Application.Current.Resources["SystemAccentColorLight3"] = ChangeColorLuminosity(e.AccentColor1, 0.7);
  115. static Color ChangeColorLuminosity(Color color, double luminosityFactor)
  116. {
  117. var red = (double)color.R;
  118. var green = (double)color.G;
  119. var blue = (double)color.B;
  120. if (luminosityFactor < 0)
  121. {
  122. luminosityFactor = 1 + luminosityFactor;
  123. red *= luminosityFactor;
  124. green *= luminosityFactor;
  125. blue *= luminosityFactor;
  126. }
  127. else if (luminosityFactor >= 0)
  128. {
  129. red = (255 - red) * luminosityFactor + red;
  130. green = (255 - green) * luminosityFactor + green;
  131. blue = (255 - blue) * luminosityFactor + blue;
  132. }
  133. return new Color(color.A, (byte)red, (byte)green, (byte)blue);
  134. }
  135. }
  136. }
  137. }