Skip to content

Commit

Permalink
Add more overloads for AddHandler/SetValue
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbitism authored Apr 21, 2024
1 parent 2e1bdeb commit 3e3cf55
Show file tree
Hide file tree
Showing 4 changed files with 457 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,43 @@ namespace Irihi.Avalonia.Shared.Helpers;

public static class AvaloniaPropertyExtension
{
public static void SetValue<T>(this AvaloniaProperty<T> property, T value, params AvaloniaObject?[] objects)
public static void SetValue<T>(
this AvaloniaProperty<T> property,
T value,
params AvaloniaObject?[] objects)
{
foreach (var obj in objects)
{
obj?.SetValue(property, value);
}
}

public static void SetValue<TValue, TItem>(this AvaloniaProperty<TValue> property, TValue value, params TItem?[] objects) where TItem: AvaloniaObject
public static void SetValue<T, TControl>(
this AvaloniaProperty<T> property,
T value,
IEnumerable<TControl?> objects)
where TControl: AvaloniaObject
{
foreach (var obj in objects)
{
obj?.SetValue(property, value);
}
}
}

public static void AffectsPseudoClass<TControl>(this AvaloniaProperty<bool> property, string pseudoClass, RoutedEvent<RoutedEventArgs>? routedEvent = null)
public static void AffectsPseudoClass<TControl>(
this AvaloniaProperty<bool> property,
string pseudoClass,
RoutedEvent<RoutedEventArgs>? routedEvent = null)
where TControl: Control
{
property.Changed.AddClassHandler<TControl, bool>((control, args) => {OnPropertyChanged(control, args, pseudoClass, routedEvent); });
}

private static void OnPropertyChanged<TControl, TArgs>(TControl control, AvaloniaPropertyChangedEventArgs<bool> args, string pseudoClass, RoutedEvent<TArgs>? routedEvent)
private static void OnPropertyChanged<TControl, TArgs>(
TControl control,
AvaloniaPropertyChangedEventArgs<bool> args,
string pseudoClass,
RoutedEvent<TArgs>? routedEvent)
where TControl: Control
where TArgs: RoutedEventArgs, new()
{
Expand All @@ -39,7 +53,9 @@ private static void OnPropertyChanged<TControl, TArgs>(TControl control, Avaloni
}
}

public static void AffectsPseudoClass<TControl, TArgs>(this AvaloniaProperty<bool> property, string pseudoClass,
public static void AffectsPseudoClass<TControl, TArgs>(
this AvaloniaProperty<bool> property,
string pseudoClass,
RoutedEvent<TArgs>? routedEvent = null)
where TControl: Control
where TArgs: RoutedEventArgs, new()
Expand Down
158 changes: 151 additions & 7 deletions src/Irihi.Avalonia.Shared.Public/Helpers/RoutedEventExtension.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
using Avalonia.Controls;
using Avalonia.Interactivity;
using Irihi.Avalonia.Shared.Reactive;

namespace Irihi.Avalonia.Shared.Helpers;

public static class RoutedEventExtension
{
public static void AddHandler<TArgs>(this RoutedEvent<TArgs> routedEvent, EventHandler<TArgs> handler, params Interactive?[] controls)
public static void AddHandler<TArgs>(
this RoutedEvent<TArgs> routedEvent,
EventHandler<TArgs> handler,
params Interactive?[] controls)
where TArgs : RoutedEventArgs
{
foreach (var t in controls)
{
t?.AddHandler(routedEvent, handler);
}
}

public static void AddHandler<TArgs, TControl>(
this RoutedEvent<TArgs> routedEvent,
EventHandler<TArgs> handler,
params TControl?[] controls)
where TControl: Interactive
where TArgs : RoutedEventArgs
{
foreach (var t in controls)
Expand All @@ -15,7 +30,8 @@ public static void AddHandler<TArgs>(this RoutedEvent<TArgs> routedEvent, EventH
}
}

public static void AddHandler<TArgs>(this RoutedEvent<TArgs> routedEvent,
public static void AddHandler<TArgs>(
this RoutedEvent<TArgs> routedEvent,
EventHandler<TArgs> handler,
RoutingStrategies strategies = RoutingStrategies.Bubble | RoutingStrategies.Direct,
bool handledEventsToo = false,
Expand All @@ -28,17 +44,99 @@ public static void AddHandler<TArgs>(this RoutedEvent<TArgs> routedEvent,
}
}

public static void RemoveHandler<TArgs>(this RoutedEvent<TArgs> routedEvent, EventHandler<TArgs> handler, params Interactive?[] controls)
public static void AddHandler<TArgs, TControl>(
this RoutedEvent<TArgs> routedEvent,
EventHandler<TArgs> handler,
RoutingStrategies strategies = RoutingStrategies.Bubble | RoutingStrategies.Direct,
bool handledEventsToo = false,
params TControl?[] controls)
where TArgs : RoutedEventArgs
where TControl: Interactive
{
foreach (var t in controls)
{
t?.AddHandler(routedEvent, handler, strategies, handledEventsToo);
}
}

public static void AddHandler<TArgs, TControl>(
this RoutedEvent<TArgs> routedEvent,
EventHandler<TArgs> handler,
IEnumerable<TControl?> controls,
RoutingStrategies strategies = RoutingStrategies.Bubble | RoutingStrategies.Direct,
bool handledEventsToo = false)
where TArgs : RoutedEventArgs
where TControl: Interactive
{
foreach (var t in controls)
{
t?.AddHandler(routedEvent, handler, strategies, handledEventsToo);
}
}

public static void RemoveHandler<TArgs>(
this RoutedEvent<TArgs> routedEvent,
EventHandler<TArgs> handler,
params Interactive?[] controls)
where TArgs : RoutedEventArgs
{
foreach (var t in controls)
{
t?.RemoveHandler(routedEvent, handler);
}
}

public static IDisposable AddDisposableHandler<TArgs>(this RoutedEvent<TArgs> routedEvent, EventHandler<TArgs> handler, params Interactive?[] controls)

public static void RemoveHandler<TArgs, TControl>(
this RoutedEvent<TArgs> routedEvent,
EventHandler<TArgs> handler,
params TControl?[] controls)
where TArgs : RoutedEventArgs
where TControl: Interactive
{
foreach (var t in controls)
{
t?.RemoveHandler(routedEvent, handler);
}
}

public static void RemoveHandler<TArgs, TControl>(
this RoutedEvent<TArgs> routedEvent,
EventHandler<TArgs> handler,
IEnumerable<TControl?> controls)
where TArgs : RoutedEventArgs
where TControl: Interactive
{
foreach (var t in controls)
{
t?.RemoveHandler(routedEvent, handler);
}
}

public static IDisposable AddDisposableHandler<TArgs>(
this RoutedEvent<TArgs> routedEvent,
EventHandler<TArgs> handler,
params Interactive?[] controls)
where TArgs : RoutedEventArgs
{
var list = new List<IDisposable?>(controls.Length);
foreach (var t in controls)
{
var disposable = t?.AddDisposableHandler(routedEvent, handler);
if (disposable != null)
{
list.Add(disposable);
}
}
var result = new ReadonlyDisposableCollection(list);
return result;
}

public static IDisposable AddDisposableHandler<TArgs, TControl>(
this RoutedEvent<TArgs> routedEvent,
EventHandler<TArgs> handler,
params TControl?[] controls)
where TArgs : RoutedEventArgs
where TControl: Interactive
{
List<IDisposable?> list = new List<IDisposable?>(controls.Length);
foreach (var t in controls)
Expand All @@ -53,7 +151,8 @@ public static IDisposable AddDisposableHandler<TArgs>(this RoutedEvent<TArgs> ro
return result;
}

public static IDisposable AddDisposableHandler<TArgs>(this RoutedEvent<TArgs> routedEvent,
public static IDisposable AddDisposableHandler<TArgs>(
this RoutedEvent<TArgs> routedEvent,
EventHandler<TArgs> handler,
RoutingStrategies strategies = RoutingStrategies.Bubble | RoutingStrategies.Direct,
bool handledEventsToo = false,
Expand All @@ -72,4 +171,49 @@ public static IDisposable AddDisposableHandler<TArgs>(this RoutedEvent<TArgs> ro
var result = new ReadonlyDisposableCollection(list);
return result;
}

public static IDisposable AddDisposableHandler<TArgs, TControl>(
this RoutedEvent<TArgs> routedEvent,
EventHandler<TArgs> handler,
RoutingStrategies strategies = RoutingStrategies.Bubble | RoutingStrategies.Direct,
bool handledEventsToo = false,
params TControl?[] controls)
where TArgs : RoutedEventArgs
where TControl: Interactive
{
List<IDisposable?> list = new List<IDisposable?>(controls.Length);
foreach (var t in controls)
{
var disposable = t?.AddDisposableHandler(routedEvent, handler, strategies, handledEventsToo);
if (disposable != null)
{
list.Add(disposable);
}
}
var result = new ReadonlyDisposableCollection(list);
return result;
}

public static IDisposable AddDisposableHandler<TArgs, TControl>(
this RoutedEvent<TArgs> routedEvent,
EventHandler<TArgs> handler,
IEnumerable<TControl> controls,
RoutingStrategies strategies = RoutingStrategies.Bubble | RoutingStrategies.Direct,
bool handledEventsToo = false)
where TArgs : RoutedEventArgs
where TControl: Interactive
{
// list is not initialized with controls.Count() to avoid multiple enumeration
var list = new List<IDisposable?>();
foreach (var t in controls)
{
var disposable = t?.AddDisposableHandler(routedEvent, handler, strategies, handledEventsToo);
if (disposable != null)
{
list.Add(disposable);
}
}
var result = new ReadonlyDisposableCollection(list);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,17 @@ public void Property_MultipleObject_InArray()
Button b1 = new Button();
Button b2 = new Button();
Button[] buttons = {b1, b2};
Visual.IsVisibleProperty.SetValue(true, buttons);
Visual.IsVisibleProperty.SetValue<bool, Button>(true, buttons);
Assert.True(b1.IsVisible);
Assert.True(b2.IsVisible);
}

[Fact]
public void Property_MultipleObject_InList()
{
Button b1 = new Button();
Button b2 = new Button();
Visual.IsVisibleProperty.SetValue(true, new List<Button> { b1, b2 });
Assert.True(b1.IsVisible);
Assert.True(b2.IsVisible);
}
Expand Down
Loading

0 comments on commit 3e3cf55

Please sign in to comment.