浏览代码

some very initial implementation

Takoooooo 3 年之前
父节点
当前提交
69c1a37372

+ 1 - 0
samples/ControlCatalog/App.xaml

@@ -5,6 +5,7 @@
              x:CompileBindings="True"
              x:CompileBindings="True"
              x:Class="ControlCatalog.App">
              x:Class="ControlCatalog.App">
   <Application.Styles>
   <Application.Styles>
+    <FluentTheme Mode="Light"/>
     <Style Selector="TextBlock.h1, TextBlock.h2, TextBlock.h3">
     <Style Selector="TextBlock.h1, TextBlock.h2, TextBlock.h3">
       <Setter Property="TextWrapping" Value="Wrap" />
       <Setter Property="TextWrapping" Value="Wrap" />
     </Style>
     </Style>

+ 0 - 1
samples/ControlCatalog/App.xaml.cs

@@ -96,7 +96,6 @@ namespace ControlCatalog
 
 
         public override void Initialize()
         public override void Initialize()
         {
         {
-            Styles.Insert(0, FluentLight);
 
 
             AvaloniaXamlLoader.Load(this);
             AvaloniaXamlLoader.Load(this);
         }
         }

+ 2 - 8
samples/ControlCatalog/MainView.xaml.cs

@@ -11,6 +11,7 @@ using Avalonia.Media.Immutable;
 using Avalonia.Platform;
 using Avalonia.Platform;
 using ControlCatalog.Pages;
 using ControlCatalog.Pages;
 using ControlCatalog.Models;
 using ControlCatalog.Models;
+using Avalonia.Themes.Fluent;
 
 
 namespace ControlCatalog
 namespace ControlCatalog
 {
 {
@@ -43,14 +44,7 @@ namespace ControlCatalog
             {
             {
                 if (themes.SelectedItem is CatalogTheme theme)
                 if (themes.SelectedItem is CatalogTheme theme)
                 {
                 {
-                    Application.Current.Styles[0] = theme switch
-                    {
-                        CatalogTheme.FluentLight => App.FluentLight,
-                        CatalogTheme.FluentDark => App.FluentDark,
-                        CatalogTheme.DefaultLight => App.DefaultLight,
-                        CatalogTheme.DefaultDark => App.DefaultDark,
-                        _ => Application.Current.Styles[0]
-                    };
+                    (Application.Current.Styles[0] as FluentTheme).Mode = FluentThemeMode.Dark;
                 }
                 }
             };
             };
 
 

+ 71 - 6
src/Avalonia.Themes.Fluent/FluentTheme.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Collections.Generic;
 using Avalonia.Controls;
 using Avalonia.Controls;
 using Avalonia.Markup.Xaml;
 using Avalonia.Markup.Xaml;
+using Avalonia.Markup.Xaml.Styling;
 using Avalonia.Styling;
 using Avalonia.Styling;
 
 
 #nullable enable
 #nullable enable
@@ -22,6 +23,7 @@ namespace Avalonia.Themes.Fluent
         private readonly Uri _baseUri;
         private readonly Uri _baseUri;
         private IStyle[]? _loaded;
         private IStyle[]? _loaded;
         private bool _isLoading;
         private bool _isLoading;
+        private FluentThemeMode _mode;
 
 
         /// <summary>
         /// <summary>
         /// Initializes a new instance of the <see cref="FluentTheme"/> class.
         /// Initializes a new instance of the <see cref="FluentTheme"/> class.
@@ -44,7 +46,20 @@ namespace Avalonia.Themes.Fluent
         /// <summary>
         /// <summary>
         /// Gets or sets the mode of the fluent theme (light, dark).
         /// Gets or sets the mode of the fluent theme (light, dark).
         /// </summary>
         /// </summary>
-        public FluentThemeMode Mode { get; set; }
+        public FluentThemeMode Mode
+        {
+            get => _mode;
+            set
+            {
+                if (_mode != value)
+                {
+                    _mode = value;
+                    (Loaded as Styles)[3] = FluentDark[0];
+                    (Loaded as Styles)[4] = FluentDark[1];
+                }
+
+            }
+        }
 
 
         public IResourceHost? Owner => (Loaded as IResourceProvider)?.Owner;
         public IResourceHost? Owner => (Loaded as IResourceProvider)?.Owner;
 
 
@@ -58,8 +73,25 @@ namespace Avalonia.Themes.Fluent
                 if (_loaded == null)
                 if (_loaded == null)
                 {
                 {
                     _isLoading = true;
                     _isLoading = true;
-                    var loaded = (IStyle)AvaloniaXamlLoader.Load(GetUri(), _baseUri);
-                    _loaded = new[] { loaded };
+                    Styles? resultStyle = new Styles();
+
+                    resultStyle.AddRange(SharedStyles);
+
+                    if (Mode == FluentThemeMode.Light)
+                    {
+                        for (int i = 0; i < FluentLight.Count; i++)
+                        {
+                            resultStyle.Add(FluentLight[i]);
+                        }
+                    }
+                    else if (Mode == FluentThemeMode.Dark)
+                    {
+                        for (int i = 0; i < FluentDark.Count; i++)
+                        {
+                            resultStyle.Add(FluentDark[i]);
+                        }
+                    }
+                    _loaded = new[] { resultStyle };
                     _isLoading = false;
                     _isLoading = false;
                 }
                 }
 
 
@@ -105,10 +137,43 @@ namespace Avalonia.Themes.Fluent
         void IResourceProvider.AddOwner(IResourceHost owner) => (Loaded as IResourceProvider)?.AddOwner(owner);
         void IResourceProvider.AddOwner(IResourceHost owner) => (Loaded as IResourceProvider)?.AddOwner(owner);
         void IResourceProvider.RemoveOwner(IResourceHost owner) => (Loaded as IResourceProvider)?.RemoveOwner(owner);
         void IResourceProvider.RemoveOwner(IResourceHost owner) => (Loaded as IResourceProvider)?.RemoveOwner(owner);
 
 
-        private Uri GetUri() => Mode switch
+        private static Styles SharedStyles = new Styles
+        {
+            new StyleInclude(new Uri("resm:Styles?assembly=Avalonia.Themes.Fluent"))
+            {
+                Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/AccentColors.xaml")
+            },
+            new StyleInclude(new Uri("resm:Styles?assembly=Avalonia.Themes.Fluent"))
+            {
+                Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/Base.xaml")
+            },
+            new StyleInclude(new Uri("resm:Styles?assembly=Avalonia.Themes.Fluent"))
+            {
+                Source = new Uri("avares://Avalonia.Themes.Fluent/Controls/FluentControls.xaml")
+            }
+        };
+
+        private static Styles FluentLight = new Styles
         {
         {
-            FluentThemeMode.Dark => new Uri("avares://Avalonia.Themes.Fluent/FluentDark.xaml", UriKind.Absolute),
-            _ => new Uri("avares://Avalonia.Themes.Fluent/FluentLight.xaml", UriKind.Absolute),
+            new StyleInclude(new Uri("resm:Styles?assembly=Avalonia.Themes.Fluent"))
+            {
+                Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/BaseLight.xaml")
+            },
+            new StyleInclude(new Uri("resm:Styles?assembly=Avalonia.Themes.Fluent"))
+            {
+                Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/FluentControlResourcesLight.xaml")
+            }
+        };
+        private static Styles FluentDark = new Styles
+        {
+            new StyleInclude(new Uri("resm:Styles?assembly=Avalonia.Themes.Fluent"))
+            {
+                Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/BaseDark.xaml")
+            },
+            new StyleInclude(new Uri("resm:Styles?assembly=Avalonia.Themes.Fluent"))
+            {
+                Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/FluentControlResourcesDark.xaml")
+            }
         };
         };
     }
     }
 }
 }