Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added automation peer class for carousel and item #3507

Merged
merged 27 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0ccbedc
Added automation peer class for carousel and item
jamesmcroft Sep 27, 2020
36235df
Moved carousel automation peers into their own namespace
jamesmcroft Sep 29, 2020
2b9bd47
Merge branch 'master' into jamesmcroft/3506-carousel-automation
jamesmcroft Sep 29, 2020
84d9e00
Merge branch 'master' into jamesmcroft/3506-carousel-automation
Rosuavio Oct 9, 2020
d0c6b6f
Merge branch 'master' into jamesmcroft/3506-carousel-automation
Rosuavio Nov 3, 2020
7e14dc2
Merge branch 'master' into jamesmcroft/3506-carousel-automation
Rosuavio Nov 5, 2020
870b743
Merge branch 'master' into jamesmcroft/3506-carousel-automation
michael-hawker Nov 10, 2020
a706ca6
Improved implementation for Carousel automation peer
jamesmcroft Nov 11, 2020
c9825f1
Merge branch 'jamesmcroft/3506-carousel-automation' of https://github…
jamesmcroft Nov 11, 2020
662e501
Merge branch 'master' into jamesmcroft/3506-carousel-automation
Kyaa-dost Nov 13, 2020
411e39d
Added test for Carousel automation peer.
jamesmcroft Nov 18, 2020
b959fbf
Updated carousel automation peer to improve returned name, and select…
jamesmcroft Nov 22, 2020
631f6b2
Updated carousel item automation peer to remove ability to remove sel…
jamesmcroft Nov 22, 2020
ba3c81f
Fixed TextBlock to text property in automation peer
jamesmcroft Nov 22, 2020
f42f236
Fixed carousel automation tests
jamesmcroft Nov 22, 2020
41491ee
Altered GetNameCore to default to return the base implementation when…
jamesmcroft Dec 7, 2020
951774d
Merge branch 'master' into jamesmcroft/3506-carousel-automation
jamesmcroft Dec 7, 2020
8cc836f
Merge branch 'master' into jamesmcroft/3506-carousel-automation
michael-hawker Dec 15, 2020
7c8c126
Merge branch 'master' into jamesmcroft/3506-carousel-automation
jamesmcroft Jan 17, 2021
c544bb0
Merge branch 'master' into jamesmcroft/3506-carousel-automation
jamesmcroft Jan 21, 2021
3b2d965
Merge branch 'master' into jamesmcroft/3506-carousel-automation
jamesmcroft Feb 16, 2021
4dbba32
Updated Carousel tests to include new Visual UI test implementation.
jamesmcroft Feb 18, 2021
ddea9ce
Updated carousel test to not use parsed XAML string
jamesmcroft Feb 18, 2021
79ea866
Added change to rd.xml and cleaned up carousel test
jamesmcroft Feb 18, 2021
32b015d
Removed GetAutomationIdCore implementation from CarouselItem and Blad…
jamesmcroft Feb 24, 2021
df21fca
Merge branch 'master' into jamesmcroft/3506-carousel-automation
jamesmcroft Feb 24, 2021
7547a4f
Removed using directives which no longer exist
jamesmcroft Feb 24, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Microsoft.Toolkit.Uwp.SampleApp/Data/PhotoDataItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ public class PhotoDataItem
public string Category { get; set; }

public string Thumbnail { get; set; }

public override string ToString()
{
return Title;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,6 @@ protected override string GetNameCore()
return string.Empty;
}

/// <summary>
/// Called by GetAutomationId that gets the **AutomationId** of the element that is associated with the automation peer.
/// </summary>
/// <returns>
/// The string that contains the automation ID.
/// </returns>
protected override string GetAutomationIdCore()
{
string automationId = base.GetAutomationIdCore();
if (!string.IsNullOrEmpty(automationId))
{
return automationId;
}

if (this.OwnerBladeItem != null)
{
return this.GetNameCore();
}

return string.Empty;
}

/// <summary>
/// Returns the size of the set where the element that is associated with the automation peer is located.
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions Microsoft.Toolkit.Uwp.UI.Controls.Layout/Carousel/Carousel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Toolkit.Uwp.UI.Automation.Peers;
using Microsoft.Toolkit.Uwp.UI.Extensions;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Automation;
using Windows.UI.Xaml.Automation.Peers;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
Expand Down Expand Up @@ -520,6 +523,17 @@ protected override void PrepareContainerForItemOverride(DependencyObject element
{
carouselItem.IsSelected = true;
}

carouselItem.ParentCarousel = this;
}

/// <summary>
/// Creates AutomationPeer (<see cref="UIElement.OnCreateAutomationPeer"/>)
/// </summary>
/// <returns>An automation peer for this <see cref="Carousel"/>.</returns>
protected override AutomationPeer OnCreateAutomationPeer()
{
return new CarouselAutomationPeer(this);
}

private void OnCarouselItemSelected(object sender, EventArgs e)
Expand All @@ -528,5 +542,11 @@ private void OnCarouselItemSelected(object sender, EventArgs e)

SelectedItem = ItemFromContainer(item);
}

internal void SetSelectedItem(CarouselItem owner)
{
var item = ItemFromContainer(owner);
SelectedItem = item;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using Microsoft.Toolkit.Uwp.UI.Controls;
using Windows.UI.Xaml.Automation;
using Windows.UI.Xaml.Automation.Peers;
using Windows.UI.Xaml.Automation.Provider;
using Windows.UI.Xaml.Controls;

namespace Microsoft.Toolkit.Uwp.UI.Automation.Peers
{
/// <summary>
/// Defines a framework element automation peer for the <see cref="Carousel"/> control.
/// </summary>
public class CarouselAutomationPeer : ItemsControlAutomationPeer, ISelectionProvider
{
/// <summary>
/// Initializes a new instance of the <see cref="CarouselAutomationPeer"/> class.
/// </summary>
/// <param name="owner">
/// The <see cref="Carousel" /> that is associated with this <see cref="T:Windows.UI.Xaml.Automation.Peers.CarouselAutomationPeer" />.
/// </param>
public CarouselAutomationPeer(Carousel owner)
: base(owner)
{
}

/// <summary>Gets a value indicating whether the Microsoft UI Automation provider allows more than one child element to be selected concurrently.</summary>
/// <returns>True if multiple selection is allowed; otherwise, false.</returns>
public bool CanSelectMultiple => false;

/// <summary>Gets a value indicating whether the UI Automation provider requires at least one child element to be selected.</summary>
/// <returns>True if selection is required; otherwise, false.</returns>
public bool IsSelectionRequired => true;

private Carousel OwningCarousel
{
get
{
return Owner as Carousel;
}
}

/// <summary>Retrieves a UI Automation provider for each child element that is selected.</summary>
/// <returns>An array of UI Automation providers.</returns>
public IRawElementProviderSimple[] GetSelection()
{
return OwningCarousel.ContainerFromItem(this.OwningCarousel.SelectedItem) is CarouselItem selectedCarouselItem
? new[] { this.ProviderFromPeer(FromElement(selectedCarouselItem)) }
: new IRawElementProviderSimple[] { };
}

/// <summary>
/// Gets the control type for the element that is associated with the UI Automation peer.
/// </summary>
/// <returns>The control type.</returns>
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.List;
}

/// <summary>
/// Called by GetClassName that gets a human readable name that, in addition to AutomationControlType,
/// differentiates the control represented by this AutomationPeer.
/// </summary>
/// <returns>The string that contains the name.</returns>
protected override string GetClassNameCore()
{
return Owner.GetType().Name;
}

/// <summary>
/// Called by GetName.
/// </summary>
/// <returns>
/// Returns the first of these that is not null or empty:
/// - Value returned by the base implementation
/// - Name of the owning Carousel
/// - Carousel class name
/// </returns>
protected override string GetNameCore()
{
string name = this.OwningCarousel.Name;
if (!string.IsNullOrEmpty(name))
{
return name;
}

name = AutomationProperties.GetName(this.OwningCarousel);
if (!string.IsNullOrEmpty(name))
{
return name;
}

return base.GetNameCore();
}

/// <summary>
/// Gets the control pattern that is associated with the specified Windows.UI.Xaml.Automation.Peers.PatternInterface.
/// </summary>
/// <param name="patternInterface">A value from the Windows.UI.Xaml.Automation.Peers.PatternInterface enumeration.</param>
/// <returns>The object that supports the specified pattern, or null if unsupported.</returns>
protected override object GetPatternCore(PatternInterface patternInterface)
{
switch (patternInterface)
{
case PatternInterface.Selection:
return this;
}

return base.GetPatternCore(patternInterface);
}

/// <summary>
/// Gets the collection of elements that are represented in the UI Automation tree as immediate
/// child elements of the automation peer.
/// </summary>
/// <returns>The children elements.</returns>
protected override IList<AutomationPeer> GetChildrenCore()
{
Carousel owner = OwningCarousel;

ItemCollection items = owner.Items;
if (items.Count <= 0)
{
return null;
}

List<AutomationPeer> peers = new List<AutomationPeer>(items.Count);
for (int i = 0; i < items.Count; i++)
{
if (owner.ContainerFromIndex(i) is CarouselItem element)
{
peers.Add(FromElement(element) ?? CreatePeerForElement(element));
}
}

return peers;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// See the LICENSE file in the project root for more information.

using System;
using Microsoft.Toolkit.Uwp.UI.Automation.Peers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Automation.Peers;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Input;

Expand All @@ -22,6 +23,8 @@ public partial class CarouselItem : SelectorItem
private const string SelectedState = "Selected";
private const string NormalState = "Normal";

private WeakReference<Carousel> parentCarousel;

/// <summary>
/// Initializes a new instance of the <see cref="CarouselItem"/> class.
/// </summary>
Expand All @@ -33,6 +36,16 @@ public CarouselItem()
RegisterPropertyChangedCallback(SelectorItem.IsSelectedProperty, OnIsSelectedChanged);
}

internal Carousel ParentCarousel
{
get
{
this.parentCarousel.TryGetTarget(out var carousel);
return carousel;
}
set => this.parentCarousel = new WeakReference<Carousel>(value);
}

/// <inheritdoc/>
protected override void OnPointerEntered(PointerRoutedEventArgs e)
{
Expand All @@ -57,6 +70,15 @@ protected override void OnPointerPressed(PointerRoutedEventArgs e)
VisualStateManager.GoToState(this, IsSelected ? PressedSelectedState : PressedState, true);
}

/// <summary>
/// Creates AutomationPeer (<see cref="UIElement.OnCreateAutomationPeer"/>)
/// </summary>
/// <returns>An automation peer for this <see cref="CarouselItem"/>.</returns>
protected override AutomationPeer OnCreateAutomationPeer()
{
return new CarouselItemAutomationPeer(this);
}

internal event EventHandler Selected;

private void OnIsSelectedChanged(DependencyObject sender, DependencyProperty dp)
Expand Down
Loading