Skip to content

Commit

Permalink
Merge branch 'master' into fix-xmalisland-test
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-hawker authored Mar 1, 2021
2 parents f4f992b + 4a3f9ee commit f176351
Show file tree
Hide file tree
Showing 13 changed files with 500 additions and 28 deletions.
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
19 changes: 19 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,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Toolkit.Uwp.UI.Automation.Peers;
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 +522,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 +541,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

0 comments on commit f176351

Please sign in to comment.