MainView.xaml.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 (themeStyle is FluentTheme fluentTheme)
  43. {
  44. if (fluentTheme.Mode == FluentThemeMode.Dark)
  45. {
  46. fluentTheme.Mode = FluentThemeMode.Light;
  47. }
  48. }
  49. else
  50. {
  51. Application.Current.Styles[0] = new FluentTheme(new Uri("avares://ControlCatalog/Styles"));
  52. Application.Current.Styles[1] = App.DataGridFluent;
  53. }
  54. }
  55. else if (theme == CatalogTheme.FluentDark)
  56. {
  57. if (themeStyle is FluentTheme fluentTheme)
  58. {
  59. if (fluentTheme.Mode == FluentThemeMode.Light)
  60. {
  61. fluentTheme.Mode = FluentThemeMode.Dark;
  62. }
  63. }
  64. else
  65. {
  66. Application.Current.Styles[0] = new FluentTheme(new Uri("avares://ControlCatalog/Styles")) { Mode = FluentThemeMode.Dark };
  67. Application.Current.Styles[1] = App.DataGridFluent;
  68. }
  69. }
  70. else if (theme == CatalogTheme.DefaultLight)
  71. {
  72. Application.Current.Styles[0] = App.DefaultLight;
  73. Application.Current.Styles[1] = App.DataGridDefault;
  74. }
  75. else if (theme == CatalogTheme.DefaultDark)
  76. {
  77. Application.Current.Styles[0] = App.DefaultDark;
  78. Application.Current.Styles[1] = App.DataGridDefault;
  79. }
  80. }
  81. };
  82. var decorations = this.Find<ComboBox>("Decorations");
  83. decorations.SelectionChanged += (sender, e) =>
  84. {
  85. if (VisualRoot is Window window
  86. && decorations.SelectedItem is SystemDecorations systemDecorations)
  87. {
  88. window.SystemDecorations = systemDecorations;
  89. }
  90. };
  91. var transparencyLevels = this.Find<ComboBox>("TransparencyLevels");
  92. IDisposable backgroundSetter = null, paneBackgroundSetter = null;
  93. transparencyLevels.SelectionChanged += (sender, e) =>
  94. {
  95. backgroundSetter?.Dispose();
  96. paneBackgroundSetter?.Dispose();
  97. if (transparencyLevels.SelectedItem is WindowTransparencyLevel selected
  98. && selected != WindowTransparencyLevel.None)
  99. {
  100. var semiTransparentBrush = new ImmutableSolidColorBrush(Colors.Gray, 0.5);
  101. backgroundSetter = sideBar.SetValue(BackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
  102. paneBackgroundSetter = sideBar.SetValue(SplitView.PaneBackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
  103. }
  104. };
  105. }
  106. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  107. {
  108. base.OnAttachedToVisualTree(e);
  109. var decorations = this.Find<ComboBox>("Decorations");
  110. if (VisualRoot is Window window)
  111. decorations.SelectedIndex = (int)window.SystemDecorations;
  112. }
  113. }
  114. }