MainView.xaml.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections;
  3. using Avalonia;
  4. using Avalonia.Controls;
  5. using Avalonia.Markup.Xaml;
  6. using Avalonia.Media;
  7. using Avalonia.Media.Immutable;
  8. using Avalonia.Platform;
  9. using Avalonia.Themes.Fluent;
  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.FindControl<TabControl>("Sidebar");
  20. if (AvaloniaLocator.Current.GetService<IRuntimePlatform>().GetRuntimeInfo().IsDesktop)
  21. {
  22. IList tabItems = ((IList)sideBar.Items);
  23. tabItems.Add(new TabItem()
  24. {
  25. Header = "Dialogs",
  26. Content = new DialogsPage()
  27. });
  28. tabItems.Add(new TabItem()
  29. {
  30. Header = "Screens",
  31. Content = new ScreenPage()
  32. });
  33. }
  34. var themes = this.Find<ComboBox>("Themes");
  35. themes.SelectionChanged += (sender, e) =>
  36. {
  37. if (themes.SelectedItem is CatalogTheme theme)
  38. {
  39. var themeStyle = Application.Current.Styles[0];
  40. if (theme == CatalogTheme.FluentLight)
  41. {
  42. if (App.Fluent.Mode != FluentThemeMode.Light)
  43. {
  44. App.Fluent.Mode = FluentThemeMode.Light;
  45. }
  46. Application.Current.Styles[0] = App.Fluent;
  47. Application.Current.Styles[1] = App.DataGridFluent;
  48. }
  49. else if (theme == CatalogTheme.FluentDark)
  50. {
  51. if (App.Fluent.Mode != FluentThemeMode.Dark)
  52. {
  53. App.Fluent.Mode = FluentThemeMode.Dark;
  54. }
  55. Application.Current.Styles[0] = App.Fluent;
  56. Application.Current.Styles[1] = App.DataGridFluent;
  57. }
  58. else if (theme == CatalogTheme.DefaultLight)
  59. {
  60. Application.Current.Styles[0] = App.DefaultLight;
  61. Application.Current.Styles[1] = App.DataGridDefault;
  62. }
  63. else if (theme == CatalogTheme.DefaultDark)
  64. {
  65. Application.Current.Styles[0] = App.DefaultDark;
  66. Application.Current.Styles[1] = App.DataGridDefault;
  67. }
  68. }
  69. };
  70. var decorations = this.Find<ComboBox>("Decorations");
  71. decorations.SelectionChanged += (sender, e) =>
  72. {
  73. if (VisualRoot is Window window
  74. && decorations.SelectedItem is SystemDecorations systemDecorations)
  75. {
  76. window.SystemDecorations = systemDecorations;
  77. }
  78. };
  79. var transparencyLevels = this.Find<ComboBox>("TransparencyLevels");
  80. IDisposable backgroundSetter = null, paneBackgroundSetter = null;
  81. transparencyLevels.SelectionChanged += (sender, e) =>
  82. {
  83. backgroundSetter?.Dispose();
  84. paneBackgroundSetter?.Dispose();
  85. if (transparencyLevels.SelectedItem is WindowTransparencyLevel selected
  86. && selected != WindowTransparencyLevel.None)
  87. {
  88. var semiTransparentBrush = new ImmutableSolidColorBrush(Colors.Gray, 0.5);
  89. backgroundSetter = sideBar.SetValue(BackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
  90. paneBackgroundSetter = sideBar.SetValue(SplitView.PaneBackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
  91. }
  92. };
  93. }
  94. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  95. {
  96. base.OnAttachedToVisualTree(e);
  97. var decorations = this.Find<ComboBox>("Decorations");
  98. if (VisualRoot is Window window)
  99. decorations.SelectedIndex = (int)window.SystemDecorations;
  100. }
  101. }
  102. }