MainView.xaml.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 ControlCatalog.Models;
  10. using ControlCatalog.Pages;
  11. namespace ControlCatalog
  12. {
  13. public class MainView : UserControl
  14. {
  15. public MainView()
  16. {
  17. AvaloniaXamlLoader.Load(this);
  18. var sideBar = this.Get<TabControl>("Sidebar");
  19. if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime)
  20. {
  21. var tabItems = (sideBar.Items as IList);
  22. tabItems?.Add(new TabItem()
  23. {
  24. Header = "Screens",
  25. Content = new ScreenPage()
  26. });
  27. }
  28. var themes = this.Get<ComboBox>("Themes");
  29. themes.SelectedItem = App.CurrentTheme;
  30. themes.SelectionChanged += (sender, e) =>
  31. {
  32. if (themes.SelectedItem is CatalogTheme theme)
  33. {
  34. App.SetThemeVariant(theme);
  35. }
  36. };
  37. var flowDirections = this.Get<ComboBox>("FlowDirection");
  38. flowDirections.SelectionChanged += (sender, e) =>
  39. {
  40. if (flowDirections.SelectedItem is FlowDirection flowDirection)
  41. {
  42. this.FlowDirection = flowDirection;
  43. }
  44. };
  45. var decorations = this.Get<ComboBox>("Decorations");
  46. decorations.SelectionChanged += (sender, e) =>
  47. {
  48. if (VisualRoot is Window window
  49. && decorations.SelectedItem is SystemDecorations systemDecorations)
  50. {
  51. window.SystemDecorations = systemDecorations;
  52. }
  53. };
  54. var transparencyLevels = this.Get<ComboBox>("TransparencyLevels");
  55. IDisposable? backgroundSetter = null, paneBackgroundSetter = null;
  56. transparencyLevels.SelectionChanged += (sender, e) =>
  57. {
  58. backgroundSetter?.Dispose();
  59. paneBackgroundSetter?.Dispose();
  60. if (transparencyLevels.SelectedItem is WindowTransparencyLevel selected
  61. && selected != WindowTransparencyLevel.None)
  62. {
  63. var semiTransparentBrush = new ImmutableSolidColorBrush(Colors.Gray, 0.5);
  64. backgroundSetter = sideBar.SetValue(BackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
  65. paneBackgroundSetter = sideBar.SetValue(SplitView.PaneBackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
  66. }
  67. };
  68. }
  69. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  70. {
  71. base.OnAttachedToVisualTree(e);
  72. var decorations = this.Get<ComboBox>("Decorations");
  73. if (VisualRoot is Window window)
  74. decorations.SelectedIndex = (int)window.SystemDecorations;
  75. }
  76. }
  77. }