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

Announcing widget errors and warnings #3023

Merged
merged 9 commits into from
Jun 6, 2024
Merged
Changes from 4 commits
Commits
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
63 changes: 63 additions & 0 deletions tools/Dashboard/DevHome.Dashboard/ViewModels/WidgetViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// Licensed under the MIT License.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using AdaptiveCards.ObjectModel.WinUI3;
using AdaptiveCards.Rendering.WinUI3;
Expand Down Expand Up @@ -42,6 +46,7 @@ public partial class WidgetViewModel : ObservableObject

private readonly DispatcherQueue _dispatcherQueue;
private readonly WidgetAdaptiveCardRenderingService _renderingService;
private readonly IScreenReaderService _screenReaderService;

private RenderedAdaptiveCard _renderedCard;

Expand Down Expand Up @@ -98,9 +103,11 @@ public WidgetViewModel(
WidgetSize widgetSize,
ComSafeWidgetDefinition widgetDefinition,
WidgetAdaptiveCardRenderingService adaptiveCardRenderingService,
IScreenReaderService screenReaderService,
DispatcherQueue dispatcherQueue)
{
_renderingService = adaptiveCardRenderingService;
_screenReaderService = screenReaderService;
_dispatcherQueue = dispatcherQueue;

Widget = widget;
Expand Down Expand Up @@ -183,6 +190,7 @@ await Task.Run(async () =>
{
_renderedCard.Action += HandleAdaptiveAction;
WidgetFrameworkElement = _renderedCard.FrameworkElement;
AnnounceWarnings(card.AdaptiveCard);
guimafelipe marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
Expand Down Expand Up @@ -274,6 +282,8 @@ private Grid GetErrorCard(string error, string subError = null)
};
sp.Children.Add(errorText);

var errorTextToAnnounce = errorText.Text;

if (subError is not null)
{
var subErrorText = new TextBlock
Expand All @@ -285,8 +295,11 @@ private Grid GetErrorCard(string error, string subError = null)
};

sp.Children.Add(subErrorText);
errorTextToAnnounce += $" {subErrorText.Text}";
}

_screenReaderService.Announce(errorTextToAnnounce);

grid.Children.Add(sp);
return grid;
}
Expand Down Expand Up @@ -339,4 +352,54 @@ public void UnsubscribeFromWidgetUpdates()
{
Widget.WidgetUpdated -= HandleWidgetUpdated;
}

private void AnnounceWarnings(AdaptiveCard card)
{
// We are treating any text inside a container with the "Warning" style
// as an actual warning to be announced.
// For now, the only types of containers widgets use are Containers and Columns. In the future,
// we may add Caroussels, Tables and Facts to this list.
var containerTypes = new Dictionary<Type, string>
{
{ typeof(AdaptiveContainer), "get_Items" },
{ typeof(AdaptiveColumn), "get_Items" },
{ typeof(AdaptiveColumnSet), "get_Columns" },
};

foreach (var element in card.Body)
{
SearchForWarning(element, containerTypes, false);
}
}

private void SearchForWarning(IAdaptiveCardElement element, Dictionary<Type, string> containerTypes, bool isInsideWarningContainer)
{
// We are only interested in plain texts. Buttons, Actions, Images
// and textboxes are all ignored. Including ActionSets and ImageSets.
if (element is AdaptiveTextBlock textBlock)
{
if (isInsideWarningContainer)
{
_screenReaderService.Announce(textBlock.Text);
guimafelipe marked this conversation as resolved.
Show resolved Hide resolved
}

return;
}

if (element is IAdaptiveContainerBase containerElement)
guimafelipe marked this conversation as resolved.
Show resolved Hide resolved
{
foreach (var containerType in containerTypes)
guimafelipe marked this conversation as resolved.
Show resolved Hide resolved
{
if (containerElement.GetType() == containerType.Key)
{
MethodInfo itemsMethod = containerType.Key.GetMethod(containerType.Value, BindingFlags.Public | BindingFlags.Instance);

foreach (var subelement in itemsMethod.Invoke(containerElement, null) as IEnumerable)
{
SearchForWarning((IAdaptiveCardElement)subelement, containerTypes, isInsideWarningContainer || (containerElement.Style == ContainerStyle.Warning));
}
}
}
}
}
}
Loading