123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- using System;
- using System.Collections;
- using System.Threading.Tasks;
- using Avalonia;
- using Avalonia.Controls;
- using Avalonia.Controls.ApplicationLifetimes;
- using Avalonia.LogicalTree;
- using Avalonia.Markup.Xaml;
- using Avalonia.Media;
- using Avalonia.Media.Immutable;
- using Avalonia.Platform;
- using Avalonia.VisualTree;
- using Avalonia.Styling;
- using ControlCatalog.Models;
- using ControlCatalog.Pages;
- using ControlCatalog.ViewModels;
- namespace ControlCatalog
- {
- public class MainView : UserControl
- {
- public MainView()
- {
- AvaloniaXamlLoader.Load(this);
-
- var sideBar = this.Get<TabControl>("Sidebar");
- var themes = this.Get<ComboBox>("Themes");
- themes.SelectedItem = App.CurrentTheme;
- themes.SelectionChanged += (sender, e) =>
- {
- if (themes.SelectedItem is CatalogTheme theme)
- {
- App.SetCatalogThemes(theme);
- }
- };
- var themeVariants = this.Get<ComboBox>("ThemeVariants");
- themeVariants.SelectedItem = Application.Current!.RequestedThemeVariant;
- themeVariants.SelectionChanged += (sender, e) =>
- {
- if (themeVariants.SelectedItem is ThemeVariant themeVariant)
- {
- Application.Current!.RequestedThemeVariant = themeVariant;
- }
- };
- var flowDirections = this.Get<ComboBox>("FlowDirection");
- flowDirections.SelectionChanged += (sender, e) =>
- {
- if (flowDirections.SelectedItem is FlowDirection flowDirection)
- {
- TopLevel.GetTopLevel(this)!.FlowDirection = flowDirection;
- }
- };
- var decorations = this.Get<ComboBox>("Decorations");
- decorations.SelectionChanged += (sender, e) =>
- {
- if (VisualRoot is Window window
- && decorations.SelectedItem is SystemDecorations systemDecorations)
- {
- window.SystemDecorations = systemDecorations;
- }
- };
- var transparencyLevels = this.Get<ComboBox>("TransparencyLevels");
- IDisposable? topLevelBackgroundSideSetter = null, sideBarBackgroundSetter = null, paneBackgroundSetter = null;
- transparencyLevels.SelectionChanged += (sender, e) =>
- {
- topLevelBackgroundSideSetter?.Dispose();
- sideBarBackgroundSetter?.Dispose();
- paneBackgroundSetter?.Dispose();
- if (transparencyLevels.SelectedItem is WindowTransparencyLevel selected)
- {
- var topLevel = (TopLevel)this.GetVisualRoot()!;
- topLevel.TransparencyLevelHint = new[] { selected };
- if (topLevel.ActualTransparencyLevel != WindowTransparencyLevel.None &&
- topLevel.ActualTransparencyLevel == selected)
- {
- var transparentBrush = new ImmutableSolidColorBrush(Colors.White, 0);
- var semiTransparentBrush = new ImmutableSolidColorBrush(Colors.Gray, 0.2);
- topLevelBackgroundSideSetter = topLevel.SetValue(BackgroundProperty, transparentBrush, Avalonia.Data.BindingPriority.Style);
- sideBarBackgroundSetter = sideBar.SetValue(BackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
- paneBackgroundSetter = sideBar.SetValue(SplitView.PaneBackgroundProperty, semiTransparentBrush, Avalonia.Data.BindingPriority.Style);
- }
- }
- };
- }
- internal MainWindowViewModel ViewModel => (MainWindowViewModel)DataContext!;
-
- protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
- {
- base.OnAttachedToVisualTree(e);
- var decorations = this.Get<ComboBox>("Decorations");
- if (VisualRoot is Window window)
- decorations.SelectedIndex = (int)window.SystemDecorations;
- var insets = TopLevel.GetTopLevel(this)!.InsetsManager;
- if (insets != null)
- {
- // In real life application these events should be unsubscribed to avoid memory leaks.
- ViewModel.SafeAreaPadding = insets.SafeAreaPadding;
- insets.SafeAreaChanged += (sender, args) =>
- {
- ViewModel.SafeAreaPadding = insets.SafeAreaPadding;
- };
- ViewModel.DisplayEdgeToEdge = insets.DisplayEdgeToEdge;
- ViewModel.IsSystemBarVisible = insets.IsSystemBarVisible ?? true;
- ViewModel.PropertyChanged += async (sender, args) =>
- {
- if (args.PropertyName == nameof(ViewModel.DisplayEdgeToEdge))
- {
- insets.DisplayEdgeToEdge = ViewModel.DisplayEdgeToEdge;
- }
- else if (args.PropertyName == nameof(ViewModel.IsSystemBarVisible))
- {
- insets.IsSystemBarVisible = ViewModel.IsSystemBarVisible;
- }
- // Give the OS some time to apply new values and refresh the view model.
- await Task.Delay(100);
- ViewModel.DisplayEdgeToEdge = insets.DisplayEdgeToEdge;
- ViewModel.IsSystemBarVisible = insets.IsSystemBarVisible ?? true;
- };
- }
- }
- }
- }
|