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.
- Loading branch information
1 parent
dc236fb
commit 886d60b
Showing
4 changed files
with
165 additions
and
133 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
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,103 @@ | ||
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<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 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 (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; | ||
} | ||
} |
45 changes: 14 additions & 31 deletions
45
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,32 @@ | ||
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>>(_ => | ||
var dispose = Observable | ||
.FromEventPattern(mainWindow, nameof(mainWindow.Activated)) | ||
.Subscribe(new AnonymousObserver<EventPattern<object>>(e => | ||
{ | ||
if (Command is { } cmd && cmd.CanExecute(default)) | ||
{ | ||
cmd.Execute(default); | ||
} | ||
ExecuteCommand(); | ||
})); | ||
disposables.Add(dispose); | ||
} | ||
} | ||
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
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