Преглед на файлове

Implement FluentTheme class.

For easier selection of fluent theme in App.xaml.
Steven Kirk преди 4 години
родител
ревизия
289e787938
променени са 4 файла, в които са добавени 119 реда и са изтрити 2 реда
  1. 1 1
      samples/RenderDemo/App.xaml
  2. 1 1
      samples/Sandbox/App.axaml
  3. 114 0
      src/Avalonia.Themes.Fluent/FluentTheme.cs
  4. 3 0
      src/Avalonia.Themes.Fluent/Properties/AssemblyInfo.cs

+ 1 - 1
samples/RenderDemo/App.xaml

@@ -3,7 +3,7 @@
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     x:Class="RenderDemo.App">
     <Application.Styles>
-        <StyleInclude Source="avares://Avalonia.Themes.Fluent/FluentLight.xaml"/>
+        <FluentTheme/>
         <StyleInclude Source="avares://RenderDemo/SideBar.xaml"/>
     </Application.Styles>
 </Application>

+ 1 - 1
samples/Sandbox/App.axaml

@@ -3,6 +3,6 @@
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     x:Class="Sandbox.App">
     <Application.Styles>
-        <StyleInclude Source="avares://Avalonia.Themes.Fluent/FluentDark.xaml"/>
+        <FluentTheme Scheme="Dark"/>
     </Application.Styles>
 </Application>

+ 114 - 0
src/Avalonia.Themes.Fluent/FluentTheme.cs

@@ -0,0 +1,114 @@
+using System;
+using System.Collections.Generic;
+using Avalonia.Controls;
+using Avalonia.Markup.Xaml;
+using Avalonia.Styling;
+
+#nullable enable
+
+namespace Avalonia.Themes.Fluent
+{
+    public enum FluentColorScheme
+    {
+        Light,
+        Dark,
+    }
+
+    /// <summary>
+    /// Includes the fluent theme in an application.
+    /// </summary>
+    public class FluentTheme : IStyle, IResourceProvider
+    {
+        private readonly Uri _baseUri;
+        private IStyle[]? _loaded;
+        private bool _isLoading;
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="FluentTheme"/> class.
+        /// </summary>
+        /// <param name="baseUri">The base URL for the XAML context.</param>
+        public FluentTheme(Uri baseUri)
+        {
+            _baseUri = baseUri;
+        }
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="FluentTheme"/> class.
+        /// </summary>
+        /// <param name="serviceProvider">The XAML service provider.</param>
+        public FluentTheme(IServiceProvider serviceProvider)
+        {
+            _baseUri = ((IUriContext)serviceProvider.GetService(typeof(IUriContext))).BaseUri;
+        }
+
+        /// <summary>
+        /// Gets or sets the color scheme to use (light, dark).
+        /// </summary>
+        public FluentColorScheme Scheme { get; set; }
+
+        public IResourceHost? Owner => (Loaded as IResourceProvider)?.Owner;
+
+        /// <summary>
+        /// Gets the loaded style.
+        /// </summary>
+        public IStyle Loaded
+        {
+            get
+            {
+                if (_loaded == null)
+                {
+                    _isLoading = true;
+                    var loaded = (IStyle)AvaloniaXamlLoader.Load(GetUri(), _baseUri);
+                    _loaded = new[] { loaded };
+                    _isLoading = false;
+                }
+
+                return _loaded?[0]!;
+            }
+        }
+
+        bool IResourceNode.HasResources => (Loaded as IResourceProvider)?.HasResources ?? false;
+
+        IReadOnlyList<IStyle> IStyle.Children => _loaded ?? Array.Empty<IStyle>();
+
+        public event EventHandler OwnerChanged
+        {
+            add
+            {
+                if (Loaded is IResourceProvider rp)
+                {
+                    rp.OwnerChanged += value;
+                }
+            }
+            remove
+            {
+                if (Loaded is IResourceProvider rp)
+                {
+                    rp.OwnerChanged -= value;
+                }
+            }
+        }
+
+        public SelectorMatchResult TryAttach(IStyleable target, IStyleHost? host) => Loaded.TryAttach(target, host);
+
+        public bool TryGetResource(object key, out object? value)
+        {
+            if (!_isLoading && Loaded is IResourceProvider p)
+            {
+                return p.TryGetResource(key, out value);
+            }
+
+            value = null;
+            return false;
+        }
+
+        void IResourceProvider.AddOwner(IResourceHost owner) => (Loaded as IResourceProvider)?.AddOwner(owner);
+        void IResourceProvider.RemoveOwner(IResourceHost owner) => (Loaded as IResourceProvider)?.RemoveOwner(owner);
+
+        private Uri GetUri() => Scheme switch
+        {
+            FluentColorScheme.Dark => new Uri("avares://Avalonia.Themes.Fluent/FluentDark.xaml", UriKind.Absolute),
+            _ => new Uri("avares://Avalonia.Themes.Fluent/FluentLight.xaml", UriKind.Absolute),
+        };
+    }
+}

+ 3 - 0
src/Avalonia.Themes.Fluent/Properties/AssemblyInfo.cs

@@ -0,0 +1,3 @@
+using Avalonia.Metadata;
+
+[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia.Themes.Fluent")]