Skip to content

Commit

Permalink
feat: Implement ToggleButtonAutomationPeer
Browse files Browse the repository at this point in the history
  • Loading branch information
morning4coffe-dev authored and MartinZikmund committed Jun 17, 2024
1 parent b515ef6 commit ef52c88
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 58 deletions.
116 changes: 60 additions & 56 deletions src/Uno.UI/UI/Xaml/Automation/Peers/ToggleButtonAutomationPeer.cs
Original file line number Diff line number Diff line change
@@ -1,81 +1,85 @@
using Microsoft.UI.Xaml.Controls.Primitives;
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
// MUX Reference ToggleButtonAutomationPeer_Partial.cpp, tag winui3/release/1.4.2
using Microsoft.UI.Xaml.Controls.Primitives;
using static Microsoft/* UWP don't rename */.UI.Xaml.Controls._Tracing;

namespace Microsoft.UI.Xaml.Automation.Peers
namespace Microsoft.UI.Xaml.Automation.Peers;

/// <summary>
/// Exposes ToggleButton types to Microsoft UI Automation.
/// </summary>
public partial class ToggleButtonAutomationPeer : ButtonBaseAutomationPeer, Provider.IToggleProvider
{
public partial class ToggleButtonAutomationPeer : ButtonBaseAutomationPeer, Provider.IToggleProvider
public ToggleButtonAutomationPeer(ToggleButton owner) : base(owner)
{
public ToggleButtonAutomationPeer(ToggleButton owner) : base(owner)
{
}
}

protected override string GetClassNameCore() => "ToggleButton";
protected override string GetClassNameCore() => nameof(ToggleButton);

protected override AutomationControlType GetAutomationControlTypeCore() => AutomationControlType.Button;
protected override AutomationControlType GetAutomationControlTypeCore()
=> AutomationControlType.Button;

public ToggleState ToggleState
/// <summary>
/// Gets the toggle state of the control.
/// </summary>
public ToggleState ToggleState
=> ((ToggleButton)Owner).IsChecked switch
{
get
{
switch (((ToggleButton)Owner).IsChecked)
{
case (true):
return ToggleState.On;
case (false):
return ToggleState.Off;
default:
return ToggleState.Indeterminate;
}
}
}
(true) => ToggleState.On,
(false) => ToggleState.Off,
_ => ToggleState.Indeterminate,
};

public void Toggle()
/// <summary>
/// Cycles through the toggle states of a control.
/// </summary>
public void Toggle()
{
if (IsEnabled())
{
if (IsEnabled())
{
((ToggleButton)Owner).AutomationToggleButtonOnToggle();
}
((ToggleButton)Owner).AutomationToggleButtonOnToggle();
}
}

internal void RaiseToggleStatePropertyChangedEvent(object pOldValue, object pNewValue)
{
var oldValue = ConvertToToggleState(pOldValue);
internal void RaiseToggleStatePropertyChangedEvent(object pOldValue, object pNewValue)
{
var oldValue = ConvertToToggleState(pOldValue);

var newValue = ConvertToToggleState(pNewValue);
var newValue = ConvertToToggleState(pNewValue);

MUX_ASSERT(oldValue != ToggleState.Indeterminate);
MUX_ASSERT(newValue != ToggleState.Indeterminate);
MUX_ASSERT(oldValue != ToggleState.Indeterminate);
MUX_ASSERT(newValue != ToggleState.Indeterminate);

if (oldValue != newValue)
{
RaisePropertyChangedEvent(TogglePatternIdentifiers.ToggleStateProperty, oldValue, newValue);
}
if (oldValue != newValue)
{
RaisePropertyChangedEvent(TogglePatternIdentifiers.ToggleStateProperty, oldValue, newValue);
}
}

/// <summary>
/// Convert the Boolean in Inspectable to the ToggleState Enum, if the Inspectable is null
/// that corresponds to Indeterminate state.
/// </summary>
internal static ToggleState ConvertToToggleState(object pValue)
{
var pToggleState = ToggleState.Indeterminate;

/// <summary>
/// Convert the Boolean in Inspectable to the ToggleState Enum, if the Inspectable is null
/// that corresponds to Indeterminate state.
/// </summary>
internal static ToggleState ConvertToToggleState(object pValue)
if (pValue != null)
{
var pToggleState = ToggleState.Indeterminate;
var bValue = (bool)pValue;

if (pValue != null)
if (bValue)
{
var bValue = (bool)pValue;

if (bValue)
{
pToggleState = ToggleState.On;
}
else
{

pToggleState = ToggleState.Off;
}
pToggleState = ToggleState.On;
}
else
{

return pToggleState;
pToggleState = ToggleState.Off;
}
}

return pToggleState;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// MUX Reference ToggleMenuFlyoutItemAutomationPeer_Partial.cpp, tag winui3/release/1.4.2
namespace Microsoft.UI.Xaml.Automation.Peers;

/// <summary>
/// Exposes ToggleMenuFlyoutItem types to Microsoft UI Automation.
/// </summary>
public partial class ToggleMenuFlyoutItemAutomationPeer : FrameworkElementAutomationPeer, Provider.IToggleProvider
{
public ToggleMenuFlyoutItemAutomationPeer(Controls.ToggleMenuFlyoutItem owner) : base(owner)
Expand Down Expand Up @@ -63,6 +66,10 @@ protected override int GetSizeOfSetCore()
return returnValue;
}

/// <summary>
/// Cycles through the toggle states of a control.
/// </summary>
/// <exception cref="ElementNotEnabledException"></exception>
public void Toggle()
{
if (!IsEnabled())
Expand All @@ -73,6 +80,9 @@ public void Toggle()
(Owner as Controls.ToggleMenuFlyoutItem).Invoke();
}

/// <summary>
/// Gets the toggle state of the control.
/// </summary>
public ToggleState ToggleState
{
get
Expand All @@ -97,11 +107,13 @@ internal void RaisePropertyChangedEvent(object oldValue, object newValue)

if (oldToggleState != newToggleState)
{
//base.RaisePropertyChangedEvent(AutomationProperties.ToggleStateProperty, oldToggleState, newToggleState);
RaisePropertyChangedEvent(TogglePatternIdentifiers.ToggleStateProperty, oldToggleState, newToggleState);
}
}

// Convert the Boolean in Inspectable to the ToggleState Enum, if the Inspectable is NULL that corresponds to Indeterminate state.
/// <summary>
/// Convert the Boolean in Inspectable to the ToggleState Enum, if the Inspectable is NULL that corresponds to Indeterminate state.
/// </summary>
private ToggleState ConvertToToggleState(object value)
{
if (value is bool v)
Expand Down

0 comments on commit ef52c88

Please sign in to comment.