Browse Source

Remove StyleResource and StyleBinding. Fold XamlBinding into Binding since that is the only place it is being used now.

Jeremy Koritzinsky 8 years ago
parent
commit
52617b4a58

+ 2 - 1
Avalonia.sln

@@ -1,7 +1,7 @@
 
 Microsoft Visual Studio Solution File, Format Version 12.00
 # Visual Studio 15
-VisualStudioVersion = 15.0.26730.3
+VisualStudioVersion = 15.0.26730.10
 MinimumVisualStudioVersion = 10.0.40219.1
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.Base", "src\Avalonia.Base\Avalonia.Base.csproj", "{B09B78D8-9B26-48B0-9149-D64A2F120F3F}"
 EndProject
@@ -204,6 +204,7 @@ Global
 		tests\Avalonia.RenderTests\Avalonia.RenderTests.projitems*{48840edd-24bf-495d-911e-2eb12ae75d3b}*SharedItemsImports = 13
 		src\Shared\PlatformSupport\PlatformSupport.projitems*{4a1abb09-9047-4bd5-a4ad-a055e52c5ee0}*SharedItemsImports = 4
 		src\Shared\PlatformSupport\PlatformSupport.projitems*{7863ea94-f0fb-4386-bf8c-e5bfa761560a}*SharedItemsImports = 4
+		src\Shared\PlatformSupport\PlatformSupport.projitems*{7b92af71-6287-4693-9dcb-bd5b6e927e23}*SharedItemsImports = 4
 		src\Shared\RenderHelpers\RenderHelpers.projitems*{7d2d3083-71dd-4cc9-8907-39a0d86fb322}*SharedItemsImports = 4
 		src\Windows\Avalonia.Win32\Avalonia.Win32.Shared.projitems*{811a76cf-1cf6-440f-963b-bbe31bd72a82}*SharedItemsImports = 4
 		src\Windows\Avalonia.Win32\Avalonia.Win32.Shared.projitems*{9defc6b7-845b-4d8f-afc0-d32bf0032b8c}*SharedItemsImports = 13

+ 0 - 3
src/Markup/Avalonia.Markup.Xaml/Avalonia.Markup.Xaml.csproj

@@ -39,7 +39,6 @@
         <Compile Include="MarkupExtensions\StaticResourceExtension.cs" />
         <Compile Include="MarkupExtensions\StyleIncludeExtension.cs" />
         <Compile Include="PortableXaml\AvaloniaXamlContext.cs" />
-        <Compile Include="PortableXaml\XamlBinding.cs" />
         <Compile Include="PortableXaml\AttributeExtensions.cs" />
         <Compile Include="PortableXaml\AvaloniaMemberAttributeProvider.cs" />
         <Compile Include="PortableXaml\AvaloniaNameScope.cs" />
@@ -73,8 +72,6 @@
         <Compile Include="Data\DelayedBinding.cs" />
         <Compile Include="Data\MultiBinding.cs" />
         <Compile Include="Data\RelativeSource.cs" />
-        <Compile Include="Data\StyleResourceBinding.cs" />
-        <Compile Include="MarkupExtensions\StyleResourceExtension.cs" />
         <Compile Include="MarkupExtensions\BindingExtension.cs" />
         <Compile Include="MarkupExtensions\RelativeSourceExtension.cs" />
         <Compile Include="MarkupExtensions\StaticExtension.cs" />

+ 4 - 0
src/Markup/Avalonia.Markup.Xaml/Data/Binding.cs

@@ -83,6 +83,8 @@ namespace Avalonia.Markup.Xaml.Data
         /// </summary>
         public object Source { get; set; }
 
+        internal WeakReference DefaultAnchor { get; set; }
+
         /// <inheritdoc/>
         public InstancedBinding Initiate(
             IAvaloniaObject target,
@@ -92,6 +94,8 @@ namespace Avalonia.Markup.Xaml.Data
         {
             Contract.Requires<ArgumentNullException>(target != null);
 
+            anchor = anchor ?? DefaultAnchor.Target;
+
             var pathInfo = ParsePath(Path);
             ValidateState(pathInfo);
             enableDataValidation = enableDataValidation && Priority == BindingPriority.LocalValue;

+ 0 - 69
src/Markup/Avalonia.Markup.Xaml/Data/StyleResourceBinding.cs

@@ -1,69 +0,0 @@
-// Copyright (c) The Avalonia Project. All rights reserved.
-// Licensed under the MIT license. See licence.md file in the project root for full license information.
-
-using System;
-using System.Reactive.Disposables;
-using System.Reactive.Linq;
-using System.Reactive.Subjects;
-using Avalonia.Controls;
-using Avalonia.Data;
-using Avalonia.Styling;
-
-namespace Avalonia.Markup.Xaml.Data
-{
-    public class StyleResourceBinding : IBinding
-    {
-        /// <summary>
-        /// Initializes a new instance of the <see cref="StyleResourceBinding"/> class.
-        /// </summary>
-        /// <param name="name">The resource name.</param>
-        public StyleResourceBinding(string name)
-        {
-            Name = name;
-        }
-
-        /// <inheritdoc/>
-        public BindingMode Mode => BindingMode.OneTime;
-
-        /// <summary>
-        /// Gets the resource name.
-        /// </summary>
-        public string Name { get; }
-
-        /// <inheritdoc/>
-        public BindingPriority Priority => BindingPriority.LocalValue;
-
-        /// <inheritdoc/>
-        public InstancedBinding Initiate(
-            IAvaloniaObject target,
-            AvaloniaProperty targetProperty,
-            object anchor = null,
-            bool enableDataValidation = false)
-        {
-            var host = (target as IControl) ?? (anchor as IControl);
-            var style = anchor as IStyle;
-            var resource = AvaloniaProperty.UnsetValue;
-
-            if (host != null)
-            {
-                resource = host.FindResource(Name);
-            }
-            else if (style != null)
-            {
-                if (!style.TryGetResource(Name, out resource))
-                {
-                    resource = AvaloniaProperty.UnsetValue;
-                }
-            }
-
-            if (resource != AvaloniaProperty.UnsetValue)
-            {
-                return new InstancedBinding(resource, Priority);
-            }
-            else
-            {
-                return null;
-            }
-        }
-    }
-}

+ 24 - 3
src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/BindingExtension.cs

@@ -7,8 +7,13 @@ using System;
 
 namespace Avalonia.Markup.Xaml.MarkupExtensions
 {
+    using Avalonia.Controls;
+    using Avalonia.Styling;
+    using Portable.Xaml;
+    using Portable.Xaml.ComponentModel;
     using Portable.Xaml.Markup;
     using PortableXaml;
+    using System.ComponentModel;
 
     [MarkupExtensionReturnType(typeof(IBinding))]
     public class BindingExtension : MarkupExtension
@@ -24,7 +29,7 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions
 
         public override object ProvideValue(IServiceProvider serviceProvider)
         {
-            var b = new Binding
+            return new Binding
             {
                 Converter = Converter,
                 ConverterParameter = ConverterParameter,
@@ -33,10 +38,26 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions
                 Mode = Mode,
                 Path = Path,
                 Priority = Priority,
-                RelativeSource = RelativeSource
+                RelativeSource = RelativeSource,
+                DefaultAnchor = new WeakReference(GetDefaultAnchor((ITypeDescriptorContext)serviceProvider))
             };
+        }
+
+
+        private static object GetDefaultAnchor(ITypeDescriptorContext context)
+        {
+            object anchor = null;
+
+            // The target is not a control, so we need to find an anchor that will let us look
+            // up named controls and style resources. First look for the closest IControl in
+            // the context.
+            anchor = context.GetFirstAmbientValue<IControl>();
 
-            return XamlBinding.FromMarkupExtensionContext(b, serviceProvider);
+            // If a control was not found, then try to find the highest-level style as the XAML
+            // file could be a XAML file containing only styles.
+            return anchor ??
+                    context.GetService<IRootObjectProvider>()?.RootObject as IStyle ??
+                    context.GetLastOrDefaultAmbientValue<IStyle>();
         }
 
         public IValueConverter Converter { get; set; }

+ 0 - 31
src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StyleResourceExtension.cs

@@ -1,31 +0,0 @@
-// Copyright (c) The Avalonia Project. All rights reserved.
-// Licensed under the MIT license. See licence.md file in the project root for full license information.
-
-using Avalonia.Data;
-using Avalonia.Markup.Xaml.Data;
-using System;
-
-namespace Avalonia.Markup.Xaml.MarkupExtensions
-{
-    using Portable.Xaml.Markup;
-    using PortableXaml;
-
-    [MarkupExtensionReturnType(typeof(IBinding))]
-    public class StyleResourceExtension : MarkupExtension
-    {
-        public StyleResourceExtension(string name)
-        {
-            Name = name;
-        }
-
-        public override object ProvideValue(IServiceProvider serviceProvider)
-        {
-            return XamlBinding.FromMarkupExtensionContext(
-                            new StyleResourceBinding(Name),
-                            serviceProvider);
-        }
-
-        [ConstructorArgument("name")]
-        public string Name { get; set; }
-    }
-}

+ 1 - 11
src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaXamlType.cs

@@ -210,11 +210,6 @@ namespace Avalonia.Markup.Xaml.PortableXaml
                                             value);
                 }
 
-                if (value is XamlBinding)
-                {
-                    value = (value as XamlBinding).Value;
-                }
-
                 if (UpdateListInsteadSet &&
                     value != null &&
                     UpdateListInsteadSetValue(instance, value))
@@ -315,9 +310,7 @@ namespace Avalonia.Markup.Xaml.PortableXaml
                         if (!Member.AssignBinding)
                             ApplyBinding(obj, (IBinding)value);
                         else
-                            obj.SetValue(Property, value is XamlBinding ?
-                                                        (value as XamlBinding).Value :
-                                                        value);
+                            obj.SetValue(Property, value);
                     }
                     else
                     {
@@ -346,12 +339,9 @@ namespace Avalonia.Markup.Xaml.PortableXaml
             {
                 var control = obj as IControl;
                 var property = Property;
-                var xamlBinding = binding as XamlBinding;
 
                 if (control != null && property != Control.DataContextProperty)
                     DelayedBinding.Add(control, property, binding);
-                else if (xamlBinding != null)
-                    obj.Bind(property, xamlBinding.Value, xamlBinding.Anchor?.Target);
                 else
                     obj.Bind(property, binding);
             }

+ 0 - 63
src/Markup/Avalonia.Markup.Xaml/PortableXaml/XamlBinding.cs

@@ -1,63 +0,0 @@
-using Avalonia.Controls;
-using Avalonia.Data;
-using Avalonia.Styling;
-using Portable.Xaml;
-using Portable.Xaml.ComponentModel;
-using System.ComponentModel;
-using Portable.Xaml.Markup;
-using System;
-
-namespace Avalonia.Markup.Xaml.PortableXaml
-{
-    internal class XamlBinding : IBinding
-    {
-        public static IBinding FromMarkupExtensionContext(
-                                    IBinding binding,
-                                    IServiceProvider serviceProvider)
-        {
-            var context = (ITypeDescriptorContext)serviceProvider;
-            var pvt = context.GetService<IProvideValueTarget>();
-
-            if (pvt.TargetObject is IControl) return binding;
-
-            object anchor = GetDefaultAnchor(context);
-
-            if (anchor == null) return binding;
-
-            return new XamlBinding(binding, anchor);
-        }
-
-        private static object GetDefaultAnchor(ITypeDescriptorContext context)
-        {
-            object anchor = null;
-
-            // The target is not a control, so we need to find an anchor that will let us look
-            // up named controls and style resources. First look for the closest IControl in
-            // the context.
-            anchor = context.GetFirstAmbientValue<IControl>();
-
-            // If a control was not found, then try to find the highest-level style as the XAML
-            // file could be a XAML file containing only styles.
-            return anchor ??
-                    context.GetService<IRootObjectProvider>()?.RootObject as IStyle ??
-                    context.GetLastOrDefaultAmbientValue<IStyle>();
-        }
-
-        private XamlBinding(IBinding binding, object anchor)
-        {
-            Value = binding;
-
-            Anchor = new WeakReference(anchor);
-        }
-
-        public WeakReference Anchor { get; }
-
-        public IBinding Value { get; }
-
-        public InstancedBinding Initiate(IAvaloniaObject target, AvaloniaProperty targetProperty, object anchor = null, bool enableDataValidation = false)
-        {
-            return Value.Initiate(target, targetProperty,
-                            anchor ?? Anchor.Target, enableDataValidation);
-        }
-    }
-}

+ 4 - 13
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs

@@ -411,8 +411,8 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
                 var xaml = @"
 <Styles xmlns='https://github.com/avaloniaui'>
   <Style Selector='CheckBox'>
-    <Setter Property='BorderBrush' Value='{StyleResource ThemeBorderMidBrush}'/>
-    <Setter Property='BorderThickness' Value='{StyleResource ThemeBorderThickness}'/>
+    <Setter Property='BorderBrush' Value='{DynamicResource ThemeBorderMidBrush}'/>
+    <Setter Property='BorderThickness' Value='{DynamicResource ThemeBorderThickness}'/>
     <Setter Property='Template'>
       <ControlTemplate>
         <Grid ColumnDefinitions='Auto,*'>
@@ -423,7 +423,7 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
                   Height='18'
                   VerticalAlignment='Center'>
             <Path Name='checkMark'
-                  Fill='{StyleResource HighlightBrush}'
+                  Fill='{StaticResource HighlightBrush}'
                   Width='11'
                   Height='10'
                   Stretch='Uniform'
@@ -457,8 +457,6 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
                 Assert.Equal(CheckBox.BorderThicknessProperty, setters[1].Property);
                 Assert.Equal(CheckBox.TemplateProperty, setters[2].Property);
 
-                Assert.IsType<StyleResourceBinding>(setters[0].Value);
-                Assert.IsType<StyleResourceBinding>(setters[1].Value);
                 Assert.IsType<ControlTemplate>(setters[2].Value);
             }
         }
@@ -772,14 +770,7 @@ do we need it?")]
 <Window xmlns='https://github.com/avaloniaui'
                 xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
                 xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
-    <Window.Styles>
-        <Style>
-            <Style.Resources>
-                <x:Double x:Key='Double'>100</x:Double>
-            </Style.Resources>
-        </Style>
-    </Window.Styles>
-    <local:InitializationOrderTracker Width='100' Height='{StyleResource Double}'
+    <local:InitializationOrderTracker Width='100' Height='100'
                      Tag='{Binding Height, RelativeSource={RelativeSource Self}}' />
 </Window>";