MainView.xaml.cs 4.8 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 (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. App.Default.Mode = Avalonia.Themes.Default.SimpleThemeMode.Light;
  61. Application.Current.Styles[0] = App.DefaultLight;
  62. Application.Current.Styles[1] = App.DataGridDefault;
  63. }
  64. else if (theme == CatalogTheme.DefaultDark)
  65. {
  66. App.Default.Mode = Avalonia.Themes.Default.SimpleThemeMode.Dark;
  67. Application.Current.Styles[0] = App.DefaultDark;
  68. Application.Current.Styles[1] = App.DataGridDefault;
  69. }
  70. }
  71. };
  72. var flowDirections = this.Find<ComboBox>("FlowDirection");
  73. flowDirections.SelectionChanged += (sender, e) =>
  74. {
  75. if (flowDirections.SelectedItem is FlowDirection flowDirection)
  76. {
  77. this.FlowDirection = flowDirection;
  78. }
  79. };
  80. var decorations = this.Find<ComboBox>("Decorations");
  81. decorations.SelectionChanged += (sender, e) =>
  82. {
  83. if (VisualRoot is Window window
  84. && decorations.SelectedItem is SystemDecorations systemDecorations)
  85. {
  86. window.SystemDecorations = systemDecorations;
  87. }
  88. };
  89. var transparencyLevels = this.Find<ComboBox>("TransparencyLevels");
  90. IDisposable backgroundSetter = null, paneBackgroundSetter = null;
  91. transparencyLevels.SelectionChanged += (sender, e) =>
  92. {
  93. backgroundSetter?.Dispose();
  94. paneBackgroundSetter?.Dispose();
  95. if (transparencyLevels.SelectedItem is WindowTransparencyLevel selected
  96. && selected != WindowTransparencyLevel.None)
  97. {
  98. var semiTransparentBrush = new ImmutableSolidColorBrush(Colors.Gray, 0.5);
  99. backgroundSetter = sideBar.SetValue(BackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
  100. paneBackgroundSetter = sideBar.SetValue(SplitView.PaneBackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
  101. }
  102. };
  103. }
  104. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  105. {
  106. base.OnAttachedToVisualTree(e);
  107. var decorations = this.Find<ComboBox>("Decorations");
  108. if (VisualRoot is Window window)
  109. decorations.SelectedIndex = (int)window.SystemDecorations;
  110. }
  111. }
  112. }