|
| 1 | +using Avalonia.Reactive; |
| 2 | +using Avalonia.Xaml.Interactions.Responsive; |
| 3 | +using Avalonia.Xaml.Interactivity; |
| 4 | + |
| 5 | +namespace BD.WTTS.Behaviors; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Observes <see cref="Behavior{T}.AssociatedObject"/> control or <see cref="SourceControl"/> control <see cref="Visual.Bounds"/> property changes and if triggered sets or removes style classes when conditions from <see cref="AdaptiveClassSetter"/> are met. |
| 9 | +/// </summary> |
| 10 | +public class AdaptiveBehavior : Behavior<Control> |
| 11 | +{ |
| 12 | + private IDisposable? _disposable; |
| 13 | + private AvaloniaList<AdaptiveClassSetter>? _setters; |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// Identifies the <seealso cref="SourceControl"/> avalonia property. |
| 17 | + /// </summary> |
| 18 | + public static readonly StyledProperty<Control?> SourceControlProperty = |
| 19 | + AvaloniaProperty.Register<AdaptiveBehavior, Control?>(nameof(SourceControl)); |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Identifies the <seealso cref="TargetControl"/> avalonia property. |
| 23 | + /// </summary> |
| 24 | + public static readonly StyledProperty<Control?> TargetControlProperty = |
| 25 | + AvaloniaProperty.Register<AdaptiveBehavior, Control?>(nameof(TargetControl)); |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Identifies the <seealso cref="Setters"/> avalonia property. |
| 29 | + /// </summary> |
| 30 | + public static readonly DirectProperty<AdaptiveBehavior, AvaloniaList<AdaptiveClassSetter>> SettersProperty = |
| 31 | + AvaloniaProperty.RegisterDirect<AdaptiveBehavior, AvaloniaList<AdaptiveClassSetter>>(nameof(Setters), t => t.Setters); |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Gets or sets the the source control that <see cref="Visual.BoundsProperty"/> property are observed from, if not set <see cref="Behavior{T}.AssociatedObject"/> is used. This is a avalonia property. |
| 35 | + /// </summary> |
| 36 | + [ResolveByName] |
| 37 | + public Control? SourceControl |
| 38 | + { |
| 39 | + get => GetValue(SourceControlProperty); |
| 40 | + set => SetValue(SourceControlProperty, value); |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Gets or sets the target control that class name that should be added or removed when triggered, if not set <see cref="Behavior{T}.AssociatedObject"/> is used or <see cref="AdaptiveClassSetter.TargetControl"/> from <see cref="AdaptiveClassSetter"/>. This is a avalonia property. |
| 45 | + /// </summary> |
| 46 | + [ResolveByName] |
| 47 | + public Control? TargetControl |
| 48 | + { |
| 49 | + get => GetValue(TargetControlProperty); |
| 50 | + set => SetValue(TargetControlProperty, value); |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Gets adaptive class setters collection. This is a avalonia property. |
| 55 | + /// </summary> |
| 56 | + [Content] |
| 57 | + public AvaloniaList<AdaptiveClassSetter> Setters => _setters ??= new AvaloniaList<AdaptiveClassSetter>(); |
| 58 | + |
| 59 | + /// <inheritdoc/> |
| 60 | + protected override void OnAttachedToVisualTree() |
| 61 | + { |
| 62 | + base.OnAttachedToVisualTree(); |
| 63 | + |
| 64 | + StopObserving(); |
| 65 | + StartObserving(); |
| 66 | + } |
| 67 | + |
| 68 | + /// <inheritdoc/> |
| 69 | + protected override void OnDetachedFromVisualTree() |
| 70 | + { |
| 71 | + base.OnDetachedFromVisualTree(); |
| 72 | + |
| 73 | + StopObserving(); |
| 74 | + } |
| 75 | + |
| 76 | + private void StartObserving() |
| 77 | + { |
| 78 | + var sourceControl = GetValue(SourceControlProperty) is not null |
| 79 | + ? SourceControl |
| 80 | + : AssociatedObject; |
| 81 | + |
| 82 | + if (sourceControl is not null) |
| 83 | + { |
| 84 | + _disposable = ObserveBounds(sourceControl); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private void StopObserving() |
| 89 | + { |
| 90 | + _disposable?.Dispose(); |
| 91 | + } |
| 92 | + |
| 93 | + private IDisposable ObserveBounds(Control sourceControl) |
| 94 | + { |
| 95 | + if (sourceControl is null) |
| 96 | + { |
| 97 | + throw new ArgumentNullException(nameof(sourceControl)); |
| 98 | + } |
| 99 | + |
| 100 | + return sourceControl.GetObservable(Visual.BoundsProperty) |
| 101 | + .Subscribe(new AnonymousObserver<Rect>(bounds => ValueChanged(sourceControl, Setters, bounds))); |
| 102 | + } |
| 103 | + |
| 104 | + private void ValueChanged(Control? sourceControl, AvaloniaList<AdaptiveClassSetter>? setters, Rect bounds) |
| 105 | + { |
| 106 | + if (sourceControl is null || setters is null) |
| 107 | + { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + foreach (var setter in setters) |
| 112 | + { |
| 113 | + var isMinOrMaxWidthSet = setter.IsSet(AdaptiveClassSetter.MinWidthProperty) |
| 114 | + || setter.IsSet(AdaptiveClassSetter.MaxWidthProperty); |
| 115 | + var widthConditionTriggered = GetResult(setter.MinWidthOperator, bounds.Width, setter.MinWidth) |
| 116 | + && GetResult(setter.MaxWidthOperator, bounds.Width, setter.MaxWidth); |
| 117 | + |
| 118 | + var isMinOrMaxHeightSet = setter.IsSet(AdaptiveClassSetter.MinHeightProperty) |
| 119 | + || setter.IsSet(AdaptiveClassSetter.MaxHeightProperty); |
| 120 | + var heightConditionTriggered = GetResult(setter.MinHeightOperator, bounds.Height, setter.MinHeight) |
| 121 | + && GetResult(setter.MaxHeightOperator, bounds.Height, setter.MaxHeight); |
| 122 | + |
| 123 | + var isAddClassTriggered = isMinOrMaxWidthSet switch |
| 124 | + { |
| 125 | + true when !isMinOrMaxHeightSet => widthConditionTriggered, |
| 126 | + false when isMinOrMaxHeightSet => heightConditionTriggered, |
| 127 | + true when isMinOrMaxHeightSet => widthConditionTriggered && heightConditionTriggered, |
| 128 | + _ => false |
| 129 | + }; |
| 130 | + |
| 131 | + var targetControl = setter.GetValue(AdaptiveClassSetter.TargetControlProperty) is not null |
| 132 | + ? setter.TargetControl |
| 133 | + : GetValue(TargetControlProperty) is not null |
| 134 | + ? TargetControl |
| 135 | + : AssociatedObject; |
| 136 | + |
| 137 | + if (targetControl is not null) |
| 138 | + { |
| 139 | + var className = setter.ClassName; |
| 140 | + var isPseudoClass = setter.IsPseudoClass; |
| 141 | + |
| 142 | + if (isAddClassTriggered) |
| 143 | + { |
| 144 | + Add(targetControl, className, isPseudoClass); |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + Remove(targetControl, className, isPseudoClass); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private bool GetResult(ComparisonConditionType comparisonConditionType, double property, double value) |
| 155 | + { |
| 156 | + return comparisonConditionType switch |
| 157 | + { |
| 158 | + // ReSharper disable once CompareOfFloatsByEqualityOperator |
| 159 | + ComparisonConditionType.Equal => property == value, |
| 160 | + // ReSharper disable once CompareOfFloatsByEqualityOperator |
| 161 | + ComparisonConditionType.NotEqual => property != value, |
| 162 | + ComparisonConditionType.LessThan => property < value, |
| 163 | + ComparisonConditionType.LessThanOrEqual => property <= value, |
| 164 | + ComparisonConditionType.GreaterThan => property > value, |
| 165 | + ComparisonConditionType.GreaterThanOrEqual => property >= value, |
| 166 | + _ => throw new ArgumentOutOfRangeException() |
| 167 | + }; |
| 168 | + } |
| 169 | + |
| 170 | + private static void Add(Control targetControl, string? className, bool isPseudoClass) |
| 171 | + { |
| 172 | + if (className is null || string.IsNullOrEmpty(className) || targetControl.Classes.Contains(className)) |
| 173 | + { |
| 174 | + return; |
| 175 | + } |
| 176 | + |
| 177 | + if (isPseudoClass) |
| 178 | + { |
| 179 | + ((IPseudoClasses)targetControl.Classes).Add(className); |
| 180 | + } |
| 181 | + else |
| 182 | + { |
| 183 | + targetControl.Classes.Add(className); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + private static void Remove(Control targetControl, string? className, bool isPseudoClass) |
| 188 | + { |
| 189 | + if (className is null || string.IsNullOrEmpty(className) || !targetControl.Classes.Contains(className)) |
| 190 | + { |
| 191 | + return; |
| 192 | + } |
| 193 | + |
| 194 | + if (isPseudoClass) |
| 195 | + { |
| 196 | + ((IPseudoClasses)targetControl.Classes).Remove(className); |
| 197 | + } |
| 198 | + else |
| 199 | + { |
| 200 | + targetControl.Classes.Remove(className); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments