Skip to content

Commit

Permalink
Adding a few automation peers for #647
Browse files Browse the repository at this point in the history
  • Loading branch information
batzen committed Nov 12, 2018
1 parent d8144c8 commit 26033f3
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 0 deletions.
61 changes: 61 additions & 0 deletions Fluent.Ribbon/Automation/Peers/DropDownButtonAutomationPeer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
namespace Fluent.Automation.Peers
{
using System.Runtime.CompilerServices;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using JetBrains.Annotations;

/// <summary>
/// Automation peer for <see cref="DropDownButton"/>.
/// </summary>
public class DropDownButtonAutomationPeer : HeaderedControlAutomationPeer, IToggleProvider
{
/// <summary>
/// Creates a new instance.
/// </summary>
public DropDownButtonAutomationPeer([NotNull] DropDownButton owner)
: base(owner)
{
}

/// <inheritdoc />
protected override string GetClassNameCore()
{
return "DropDownButton";
}

/// <inheritdoc />
public override object GetPattern(PatternInterface patternInterface)
{
if (patternInterface == PatternInterface.Toggle)
{
return this;
}

return base.GetPattern(patternInterface);
}

/// <inheritdoc />
public void Toggle()
{
((DropDownButton)this.Owner).IsDropDownOpen = !((DropDownButton)this.Owner).IsDropDownOpen;
}

/// <inheritdoc />
public ToggleState ToggleState => ConvertToToggleState(((DropDownButton)this.Owner).IsDropDownOpen);

private static ToggleState ConvertToToggleState(bool value)
{
return value
? ToggleState.On
: ToggleState.Off;
}

[MethodImpl(MethodImplOptions.NoInlining)]
internal virtual void RaiseToggleStatePropertyChangedEvent(bool oldValue, bool newValue)
{
this.RaisePropertyChangedEvent(TogglePatternIdentifiers.ToggleStateProperty, ConvertToToggleState(oldValue), ConvertToToggleState(newValue));
}
}
}
37 changes: 37 additions & 0 deletions Fluent.Ribbon/Automation/Peers/HeaderedControlAutomationPeer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace Fluent.Automation.Peers
{
using System.Windows;
using System.Windows.Automation.Peers;
using JetBrains.Annotations;

/// <summary>
/// Base automation peer for <see cref="IHeaderedControl"/>.
/// </summary>
public abstract class HeaderedControlAutomationPeer : FrameworkElementAutomationPeer
{
/// <summary>
/// Creates a new instance.
/// </summary>
protected HeaderedControlAutomationPeer([NotNull] FrameworkElement owner)
: base(owner)
{
}

/// <inheritdoc />
protected override string GetNameCore()
{
var text = base.GetNameCore();
var owner = (IHeaderedControl)this.Owner;

if (string.IsNullOrEmpty(text))
{
if (owner.Header is string headerString)
{
return headerString;
}
}

return text;
}
}
}
19 changes: 19 additions & 0 deletions Fluent.Ribbon/Automation/Peers/InRibbonGalleryAutomationPeer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Fluent.Automation.Peers
{
using JetBrains.Annotations;

/// <summary>
/// Automation peer for <see cref="InRibbonGallery" />
/// </summary>
// todo: add full automation for expansion, listing items (?) etc.
public class InRibbonGalleryAutomationPeer : HeaderedControlAutomationPeer
{
/// <summary>
/// Creates a new instance.
/// </summary>
public InRibbonGalleryAutomationPeer([NotNull] InRibbonGallery owner)
: base(owner)
{
}
}
}
28 changes: 28 additions & 0 deletions Fluent.Ribbon/Automation/Peers/ScreenTipAutomationPeer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Fluent.Automation.Peers
{
using System.Windows;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using JetBrains.Annotations;

/// <summary>
/// Automation peer for <see cref="ScreenTip" />.
/// </summary>
public class ScreenTipAutomationPeer : ToolTipAutomationPeer
{
/// <summary>
/// Creates a new instance.
/// </summary>
public ScreenTipAutomationPeer([NotNull] ScreenTip owner)
: base(owner)
{
}

/// <inheritdoc />
protected override bool IsContentElementCore()
{
return true;
}
}
}
58 changes: 58 additions & 0 deletions Fluent.Ribbon/Automation/Peers/SplitButtonAutomationPeer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
namespace Fluent.Automation.Peers
{
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Threading;
using Fluent.Extensions;
using JetBrains.Annotations;

/// <summary>
/// Automation peer for <see cref="SplitButton"/>.
/// </summary>
public class SplitButtonAutomationPeer : DropDownButtonAutomationPeer, IInvokeProvider
{
/// <summary>
/// Creates a new instance.
/// </summary>
public SplitButtonAutomationPeer([NotNull] SplitButton owner)
: base(owner)
{
}

/// <inheritdoc />
protected override string GetClassNameCore()
{
return "SplitButton";
}

/// <inheritdoc />
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.SplitButton;
}

/// <inheritdoc />
public override object GetPattern(PatternInterface patternInterface)
{
if (patternInterface == PatternInterface.Invoke)
{
//return ((SplitButton)this.Owner).button;
return this;
}

return base.GetPattern(patternInterface);
}

/// <inheritdoc />
public void Invoke()
{
if (this.IsEnabled() == false)
{
throw new ElementNotEnabledException();
}

this.RunInDispatcherAsync(() => ((SplitButton)this.Owner).AutomationButtonClick(), DispatcherPriority.Input);
}
}
}
11 changes: 11 additions & 0 deletions Fluent.Ribbon/Controls/DropDownButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ namespace Fluent
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Threading;
using Fluent.Automation.Peers;
using Fluent.Extensions;
using Fluent.Internal;
using Fluent.Internal.KnownBoxes;
Expand Down Expand Up @@ -653,8 +655,11 @@ private static void OnIsDropDownOpenChanged(DependencyObject d, DependencyProper
{
var control = (DropDownButton)d;

var oldValue = (bool)e.OldValue;
var newValue = (bool)e.NewValue;

(UIElementAutomationPeer.FromElement(control) as DropDownButtonAutomationPeer)?.RaiseToggleStatePropertyChangedEvent(oldValue, newValue);

control.OnIsDropDownOpenChanged(newValue);
}

Expand Down Expand Up @@ -847,6 +852,12 @@ protected override IEnumerator LogicalChildren
}
}

/// <inheritdoc />
protected override AutomationPeer OnCreateAutomationPeer()
{
return new DropDownButtonAutomationPeer(this);
}

#region MenuItem workarounds

private void OnSubmenuOpened(object sender, RoutedEventArgs e)
Expand Down
8 changes: 8 additions & 0 deletions Fluent.Ribbon/Controls/InRibbonGallery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Fluent
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
Expand All @@ -15,6 +16,7 @@ namespace Fluent
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Fluent.Automation.Peers;
using Fluent.Extensibility;
using Fluent.Extensions;
using Fluent.Internal;
Expand Down Expand Up @@ -1585,5 +1587,11 @@ void ILogicalChildSupport.RemoveLogicalChild(object child)
{
this.RemoveLogicalChild(child);
}

/// <inheritdoc />
protected override AutomationPeer OnCreateAutomationPeer()
{
return new InRibbonGalleryAutomationPeer(this);
}
}
}
8 changes: 8 additions & 0 deletions Fluent.Ribbon/Controls/ScreenTip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ namespace Fluent
{
using System;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using Fluent.Automation.Peers;
using Fluent.Internal.KnownBoxes;

/// <summary>
Expand Down Expand Up @@ -393,6 +395,12 @@ private void OnFocusedElementPreviewKeyDown(object sender, KeyEventArgs e)
}

#endregion

/// <inheritdoc />
protected override AutomationPeer OnCreateAutomationPeer()
{
return new ScreenTipAutomationPeer(this);
}
}

/// <summary>
Expand Down
13 changes: 13 additions & 0 deletions Fluent.Ribbon/Controls/SplitButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ namespace Fluent
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using Fluent.Automation.Peers;
using Fluent.Extensibility;
using Fluent.Internal.KnownBoxes;

Expand Down Expand Up @@ -466,6 +468,12 @@ protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
}
}

/// <inheritdoc />
protected override AutomationPeer OnCreateAutomationPeer()
{
return new SplitButtonAutomationPeer(this);
}

#region Overrides of DropDownButton

/// <inheritdoc />
Expand All @@ -481,6 +489,11 @@ protected override void OnKeyDown(KeyEventArgs e)

#endregion

internal void AutomationButtonClick()
{
this.button.InvokeClick();
}

private void OnButtonClick(object sender, RoutedEventArgs e)
{
e.Handled = true;
Expand Down

0 comments on commit 26033f3

Please sign in to comment.