MainView.xaml.cs 7.5 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.VisualTree;
  12. using ControlCatalog.Models;
  13. using ControlCatalog.Pages;
  14. namespace ControlCatalog
  15. {
  16. public class MainView : UserControl
  17. {
  18. private readonly IPlatformSettings _platformSettings;
  19. public MainView()
  20. {
  21. AvaloniaXamlLoader.Load(this);
  22. _platformSettings = AvaloniaLocator.Current.GetRequiredService<IPlatformSettings>();
  23. PlatformSettingsOnColorValuesChanged(_platformSettings, _platformSettings.GetColorValues());
  24. var sideBar = this.Get<TabControl>("Sidebar");
  25. if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime)
  26. {
  27. var tabItems = (sideBar.Items as IList);
  28. tabItems?.Add(new TabItem()
  29. {
  30. Header = "Screens",
  31. Content = new ScreenPage()
  32. });
  33. }
  34. var themes = this.Get<ComboBox>("Themes");
  35. themes.SelectedItem = App.CurrentTheme;
  36. themes.SelectionChanged += (sender, e) =>
  37. {
  38. if (themes.SelectedItem is CatalogTheme theme)
  39. {
  40. App.SetThemeVariant(theme);
  41. ((TopLevel?)this.GetVisualRoot())?.PlatformImpl?.SetFrameThemeVariant(theme switch
  42. {
  43. CatalogTheme.FluentLight => PlatformThemeVariant.Light,
  44. CatalogTheme.FluentDark => PlatformThemeVariant.Dark,
  45. CatalogTheme.SimpleLight => PlatformThemeVariant.Light,
  46. CatalogTheme.SimpleDark => PlatformThemeVariant.Dark,
  47. _ => throw new ArgumentOutOfRangeException()
  48. });
  49. }
  50. };
  51. var flowDirections = this.Get<ComboBox>("FlowDirection");
  52. flowDirections.SelectionChanged += (sender, e) =>
  53. {
  54. if (flowDirections.SelectedItem is FlowDirection flowDirection)
  55. {
  56. TopLevel.GetTopLevel(this).FlowDirection = flowDirection;
  57. }
  58. };
  59. var decorations = this.Get<ComboBox>("Decorations");
  60. decorations.SelectionChanged += (sender, e) =>
  61. {
  62. if (VisualRoot is Window window
  63. && decorations.SelectedItem is SystemDecorations systemDecorations)
  64. {
  65. window.SystemDecorations = systemDecorations;
  66. }
  67. };
  68. var transparencyLevels = this.Get<ComboBox>("TransparencyLevels");
  69. IDisposable? topLevelBackgroundSideSetter = null, sideBarBackgroundSetter = null, paneBackgroundSetter = null;
  70. transparencyLevels.SelectionChanged += (sender, e) =>
  71. {
  72. topLevelBackgroundSideSetter?.Dispose();
  73. sideBarBackgroundSetter?.Dispose();
  74. paneBackgroundSetter?.Dispose();
  75. if (transparencyLevels.SelectedItem is WindowTransparencyLevel selected)
  76. {
  77. var topLevel = (TopLevel)this.GetVisualRoot()!;
  78. topLevel.TransparencyLevelHint = selected;
  79. if (selected != WindowTransparencyLevel.None)
  80. {
  81. var transparentBrush = new ImmutableSolidColorBrush(Colors.White, 0);
  82. var semiTransparentBrush = new ImmutableSolidColorBrush(Colors.Gray, 0.2);
  83. topLevelBackgroundSideSetter = topLevel.SetValue(BackgroundProperty, transparentBrush, Avalonia.Data.BindingPriority.Style);
  84. sideBarBackgroundSetter = sideBar.SetValue(BackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
  85. paneBackgroundSetter = sideBar.SetValue(SplitView.PaneBackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
  86. }
  87. }
  88. };
  89. }
  90. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  91. {
  92. base.OnAttachedToVisualTree(e);
  93. var decorations = this.Get<ComboBox>("Decorations");
  94. if (VisualRoot is Window window)
  95. decorations.SelectedIndex = (int)window.SystemDecorations;
  96. _platformSettings.ColorValuesChanged += PlatformSettingsOnColorValuesChanged;
  97. PlatformSettingsOnColorValuesChanged(_platformSettings, _platformSettings.GetColorValues());
  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. }