Browse Source

Added property changed event for IToggleProvider (#20486)

release/latest
Melissa 2 months ago
committed by Julien Lebosquain
parent
commit
f588034cbf
  1. 26
      src/Avalonia.Controls/Automation/Peers/ToggleButtonAutomationPeer.cs
  2. 15
      src/Avalonia.Controls/Automation/TogglePatternIdentifiers.cs
  3. 6
      src/Windows/Avalonia.Win32.Automation/AutomationNode.cs

26
src/Avalonia.Controls/Automation/Peers/ToggleButtonAutomationPeer.cs

@ -1,4 +1,5 @@
using Avalonia.Automation.Provider; using Avalonia.Automation.Provider;
using Avalonia.Controls.Automation;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
namespace Avalonia.Automation.Peers namespace Avalonia.Automation.Peers
@ -8,19 +9,28 @@ namespace Avalonia.Automation.Peers
public ToggleButtonAutomationPeer(ToggleButton owner) public ToggleButtonAutomationPeer(ToggleButton owner)
: base(owner) : base(owner)
{ {
Owner.PropertyChanged += (a, e) =>
{
if (e.Property == ToggleButton.IsCheckedProperty)
{
RaisePropertyChangedEvent(
TogglePatternIdentifiers.ToggleStateProperty,
ToState((bool?)e.OldValue),
ToState((bool?)e.NewValue));
}
};
} }
public new ToggleButton Owner => (ToggleButton)base.Owner; public new ToggleButton Owner => (ToggleButton)base.Owner;
ToggleState IToggleProvider.ToggleState private ToggleState ToState(bool? value) => value switch
{ {
get => Owner.IsChecked switch true => ToggleState.On,
{ false => ToggleState.Off,
true => ToggleState.On, null => ToggleState.Indeterminate,
false => ToggleState.Off, };
null => ToggleState.Indeterminate,
}; ToggleState IToggleProvider.ToggleState => ToState(Owner.IsChecked);
}
void IToggleProvider.Toggle() void IToggleProvider.Toggle()
{ {

15
src/Avalonia.Controls/Automation/TogglePatternIdentifiers.cs

@ -0,0 +1,15 @@
using Avalonia.Automation.Provider;
namespace Avalonia.Automation
{
/// <summary>
/// Contains values used as identifiers by <see cref="IToggleProvider"/>.
/// </summary>
public static class TogglePatternIdentifiers
{
/// <summary>
/// Identifies the <see cref="IToggleProvider.ToggleState"/> property.
/// </summary>
public static AutomationProperty ToggleStateProperty { get; } = new AutomationProperty();
}
}

6
src/Windows/Avalonia.Win32.Automation/AutomationNode.cs

@ -54,10 +54,8 @@ namespace Avalonia.Win32.Automation
{ SelectionPatternIdentifiers.IsSelectionRequiredProperty, UiaPropertyId.SelectionIsSelectionRequired }, { SelectionPatternIdentifiers.IsSelectionRequiredProperty, UiaPropertyId.SelectionIsSelectionRequired },
{ SelectionPatternIdentifiers.SelectionProperty, UiaPropertyId.SelectionSelection }, { SelectionPatternIdentifiers.SelectionProperty, UiaPropertyId.SelectionSelection },
{ SelectionItemPatternIdentifiers.IsSelectedProperty, UiaPropertyId.SelectionItemIsSelected }, { SelectionItemPatternIdentifiers.IsSelectedProperty, UiaPropertyId.SelectionItemIsSelected },
{ { SelectionItemPatternIdentifiers.SelectionContainerProperty, UiaPropertyId.SelectionItemSelectionContainer },
SelectionItemPatternIdentifiers.SelectionContainerProperty, { TogglePatternIdentifiers.ToggleStateProperty, UiaPropertyId.ToggleToggleState },
UiaPropertyId.SelectionItemSelectionContainer
}
}; };
private static ConditionalWeakTable<AutomationPeer, AutomationNode> s_nodes = new(); private static ConditionalWeakTable<AutomationPeer, AutomationNode> s_nodes = new();

Loading…
Cancel
Save