Browse Source

Make Pseudoclass method typed.

Steven Kirk 7 years ago
parent
commit
576cc91573

+ 1 - 1
src/Avalonia.Controls/Button.cs

@@ -80,7 +80,7 @@ namespace Avalonia.Controls
             FocusableProperty.OverrideDefaultValue(typeof(Button), true);
             CommandProperty.Changed.Subscribe(CommandChanged);
             IsDefaultProperty.Changed.Subscribe(IsDefaultChanged);
-            PseudoClass(IsPressedProperty, ":pressed");
+            PseudoClass<Button>(IsPressedProperty, ":pressed");
         }
 
         /// <summary>

+ 3 - 3
src/Avalonia.Controls/ButtonSpinner.cs

@@ -85,8 +85,8 @@ namespace Avalonia.Controls
         static ButtonSpinner()
         {
             AllowSpinProperty.Changed.Subscribe(AllowSpinChanged);
-            PseudoClass(ButtonSpinnerLocationProperty, location => location == Location.Left, ":left");
-            PseudoClass(ButtonSpinnerLocationProperty, location => location == Location.Right, ":right");
+            PseudoClass<ButtonSpinner, Location>(ButtonSpinnerLocationProperty, location => location == Location.Left, ":left");
+            PseudoClass<ButtonSpinner, Location>(ButtonSpinnerLocationProperty, location => location == Location.Right, ":right");
         }
 
         /// <summary>
@@ -260,4 +260,4 @@ namespace Avalonia.Controls
             }
         }
     }
-}
+}

+ 2 - 2
src/Avalonia.Controls/ContentControl.cs

@@ -45,8 +45,8 @@ namespace Avalonia.Controls
         static ContentControl()
         {
             ContentControlMixin.Attach<ContentControl>(ContentProperty, x => x.LogicalChildren);
-            PseudoClass(ContentProperty, x => x != null, ":valid");
-            PseudoClass(ContentProperty, x => x == null, ":invalid");
+            PseudoClass<ContentControl, object>(ContentProperty, x => x != null, ":valid");
+            PseudoClass<ContentControl, object>(ContentProperty, x => x == null, ":invalid");
         }
 
         /// <summary>

+ 5 - 5
src/Avalonia.Controls/Expander.cs

@@ -35,12 +35,12 @@ namespace Avalonia.Controls
 
         static Expander()
         {
-            PseudoClass(ExpandDirectionProperty, d => d == ExpandDirection.Down, ":down");
-            PseudoClass(ExpandDirectionProperty, d => d == ExpandDirection.Up, ":up");
-            PseudoClass(ExpandDirectionProperty, d => d == ExpandDirection.Left, ":left");
-            PseudoClass(ExpandDirectionProperty, d => d == ExpandDirection.Right, ":right");
+            PseudoClass<Expander, ExpandDirection>(ExpandDirectionProperty, d => d == ExpandDirection.Down, ":down");
+            PseudoClass<Expander, ExpandDirection>(ExpandDirectionProperty, d => d == ExpandDirection.Up, ":up");
+            PseudoClass<Expander, ExpandDirection>(ExpandDirectionProperty, d => d == ExpandDirection.Left, ":left");
+            PseudoClass<Expander, ExpandDirection>(ExpandDirectionProperty, d => d == ExpandDirection.Right, ":right");
 
-            PseudoClass(IsExpandedProperty, ":expanded");
+            PseudoClass<Expander>(IsExpandedProperty, ":expanded");
 
             IsExpandedProperty.Changed.AddClassHandler<Expander>(x => x.OnIsExpandedChanged);
         }

+ 2 - 2
src/Avalonia.Controls/Primitives/ScrollBar.cs

@@ -54,8 +54,8 @@ namespace Avalonia.Controls.Primitives
         /// </summary>
         static ScrollBar()
         {
-            PseudoClass(OrientationProperty, o => o == Orientation.Vertical, ":vertical");
-            PseudoClass(OrientationProperty, o => o == Orientation.Horizontal, ":horizontal");
+            PseudoClass<ScrollBar, Orientation>(OrientationProperty, o => o == Orientation.Vertical, ":vertical");
+            PseudoClass<ScrollBar, Orientation>(OrientationProperty, o => o == Orientation.Horizontal, ":horizontal");
 
             Thumb.DragDeltaEvent.AddClassHandler<ScrollBar>(o => o.OnThumbDragDelta, RoutingStrategies.Bubble);
             Thumb.DragCompletedEvent.AddClassHandler<ScrollBar>(o => o.OnThumbDragComplete, RoutingStrategies.Bubble);

+ 3 - 3
src/Avalonia.Controls/Primitives/ToggleButton.cs

@@ -24,9 +24,9 @@ namespace Avalonia.Controls.Primitives
 
         static ToggleButton()
         {
-            PseudoClass(IsCheckedProperty, c => c == true, ":checked");
-            PseudoClass(IsCheckedProperty, c => c == false, ":unchecked");
-            PseudoClass(IsCheckedProperty, c => c == null, ":indeterminate");
+            PseudoClass<ToggleButton, bool?>(IsCheckedProperty, c => c == true, ":checked");
+            PseudoClass<ToggleButton, bool?>(IsCheckedProperty, c => c == false, ":unchecked");
+            PseudoClass<ToggleButton, bool?>(IsCheckedProperty, c => c == null, ":indeterminate");
         }
 
         public bool? IsChecked

+ 2 - 0
src/Avalonia.Controls/Primitives/Track.cs

@@ -39,6 +39,8 @@ namespace Avalonia.Controls.Primitives
 
         static Track()
         {
+            PseudoClass<Track, Orientation>(OrientationProperty, o => o == Orientation.Vertical, ":vertical");
+            PseudoClass<Track, Orientation>(OrientationProperty, o => o == Orientation.Horizontal, ":horizontal");
             ThumbProperty.Changed.AddClassHandler<Track>(x => x.ThumbChanged);
             IncreaseButtonProperty.Changed.AddClassHandler<Track>(x => x.ButtonChanged);
             DecreaseButtonProperty.Changed.AddClassHandler<Track>(x => x.ButtonChanged);

+ 3 - 3
src/Avalonia.Controls/ProgressBar.cs

@@ -33,9 +33,9 @@ namespace Avalonia.Controls
 
         static ProgressBar()
         {
-            PseudoClass(OrientationProperty, o => o == Avalonia.Controls.Orientation.Vertical, ":vertical");
-            PseudoClass(OrientationProperty, o => o == Avalonia.Controls.Orientation.Horizontal, ":horizontal");
-            PseudoClass(IsIndeterminateProperty, ":indeterminate");
+            PseudoClass<ProgressBar, Orientation>(OrientationProperty, o => o == Avalonia.Controls.Orientation.Vertical, ":vertical");
+            PseudoClass<ProgressBar, Orientation>(OrientationProperty, o => o == Avalonia.Controls.Orientation.Horizontal, ":horizontal");
+            PseudoClass<ProgressBar>(IsIndeterminateProperty, ":indeterminate");
 
             ValueProperty.Changed.AddClassHandler<ProgressBar>(x => x.ValueChanged);
         }

+ 2 - 0
src/Avalonia.Controls/Slider.cs

@@ -42,6 +42,8 @@ namespace Avalonia.Controls
         static Slider()
         {
             OrientationProperty.OverrideDefaultValue(typeof(Slider), Orientation.Horizontal);
+            PseudoClass<Slider, Orientation>(OrientationProperty, o => o == Orientation.Vertical, ":vertical");
+            PseudoClass<Slider, Orientation>(OrientationProperty, o => o == Orientation.Horizontal, ":horizontal");
             Thumb.DragStartedEvent.AddClassHandler<Slider>(x => x.OnThumbDragStarted, RoutingStrategies.Bubble);
             Thumb.DragDeltaEvent.AddClassHandler<Slider>(x => x.OnThumbDragDelta, RoutingStrategies.Bubble);
             Thumb.DragCompletedEvent.AddClassHandler<Slider>(x => x.OnThumbDragCompleted, RoutingStrategies.Bubble);

+ 3 - 3
src/Avalonia.Input/InputElement.cs

@@ -168,9 +168,9 @@ namespace Avalonia.Input
             PointerReleasedEvent.AddClassHandler<InputElement>(x => x.OnPointerReleased);
             PointerWheelChangedEvent.AddClassHandler<InputElement>(x => x.OnPointerWheelChanged);
 
-            PseudoClass(IsEnabledCoreProperty, x => !x, ":disabled");
-            PseudoClass(IsFocusedProperty, ":focus");
-            PseudoClass(IsPointerOverProperty, ":pointerover");
+            PseudoClass<InputElement, bool>(IsEnabledCoreProperty, x => !x, ":disabled");
+            PseudoClass<InputElement>(IsFocusedProperty, ":focus");
+            PseudoClass<InputElement>(IsPointerOverProperty, ":pointerover");
         }
 
         /// <summary>

+ 12 - 8
src/Avalonia.Styling/StyledElement.cs

@@ -491,24 +491,28 @@ namespace Avalonia
         /// <summary>
         /// Adds a pseudo-class to be set when a property is true.
         /// </summary>
+        /// <typeparam name="TOwner">The type to apply the pseudo-class to.</typeparam>
         /// <param name="property">The property.</param>
         /// <param name="className">The pseudo-class.</param>
-        protected static void PseudoClass(AvaloniaProperty<bool> property, string className)
+        protected static void PseudoClass<TOwner>(AvaloniaProperty<bool> property, string className)
+            where TOwner : class, IStyledElement
         {
-            PseudoClass(property, x => x, className);
+            PseudoClass<TOwner, bool>(property, x => x, className);
         }
 
         /// <summary>
         /// Adds a pseudo-class to be set when a property equals a certain value.
         /// </summary>
-        /// <typeparam name="T">The type of the property.</typeparam>
+        /// <typeparam name="TProperty">The type of the property.</typeparam>
+        /// <typeparam name="TOwner">The type to apply the pseudo-class to.</typeparam>
         /// <param name="property">The property.</param>
         /// <param name="selector">Returns a boolean value based on the property value.</param>
         /// <param name="className">The pseudo-class.</param>
-        protected static void PseudoClass<T>(
-            AvaloniaProperty<T> property,
-            Func<T, bool> selector,
+        protected static void PseudoClass<TOwner, TProperty>(
+            AvaloniaProperty<TProperty> property,
+            Func<TProperty, bool> selector,
             string className)
+                where TOwner : class, IStyledElement
         {
             Contract.Requires<ArgumentNullException>(property != null);
             Contract.Requires<ArgumentNullException>(selector != null);
@@ -520,10 +524,10 @@ namespace Avalonia
             }
 
             property.Changed.Merge(property.Initialized)
-                .Where(e => e.Sender is StyledElement)
+                .Where(e => e.Sender is TOwner)
                 .Subscribe(e =>
                 {
-                    if (selector((T)e.NewValue))
+                    if (selector((TProperty)e.NewValue))
                     {
                         ((StyledElement)e.Sender).PseudoClasses.Add(className);
                     }