This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into release/11.1.0-beta1.1
- Loading branch information
Showing
36 changed files
with
1,526 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/Avalonia.Xaml.Interactions.Custom/ExecuteCommandBehaviorBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
using System.Windows.Input; | ||
using Avalonia.Controls; | ||
using Avalonia.Threading; | ||
using Avalonia.VisualTree; | ||
|
||
namespace Avalonia.Xaml.Interactions.Custom; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public abstract class ExecuteCommandBehaviorBase : AttachedToVisualTreeBehavior<Control> | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
public static readonly StyledProperty<bool> IsEnabledProperty = | ||
AvaloniaProperty.Register<ExecuteCommandBehaviorBase, bool>(nameof(IsEnabled), true); | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public static readonly StyledProperty<ICommand?> CommandProperty = | ||
AvaloniaProperty.Register<ExecuteCommandBehaviorBase, ICommand?>(nameof(Command)); | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public static readonly StyledProperty<object?> CommandParameterProperty = | ||
AvaloniaProperty.Register<ExecuteCommandBehaviorBase, object?>(nameof(CommandParameter)); | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public static readonly StyledProperty<bool> FocusTopLevelProperty = | ||
AvaloniaProperty.Register<ExecuteCommandBehaviorBase, bool>(nameof(FocusTopLevel)); | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public static readonly StyledProperty<Control?> FocusControlProperty = | ||
AvaloniaProperty.Register<ExecuteCommandBehaviorBase, Control?>(nameof(CommandParameter)); | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public bool IsEnabled | ||
{ | ||
get => GetValue(IsEnabledProperty); | ||
set => SetValue(IsEnabledProperty, value); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public ICommand? Command | ||
{ | ||
get => GetValue(CommandProperty); | ||
set => SetValue(CommandProperty, value); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public object? CommandParameter | ||
{ | ||
get => GetValue(CommandParameterProperty); | ||
set => SetValue(CommandParameterProperty, value); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public bool FocusTopLevel | ||
{ | ||
get => GetValue(FocusTopLevelProperty); | ||
set => SetValue(FocusTopLevelProperty, value); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
[ResolveByName] | ||
public Control? FocusControl | ||
{ | ||
get => GetValue(FocusControlProperty); | ||
set => SetValue(FocusControlProperty, value); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <returns></returns> | ||
protected bool ExecuteCommand() | ||
{ | ||
if (!IsEnabled) | ||
{ | ||
return false; | ||
} | ||
|
||
if (AssociatedObject is not { IsVisible: true, IsEnabled: true }) | ||
{ | ||
return false; | ||
} | ||
|
||
if (Command?.CanExecute(CommandParameter) != true) | ||
{ | ||
return false; | ||
} | ||
|
||
if (FocusTopLevel) | ||
{ | ||
Dispatcher.UIThread.Post(() => (AssociatedObject?.GetVisualRoot() as TopLevel)?.Focus()); | ||
} | ||
|
||
if (FocusControl is { } focusControl) | ||
{ | ||
Dispatcher.UIThread.Post(() => focusControl.Focus()); | ||
} | ||
|
||
Command.Execute(CommandParameter); | ||
return true; | ||
} | ||
} |
50 changes: 18 additions & 32 deletions
50
src/Avalonia.Xaml.Interactions.Custom/ExecuteCommandOnActivatedBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,35 @@ | ||
using System.Reactive; | ||
using System.Reactive.Disposables; | ||
using System.Reactive.Linq; | ||
using System.Windows.Input; | ||
using Avalonia.Controls; | ||
using Avalonia.Controls.ApplicationLifetimes; | ||
|
||
namespace Avalonia.Xaml.Interactions.Custom; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public class ExecuteCommandOnActivatedBehavior : DisposingBehavior<Control> | ||
public class ExecuteCommandOnActivatedBehavior : ExecuteCommandBehaviorBase | ||
{ | ||
public static readonly StyledProperty<ICommand?> CommandProperty = | ||
AvaloniaProperty.Register<ExecuteCommandOnActivatedBehavior, ICommand?>(nameof(Command)); | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public ICommand? Command | ||
{ | ||
get => GetValue(CommandProperty); | ||
set => SetValue(CommandProperty, value); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="disposables"></param> | ||
protected override void OnAttached(CompositeDisposable disposables) | ||
{ | ||
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime lifetime) | ||
{ | ||
var mainWindow = lifetime.MainWindow; | ||
/// <param name="disposable"></param> | ||
protected override void OnAttachedToVisualTree(CompositeDisposable disposable) | ||
{ | ||
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime lifetime) | ||
{ | ||
var mainWindow = lifetime.MainWindow; | ||
|
||
var dispose = Observable | ||
.FromEventPattern(mainWindow, nameof(mainWindow.Activated)) | ||
.Subscribe(new AnonymousObserver<EventPattern<object>>(_ => | ||
{ | ||
if (Command is { } cmd && cmd.CanExecute(default)) | ||
if (mainWindow is not null) | ||
{ | ||
var dispose = Observable | ||
.FromEventPattern(mainWindow, nameof(mainWindow.Activated)) | ||
.Subscribe(new AnonymousObserver<EventPattern<object>>(e => | ||
{ | ||
cmd.Execute(default); | ||
} | ||
})); | ||
disposables.Add(dispose); | ||
} | ||
} | ||
ExecuteCommand(); | ||
})); | ||
disposable.Add(dispose); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.