using System;
using System.Windows.Input;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Reactive;
namespace Avalonia.Controls
{
///
/// A button with primary and secondary parts that can each be pressed separately.
/// The primary part behaves like a and the secondary part opens a flyout.
///
[TemplatePart("PART_PrimaryButton", typeof(Button))]
[TemplatePart("PART_SecondaryButton", typeof(Button))]
[PseudoClasses(pcFlyoutOpen, pcPressed)]
public class SplitButton : ContentControl, ICommandSource, IClickableControl
{
internal const string pcChecked = ":checked";
internal const string pcPressed = ":pressed";
internal const string pcFlyoutOpen = ":flyout-open";
///
/// Raised when the user presses the primary part of the .
///
public event EventHandler? Click
{
add => AddHandler(ClickEvent, value);
remove => RemoveHandler(ClickEvent, value);
}
///
/// Defines the event.
///
public static readonly RoutedEvent ClickEvent =
RoutedEvent.Register(
nameof(Click),
RoutingStrategies.Bubble);
///
/// Defines the property.
///
public static readonly StyledProperty CommandProperty =
Button.CommandProperty.AddOwner();
///
/// Defines the property.
///
public static readonly StyledProperty CommandParameterProperty =
Button.CommandParameterProperty.AddOwner();
///
/// Defines the property
///
public static readonly StyledProperty FlyoutProperty =
Button.FlyoutProperty.AddOwner();
///
/// Defines the property.
///
public static readonly StyledProperty HotKeyProperty =
Button.HotKeyProperty.AddOwner();
private Button? _primaryButton = null;
private Button? _secondaryButton = null;
private KeyGesture? _hotkey = default;
private bool _commandCanExecute = true;
private bool _isAttachedToLogicalTree = false;
private bool _isFlyoutOpen = false;
private bool _isKeyboardPressed = false;
private IDisposable? _flyoutPropertyChangedDisposable;
///
/// Initializes a new instance of the class.
///
public SplitButton()
{
}
///
/// Gets or sets the invoked when the primary part is pressed.
///
public ICommand? Command
{
get => GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
///
/// Gets or sets a parameter to be passed to the .
///
public object? CommandParameter
{
get => GetValue(CommandParameterProperty);
set => SetValue(CommandParameterProperty, value);
}
///
/// Gets or sets the that is shown when the secondary part is pressed.
///
public FlyoutBase? Flyout
{
get => GetValue(FlyoutProperty);
set => SetValue(FlyoutProperty, value);
}
///
/// Gets or sets an associated with this control
///
public KeyGesture? HotKey
{
get => GetValue(HotKeyProperty);
set => SetValue(HotKeyProperty, value);
}
///
/// Gets a value indicating whether the button is currently checked.
///
///
/// This property exists only for the derived and is
/// unused (set to false) within . Doing this allows the
/// two controls to share a default style.
///
internal virtual bool InternalIsChecked => false;
///
protected override bool IsEnabledCore => base.IsEnabledCore && _commandCanExecute;
///
void ICommandSource.CanExecuteChanged(object sender, EventArgs e) => this.CanExecuteChanged(sender, e);
///
private void CanExecuteChanged(object? sender, EventArgs e)
{
(var command, var parameter) = (Command, CommandParameter);
CanExecuteChanged(command, parameter);
}
private void CanExecuteChanged(ICommand? command, object? parameter)
{
if (!((ILogical)this).IsAttachedToLogicalTree)
{
return;
}
var canExecute = command is null || command.CanExecute(parameter);
if (canExecute != _commandCanExecute)
{
_commandCanExecute = canExecute;
UpdateIsEffectivelyEnabled();
}
}
///
/// Updates the visual state of the control by applying latest PseudoClasses.
///
protected void UpdatePseudoClasses()
{
PseudoClasses.Set(pcFlyoutOpen, _isFlyoutOpen);
PseudoClasses.Set(pcPressed, _isKeyboardPressed);
PseudoClasses.Set(pcChecked, InternalIsChecked);
}
///
/// Opens the secondary button's flyout.
///
protected void OpenFlyout()
{
Flyout?.ShowAt(this);
}
///
/// Closes the secondary button's flyout.
///
protected void CloseFlyout()
{
Flyout?.Hide();
}
///
/// Registers all flyout events.
///
/// The flyout to connect events to.
private void RegisterFlyoutEvents(FlyoutBase? flyout)
{
if (flyout != null)
{
flyout.Opened += Flyout_Opened;
flyout.Closed += Flyout_Closed;
_flyoutPropertyChangedDisposable = flyout.GetPropertyChangedObservable(Popup.PlacementProperty).Subscribe(Flyout_PlacementPropertyChanged);
}
}
///
/// Explicitly unregisters all flyout events.
///
/// The flyout to disconnect events from.
private void UnregisterFlyoutEvents(FlyoutBase? flyout)
{
if (flyout != null)
{
flyout.Opened -= Flyout_Opened;
flyout.Closed -= Flyout_Closed;
_flyoutPropertyChangedDisposable?.Dispose();
_flyoutPropertyChangedDisposable = null;
}
}
///
/// Explicitly unregisters all events related to the two buttons in OnApplyTemplate().
///
private void UnregisterEvents()
{
if (_primaryButton != null)
{
_primaryButton.Click -= PrimaryButton_Click;
}
if (_secondaryButton != null)
{
_secondaryButton.Click -= SecondaryButton_Click;
_secondaryButton.RemoveHandler(PointerPressedEvent, SecondaryButton_PreviewPointerPressed);
}
}
///
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
UnregisterEvents();
UnregisterFlyoutEvents(Flyout);
_primaryButton = e.NameScope.Find("PART_PrimaryButton");
_secondaryButton = e.NameScope.Find("PART_SecondaryButton");
if (_primaryButton != null)
{
_primaryButton.Click += PrimaryButton_Click;
}
if (_secondaryButton != null)
{
_secondaryButton.Click += SecondaryButton_Click;
_secondaryButton.AddHandler(PointerPressedEvent, SecondaryButton_PreviewPointerPressed, RoutingStrategies.Tunnel);
}
RegisterFlyoutEvents(Flyout);
UpdatePseudoClasses();
}
///
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
{
base.OnAttachedToLogicalTree(e);
// Control attached again, set Hotkey to create a hotkey manager for this control
SetCurrentValue(HotKeyProperty, _hotkey);
if (Command != null)
{
Command.CanExecuteChanged += CanExecuteChanged;
CanExecuteChanged(this, EventArgs.Empty);
}
_isAttachedToLogicalTree = true;
}
///
protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
{
base.OnDetachedFromLogicalTree(e);
// This will cause the hotkey manager to dispose the observer and the reference to this control
_hotkey = HotKey;
SetCurrentValue(HotKeyProperty, null);
if (Command != null)
{
Command.CanExecuteChanged -= CanExecuteChanged;
}
_isAttachedToLogicalTree = false;
}
///
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs e)
{
if (e.Property == CommandProperty)
{
// Must unregister events here while a reference to the old command still exists
var (oldValue, newValue) = e.GetOldAndNewValue();
if (_isAttachedToLogicalTree)
{
if (oldValue is ICommand oldCommand)
{
oldCommand.CanExecuteChanged -= CanExecuteChanged;
}
if (newValue is ICommand newCommand)
{
newCommand.CanExecuteChanged += CanExecuteChanged;
}
}
CanExecuteChanged(newValue, CommandParameter);
}
else if (e.Property == CommandParameterProperty && IsLoaded)
{
CanExecuteChanged(Command, e.NewValue);
}
else if (e.Property == FlyoutProperty)
{
var (oldFlyout, newFlyout) = e.GetOldAndNewValue();
// If flyout is changed while one is already open, make sure we
// close the old one first
// This is the same behavior as Button
if (oldFlyout != null &&
oldFlyout.IsOpen)
{
oldFlyout.Hide();
}
// Must unregister events here while a reference to the old flyout still exists
UnregisterFlyoutEvents(oldFlyout);
RegisterFlyoutEvents(newFlyout);
UpdatePseudoClasses();
}
base.OnPropertyChanged(e);
}
///
protected override void OnKeyDown(KeyEventArgs e)
{
var key = e.Key;
if ((IsFocused && key == Key.Space) || key == Key.Enter)
{
_isKeyboardPressed = true;
UpdatePseudoClasses();
}
base.OnKeyDown(e);
}
///
protected override void OnKeyUp(KeyEventArgs e)
{
var key = e.Key;
if ((IsFocused && key == Key.Space) || key == Key.Enter)
{
_isKeyboardPressed = false;
UpdatePseudoClasses();
// Consider this a click on the primary button
if (IsEffectivelyEnabled)
{
OnClickPrimary(null);
e.Handled = true;
}
}
else if (key == Key.Down && e.KeyModifiers.HasAllFlags(KeyModifiers.Alt) && IsEffectivelyEnabled
&& !XYFocusHelpers.IsAllowedXYNavigationMode(this, e.KeyDeviceType))
{
OpenFlyout();
e.Handled = true;
}
else if (key == Key.F4 && IsEffectivelyEnabled)
{
OpenFlyout();
e.Handled = true;
}
else if (e.Key == Key.Escape && _isFlyoutOpen)
{
// If Flyout doesn't have focusable content, close the flyout here
// This is the same behavior as Button
CloseFlyout();
e.Handled = true;
}
base.OnKeyUp(e);
}
///
/// Invokes the event when the primary button part is clicked.
///
/// The event args from the internal Click event.
protected virtual void OnClickPrimary(RoutedEventArgs? e)
{
(var command, var parameter) = (Command, CommandParameter);
// Note: It is not currently required to check enabled status; however, this is a failsafe
if (IsEffectivelyEnabled)
{
var eventArgs = new RoutedEventArgs(ClickEvent);
RaiseEvent(eventArgs);
if (!eventArgs.Handled && command?.CanExecute(parameter) == true)
{
command.Execute(parameter);
eventArgs.Handled = true;
}
}
}
///
/// Invoked when the secondary button part is clicked.
///
/// The event args from the internal Click event.
protected virtual void OnClickSecondary(RoutedEventArgs? e)
{
// Note: It is not currently required to check enabled status; however, this is a failsafe
if (IsEffectivelyEnabled)
{
if (_isFlyoutOpen)
{
CloseFlyout();
}
else
{
OpenFlyout();
}
}
}
///
/// Invoked when the split button's flyout is opened.
///
protected virtual void OnFlyoutOpened()
{
// Available for derived types
}
///
/// Invoked when the split button's flyout is closed.
///
protected virtual void OnFlyoutClosed()
{
// Available for derived types
}
///
/// Event handler for when the internal primary button part is clicked.
///
private void PrimaryButton_Click(object? sender, RoutedEventArgs e)
{
// Handle internal button click, so it won't bubble outside together with SplitButton.ClickEvent.
e.Handled = true;
OnClickPrimary(e);
}
///
/// Event handler for when the internal secondary button part is clicked.
///
private void SecondaryButton_Click(object? sender, RoutedEventArgs e)
{
// Handle internal button click, so it won't bubble outside.
e.Handled = true;
OnClickSecondary(e);
}
///
/// Event handler for when the internal secondary button part is pressed.
///
private void SecondaryButton_PreviewPointerPressed(object? sender, PointerPressedEventArgs e)
{
if (_isFlyoutOpen && _secondaryButton?.IsEffectivelyEnabled == true)
{
if (e.GetCurrentPoint(_secondaryButton).Properties.IsLeftButtonPressed)
{
// When a flyout is open with OverlayDismissEventPassThrough enabled and the secondary button
// is pressed, close the flyout
e.Handled = true;
OnClickSecondary(e);
}
}
}
///
/// Called when the property changes.
///
private void Flyout_PlacementPropertyChanged(AvaloniaPropertyChangedEventArgs e)
{
UpdatePseudoClasses();
}
///
/// Event handler for when the split button's flyout is opened.
///
private void Flyout_Opened(object? sender, EventArgs e)
{
var flyout = sender as FlyoutBase;
// It is possible to share flyouts among multiple controls including SplitButton.
// This can cause a problem here since all controls that share a flyout receive
// the same Opened/Closed events at the same time.
// For SplitButton that means they all would be updating their pseudoclasses accordingly.
// In other words, all SplitButtons with a shared Flyout would have the backgrounds changed together.
// To fix this, only continue here if the Flyout target matches this SplitButton instance.
if (object.ReferenceEquals(flyout?.Target, this))
{
_isFlyoutOpen = true;
UpdatePseudoClasses();
OnFlyoutOpened();
}
}
///
/// Event handler for when the split button's flyout is closed.
///
private void Flyout_Closed(object? sender, EventArgs e)
{
var flyout = sender as FlyoutBase;
// See comments in Flyout_Opened
if (object.ReferenceEquals(flyout?.Target, this))
{
_isFlyoutOpen = false;
UpdatePseudoClasses();
OnFlyoutClosed();
}
}
void IClickableControl.RaiseClick() =>
(_primaryButton as IClickableControl)?.RaiseClick();
}
}