MainView.xaml.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 = "Screens",
  26. Content = new ScreenPage()
  27. });
  28. }
  29. var themes = this.Get<ComboBox>("Themes");
  30. themes.SelectionChanged += (sender, e) =>
  31. {
  32. if (themes.SelectedItem is CatalogTheme theme)
  33. {
  34. var themeStyle = Application.Current.Styles[0];
  35. if (theme == CatalogTheme.FluentLight)
  36. {
  37. if (App.Fluent.Mode != FluentThemeMode.Light)
  38. {
  39. App.Fluent.Mode = FluentThemeMode.Light;
  40. }
  41. Application.Current.Styles[0] = App.Fluent;
  42. Application.Current.Styles[1] = App.ColorPickerFluent;
  43. Application.Current.Styles[2] = App.DataGridFluent;
  44. }
  45. else if (theme == CatalogTheme.FluentDark)
  46. {
  47. if (App.Fluent.Mode != FluentThemeMode.Dark)
  48. {
  49. App.Fluent.Mode = FluentThemeMode.Dark;
  50. }
  51. Application.Current.Styles[0] = App.Fluent;
  52. Application.Current.Styles[1] = App.ColorPickerFluent;
  53. Application.Current.Styles[2] = App.DataGridFluent;
  54. }
  55. else if (theme == CatalogTheme.DefaultLight)
  56. {
  57. App.Default.Mode = Avalonia.Themes.Simple.SimpleThemeMode.Light;
  58. Application.Current.Styles[0] = App.DefaultLight;
  59. Application.Current.Styles[1] = App.ColorPickerDefault;
  60. Application.Current.Styles[2] = App.DataGridDefault;
  61. }
  62. else if (theme == CatalogTheme.DefaultDark)
  63. {
  64. App.Default.Mode = Avalonia.Themes.Simple.SimpleThemeMode.Dark;
  65. Application.Current.Styles[0] = App.DefaultDark;
  66. Application.Current.Styles[1] = App.ColorPickerDefault;
  67. Application.Current.Styles[2] = App.DataGridDefault;
  68. }
  69. }
  70. };
  71. var flowDirections = this.Get<ComboBox>("FlowDirection");
  72. flowDirections.SelectionChanged += (sender, e) =>
  73. {
  74. if (flowDirections.SelectedItem is FlowDirection flowDirection)
  75. {
  76. this.FlowDirection = flowDirection;
  77. }
  78. };
  79. var decorations = this.Get<ComboBox>("Decorations");
  80. decorations.SelectionChanged += (sender, e) =>
  81. {
  82. if (VisualRoot is Window window
  83. && decorations.SelectedItem is SystemDecorations systemDecorations)
  84. {
  85. window.SystemDecorations = systemDecorations;
  86. }
  87. };
  88. var transparencyLevels = this.Get<ComboBox>("TransparencyLevels");
  89. IDisposable? backgroundSetter = null, paneBackgroundSetter = null;
  90. transparencyLevels.SelectionChanged += (sender, e) =>
  91. {
  92. backgroundSetter?.Dispose();
  93. paneBackgroundSetter?.Dispose();
  94. if (transparencyLevels.SelectedItem is WindowTransparencyLevel selected
  95. && selected != WindowTransparencyLevel.None)
  96. {
  97. var semiTransparentBrush = new ImmutableSolidColorBrush(Colors.Gray, 0.5);
  98. backgroundSetter = sideBar.SetValue(BackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
  99. paneBackgroundSetter = sideBar.SetValue(SplitView.PaneBackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
  100. }
  101. };
  102. }
  103. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  104. {
  105. base.OnAttachedToVisualTree(e);
  106. var decorations = this.Get<ComboBox>("Decorations");
  107. if (VisualRoot is Window window)
  108. decorations.SelectedIndex = (int)window.SystemDecorations;
  109. }
  110. }
  111. }