From 25c51567251865fcc3d5ef892c74bd1446597e92 Mon Sep 17 00:00:00 2001 From: Takoooooo Date: Thu, 13 Jan 2022 17:53:31 +0200 Subject: [PATCH] wip --- samples/ControlCatalog/App.xaml.cs | 21 +++-------- samples/ControlCatalog/MainView.xaml.cs | 2 + src/Avalonia.Themes.Default/SimpleTheme.cs | 44 ++++++++++++++++------ 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/samples/ControlCatalog/App.xaml.cs b/samples/ControlCatalog/App.xaml.cs index 505f486a6d..f6d382f99d 100644 --- a/samples/ControlCatalog/App.xaml.cs +++ b/samples/ControlCatalog/App.xaml.cs @@ -5,6 +5,7 @@ using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml.Styling; using Avalonia.Styling; +using Avalonia.Themes.Default; using Avalonia.Themes.Fluent; using ControlCatalog.ViewModels; @@ -29,6 +30,8 @@ namespace ControlCatalog public static FluentTheme Fluent = new FluentTheme(new Uri("avares://ControlCatalog/Styles")); + public static SimpleTheme Default = new SimpleTheme(new Uri("avares://ControlCatalog/Styles")); + public static Styles DefaultLight = new Styles { new StyleInclude(new Uri("resm:Styles?assembly=ControlCatalog")) @@ -43,14 +46,7 @@ namespace ControlCatalog { Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/BaseLight.xaml") }, - new StyleInclude(new Uri("resm:Styles?assembly=ControlCatalog")) - { - Source = new Uri("avares://Avalonia.Themes.Default/Accents/BaseLight.xaml") - }, - new StyleInclude(new Uri("resm:Styles?assembly=ControlCatalog")) - { - Source = new Uri("avares://Avalonia.Themes.Default/DefaultTheme.xaml") - } + Default }; public static Styles DefaultDark = new Styles @@ -67,14 +63,7 @@ namespace ControlCatalog { Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/BaseDark.xaml") }, - new StyleInclude(new Uri("resm:Styles?assembly=ControlCatalog")) - { - Source = new Uri("avares://Avalonia.Themes.Default/Accents/BaseDark.xaml") - }, - new StyleInclude(new Uri("resm:Styles?assembly=ControlCatalog")) - { - Source = new Uri("avares://Avalonia.Themes.Default/DefaultTheme.xaml") - } + Default }; public override void Initialize() diff --git a/samples/ControlCatalog/MainView.xaml.cs b/samples/ControlCatalog/MainView.xaml.cs index 0579355831..79cf07c8d9 100644 --- a/samples/ControlCatalog/MainView.xaml.cs +++ b/samples/ControlCatalog/MainView.xaml.cs @@ -63,11 +63,13 @@ namespace ControlCatalog } else if (theme == CatalogTheme.DefaultLight) { + App.Default.Mode = Avalonia.Themes.Default.SimpleThemeMode.Light; Application.Current.Styles[0] = App.DefaultLight; Application.Current.Styles[1] = App.DataGridDefault; } else if (theme == CatalogTheme.DefaultDark) { + App.Default.Mode = Avalonia.Themes.Default.SimpleThemeMode.Dark; Application.Current.Styles[0] = App.DefaultDark; Application.Current.Styles[1] = App.DataGridDefault; } diff --git a/src/Avalonia.Themes.Default/SimpleTheme.cs b/src/Avalonia.Themes.Default/SimpleTheme.cs index afdd959182..f5daf9ce55 100644 --- a/src/Avalonia.Themes.Default/SimpleTheme.cs +++ b/src/Avalonia.Themes.Default/SimpleTheme.cs @@ -14,6 +14,7 @@ namespace Avalonia.Themes.Default private bool _isLoading; private Styles _simpleDark = new(); private Styles _simpleLight = new(); + private Styles _sharedStyles = new(); private IStyle? _loaded; /// @@ -32,7 +33,12 @@ namespace Avalonia.Themes.Default /// The XAML service provider. public SimpleTheme(IServiceProvider serviceProvider) { - _baseUri = ((IUriContext)serviceProvider.GetService(typeof(IUriContext))).BaseUri; + var service = serviceProvider.GetService(typeof(IUriContext)); + if (service == null) + { + throw new Exception("There is no service object of type IUriContext!"); + } + _baseUri = ((IUriContext)service).BaseUri; InitStyles(_baseUri); } @@ -46,6 +52,22 @@ namespace Avalonia.Themes.Default get => GetValue(ModeProperty); set => SetValue(ModeProperty, value); } + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) + { + base.OnPropertyChanged(change); + if (change.Property == ModeProperty) + { + if (Mode == SimpleThemeMode.Dark) + { + (Loaded as Styles)![1] = _simpleDark[0]; + } + else + { + (Loaded as Styles)![1] = _simpleLight[0]; + } + } + } + public IStyle Loaded { get @@ -56,11 +78,11 @@ namespace Avalonia.Themes.Default if (Mode == SimpleThemeMode.Light) { - _loaded = _simpleLight; + _loaded = new Styles { _sharedStyles , _simpleLight}; } else if (Mode == SimpleThemeMode.Dark) { - _loaded = _simpleDark; + _loaded = new Styles { _sharedStyles, _simpleDark }; } _isLoading = false; } @@ -110,16 +132,18 @@ namespace Avalonia.Themes.Default void IResourceProvider.RemoveOwner(IResourceHost owner) => (Loaded as IResourceProvider)?.RemoveOwner(owner); private void InitStyles(Uri baseUri) { - - _simpleLight = new Styles + _sharedStyles = new Styles { new StyleInclude(baseUri) { - Source = new Uri("avares://Avalonia.Themes.Default/Accents/BaseLight.xaml") - }, + Source = new Uri("avares://Avalonia.Themes.Default/DefaultTheme.xaml") + } + }; + _simpleLight = new Styles + { new StyleInclude(baseUri) { - Source = new Uri("avares://Avalonia.Themes.Default/DefaultTheme.xaml") + Source = new Uri("avares://Avalonia.Themes.Default/Accents/BaseLight.xaml") } }; @@ -128,10 +152,6 @@ namespace Avalonia.Themes.Default new StyleInclude(baseUri) { Source = new Uri("avares://Avalonia.Themes.Default/Accents/BaseDark.xaml") - }, - new StyleInclude(baseUri) - { - Source = new Uri("avares://Avalonia.Themes.Default/DefaultTheme.xaml") } }; }