MainView.xaml.cs 5.1 KB

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