MainView.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections;
  3. using System.Threading.Tasks;
  4. using Avalonia;
  5. using Avalonia.Controls;
  6. using Avalonia.Controls.ApplicationLifetimes;
  7. using Avalonia.LogicalTree;
  8. using Avalonia.Markup.Xaml;
  9. using Avalonia.Media;
  10. using Avalonia.Media.Immutable;
  11. using Avalonia.Platform;
  12. using Avalonia.VisualTree;
  13. using Avalonia.Styling;
  14. using ControlCatalog.Models;
  15. using ControlCatalog.Pages;
  16. using ControlCatalog.ViewModels;
  17. namespace ControlCatalog
  18. {
  19. public class MainView : UserControl
  20. {
  21. public MainView()
  22. {
  23. AvaloniaXamlLoader.Load(this);
  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.SetCatalogThemes(theme);
  41. }
  42. };
  43. var themeVariants = this.Get<ComboBox>("ThemeVariants");
  44. themeVariants.SelectedItem = Application.Current!.RequestedThemeVariant;
  45. themeVariants.SelectionChanged += (sender, e) =>
  46. {
  47. if (themeVariants.SelectedItem is ThemeVariant themeVariant)
  48. {
  49. Application.Current!.RequestedThemeVariant = themeVariant;
  50. }
  51. };
  52. var flowDirections = this.Get<ComboBox>("FlowDirection");
  53. flowDirections.SelectionChanged += (sender, e) =>
  54. {
  55. if (flowDirections.SelectedItem is FlowDirection flowDirection)
  56. {
  57. TopLevel.GetTopLevel(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 = new[] { selected };
  80. if (topLevel.ActualTransparencyLevel != WindowTransparencyLevel.None &&
  81. topLevel.ActualTransparencyLevel == selected)
  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. internal MainWindowViewModel ViewModel => (MainWindowViewModel)DataContext!;
  93. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  94. {
  95. base.OnAttachedToVisualTree(e);
  96. var decorations = this.Get<ComboBox>("Decorations");
  97. if (VisualRoot is Window window)
  98. decorations.SelectedIndex = (int)window.SystemDecorations;
  99. var insets = TopLevel.GetTopLevel(this)!.InsetsManager;
  100. if (insets != null)
  101. {
  102. // In real life application these events should be unsubscribed to avoid memory leaks.
  103. ViewModel.SafeAreaPadding = insets.SafeAreaPadding;
  104. insets.SafeAreaChanged += (sender, args) =>
  105. {
  106. ViewModel.SafeAreaPadding = insets.SafeAreaPadding;
  107. };
  108. ViewModel.DisplayEdgeToEdge = insets.DisplayEdgeToEdge;
  109. ViewModel.IsSystemBarVisible = insets.IsSystemBarVisible ?? true;
  110. ViewModel.PropertyChanged += async (sender, args) =>
  111. {
  112. if (args.PropertyName == nameof(ViewModel.DisplayEdgeToEdge))
  113. {
  114. insets.DisplayEdgeToEdge = ViewModel.DisplayEdgeToEdge;
  115. }
  116. else if (args.PropertyName == nameof(ViewModel.IsSystemBarVisible))
  117. {
  118. insets.IsSystemBarVisible = ViewModel.IsSystemBarVisible;
  119. }
  120. // Give the OS some time to apply new values and refresh the view model.
  121. await Task.Delay(100);
  122. ViewModel.DisplayEdgeToEdge = insets.DisplayEdgeToEdge;
  123. ViewModel.IsSystemBarVisible = insets.IsSystemBarVisible ?? true;
  124. };
  125. }
  126. }
  127. }
  128. }