Skip to content

Commit

Permalink
Tupha/select action show card dotnet bugs (#1571)
Browse files Browse the repository at this point in the history
* ensured only 1 level of ShowCards is supported

* refactored and cleaned up

* cleaned up code

* removed redundant white spaces
  • Loading branch information
tuanh118 authored Aug 8, 2018
1 parent bb19161 commit 180d4a4
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,7 @@ public static void AddActions(Grid uiContainer, IList<AdaptiveAction> actions, A

bool isInline = (actionsConfig.ShowCard.ActionMode == ShowCardActionMode.Inline);

if (isInline && actionsToProcess.Any(a => a is AdaptiveShowCardAction))
{
uiContainer.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
}

int iPos = 0;
List<FrameworkElement> actionBarCards = new List<FrameworkElement>();

// See if all actions have icons, otherwise force the icon placement to the left
var oldConfigIconPlacement = actionsConfig.IconPlacement;
Expand Down Expand Up @@ -94,7 +88,8 @@ public static void AddActions(Grid uiContainer, IList<AdaptiveAction> actions, A

if (action is AdaptiveShowCardAction showCardAction)
{
if (isInline)
// Only support 1 level of showCard
if (isInline && context.CardDepth == 1)
{
Grid uiShowCardContainer = new Grid();
uiShowCardContainer.Style = context.GetStyle("Adaptive.Actions.ShowCard");
Expand All @@ -113,18 +108,8 @@ public static void AddActions(Grid uiContainer, IList<AdaptiveAction> actions, A

uiShowCardContainer.Children.Add(uiShowCardWrapper);

actionBarCards.Add(uiShowCardContainer);
Grid.SetRow(uiShowCardContainer, uiContainer.RowDefinitions.Count - 1);
uiContainer.Children.Add(uiShowCardContainer);

uiAction.Click += (sender, e) =>
{
bool showCard = (uiShowCardContainer.Visibility != Visibility.Visible);
foreach (var actionBarCard in actionBarCards)
actionBarCard.Visibility = Visibility.Collapsed;
if (showCard)
uiShowCardContainer.Visibility = Visibility.Visible;
};
// Add to the list of show cards in context
context.ActionShowCards.Add(new Tuple<FrameworkElement, Button>(uiShowCardContainer, uiAction));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,49 @@ public static FrameworkElement RenderAdaptiveCardWrapper(AdaptiveCard card, Adap

grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });


AdaptiveContainerRenderer.AddContainerElements(grid, card.Body, context);
AdaptiveActionSetRenderer.AddActions(grid, card.Actions, context);

// Only handle Action show cards for the main card
if (context.CardDepth == 1)
{
// Define a new row to contain all the show cards
if (context.ActionShowCards.Count > 0)
{
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
}

foreach (var showCardTuple in context.ActionShowCards)
{
var currentShowCard = showCardTuple.Item1;
var uiButton = showCardTuple.Item2;

Grid.SetRow(currentShowCard, grid.RowDefinitions.Count - 1);
grid.Children.Add(currentShowCard);

// Assign on click function to all button elements
uiButton.Click += (sender, e) =>
{
bool isCardCollapsed = (currentShowCard.Visibility != Visibility.Visible);

// Collapse all the show cards
foreach (var t in context.ActionShowCards)
{
var showCard = t.Item1;
showCard.Visibility = Visibility.Collapsed;
}

// If current card is previously collapsed, show it
if (isCardCollapsed)
currentShowCard.Visibility = Visibility.Visible;
};
}
}

outerGrid.Children.Add(grid);
return outerGrid;
}


/// <summary>
/// Renders an adaptive card.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

Expand Down Expand Up @@ -111,6 +112,10 @@ public SolidColorBrush GetColorBrush(string color)
}
}

// Flag to distinuish the main card and action show cards
public int CardDepth = 0;

public IList<Tuple<FrameworkElement, Button>> ActionShowCards = new List<Tuple<FrameworkElement, Button>>();

public virtual Style GetStyle(string styleName)
{
Expand Down Expand Up @@ -148,7 +153,21 @@ public FrameworkElement Render(AdaptiveTypedElement element)
var renderer = ElementRenderers.Get(element.GetType());
if (renderer != null)
{
return renderer.Invoke(element, this);
// Increment card depth before rendering the inner card
if (element is AdaptiveCard)
{
CardDepth += 1;
}

var rendered = renderer.Invoke(element, this);

// Decrement card depth after inner card is rendered
if (element is AdaptiveCard)
{
CardDepth -= 1;
}

return rendered;
}

Warnings.Add(new AdaptiveWarning(-1, $"No renderer for element '{element.Type}'"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

Expand All @@ -10,16 +11,53 @@ public static FrameworkElement RenderSelectAction(this AdaptiveRenderContext con
{
if (context.Config.SupportsInteractivity)
{
var uiButton = (Button) context.Render(selectAction);
var uiButton = (Button)context.Render(selectAction);
uiButton.HorizontalAlignment = HorizontalAlignment.Left;
uiButton.Background = new SolidColorBrush(Colors.Transparent);
uiButton.BorderThickness = new Thickness(0);
uiButton.Content = uiElement;
uiButton.Style = context.GetStyle("Adaptive.Action.Tap");

// Handle ShowCard
if (selectAction is AdaptiveShowCardAction showCardAction)
{
var actionsConfig = context.Config.Actions;
bool isInline = (actionsConfig.ShowCard.ActionMode == ShowCardActionMode.Inline);
if (isInline && context.CardDepth == 1)
{
FrameworkElement uiShowCardContainer = showCardAction.CreateShowCard(context, actionsConfig);

// Add to the list of show cards in context
context.ActionShowCards.Add(new Tuple<FrameworkElement, Button>(uiShowCardContainer, uiButton));
}
}

return uiButton;
}

return uiElement;
}

public static FrameworkElement CreateShowCard(this AdaptiveShowCardAction showCardAction, AdaptiveRenderContext context, ActionsConfig actionsConfig)
{
Grid uiShowCardContainer = new Grid();
uiShowCardContainer.Style = context.GetStyle("Adaptive.Actions.ShowCard");
uiShowCardContainer.DataContext = showCardAction;
uiShowCardContainer.Margin = new Thickness(0, actionsConfig.ShowCard.InlineTopMargin, 0, 0);
uiShowCardContainer.Visibility = Visibility.Collapsed;

// render the card
var uiShowCardWrapper = (Grid)context.Render(showCardAction.Card);
uiShowCardWrapper.Background = context.GetColorBrush("Transparent");
uiShowCardWrapper.DataContext = showCardAction;

// Remove the card padding
var innerCard = (Grid)uiShowCardWrapper.Children[0];
innerCard.Margin = new Thickness(0);

uiShowCardContainer.Children.Add(uiShowCardWrapper);

return uiShowCardContainer;
}
}
}

0 comments on commit 180d4a4

Please sign in to comment.