From 17740b0055264031aff1e0be1260bca56b642be4 Mon Sep 17 00:00:00 2001 From: Bastian Schmidt Date: Sun, 16 Dec 2018 11:50:31 +0100 Subject: [PATCH] Adding automation peer for RibbonGroupBox #647 --- .../Peers/RibbonControlDataAutomationPeer.cs | 52 +++++++++ .../Peers/RibbonGroupBoxAutomationPeer.cs | 102 ++++++++++++++++++ Fluent.Ribbon/Controls/RibbonGroupBox.cs | 10 +- 3 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 Fluent.Ribbon/Automation/Peers/RibbonControlDataAutomationPeer.cs create mode 100644 Fluent.Ribbon/Automation/Peers/RibbonGroupBoxAutomationPeer.cs diff --git a/Fluent.Ribbon/Automation/Peers/RibbonControlDataAutomationPeer.cs b/Fluent.Ribbon/Automation/Peers/RibbonControlDataAutomationPeer.cs new file mode 100644 index 000000000..efbb268a6 --- /dev/null +++ b/Fluent.Ribbon/Automation/Peers/RibbonControlDataAutomationPeer.cs @@ -0,0 +1,52 @@ +namespace Fluent.Automation.Peers +{ + using System.Windows.Automation.Peers; + using Fluent.Extensions; + + /// + /// Automation peer for ribbon control items. + /// + public class RibbonControlDataAutomationPeer : ItemAutomationPeer + { + /// + /// Creates a new instance. + /// + public RibbonControlDataAutomationPeer(object item, ItemsControlAutomationPeer itemsControlPeer) + : base(item, itemsControlPeer) + { + } + + /// + protected override AutomationControlType GetAutomationControlTypeCore() + { + return AutomationControlType.ListItem; + } + + /// + protected override string GetClassNameCore() + { + var wrapperPeer = this.GetWrapperPeer(); + + if (wrapperPeer != null) + { + return wrapperPeer.GetClassName(); + } + + return string.Empty; + } + + /// + public override object GetPattern(PatternInterface patternInterface) + { + object result = null; + var wrapperPeer = this.GetWrapperPeer(); + + if (wrapperPeer != null) + { + result = wrapperPeer.GetPattern(patternInterface); + } + + return result; + } + } +} \ No newline at end of file diff --git a/Fluent.Ribbon/Automation/Peers/RibbonGroupBoxAutomationPeer.cs b/Fluent.Ribbon/Automation/Peers/RibbonGroupBoxAutomationPeer.cs new file mode 100644 index 000000000..361e2f321 --- /dev/null +++ b/Fluent.Ribbon/Automation/Peers/RibbonGroupBoxAutomationPeer.cs @@ -0,0 +1,102 @@ +namespace Fluent.Automation.Peers +{ + using System.Collections.Generic; + using System.Runtime.CompilerServices; + using System.Windows.Automation; + using System.Windows.Automation.Peers; + using JetBrains.Annotations; + + /// + /// Automation peer for . + /// + public class RibbonGroupBoxAutomationPeer : ItemsControlAutomationPeer + { + private RibbonGroupHeaderAutomationPeer headerPeer; + + private RibbonGroupBox OwningGroup => (RibbonGroupBox)this.Owner; + + private RibbonGroupHeaderAutomationPeer HeaderPeer + { + get + { + if (this.headerPeer == null + || !this.headerPeer.Owner.IsDescendantOf(this.OwningGroup)) + { + if (this.OwningGroup.State == RibbonGroupBoxState.Collapsed) + { + if (this.OwningGroup.DropDownPopup != null) + { + this.headerPeer = new RibbonGroupHeaderAutomationPeer(this.OwningGroup.DropDownPopup); + } + } + else if (this.OwningGroup.Header != null) + { + this.headerPeer = new RibbonGroupHeaderAutomationPeer(this.OwningGroup); + } + } + + return this.headerPeer; + } + } + + /// + /// Creates a new instance. + /// + public RibbonGroupBoxAutomationPeer([NotNull] RibbonGroupBox owner) + : base(owner) + { + } + + /// + protected override List GetChildrenCore() + { + var list = base.GetChildrenCore(); + + if (this.HeaderPeer != null) + { + if (list == null) + { + list = new List(1); + } + + list.Add(this.HeaderPeer); + } + + return list; + } + + /// + protected override string GetClassNameCore() + { + return this.Owner.GetType().Name; + } + + /// + public override object GetPattern(PatternInterface patternInterface) + { + if (patternInterface == PatternInterface.Scroll) + { + return null; + } + + return base.GetPattern(patternInterface); + } + + /// + protected override void SetFocusCore() + { + } + + /// + protected override ItemAutomationPeer CreateItemAutomationPeer(object item) + { + return new RibbonControlDataAutomationPeer(item, this); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + internal void RaiseExpandCollapseAutomationEvent(bool oldValue, bool newValue) + { + this.EventsSource?.RaisePropertyChangedEvent(ExpandCollapsePatternIdentifiers.ExpandCollapseStateProperty, oldValue ? ExpandCollapseState.Expanded : ExpandCollapseState.Collapsed, newValue ? ExpandCollapseState.Expanded : ExpandCollapseState.Collapsed); + } + } +} \ No newline at end of file diff --git a/Fluent.Ribbon/Controls/RibbonGroupBox.cs b/Fluent.Ribbon/Controls/RibbonGroupBox.cs index 1e7ff55f1..63632de43 100644 --- a/Fluent.Ribbon/Controls/RibbonGroupBox.cs +++ b/Fluent.Ribbon/Controls/RibbonGroupBox.cs @@ -7,6 +7,7 @@ namespace Fluent using System.ComponentModel; using System.Linq; using System.Windows; + using System.Windows.Automation.Peers; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Data; @@ -971,9 +972,11 @@ private void OnDialogLauncherButtonClick(object sender, RoutedEventArgs e) /// The event data private static void OnIsDropDownOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { - var ribbon = (RibbonGroupBox)d; + var groupBox = (RibbonGroupBox)d; - ribbon.OnIsDropDownOpenChanged(); + groupBox.OnIsDropDownOpenChanged(); + + (UIElementAutomationPeer.FromElement(groupBox) as Fluent.Automation.Peers.RibbonGroupBoxAutomationPeer)?.RaiseExpandCollapseAutomationEvent((bool)e.OldValue, (bool)e.NewValue); } private void OnIsDropDownOpenChanged() @@ -1154,5 +1157,8 @@ void ILogicalChildSupport.RemoveLogicalChild(object child) { this.RemoveLogicalChild(child); } + + /// + protected override AutomationPeer OnCreateAutomationPeer() => new Fluent.Automation.Peers.RibbonGroupBoxAutomationPeer(this); } } \ No newline at end of file