MainView.xaml.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections;
  3. using Avalonia;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.ApplicationLifetimes;
  6. using Avalonia.Markup.Xaml;
  7. using Avalonia.Media;
  8. using Avalonia.Media.Immutable;
  9. using Avalonia.VisualTree;
  10. using ControlCatalog.Models;
  11. using ControlCatalog.Pages;
  12. namespace ControlCatalog
  13. {
  14. public class MainView : UserControl
  15. {
  16. public MainView()
  17. {
  18. AvaloniaXamlLoader.Load(this);
  19. var sideBar = this.Get<TabControl>("Sidebar");
  20. if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime)
  21. {
  22. var tabItems = (sideBar.Items as IList);
  23. tabItems?.Add(new TabItem()
  24. {
  25. Header = "Screens",
  26. Content = new ScreenPage()
  27. });
  28. }
  29. var themes = this.Get<ComboBox>("Themes");
  30. themes.SelectedItem = App.CurrentTheme;
  31. themes.SelectionChanged += (sender, e) =>
  32. {
  33. if (themes.SelectedItem is CatalogTheme theme)
  34. {
  35. App.SetThemeVariant(theme);
  36. }
  37. };
  38. var flowDirections = this.Get<ComboBox>("FlowDirection");
  39. flowDirections.SelectionChanged += (sender, e) =>
  40. {
  41. if (flowDirections.SelectedItem is FlowDirection flowDirection)
  42. {
  43. this.FlowDirection = flowDirection;
  44. }
  45. };
  46. var decorations = this.Get<ComboBox>("Decorations");
  47. decorations.SelectionChanged += (sender, e) =>
  48. {
  49. if (VisualRoot is Window window
  50. && decorations.SelectedItem is SystemDecorations systemDecorations)
  51. {
  52. window.SystemDecorations = systemDecorations;
  53. }
  54. };
  55. var transparencyLevels = this.Get<ComboBox>("TransparencyLevels");
  56. IDisposable? topLevelBackgroundSideSetter = null, sideBarBackgroundSetter = null, paneBackgroundSetter = null;
  57. transparencyLevels.SelectionChanged += (sender, e) =>
  58. {
  59. topLevelBackgroundSideSetter?.Dispose();
  60. sideBarBackgroundSetter?.Dispose();
  61. paneBackgroundSetter?.Dispose();
  62. if (transparencyLevels.SelectedItem is WindowTransparencyLevel selected)
  63. {
  64. var topLevel = (TopLevel)this.GetVisualRoot()!;
  65. topLevel.TransparencyLevelHint = selected;
  66. if (selected != WindowTransparencyLevel.None)
  67. {
  68. var transparentBrush = new ImmutableSolidColorBrush(Colors.White, 0);
  69. var semiTransparentBrush = new ImmutableSolidColorBrush(Colors.Gray, 0.2);
  70. topLevelBackgroundSideSetter = topLevel.SetValue(BackgroundProperty, transparentBrush, Avalonia.Data.BindingPriority.Style);
  71. sideBarBackgroundSetter = sideBar.SetValue(BackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
  72. paneBackgroundSetter = sideBar.SetValue(SplitView.PaneBackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
  73. }
  74. }
  75. };
  76. }
  77. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  78. {
  79. base.OnAttachedToVisualTree(e);
  80. var decorations = this.Get<ComboBox>("Decorations");
  81. if (VisualRoot is Window window)
  82. decorations.SelectedIndex = (int)window.SystemDecorations;
  83. }
  84. }
  85. }