Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Mar 5, 2024
1 parent b04d772 commit 33c5ad3
Show file tree
Hide file tree
Showing 28 changed files with 413 additions and 417 deletions.
78 changes: 40 additions & 38 deletions src/Avalonia.Xaml.Interactions.Custom/BoundsObserverBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,71 @@
namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
///
/// Observes the bounds of an associated <see cref="Control"/> and updates its Width and Height properties.
/// </summary>
public class BoundsObserverBehavior : DisposingBehavior<Control>
{
/// <summary>
///
/// Defines the <see cref="Bounds"/> property.
/// </summary>
public static readonly StyledProperty<Rect> BoundsProperty =
AvaloniaProperty.Register<BoundsObserverBehavior, Rect>(nameof(Bounds), defaultBindingMode: BindingMode.OneWay);
public static readonly StyledProperty<Rect> BoundsProperty =
AvaloniaProperty.Register<BoundsObserverBehavior, Rect>(nameof(Bounds), defaultBindingMode: BindingMode.OneWay);

/// <summary>
///
/// Defines the <see cref="Width"/> property.
/// </summary>
public static readonly StyledProperty<double> WidthProperty =
AvaloniaProperty.Register<BoundsObserverBehavior, double>(nameof(Width), defaultBindingMode: BindingMode.TwoWay);
public static readonly StyledProperty<double> WidthProperty =
AvaloniaProperty.Register<BoundsObserverBehavior, double>(nameof(Width),
defaultBindingMode: BindingMode.TwoWay);

/// <summary>
///
/// Defines the <see cref="Height"/> property.
/// </summary>
public static readonly StyledProperty<double> HeightProperty =
AvaloniaProperty.Register<BoundsObserverBehavior, double>(nameof(Height), defaultBindingMode: BindingMode.TwoWay);
public static readonly StyledProperty<double> HeightProperty =
AvaloniaProperty.Register<BoundsObserverBehavior, double>(nameof(Height),
defaultBindingMode: BindingMode.TwoWay);

/// <summary>
///
/// Gets or sets the bounds of the associated control. This is a styled Avalonia property.
/// </summary>
public Rect Bounds
{
get => GetValue(BoundsProperty);
set => SetValue(BoundsProperty, value);
}
public Rect Bounds
{
get => GetValue(BoundsProperty);
set => SetValue(BoundsProperty, value);
}

/// <summary>
///
/// Gets or sets the width of the associated control. This is a two-way bound Avalonia property.
/// </summary>
public double Width
{
get => GetValue(WidthProperty);
set => SetValue(WidthProperty, value);
}
public double Width
{
get => GetValue(WidthProperty);
set => SetValue(WidthProperty, value);
}

/// <summary>
///
/// Gets or sets the height of the associated control. This is a two-way bound Avalonia property.
/// </summary>
public double Height
{
get => GetValue(HeightProperty);
set => SetValue(HeightProperty, value);
}
public double Height
{
get => GetValue(HeightProperty);
set => SetValue(HeightProperty, value);
}

/// <summary>
///
/// Attaches the behavior to the associated control and starts observing its bounds to update the Width and Height properties accordingly.
/// </summary>
/// <param name="disposables"></param>
protected override void OnAttached(CompositeDisposable disposables)
{
if (AssociatedObject is not null)
{
disposables.Add(this.GetObservable(BoundsProperty)
.Subscribe(new AnonymousObserver<Rect>(bounds =>
/// <param name="disposables">A composite disposable used to manage the lifecycle of subscriptions and other disposables.</param>
protected override void OnAttached(CompositeDisposable disposables)
{
if (AssociatedObject is not null)
{
disposables.Add(this.GetObservable(BoundsProperty)
.Subscribe(new AnonymousObserver<Rect>(bounds =>
{
Width = bounds.Width;
Height = bounds.Height;
})));
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,67 +14,67 @@ public class ButtonExecuteCommandOnKeyDownBehavior : AttachedToVisualTreeBehavio
/// <summary>
///
/// </summary>
public static readonly StyledProperty<Key?> KeyProperty =
AvaloniaProperty.Register<ButtonExecuteCommandOnKeyDownBehavior, Key?>(nameof(Key));
public static readonly StyledProperty<Key?> KeyProperty =
AvaloniaProperty.Register<ButtonExecuteCommandOnKeyDownBehavior, Key?>(nameof(Key));

/// <summary>
///
/// </summary>
public static readonly StyledProperty<bool> IsEnabledProperty =
AvaloniaProperty.Register<ButtonExecuteCommandOnKeyDownBehavior, bool>(nameof(IsEnabled));
public static readonly StyledProperty<bool> IsEnabledProperty =
AvaloniaProperty.Register<ButtonExecuteCommandOnKeyDownBehavior, bool>(nameof(IsEnabled));

/// <summary>
///
/// </summary>
public Key? Key
{
get => GetValue(KeyProperty);
set => SetValue(KeyProperty, value);
}
public Key? Key
{
get => GetValue(KeyProperty);
set => SetValue(KeyProperty, value);
}

/// <summary>
///
/// </summary>
public bool IsEnabled
{
get => GetValue(IsEnabledProperty);
set => SetValue(IsEnabledProperty, value);
}
public bool IsEnabled
{
get => GetValue(IsEnabledProperty);
set => SetValue(IsEnabledProperty, value);
}

/// <summary>
///
/// </summary>
/// <param name="disposables"></param>
protected override void OnAttachedToVisualTree(CompositeDisposable disposables)
{
var button = AssociatedObject;
if (button is null)
{
return;
}
protected override void OnAttachedToVisualTree(CompositeDisposable disposables)
{
var button = AssociatedObject;
if (button is null)
{
return;
}

if (button.GetVisualRoot() is InputElement inputRoot)
{
if (button.GetVisualRoot() is InputElement inputRoot)
{
var disposable = inputRoot.AddDisposableHandler(InputElement.KeyDownEvent, RootDefaultKeyDown);
disposables.Add(disposable);
}
}
}
}

private void RootDefaultKeyDown(object? sender, KeyEventArgs e)
{
var button = AssociatedObject;
if (button is null)
{
return;
}
private void RootDefaultKeyDown(object? sender, KeyEventArgs e)
{
var button = AssociatedObject;
if (button is null)
{
return;
}

if (Key is { } && e.Key == Key && button.IsVisible && button.IsEnabled && IsEnabled)
{
if (!e.Handled && button.Command?.CanExecute(button.CommandParameter) == true)
{
button.Command.Execute(button.CommandParameter);
e.Handled = true;
}
}
}
if (Key is { } && e.Key == Key && button.IsVisible && button.IsEnabled && IsEnabled)
{
if (!e.Handled && button.Command?.CanExecute(button.CommandParameter) == true)
{
button.Command.Execute(button.CommandParameter);
e.Handled = true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ private void UpdateAvaloniaPropertyValue(AvaloniaObject targetObject, AvaloniaPr
var valueAsString = Value.ToString();
if (valueAsString is not null)
{
result = propertyTypeInfo.IsEnum
? Enum.Parse(propertyType, valueAsString, false)
result = propertyTypeInfo.IsEnum
? Enum.Parse(propertyType, valueAsString, false)
: TypeConverterHelper.Convert(valueAsString, propertyType);
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/Avalonia.Xaml.Interactions.Custom/DisposingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ namespace Avalonia.Xaml.Interactions.Custom;
/// <typeparam name="T">The object type to attach to</typeparam>
public abstract class DisposingBehavior<T> : Behavior<T> where T : AvaloniaObject
{
private CompositeDisposable? _disposables;
private CompositeDisposable? _disposables;

/// <inheritdoc />
protected override void OnAttached()
{
base.OnAttached();
protected override void OnAttached()

Check warning on line 15 in src/Avalonia.Xaml.Interactions.Custom/DisposingBehavior.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Base member 'Avalonia.Xaml.Interactivity.Behavior<T>.OnAttached()' with 'RequiresUnreferencedCodeAttribute' has a derived member 'Avalonia.Xaml.Interactions.Custom.DisposingBehavior<T>.OnAttached()' without 'RequiresUnreferencedCodeAttribute'. 'RequiresUnreferencedCodeAttribute' annotations must match across all interface implementations or overrides.
{
base.OnAttached();

_disposables?.Dispose();
_disposables?.Dispose();

_disposables = new CompositeDisposable();
_disposables = new CompositeDisposable();

OnAttached(_disposables);
}
OnAttached(_disposables);
}

/// <summary>
/// Called after the behavior is attached to the <see cref="Behavior.AssociatedObject"/>.
/// </summary>
/// <param name="disposables">The group of disposable resources that are disposed together.</param>
protected abstract void OnAttached(CompositeDisposable disposables);
protected abstract void OnAttached(CompositeDisposable disposables);

/// <inheritdoc />
protected override void OnDetaching()
{
base.OnDetaching();
protected override void OnDetaching()
{
base.OnDetaching();

_disposables?.Dispose();
}
_disposables?.Dispose();
}
}
24 changes: 12 additions & 12 deletions src/Avalonia.Xaml.Interactions.Custom/DisposingTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ namespace Avalonia.Xaml.Interactions.Custom;
/// </summary>
public abstract class DisposingTrigger : Trigger
{
private readonly CompositeDisposable _disposables = new();
private readonly CompositeDisposable _disposables = new();

/// <summary>
///
/// </summary>
protected override void OnAttached()
{
base.OnAttached();
protected override void OnAttached()
{
base.OnAttached();

OnAttached(_disposables);
}
OnAttached(_disposables);
}

/// <summary>
///
/// </summary>
/// <param name="disposables"></param>
protected abstract void OnAttached(CompositeDisposable disposables);
protected abstract void OnAttached(CompositeDisposable disposables);

/// <summary>
///
/// </summary>
protected override void OnDetaching()
{
base.OnDetaching();
protected override void OnDetaching()
{
base.OnDetaching();

_disposables.Dispose();
}
_disposables.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private void Parent_PointerMoved(object? sender, PointerEventArgs args)
tr.X += pos.X - _previous.X;
tr.Y += pos.Y - _previous.Y;
}

_previous = pos;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,36 @@ public class ExecuteCommandOnDoubleTappedBehavior : DisposingBehavior<Control>
/// <summary>
///
/// </summary>
public static readonly StyledProperty<ICommand?> CommandProperty =
AvaloniaProperty.Register<ExecuteCommandOnDoubleTappedBehavior, ICommand?>(nameof(Command));
public static readonly StyledProperty<ICommand?> CommandProperty =
AvaloniaProperty.Register<ExecuteCommandOnDoubleTappedBehavior, ICommand?>(nameof(Command));

/// <summary>
///
/// </summary>
public ICommand? Command
{
get => GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
public ICommand? Command
{
get => GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}

/// <summary>
///
/// </summary>
/// <param name="disposables"></param>
protected override void OnAttached(CompositeDisposable disposables)
{
protected override void OnAttached(CompositeDisposable disposables)
{
var disposable = Gestures.DoubleTappedEvent.AddClassHandler<InputElement>(
(x, _) =>
{
if (Equals(x, AssociatedObject))
{
if (Command is { } cmd && cmd.CanExecute(default))
{
cmd.Execute(default);
}
}
},
RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
(x, _) =>
{
if (Equals(x, AssociatedObject))
{
if (Command is { } cmd && cmd.CanExecute(default))
{
cmd.Execute(default);
}
}
},
RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
disposables.Add(disposable);
}
}
}
Loading

0 comments on commit 33c5ad3

Please sign in to comment.