Пример #1
0
        public static FrameworkElement Render(AdaptiveFactSet factSet, AdaptiveRenderContext context)
        {
            var uiFactSet = new Grid();

            // grid.Margin = factSet.Theme.FactSetMargins;
            uiFactSet.Style = context.GetStyle("Adaptive.FactSet");

            uiFactSet.ColumnDefinitions.Add(new ColumnDefinition()
            {
                Width = GridLength.Auto
            });
            uiFactSet.ColumnDefinitions.Add(new ColumnDefinition()
            {
                Width = new GridLength(1, GridUnitType.Star)
            });
            int iRow = 0;

            foreach (var fact in factSet.Facts)
            {
                var uiTitle = context.Render(new AdaptiveTextBlock()
                {
                    Size     = context.Config.FactSet.Title.Size,
                    Color    = context.Config.FactSet.Title.Color,
                    IsSubtle = context.Config.FactSet.Title.IsSubtle,
                    Weight   = context.Config.FactSet.Title.Weight,
                    Wrap     = context.Config.FactSet.Title.Wrap,
                    Text     = fact.Title
                });

                uiTitle.Style  = context.GetStyle("Adaptive.Fact.Title");
                uiTitle.Margin = new Thickness(left: 0, top: 0, right: context.Config.FactSet.Spacing, bottom: 0);

                var uiValue = context.Render(new AdaptiveTextBlock()
                {
                    Size     = context.Config.FactSet.Value.Size,
                    Color    = context.Config.FactSet.Value.Color,
                    IsSubtle = context.Config.FactSet.Value.IsSubtle,
                    Weight   = context.Config.FactSet.Value.Weight,
                    Wrap     = context.Config.FactSet.Value.Wrap,
                    Text     = fact.Value
                });

                uiValue.Style = context.GetStyle("Adaptive.Fact.Value");

                uiFactSet.RowDefinitions.Add(new RowDefinition()
                {
                    Height = GridLength.Auto
                });

                Grid.SetColumn(uiTitle, 0);
                Grid.SetRow(uiTitle, iRow);
                uiFactSet.Children.Add(uiTitle);

                Grid.SetColumn(uiValue, 1);
                Grid.SetRow(uiValue, iRow++);
                uiFactSet.Children.Add(uiValue);
            }
            return(uiFactSet);
        }
Пример #2
0
        public static FrameworkElement Render(AdaptiveNumberInput input, AdaptiveRenderContext context)
        {
            if (context.Config.SupportsInteractivity)
            {
                IntegerUpDown numberPicker = new IntegerUpDown();
                // numberPicker.ShowButtonSpinner = true;

                if (!Double.IsNaN(input.Value))
                {
                    numberPicker.Value = Convert.ToInt32(input.Value);
                }

                if (!Double.IsNaN(input.Min))
                {
                    numberPicker.Minimum = Convert.ToInt32(input.Min);
                }

                if (!Double.IsNaN(input.Max))
                {
                    numberPicker.Minimum = Convert.ToInt32(input.Max);
                }

                numberPicker.Watermark   = input.Placeholder;
                numberPicker.Style       = context.GetStyle("Adaptive.Input.Number");
                numberPicker.DataContext = input;
                context.InputBindings.Add(input.Id, () => numberPicker.Value?.ToString());
                return(numberPicker);
            }
            else
            {
                var textBlock = AdaptiveTypedElementConverter.CreateElement <AdaptiveTextBlock>();
                textBlock.Text = XamlUtilities.GetFallbackText(input) ?? input.Placeholder;
                return(context.Render(textBlock));
            }
        }
Пример #3
0
 public static FrameworkElement Render(AdaptiveTimeInput input, AdaptiveRenderContext context)
 {
     if (context.Config.SupportsInteractivity)
     {
         var      timePicker = new TimePicker();
         DateTime value;
         if (IsSupportedTimeFormat(input.Value) && DateTime.TryParse(input.Value, out value))
         {
             timePicker.Value = value;
         }
         TimeSpan minValue;
         if (IsSupportedTimeFormat(input.Min) && TimeSpan.TryParse(input.Min, out minValue))
         {
             timePicker.EndTime = minValue;
         }
         TimeSpan maxValue;
         if (IsSupportedTimeFormat(input.Max) && TimeSpan.TryParse(input.Max, out maxValue))
         {
             timePicker.EndTime = maxValue;
         }
         timePicker.Watermark   = input.Placeholder;
         timePicker.Style       = context.GetStyle("Adaptive.Input.Time");
         timePicker.DataContext = input;
         context.InputBindings.Add(input.Id, () => ToIso8601Time(timePicker.Text));
         return(timePicker);
     }
     else
     {
         var textBlock = AdaptiveTypedElementConverter.CreateElement <AdaptiveTextBlock>();
         textBlock.Text = XamlUtilities.GetFallbackText(input) ?? input.Placeholder;
         return(context.Render(textBlock));
     }
 }
Пример #4
0
        public static FrameworkElement Render(AdaptiveTextInput input, AdaptiveRenderContext context)
        {
            if (context.Config.SupportsInteractivity)
            {
                var textBox = new WatermarkTextBox()
                {
                    Text = input.Value
                };
                if (input.IsMultiline == true)
                {
                    textBox.AcceptsReturn = true;
                    textBox.TextWrapping  = TextWrapping.Wrap;
                    textBox.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
                }
                if (input.MaxLength > 0)
                {
                    textBox.MaxLength = input.MaxLength;
                }

                textBox.Watermark   = input.Placeholder;
                textBox.Style       = context.GetStyle($"Adaptive.Input.Text.{input.Style}");
                textBox.DataContext = input;
                context.InputBindings.Add(input.Id, () => textBox.Text);
                return(textBox);
            }
            else
            {
                var textBlock = AdaptiveTypedElementConverter.CreateElement <AdaptiveTextBlock>();
                textBlock.Text = XamlUtilities.GetFallbackText(input) ?? input.Placeholder;
                return(context.Render(textBlock));
            }
        }
Пример #5
0
 public static FrameworkElement Render(AdaptiveDateInput input, AdaptiveRenderContext context)
 {
     if (context.Config.SupportsInteractivity)
     {
         var datePicker = new DatePicker();
         datePicker.ToolTip = input.Placeholder;
         DateTime value;
         if (DateTime.TryParse(input.Value, out value))
         {
             datePicker.SelectedDate = value;
         }
         DateTime minValue;
         if (DateTime.TryParse(input.Min, out minValue))
         {
             datePicker.DisplayDateStart = minValue;
         }
         DateTime maxValue;
         if (DateTime.TryParse(input.Max, out maxValue))
         {
             datePicker.DisplayDateEnd = maxValue;
         }
         datePicker.Style       = context.GetStyle("Adaptive.Input.Date");
         datePicker.DataContext = input;
         context.InputBindings.Add(input.Id, () => ToIso8601Date(datePicker.Text));
         return(datePicker);
     }
     else
     {
         var textBlock = AdaptiveTypedElementConverter.CreateElement <AdaptiveTextBlock>();
         textBlock.Text = XamlUtilities.GetFallbackText(input) ?? input.Placeholder;
         return(context.Render(textBlock));
     }
 }
        public static void AddContainerElements(Grid uiContainer, IList <AdaptiveElement> elements, AdaptiveRenderContext context)
        {
            foreach (var cardElement in elements)
            {
                // each element has a row
                FrameworkElement uiElement = context.Render(cardElement);
                if (uiElement != null)
                {
                    if (cardElement.Separator && uiContainer.Children.Count > 0)
                    {
                        AddSeperator(context, cardElement, uiContainer);
                    }
                    else if (uiContainer.Children.Count > 0)
                    {
                        var spacing = context.Config.GetSpacing(cardElement.Spacing);
                        uiElement.Margin = new Thickness(0, spacing, 0, 0);
                    }

                    uiContainer.RowDefinitions.Add(new RowDefinition()
                    {
                        Height = GridLength.Auto
                    });
                    Grid.SetRow(uiElement, uiContainer.RowDefinitions.Count - 1);
                    uiContainer.Children.Add(uiElement);
                }
            }
        }
 public static FrameworkElement Render(AdaptiveTimeInput input, AdaptiveRenderContext context)
 {
     if (context.Config.SupportsInteractivity)
     {
         var textBox = new TextBox()
         {
             Text = input.Value
         };
         textBox.SetPlaceholder(input.Placeholder);
         textBox.Style = context.GetStyle($"Adaptive.Input.Text.Time");
         textBox.SetContext(input);
         context.InputBindings.Add(input.Id, () => textBox.Text);
         return(textBox);
     }
     else
     {
         AdaptiveContainer container = AdaptiveTypedElementConverter.CreateElement <AdaptiveContainer>();
         container.Spacing   = input.Spacing;
         container.Separator = input.Separator;
         AdaptiveTextBlock textBlock = AdaptiveTypedElementConverter.CreateElement <AdaptiveTextBlock>();
         textBlock.Text = XamlUtilities.GetFallbackText(input) ?? input.Placeholder;
         container.Items.Add(textBlock);
         if (input.Value != null)
         {
             textBlock       = AdaptiveTypedElementConverter.CreateElement <AdaptiveTextBlock>();
             textBlock.Text  = input.Value;
             textBlock.Color = AdaptiveTextColor.Accent;
             textBlock.Wrap  = true;
             container.Items.Add(textBlock);
         }
         return(context.Render(container));
     }
 }
Пример #8
0
        public static FrameworkElement Render(AdaptiveContainer container, AdaptiveRenderContext context)
        {
            var containerStyle = context.Config.ContainerStyles.Default;
            var uiContainer    = new Grid();

            //uiContainer.Margin = new Thickness(context.Config.Spacing.Padding);
            uiContainer.Style = context.GetStyle("Adaptive.Container");
            AddContainerElements(uiContainer, container.Items, context);

            if (container.SelectAction != null)
            {
                var uiButton = (Button)context.Render(container.SelectAction);
                if (uiButton != null)
                {
                    uiButton.Content = uiContainer;
                    uiButton.Style   = context.GetStyle("Adaptive.Action.Tap");
                    return(uiButton);
                }
            }

            Grid uiOuterContainer = new Grid();

            uiOuterContainer.Background = context.GetColorBrush(containerStyle.BackgroundColor);
            uiOuterContainer.Children.Add(uiContainer);
            Border border = new Border();

            border.Child = uiOuterContainer;
            return(border);
        }
        public static FrameworkElement Render(AdaptiveImageSet imageSet, AdaptiveRenderContext context)
        {
            var uiImageSet = new ListBox();

            uiImageSet.BorderThickness = new Thickness(0);
            uiImageSet.Background      = new SolidColorBrush(Colors.Transparent);
            ScrollViewer.SetHorizontalScrollBarVisibility(uiImageSet, ScrollBarVisibility.Disabled);
            var itemsPanelTemplate = new ItemsPanelTemplate();
            var factory            = new FrameworkElementFactory(typeof(WrapPanel));

            // factory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
            itemsPanelTemplate.VisualTree = factory;
            uiImageSet.ItemsPanel         = itemsPanelTemplate;

            uiImageSet.Style = context.GetStyle("Adaptive.ImageSet");
            foreach (var image in imageSet.Images)
            {
                if (image.Size == AdaptiveImageSize.Auto)
                {
                    if (imageSet.ImageSize != AdaptiveImageSize.Auto)
                    {
                        image.Size = imageSet.ImageSize;
                    }
                    else
                    {
                        image.Size = context.Config.ImageSet.ImageSize;
                    }
                }

                var uiImage = context.Render(image);
                uiImageSet.Add(uiImage);
            }
            return(uiImageSet);
        }
Пример #10
0
        public static FrameworkElement Render(AdaptiveToggleInput input, AdaptiveRenderContext context)
        {
            if (context.Config.SupportsInteractivity)
            {
                var uiToggle = new CheckBox();

                uiToggle.Content = input.Title;
                uiToggle.SetState(input.Value == (input.ValueOn ?? "true"));
                uiToggle.Style = context.GetStyle($"Adaptive.Input.Toggle");
                uiToggle.SetContext(input);
                context.InputBindings.Add(input.Id, () => uiToggle.GetState() == true ? input.ValueOn ?? "true" : input.ValueOff ?? "false");
                return(uiToggle);
            }
            else
            {
                AdaptiveContainer container = AdaptiveTypedElementConverter.CreateElement <AdaptiveContainer>();
                container.Spacing   = input.Spacing;
                container.Separator = input.Separator;

                AdaptiveTextBlock textBlock = AdaptiveTypedElementConverter.CreateElement <AdaptiveTextBlock>();
                textBlock.Text = XamlUtilities.GetFallbackText(input);
                container.Items.Add(textBlock);
                if (input.Value != null)
                {
                    textBlock       = AdaptiveTypedElementConverter.CreateElement <AdaptiveTextBlock>();
                    textBlock.Text  = (input.Value == (input.ValueOn ?? "true")) ? input.ValueOn ?? "selected" : input.ValueOff ?? "not selected";
                    textBlock.Color = AdaptiveTextColor.Accent;
                    textBlock.Wrap  = true;
                    container.Items.Add(textBlock);
                }
                return(context.Render(container));
            }
        }
        public static void AddContainerElements(Grid uiContainer, IList <AdaptiveElement> elements, AdaptiveRenderContext context)
        {
            foreach (var cardElement in elements)
            {
                // each element has a row
                FrameworkElement uiElement = context.Render(cardElement);
                if (uiElement != null)
                {
                    if (cardElement.Separator && uiContainer.Children.Count > 0)
                    {
                        AddSeparator(context, cardElement, uiContainer);
                    }
                    else if (uiContainer.Children.Count > 0)
                    {
                        var       spacing        = context.Config.GetSpacing(cardElement.Spacing);
                        Thickness renderedMargin = uiElement.Margin;
                        uiElement.Margin = new Thickness(renderedMargin.Left,
                                                         renderedMargin.Top + spacing,
                                                         renderedMargin.Right,
                                                         renderedMargin.Bottom);
                    }

                    if (cardElement.Height == AdaptiveHeight.Auto)
                    {
                        uiContainer.RowDefinitions.Add(new RowDefinition()
                        {
                            Height = GridLength.Auto
                        });
                        Grid.SetRow(uiElement, uiContainer.RowDefinitions.Count - 1);
                        uiContainer.Children.Add(uiElement);
                    }
                    else
                    {
                        if (cardElement.Type == "Container")
                        {
                            uiContainer.RowDefinitions.Add(new RowDefinition()
                            {
                                Height = new GridLength(1, GridUnitType.Star)
                            });
                            Grid.SetRow(uiElement, uiContainer.RowDefinitions.Count - 1);
                            uiContainer.Children.Add(uiElement);
                        }
                        else
                        {
                            StackPanel panel = new StackPanel();
                            panel.Children.Add(uiElement);

                            uiContainer.RowDefinitions.Add(new RowDefinition()
                            {
                                Height = new GridLength(1, GridUnitType.Star)
                            });
                            Grid.SetRow(panel, uiContainer.RowDefinitions.Count - 1);
                            uiContainer.Children.Add(panel);
                        }
                    }
                }
            }
        }
Пример #12
0
        public static FrameworkElement RenderSelectAction(this AdaptiveRenderContext context, AdaptiveAction selectAction, FrameworkElement uiElement)
        {
            if (context.Config.SupportsInteractivity)
            {
                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");
                return(uiButton);
            }

            return(uiElement);
        }
Пример #13
0
        public static FrameworkElement Render(AdaptiveTextInput input, AdaptiveRenderContext context)
        {
            if (context.Config.SupportsInteractivity)
            {
                var textBox = new WatermarkTextBox()
                {
                    Text = input.Value
                };
                if (input.IsMultiline == true)
                {
                    textBox.AcceptsReturn = true;
                    textBox.TextWrapping  = TextWrapping.Wrap;
                    textBox.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
                }
                if (input.MaxLength > 0)
                {
                    textBox.MaxLength = input.MaxLength;
                }

                textBox.Watermark   = input.Placeholder;
                textBox.Style       = context.GetStyle($"Adaptive.Input.Text.{input.Style}");
                textBox.DataContext = input;
                context.InputBindings.Add(input.Id, () => textBox.Text);
                if (input.InlineAction != null)
                {
                    if (context.Config.Actions.ShowCard.ActionMode == ShowCardActionMode.Inline &&
                        input.InlineAction.GetType() == typeof(AdaptiveShowCardAction))
                    {
                        context.Warnings.Add(new AdaptiveWarning(-1, "Inline ShowCard not supported for InlineAction"));
                    }
                    else
                    {
                        if (context.Config.SupportsInteractivity && context.ActionHandlers.IsSupported(input.InlineAction.GetType()))
                        {
                            return(AdaptiveTextInputRenderer.RenderInlineAction(input, context, textBox));
                        }
                    }
                }
                return(textBox);
            }
            else
            {
                var textBlock = AdaptiveTypedElementConverter.CreateElement <AdaptiveTextBlock>();
                textBlock.Text = XamlUtilities.GetFallbackText(input) ?? input.Placeholder;
                return(context.Render(textBlock));
            }
        }
Пример #14
0
        public static FrameworkElement Render(AdaptiveTextInput input, AdaptiveRenderContext context)
        {
            if (context.Config.SupportsInteractivity)
            {
                var textBox = new TextBox()
                {
                    Text = input.Value
                };
                if (input.IsMultiline == true)
                {
                    textBox.AcceptsReturn = true;

                    textBox.TextWrapping = TextWrapping.Wrap;
                    textBox.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
                }

                if (input.MaxLength > 0)
                {
                    textBox.MaxLength = input.MaxLength;
                }

                textBox.SetPlaceholder(input.Placeholder);
                textBox.Style = context.GetStyle($"Adaptive.Input.Text.{input.Style}");
                textBox.SetContext(input);
                context.InputBindings.Add(input.Id, () => textBox.Text);
                return(textBox);
            }
            else
            {
                AdaptiveContainer container = AdaptiveTypedElementConverter.CreateElement <AdaptiveContainer>();
                container.Spacing   = input.Spacing;
                container.Separator = input.Separator;
                AdaptiveTextBlock textBlock = AdaptiveTypedElementConverter.CreateElement <AdaptiveTextBlock>();
                textBlock.Text = XamlUtilities.GetFallbackText(input) ?? input.Placeholder;
                container.Items.Add(textBlock);
                if (input.Value != null)
                {
                    textBlock       = AdaptiveTypedElementConverter.CreateElement <AdaptiveTextBlock>();
                    textBlock.Text  = input.Value;
                    textBlock.Color = AdaptiveTextColor.Accent;
                    textBlock.Wrap  = true;
                    container.Items.Add(textBlock);
                }
                return(context.Render(container));
            }
        }
Пример #15
0
        public static FrameworkElement RenderSelectAction(this AdaptiveRenderContext context, AdaptiveAction selectAction, FrameworkElement uiElement)
        {
            if (context.Config.SupportsInteractivity)
            {
                context.IsRenderingSelectAction = true;
                var uiButton = (Button)context.Render(selectAction);
                context.IsRenderingSelectAction = false;

                // Stretch both the button and button's content to avoid empty spaces
                uiButton.HorizontalAlignment        = HorizontalAlignment.Stretch;
                uiButton.HorizontalContentAlignment = HorizontalAlignment.Stretch;

                // Adopt the child element's background to parent and set the child's background
                // to be transparent in order for button's on mouse hover color to work properly
                if (uiElement is Panel p)
                {
                    uiButton.Background = p.Background;
                    p.Background        = new SolidColorBrush(Colors.Transparent);
                }
                else
                {
                    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);
        }
Пример #16
0
        public static FrameworkElement Render(AdaptiveImage image, AdaptiveRenderContext context)
        {
            var uiImage = new Image();

            uiImage.SetSource(image.Url, context);
            uiImage.SetHorizontalAlignment(image.HorizontalAlignment);


            string style = $"Adaptive.{image.Type}";

            if (image.Style == AdaptiveImageStyle.Person)
            {
                style += $".{image.Style}";

                var mask = new RadialGradientBrush()
                {
                    GradientOrigin = new Point(0.5, 0.5),
                    Center         = new Point(0.5, 0.5),
                    RadiusX        = 0.5,
                    RadiusY        = 0.5,
                    GradientStops  = new GradientStopCollection()
                };
                mask.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#ffffffff"), .9));
                mask.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#00ffffff"), 1.0));
                uiImage.OpacityMask = mask;
            }
            uiImage.Style = context.GetStyle(style);
            uiImage.SetImageProperties(image, context);

            if (image.SelectAction != null)
            {
                var uiButton = (Button)context.Render(image.SelectAction);
                if (uiButton != null)
                {
                    uiButton.Content = uiImage;
                    uiButton.Style   = context.GetStyle("Adaptive.Action.Tap");
                    return(uiButton);
                }
            }
            return(uiImage);
        }
        public static FrameworkElement Render(AdaptiveColumn adaptiveColumn, AdaptiveRenderContext context)
        {
            var uiContainer = new Grid();

            uiContainer.Style = context.GetStyle("Adaptive.Column");

            AdaptiveContainerRenderer.AddContainerElements(uiContainer, adaptiveColumn.Items, context);

            if (adaptiveColumn.SelectAction != null)
            {
                var uiButton = (Button)context.Render(adaptiveColumn.SelectAction);
                if (uiButton != null)
                {
                    uiButton.Content = uiContainer;
                    uiButton.Style   = context.GetStyle("Adaptive.Action.Tap");
                    return(uiButton);
                }
            }

            return(uiContainer);
        }
Пример #18
0
        public static FrameworkElement Render(AdaptiveCustomElement customElement, AdaptiveRenderContext context)
        {
            try
            {
                if (customElement.ResolvedElement == null)
                {
                    context.Warnings.Add(new AdaptiveWarning(1, "Custom element resolution failed for " + customElement.Type));
                    return(null);
                }

                return(context.Render(customElement.ResolvedElement));
            }
            catch (Exception ex)
            {
                return(new TextBlock()
                {
                    Text = ex.ToString(),
                    TextWrapping = TextWrapping.Wrap,
                    FontSize = 10
                });
            }
        }
Пример #19
0
        public static FrameworkElement RenderSelectAction(this AdaptiveRenderContext context, AdaptiveAction selectAction, FrameworkElement uiElement)
        {
            if (context.Config.SupportsInteractivity)
            {
                // SelectAction doesn't allow showCard actions
                if (selectAction is AdaptiveShowCardAction)
                {
                    context.Warnings.Add(new AdaptiveWarning(-1, "Inline ShowCard not supported for SelectAction"));
                    return(uiElement);
                }

                context.IsRenderingSelectAction = true;
                var uiButton = (Button)context.Render(selectAction);
                context.IsRenderingSelectAction = false;

                // Stretch both the button and button's content to avoid empty spaces
                uiButton.HorizontalAlignment        = HorizontalAlignment.Stretch;
                uiButton.HorizontalContentAlignment = HorizontalAlignment.Stretch;

                // Adopt the child element's background to parent and set the child's background
                // to be transparent in order for button's on mouse hover color to work properly
                if (uiElement is Panel p)
                {
                    uiButton.Background = p.Background;
                    p.Background        = new SolidColorBrush(Colors.Transparent);
                }
                else
                {
                    uiButton.Background = new SolidColorBrush(Colors.Transparent);
                }
                uiButton.BorderThickness = new Thickness(0);
                uiButton.Content         = uiElement;
                uiButton.Style           = context.GetStyle("Adaptive.Action.Tap");

                return(uiButton);
            }

            return(uiElement);
        }
Пример #20
0
        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);
        }
        public static FrameworkElement Render(AdaptiveColumnSet columnSet, AdaptiveRenderContext context)
        {
            var uiColumnSet = new Grid();

            uiColumnSet.Style = context.GetStyle($"Adaptive.{columnSet.Type}");

            // Keep track of ContainerStyle.ForegroundColors before Container is rendered
            var parentRenderArgs = context.RenderArgs;
            // This is the renderArgs that will be the base for all the columns renderArgs
            var childrenRenderArgs = new AdaptiveRenderArgs(parentRenderArgs);

            Border border = new Border();

            border.Child = uiColumnSet;

            bool inheritsStyleFromParent = !columnSet.Style.HasValue;
            bool hasPadding = false;

            if (!inheritsStyleFromParent)
            {
                hasPadding = AdaptiveContainerRenderer.ApplyPadding(border, uiColumnSet, columnSet, parentRenderArgs, context);

                // Apply background color
                var columnSetStyle = context.Config.ContainerStyles.GetContainerStyleConfig(columnSet.Style);

                border.Background = context.GetColorBrush(columnSetStyle.BackgroundColor);
                childrenRenderArgs.ForegroundColors = columnSetStyle.ForegroundColors;
            }

            childrenRenderArgs.ParentStyle = (inheritsStyleFromParent) ? parentRenderArgs.ParentStyle : columnSet.Style.Value;

            for (int i = 0; i < columnSet.Columns.Count; ++i)
            {
                AdaptiveColumn column = columnSet.Columns[i];

                var childRenderArgs = new AdaptiveRenderArgs(childrenRenderArgs);
                // Reset up and down bleed for columns as that behaviour shouldn't be changed
                childRenderArgs.BleedDirection |= (BleedDirection.BleedUp | BleedDirection.BleedDown);

                if (i != 0)
                {
                    // Only the first column can bleed to the left
                    childRenderArgs.BleedDirection &= ~BleedDirection.BleedLeft;
                }

                if (i != columnSet.Columns.Count - 1)
                {
                    // Only the last column can bleed to the right
                    childRenderArgs.BleedDirection &= ~BleedDirection.BleedRight;
                }

                context.RenderArgs = childRenderArgs;

                FrameworkElement uiContainer = context.Render(column);

                // If the column couldn't be rendered and the fallback is 'drop'
                if (uiContainer != null)
                {
                    TagContent tag = null;

                    // Add vertical Separator
                    if (uiColumnSet.ColumnDefinitions.Count > 0 && (column.Separator || column.Spacing != AdaptiveSpacing.None))
                    {
                        var uiSep = new Grid();
                        uiSep.Style = context.GetStyle($"Adaptive.VerticalSeparator");

                        uiSep.VerticalAlignment = VerticalAlignment.Stretch;

                        int spacing = context.Config.GetSpacing(column.Spacing);
                        uiSep.Margin = new Thickness(spacing / 2.0, 0, spacing / 2.0, 0);

                        uiSep.Width = context.Config.Separator.LineThickness;
                        if (column.Separator && context.Config.Separator.LineColor != null)
                        {
                            uiSep.Background = context.GetColorBrush(context.Config.Separator.LineColor);
                        }

                        tag = new TagContent(uiSep, uiColumnSet);

                        uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition()
                        {
                            Width = GridLength.Auto
                        });
                        Grid.SetColumn(uiSep, uiColumnSet.ColumnDefinitions.Count - 1);
                        uiColumnSet.Children.Add(uiSep);
                    }
                    else
                    {
                        tag = new TagContent(null, uiColumnSet);
                    }

                    // do some sizing magic using the magic GridUnitType.Star
                    var width = column.Width?.ToLower();
                    if (string.IsNullOrEmpty(width))
#pragma warning disable CS0618 // Type or member is obsolete
                    {
                        width = column.Size?.ToLower();
                    }
#pragma warning restore CS0618 // Type or member is obsolete

                    ColumnDefinition columnDefinition = null;

                    if (width == null || width == AdaptiveColumnWidth.Stretch.ToLower())
                    {
                        columnDefinition = new ColumnDefinition()
                        {
                            Width = new GridLength(1, GridUnitType.Star)
                        };
                    }
                    else if (width == AdaptiveColumnWidth.Auto.ToLower())
                    {
                        columnDefinition = new ColumnDefinition()
                        {
                            Width = GridLength.Auto
                        };
                    }
                    else
                    {
                        if (double.TryParse(width, out double val) && val >= 0)
                        {
                            // Weighted proportion (number only)
                            columnDefinition = new ColumnDefinition()
                            {
                                Width = new GridLength(val, GridUnitType.Star)
                            };
                        }
                        else if (width.EndsWith("px") && double.TryParse(width.Substring(0, width.Length - 2), out double pxVal) && pxVal >= 0)
                        {
                            // Exact pixel (number followed by "px")
                            columnDefinition = new ColumnDefinition()
                            {
                                Width = new GridLength((int)pxVal, GridUnitType.Pixel)
                            };
                        }
                        else
                        {
                            columnDefinition = new ColumnDefinition()
                            {
                                Width = GridLength.Auto
                            };
                        }
                    }

                    // Store the column definition in the tag so we can toggle the visibility later
                    tag.ColumnDefinition = columnDefinition;
                    tag.ViewIndex        = uiColumnSet.ColumnDefinitions.Count;

                    uiColumnSet.ColumnDefinitions.Add(columnDefinition);

                    uiContainer.Tag = tag;

                    Grid.SetColumn(uiContainer, uiColumnSet.ColumnDefinitions.Count - 1);
                    uiColumnSet.Children.Add(uiContainer);

                    context.SetVisibility(uiContainer, column.IsVisible, tag);
                }
            }

            context.ResetSeparatorVisibilityInsideContainer(uiColumnSet);

            // Revert context's value to that of outside the Container
            context.RenderArgs = parentRenderArgs;

            uiColumnSet.MinHeight = columnSet.PixelMinHeight;

            return(RendererUtil.ApplySelectAction(border, columnSet, context));
        }
Пример #22
0
        public static FrameworkElement Render(AdaptiveColumnSet columnSet, AdaptiveRenderContext context)
        {
            var uiColumnSet = new Grid();

            uiColumnSet.Style = context.GetStyle($"Adaptive.{columnSet.Type}");

            foreach (var column in columnSet.Columns)
            {
                FrameworkElement uiContainer = context.Render(column);

                // Add vertical Seperator
                if (uiColumnSet.ColumnDefinitions.Count > 0)
                {
                    if (column.Separator || column.Spacing != AdaptiveSpacing.None)
                    {
                        var uiSep = new Grid();
                        uiSep.Style = context.GetStyle($"Adaptive.VerticalSeparator");

                        uiSep.VerticalAlignment = VerticalAlignment.Stretch;

                        int spacing = context.Config.GetSpacing(column.Spacing);
                        uiSep.Margin = new Thickness(spacing / 2.0, 0, spacing / 2.0, 0);

                        uiSep.Width = context.Config.Separator.LineThickness;
                        if (column.Separator && context.Config.Separator.LineColor != null)
                        {
                            uiSep.Background = context.GetColorBrush(context.Config.Separator.LineColor);
                        }

                        uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition()
                        {
                            Width = GridLength.Auto
                        });
                        Grid.SetColumn(uiSep, uiColumnSet.ColumnDefinitions.Count - 1);
                        uiColumnSet.Children.Add(uiSep);
                    }
                }


                // do some sizing magic using the magic GridUnitType.Star
                var width = column.Width?.ToLower();
                if (string.IsNullOrEmpty(width))
#pragma warning disable CS0618 // Type or member is obsolete
                {
                    width = column.Size?.ToLower();
                }
#pragma warning restore CS0618 // Type or member is obsolete
                if (width == null || width == AdaptiveColumnWidth.Stretch.ToLower())
                {
                    uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition()
                    {
                        Width = new GridLength(1, GridUnitType.Star)
                    });
                }
                else if (width == AdaptiveColumnWidth.Auto.ToLower())
                {
                    uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition()
                    {
                        Width = GridLength.Auto
                    });
                }
                else
                {
                    if (double.TryParse(width, out double val) && val >= 0)
                    {
                        // Weighted proportion (number only)
                        uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition()
                        {
                            Width = new GridLength(val, GridUnitType.Star)
                        });
                    }
                    else if (width.EndsWith("px") && double.TryParse(width.Substring(0, width.Length - 2), out double pxVal) && pxVal >= 0)
                    {
                        // Exact pixel (number followed by "px")
                        uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition()
                        {
                            Width = new GridLength((int)pxVal, GridUnitType.Pixel)
                        });
                    }
                    else
                    {
                        uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition()
                        {
                            Width = GridLength.Auto
                        });
                    }
                }

                Grid.SetColumn(uiContainer, uiColumnSet.ColumnDefinitions.Count - 1);
                uiColumnSet.Children.Add(uiContainer);
            }

            if (columnSet.SelectAction != null)
            {
                return(context.RenderSelectAction(columnSet.SelectAction, uiColumnSet));
            }

            if (!columnSet.IsVisible)
            {
                uiColumnSet.Visibility = Visibility.Collapsed;
            }

            return(uiColumnSet);
        }
        public static void AddContainerElements(Grid uiContainer, IList <AdaptiveElement> elements, AdaptiveRenderContext context)
        {
            foreach (var cardElement in elements)
            {
                // each element has a row
                FrameworkElement uiElement = context.Render(cardElement);
                if (uiElement != null)
                {
                    if (cardElement.Separator && uiContainer.Children.Count > 0)
                    {
                        AddSeparator(context, cardElement, uiContainer);
                    }
                    else if (uiContainer.Children.Count > 0)
                    {
                        var       spacing        = context.Config.GetSpacing(cardElement.Spacing);
                        Thickness renderedMargin = uiElement.Margin;
                        uiElement.Margin = new Thickness(renderedMargin.Left,
                                                         renderedMargin.Top + spacing,
                                                         renderedMargin.Right,
                                                         renderedMargin.Bottom);
                    }

                    if (cardElement.Type == "Container" || cardElement.Type == "ColumnSet")
                    {
                        AdaptiveCollectionElement collectionElement = (AdaptiveCollectionElement)cardElement;
                        uiElement.MinHeight = collectionElement.PixelMinHeight;
                    }

                    if (cardElement.Height != AdaptiveHeight.Stretch)
                    {
                        uiContainer.RowDefinitions.Add(new RowDefinition()
                        {
                            Height = GridLength.Auto
                        });
                        Grid.SetRow(uiElement, uiContainer.RowDefinitions.Count - 1);

                        RendererUtil.ApplyIsVisible(uiElement, cardElement);
                        Grid.SetRow(uiElement, uiContainer.RowDefinitions.Count - 1);
                        uiContainer.Children.Add(uiElement);
                    }
                    else
                    {
                        if (cardElement.Type == "Container")
                        {
                            RendererUtil.ApplyIsVisible(uiElement, cardElement);
                            uiContainer.RowDefinitions.Add(new RowDefinition()
                            {
                                Height = new GridLength(1, GridUnitType.Star)
                            });
                            Grid.SetRow(uiElement, uiContainer.RowDefinitions.Count - 1);
                            uiContainer.Children.Add(uiElement);
                        }
                        else
                        {
                            StackPanel panel = new StackPanel();
                            RendererUtil.ApplyIsVisible(panel, cardElement);
                            if (!String.IsNullOrEmpty(cardElement.Id))
                            {
                                panel.Name = cardElement.Id;
                            }
                            panel.Children.Add(uiElement);

                            uiContainer.RowDefinitions.Add(new RowDefinition()
                            {
                                Height = new GridLength(1, GridUnitType.Star)
                            });
                            Grid.SetRow(panel, uiContainer.RowDefinitions.Count - 1);
                            uiContainer.Children.Add(panel);
                        }
                    }
                }
            }
        }
Пример #24
0
        public static FrameworkElement Render(AdaptiveTextInput input, AdaptiveRenderContext context)
        {
            if (context.Config.SupportsInteractivity)
            {
                var textBox = new WatermarkTextBox()
                {
                    Text = input.Value
                };
                if (input.IsMultiline == true)
                {
                    textBox.AcceptsReturn = true;
                    textBox.TextWrapping  = TextWrapping.Wrap;
                    textBox.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
                }
                if (input.MaxLength > 0)
                {
                    textBox.MaxLength = input.MaxLength;
                }

                textBox.Watermark   = input.Placeholder;
                textBox.Style       = context.GetStyle($"Adaptive.Input.Text.{input.Style}");
                textBox.DataContext = input;
                context.InputBindings.Add(input.Id, () => textBox.Text);
                if (input.InlineAction != null)
                {
                    if (context.Config.Actions.ShowCard.ActionMode == ShowCardActionMode.Inline &&
                        input.InlineAction.GetType() == typeof(AdaptiveShowCardAction))
                    {
                        context.Warnings.Add(new AdaptiveWarning(-1, "Inline ShowCard not supported for InlineAction"));
                    }
                    else
                    {
                        if (context.Config.SupportsInteractivity && context.ActionHandlers.IsSupported(input.InlineAction.GetType()))
                        {
                            // Set up a parent view that holds textbox, separator and button
                            var parentView = new Grid();

                            // grid config for textbox
                            parentView.ColumnDefinitions.Add(new ColumnDefinition()
                            {
                                Width = new GridLength(1, GridUnitType.Star)
                            });
                            Grid.SetColumn(textBox, 0);
                            parentView.Children.Add(textBox);

                            // grid config for spacing
                            int spacing = context.Config.GetSpacing(AdaptiveSpacing.Default);
                            var uiSep   = new Grid
                            {
                                Style             = context.GetStyle($"Adaptive.Input.Text.InlineAction.Separator"),
                                VerticalAlignment = VerticalAlignment.Stretch,
                                Width             = spacing,
                            };
                            parentView.ColumnDefinitions.Add(new ColumnDefinition()
                            {
                                Width = new GridLength(spacing, GridUnitType.Pixel)
                            });
                            Grid.SetColumn(uiSep, 1);

                            // adding button
                            var   uiButton = new Button();
                            Style style    = context.GetStyle($"Adaptive.Input.Text.InlineAction.Button");
                            if (style != null)
                            {
                                uiButton.Style = style;
                            }

                            // this textblock becomes tooltip if icon url exists else becomes the tile for the button
                            var uiTitle = new TextBlock
                            {
                                Text = input.InlineAction.Title,
                            };

                            if (input.InlineAction.IconUrl != null)
                            {
                                var actionsConfig = context.Config.Actions;

                                var image = new AdaptiveImage(input.InlineAction.IconUrl)
                                {
                                    HorizontalAlignment = AdaptiveHorizontalAlignment.Center,
                                    Type = "Adaptive.Input.Text.InlineAction.Image",
                                };

                                FrameworkElement uiIcon = null;
                                uiIcon           = AdaptiveImageRenderer.Render(image, context);
                                uiButton.Content = uiIcon;

                                // adjust height
                                textBox.Loaded += (sender, e) =>
                                {
                                    uiIcon.Height = textBox.ActualHeight;
                                };

                                uiButton.ToolTip = uiTitle;
                            }
                            else
                            {
                                uiTitle.FontSize = context.Config.GetFontSize(AdaptiveFontStyle.Default, AdaptiveTextSize.Default);
                                uiTitle.Style    = context.GetStyle($"Adaptive.Input.Text.InlineAction.Title");
                                uiButton.Content = uiTitle;
                            }

                            uiButton.Click += (sender, e) =>
                            {
                                context.InvokeAction(uiButton, new AdaptiveActionEventArgs(input.InlineAction));

                                // Prevent nested events from triggering
                                e.Handled = true;
                            };

                            parentView.ColumnDefinitions.Add(new ColumnDefinition()
                            {
                                Width = GridLength.Auto
                            });
                            Grid.SetColumn(uiButton, 2);
                            parentView.Children.Add(uiButton);
                            uiButton.VerticalAlignment = VerticalAlignment.Bottom;

                            textBox.KeyDown += (sender, e) =>
                            {
                                if (e.Key == System.Windows.Input.Key.Enter)
                                {
                                    context.InvokeAction(uiButton, new AdaptiveActionEventArgs(input.InlineAction));
                                    e.Handled = true;
                                }
                            };
                            return(parentView);
                        }
                    }
                }
                return(textBox);
            }
            else
            {
                var textBlock = AdaptiveTypedElementConverter.CreateElement <AdaptiveTextBlock>();
                textBlock.Text = XamlUtilities.GetFallbackText(input) ?? input.Placeholder;
                return(context.Render(textBlock));
            }
        }
Пример #25
0
        public static FrameworkElement Render(AdaptiveColumnSet columnSet, AdaptiveRenderContext context)
        {
            var uiColumnSet = new Grid();

            uiColumnSet.Style = context.GetStyle($"Adaptive.{columnSet.Type}");

            // Keep track of ContainerStyle.ForegroundColors before Container is rendered
            var parentRenderArgs  = context.RenderArgs;
            var elementRenderArgs = new AdaptiveRenderArgs(parentRenderArgs);

            Border border = new Border();

            border.Child = uiColumnSet;

            bool inheritsStyleFromParent = !columnSet.Style.HasValue;
            bool hasPadding = false;

            if (!inheritsStyleFromParent)
            {
                hasPadding = AdaptiveContainerRenderer.ApplyPadding(border, uiColumnSet, columnSet, parentRenderArgs, context);

                // Apply background color
                var columnSetStyle = context.Config.ContainerStyles.GetContainerStyleConfig(columnSet.Style);

                border.Background = context.GetColorBrush(columnSetStyle.BackgroundColor);
                elementRenderArgs.ForegroundColors = columnSetStyle.ForegroundColors;
            }

            elementRenderArgs.ParentStyle          = (inheritsStyleFromParent) ? parentRenderArgs.ParentStyle : columnSet.Style.Value;
            elementRenderArgs.HasParentWithPadding = (hasPadding || parentRenderArgs.HasParentWithPadding);

            for (int i = 0; i < columnSet.Columns.Count; ++i)
            {
                AdaptiveColumn column = columnSet.Columns[i];

                var columnRenderArgs = new AdaptiveRenderArgs(elementRenderArgs);
                if (columnSet.Columns.Count == 1)
                {
                    columnRenderArgs.ColumnRelativePosition = ColumnPositionEnum.Only;
                }
                else
                {
                    if (i == 0)
                    {
                        columnRenderArgs.ColumnRelativePosition = ColumnPositionEnum.Begin;
                    }
                    else if (i == (columnSet.Columns.Count - 1))
                    {
                        columnRenderArgs.ColumnRelativePosition = ColumnPositionEnum.End;
                    }
                    else
                    {
                        columnRenderArgs.ColumnRelativePosition = ColumnPositionEnum.Intermediate;
                    }
                }
                context.RenderArgs = columnRenderArgs;

                FrameworkElement uiContainer = context.Render(column);

                // Add vertical Seperator
                if (uiColumnSet.ColumnDefinitions.Count > 0)
                {
                    if (column.Separator || column.Spacing != AdaptiveSpacing.None)
                    {
                        var uiSep = new Grid();
                        uiSep.Style = context.GetStyle($"Adaptive.VerticalSeparator");

                        uiSep.VerticalAlignment = VerticalAlignment.Stretch;

                        int spacing = context.Config.GetSpacing(column.Spacing);
                        uiSep.Margin = new Thickness(spacing / 2.0, 0, spacing / 2.0, 0);

                        uiSep.Width = context.Config.Separator.LineThickness;
                        if (column.Separator && context.Config.Separator.LineColor != null)
                        {
                            uiSep.Background = context.GetColorBrush(context.Config.Separator.LineColor);
                        }

                        uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition()
                        {
                            Width = GridLength.Auto
                        });
                        Grid.SetColumn(uiSep, uiColumnSet.ColumnDefinitions.Count - 1);
                        uiColumnSet.Children.Add(uiSep);
                    }
                }

                // do some sizing magic using the magic GridUnitType.Star
                var width = column.Width?.ToLower();
                if (string.IsNullOrEmpty(width))
#pragma warning disable CS0618 // Type or member is obsolete
                {
                    width = column.Size?.ToLower();
                }
#pragma warning restore CS0618 // Type or member is obsolete
                if (width == null || width == AdaptiveColumnWidth.Stretch.ToLower())
                {
                    uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition()
                    {
                        Width = new GridLength(1, GridUnitType.Star)
                    });
                }
                else if (width == AdaptiveColumnWidth.Auto.ToLower())
                {
                    uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition()
                    {
                        Width = GridLength.Auto
                    });
                }
                else
                {
                    if (double.TryParse(width, out double val) && val >= 0)
                    {
                        // Weighted proportion (number only)
                        uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition()
                        {
                            Width = new GridLength(val, GridUnitType.Star)
                        });
                    }
                    else if (width.EndsWith("px") && double.TryParse(width.Substring(0, width.Length - 2), out double pxVal) && pxVal >= 0)
                    {
                        // Exact pixel (number followed by "px")
                        uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition()
                        {
                            Width = new GridLength((int)pxVal, GridUnitType.Pixel)
                        });
                    }
                    else
                    {
                        uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition()
                        {
                            Width = GridLength.Auto
                        });
                    }
                }

                Grid.SetColumn(uiContainer, uiColumnSet.ColumnDefinitions.Count - 1);
                uiColumnSet.Children.Add(uiContainer);
            }

            if (columnSet.SelectAction != null)
            {
                return(context.RenderSelectAction(columnSet.SelectAction, border));
            }

            if (!columnSet.IsVisible)
            {
                uiColumnSet.Visibility = Visibility.Collapsed;
            }

            // Revert context's value to that of outside the Container
            context.RenderArgs = parentRenderArgs;

            return(border);
        }
        public static void AddActions(Grid uiContainer, IList <AdaptiveAction> actions, AdaptiveRenderContext context)
        {
            if (!context.Config.SupportsInteractivity)
            {
                return;
            }

            var actionsConfig    = context.Config.Actions;
            var maxActions       = actionsConfig.MaxActions;
            var actionsToProcess = actions
                                   .Take(maxActions).ToList();

            if (actionsToProcess.Any())
            {
                var uiActionBar = new UniformGrid();

                if (actionsConfig.ActionsOrientation == ActionsOrientation.Horizontal)
                {
                    uiActionBar.Columns = actionsToProcess.Count();
                }
                else
                {
                    uiActionBar.Rows = actionsToProcess.Count();
                }

                uiActionBar.HorizontalAlignment = (HorizontalAlignment)Enum.Parse(typeof(HorizontalAlignment), actionsConfig.ActionAlignment.ToString());
                uiActionBar.VerticalAlignment   = VerticalAlignment.Bottom;
                uiActionBar.Style = context.GetStyle("Adaptive.Actions");

                // For vertical, we want to subtract the top margin of the first button
                var topMargin = actionsConfig.ActionsOrientation == ActionsOrientation.Horizontal
                    ? context.Config.GetSpacing(actionsConfig.Spacing)
                    : context.Config.GetSpacing(actionsConfig.Spacing) - actionsConfig.ButtonSpacing;

                uiActionBar.Margin = new Thickness(0, topMargin, 0, 0);

                uiContainer.RowDefinitions.Add(new RowDefinition()
                {
                    Height = GridLength.Auto
                });
                Grid.SetRow(uiActionBar, uiContainer.RowDefinitions.Count - 1);
                uiContainer.Children.Add(uiActionBar);

                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>();
                foreach (var action in actionsToProcess)
                {
                    // add actions
                    var uiAction = (Button)context.Render(action);


                    if (actionsConfig.ActionsOrientation == ActionsOrientation.Horizontal)
                    {
                        if (uiActionBar.Children.Count > 0) // don't apply left margin to the first item
                        {
                            uiAction.Margin = new Thickness(actionsConfig.ButtonSpacing, 0, 0, 0);
                        }
                    }
                    else
                    {
                        uiAction.Margin = new Thickness(0, actionsConfig.ButtonSpacing, 0, 0);
                    }


                    if (actionsConfig.ActionsOrientation == ActionsOrientation.Horizontal)
                    {
                        Grid.SetColumn(uiAction, iPos++);
                    }

                    uiActionBar.Children.Add(uiAction);

                    if (action is AdaptiveShowCardAction showCardAction)
                    {
                        if (isInline)
                        {
                            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);

                            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;
                                }
                            };
                        }
                    }
                }
            }
        }
        public static void AddRenderedActions(Grid uiContainer, IList <AdaptiveAction> actions, AdaptiveRenderContext context, AdaptiveInternalID actionSetId)
        {
            if (!context.Config.SupportsInteractivity)
            {
                return;
            }

            ActionsConfig         actionsConfig    = context.Config.Actions;
            int                   maxActions       = actionsConfig.MaxActions;
            List <AdaptiveAction> actionsToProcess = GetActionsToProcess(actions, maxActions, context);

            if (actionsToProcess.Any())
            {
                var uiActionBar = new UniformGrid();

                if (actionsConfig.ActionsOrientation == ActionsOrientation.Horizontal)
                {
                    uiActionBar.Columns = actionsToProcess.Count();
                }
                else
                {
                    uiActionBar.Rows = actionsToProcess.Count();
                }

                uiActionBar.HorizontalAlignment = (HorizontalAlignment)Enum.Parse(typeof(HorizontalAlignment), actionsConfig.ActionAlignment.ToString());
                uiActionBar.VerticalAlignment   = VerticalAlignment.Bottom;
                uiActionBar.Style = context.GetStyle("Adaptive.Actions");

                // For vertical, we want to subtract the top margin of the first button
                int topMargin = actionsConfig.ActionsOrientation == ActionsOrientation.Horizontal
                    ? context.Config.GetSpacing(actionsConfig.Spacing)
                    : context.Config.GetSpacing(actionsConfig.Spacing) - actionsConfig.ButtonSpacing;

                uiActionBar.Margin = new Thickness(0, topMargin, 0, 0);

                uiContainer.RowDefinitions.Add(new RowDefinition()
                {
                    Height = GridLength.Auto
                });
                Grid.SetRow(uiActionBar, uiContainer.RowDefinitions.Count - 1);
                uiContainer.Children.Add(uiActionBar);

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

                int iPos = 0;

                // See if all actions have icons, otherwise force the icon placement to the left
                IconPlacement oldConfigIconPlacement = actionsConfig.IconPlacement;
                bool          allActionsHaveIcons    = true;
                foreach (AdaptiveAction action in actionsToProcess)
                {
                    if (string.IsNullOrEmpty(action.IconUrl))
                    {
                        allActionsHaveIcons = false;
                        break;
                    }
                }

                if (!allActionsHaveIcons)
                {
                    actionsConfig.IconPlacement = IconPlacement.LeftOfTitle;
                }

                // indicates showcard has not been seen if it's set false; meaningful only if it's used
                // when inline is supported
                bool hasSeenInlineShowCard = false;

                foreach (AdaptiveAction action in actionsToProcess)
                {
                    // add actions
                    var uiAction = context.Render(action) as Button;

                    if (uiAction == null)
                    {
                        context.Warnings.Add(new AdaptiveWarning(-1, $"action failed to render" +
                                                                 $"and valid fallback wasn't present"));
                        continue;
                    }

                    if (actionsConfig.ActionsOrientation == ActionsOrientation.Horizontal)
                    {
                        if (uiActionBar.Children.Count > 0) // don't apply left margin to the first item
                        {
                            uiAction.Margin = new Thickness(actionsConfig.ButtonSpacing, 0, 0, 0);
                        }
                    }
                    else
                    {
                        uiAction.Margin = new Thickness(0, actionsConfig.ButtonSpacing, 0, 0);
                    }

                    if (actionsConfig.ActionsOrientation == ActionsOrientation.Horizontal)
                    {
                        Grid.SetColumn(uiAction, iPos++);
                    }

                    uiActionBar.Children.Add(uiAction);

                    if (action is AdaptiveShowCardAction showCardAction && isInline)
                    {
                        if (actionSetId != null)
                        {
                            // the button's context is used as key for retrieving the corresponding showcard
                            uiAction.SetContext(actionSetId);

                            if (!hasSeenInlineShowCard)
                            {
                                // Define a new row to contain all the show cards
                                uiContainer.RowDefinitions.Add(new RowDefinition()
                                {
                                    Height = GridLength.Auto
                                });
                                // it's first showcard of the peers, create a new list
                                context.PeerShowCardsInActionSet[actionSetId] = new List <FrameworkElement>();
                            }

                            hasSeenInlineShowCard = true;

                            Grid uiShowCardContainer = new Grid();
                            uiShowCardContainer.Style       = context.GetStyle("Adaptive.Actions.ShowCard");
                            uiShowCardContainer.DataContext = showCardAction;
                            uiShowCardContainer.Visibility  = Visibility.Collapsed;
                            var padding = context.Config.Spacing.Padding;
                            // set negative margin to expand the wrapper to the edge of outer most card
                            uiShowCardContainer.Margin = new Thickness(-padding, actionsConfig.ShowCard.InlineTopMargin, -padding, -padding);
                            var showCardStyleConfig = context.Config.ContainerStyles.GetContainerStyleConfig(actionsConfig.ShowCard.Style);
                            uiShowCardContainer.Background = context.GetColorBrush(showCardStyleConfig.BackgroundColor);

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

                            uiShowCardContainer.Children.Add(uiShowCardWrapper);
                            context.ActionShowCards.Add(uiAction, uiShowCardContainer);
                            // added the rendered show card as a peer
                            context.PeerShowCardsInActionSet[actionSetId].Add(uiShowCardContainer);
                            // define where in the rows of the parent Grid the show card will occupy
                            // and add it to the parent
                            Grid.SetRow(uiShowCardContainer, uiContainer.RowDefinitions.Count - 1);
                            uiContainer.Children.Add(uiShowCardContainer);
                        }
                        else
                        {
                            context.Warnings.Add(new AdaptiveWarning(-1, $"button's corresponding showCard" +
                                                                     $" couldn't be added since the action set the button belongs to has null as internal id"));
                        }
                    }
                }

                // Restore the iconPlacement for the context.
                actionsConfig.IconPlacement = oldConfigIconPlacement;
            }
        }
        public static void AddContainerElements(Grid uiContainer, IList <AdaptiveElement> elements, AdaptiveRenderContext context)
        {
            // Keeping track of the index so we don't have to call IndexOf function on every iteration
            int index = 0;

            foreach (var cardElement in elements)
            {
                if (index != 0)
                {
                    // Only the first element can bleed to the top
                    context.RenderArgs.BleedDirection &= ~BleedDirection.BleedUp;
                }

                if (index != elements.Count - 1)
                {
                    // Only the last element can bleed to the bottom
                    context.RenderArgs.BleedDirection &= ~BleedDirection.BleedDown;
                }

                index++;

                // each element has a row
                FrameworkElement uiElement = context.Render(cardElement);
                if (uiElement != null)
                {
                    TagContent tag       = null;
                    Grid       separator = null;
                    if (cardElement.Separator && uiContainer.Children.Count > 0)
                    {
                        separator = AddSeparator(context, cardElement, uiContainer);
                    }
                    else if (uiContainer.Children.Count > 0)
                    {
                        separator = AddSpacing(context, cardElement, uiContainer);
                    }

                    tag = new TagContent(separator, uiContainer);

                    uiElement.Tag = tag;

                    // Sets the minHeight property for Container and ColumnSet
                    if (cardElement.Type == "Container" || cardElement.Type == "ColumnSet")
                    {
                        AdaptiveCollectionElement collectionElement = (AdaptiveCollectionElement)cardElement;
                        uiElement.MinHeight = collectionElement.PixelMinHeight;
                    }

                    int           rowDefinitionIndex = uiContainer.RowDefinitions.Count;
                    RowDefinition rowDefinition      = null;
                    if (cardElement.Height != AdaptiveHeight.Stretch)
                    {
                        rowDefinition = new RowDefinition()
                        {
                            Height = GridLength.Auto
                        };

                        uiContainer.RowDefinitions.Add(rowDefinition);
                        Grid.SetRow(uiElement, rowDefinitionIndex);
                        uiContainer.Children.Add(uiElement);

                        // Row definition is stored in the tag for containers and elements that stretch
                        // so when the elements are shown, the row can have it's original definition,
                        // while when the element is hidden, the extra space is not reserved in the layout
                        tag.RowDefinition = rowDefinition;
                        tag.ViewIndex     = rowDefinitionIndex;

                        context.SetVisibility(uiElement, cardElement.IsVisible, tag);
                    }
                    else
                    {
                        rowDefinition = new RowDefinition()
                        {
                            Height = new GridLength(1, GridUnitType.Star)
                        };

                        uiContainer.RowDefinitions.Add(rowDefinition);

                        // Row definition is stored in the tag for containers and elements that stretch
                        // so when the elements are shown, the row can have it's original definition,
                        // while when the element is hidden, the extra space is not reserved in the layout
                        tag.RowDefinition = rowDefinition;
                        tag.ViewIndex     = rowDefinitionIndex;

                        if (cardElement.Type == "Container")
                        {
                            Grid.SetRow(uiElement, rowDefinitionIndex);
                            uiContainer.Children.Add(uiElement);
                            context.SetVisibility(uiElement, cardElement.IsVisible, tag);
                        }
                        else
                        {
                            StackPanel panel = new StackPanel();

                            if (!String.IsNullOrEmpty(cardElement.Id))
                            {
                                panel.Name = cardElement.Id;
                            }

                            panel.Children.Add(uiElement);
                            panel.Tag = tag;

                            Grid.SetRow(panel, rowDefinitionIndex);
                            uiContainer.Children.Add(panel);
                            context.SetVisibility(panel, cardElement.IsVisible, tag);
                        }
                    }
                }
            }

            context.ResetSeparatorVisibilityInsideContainer(uiContainer);
        }
Пример #29
0
        public static void AddActions(Grid uiContainer, IList <AdaptiveAction> actions, AdaptiveRenderContext context)
        {
            if (!context.Config.SupportsInteractivity)
            {
                return;
            }

            var actionsConfig    = context.Config.Actions;
            var maxActions       = actionsConfig.MaxActions;
            var actionsToProcess = actions
                                   .Take(maxActions).ToList();

            if (actionsToProcess.Any())
            {
                var uiActionBar = new UniformGrid();

                if (actionsConfig.ActionsOrientation == ActionsOrientation.Horizontal)
                {
                    uiActionBar.Columns = actionsToProcess.Count();
                }
                else
                {
                    uiActionBar.Rows = actionsToProcess.Count();
                }

                uiActionBar.HorizontalAlignment = (HorizontalAlignment)Enum.Parse(typeof(HorizontalAlignment), actionsConfig.ActionAlignment.ToString());
                uiActionBar.VerticalAlignment   = VerticalAlignment.Bottom;
                uiActionBar.Style = context.GetStyle("Adaptive.Actions");

                // For vertical, we want to subtract the top margin of the first button
                var topMargin = actionsConfig.ActionsOrientation == ActionsOrientation.Horizontal
                    ? context.Config.GetSpacing(actionsConfig.Spacing)
                    : context.Config.GetSpacing(actionsConfig.Spacing) - actionsConfig.ButtonSpacing;

                uiActionBar.Margin = new Thickness(0, topMargin, 0, 0);

                uiContainer.RowDefinitions.Add(new RowDefinition()
                {
                    Height = GridLength.Auto
                });
                Grid.SetRow(uiActionBar, uiContainer.RowDefinitions.Count - 1);
                uiContainer.Children.Add(uiActionBar);

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

                int iPos = 0;

                // See if all actions have icons, otherwise force the icon placement to the left
                var  oldConfigIconPlacement = actionsConfig.IconPlacement;
                bool allActionsHaveIcons    = true;
                foreach (var action in actionsToProcess)
                {
                    if (string.IsNullOrEmpty(action.IconUrl))
                    {
                        allActionsHaveIcons = false;
                        break;
                    }
                }

                if (!allActionsHaveIcons)
                {
                    actionsConfig.IconPlacement = IconPlacement.LeftOfTitle;
                }

                foreach (var action in actionsToProcess)
                {
                    // add actions
                    var uiAction = (Button)context.Render(action);


                    if (actionsConfig.ActionsOrientation == ActionsOrientation.Horizontal)
                    {
                        if (uiActionBar.Children.Count > 0) // don't apply left margin to the first item
                        {
                            uiAction.Margin = new Thickness(actionsConfig.ButtonSpacing, 0, 0, 0);
                        }
                    }
                    else
                    {
                        uiAction.Margin = new Thickness(0, actionsConfig.ButtonSpacing, 0, 0);
                    }


                    if (actionsConfig.ActionsOrientation == ActionsOrientation.Horizontal)
                    {
                        Grid.SetColumn(uiAction, iPos++);
                    }

                    uiActionBar.Children.Add(uiAction);

                    if (action is AdaptiveShowCardAction showCardAction)
                    {
                        // Only support 1 level of showCard
                        if (isInline && context.CardDepth == 1)
                        {
                            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);

                            // Add to the list of show cards in context
                            context.ActionShowCards.Add(uiAction, uiShowCardContainer);
                        }
                    }
                }

                // Restore the iconPlacement for the context.
                actionsConfig.IconPlacement = oldConfigIconPlacement;
            }
        }
Пример #30
0
        public static FrameworkElement Render(AdaptiveColumnSet columnSet, AdaptiveRenderContext context)
        {
            var uiColumnSet = new Grid();

            uiColumnSet.Style = context.GetStyle($"Adaptive.{columnSet.Type}");

            foreach (var column in columnSet.Columns)
            {
                FrameworkElement uiContainer = context.Render(column);

                // Add vertical Seperator
                if (uiColumnSet.ColumnDefinitions.Count > 0)
                {
                    if (column.Separator || column.Spacing != AdaptiveSpacing.None)
                    {
                        var uiSep = new Grid();
                        uiSep.Style = context.GetStyle($"Adaptive.VerticalSeparator");

                        uiSep.VerticalAlignment = VerticalAlignment.Stretch;

                        int spacing = context.Config.GetSpacing(column.Spacing);
                        uiSep.Margin = new Thickness(spacing / 2.0, 0, spacing / 2.0, 0);

                        uiSep.Width = context.Config.Separator.LineThickness;
                        if (column.Separator && context.Config.Separator.LineColor != null)
                        {
                            uiSep.Background = context.GetColorBrush(context.Config.Separator.LineColor);
                        }

                        uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition()
                        {
                            Width = GridLength.Auto
                        });
                        Grid.SetColumn(uiSep, uiColumnSet.ColumnDefinitions.Count - 1);
                        uiColumnSet.Children.Add(uiSep);
                    }
                }


                // do some sizing magic using the magic GridUnitType.Star
                var width = column.Width?.ToLower();
                if (string.IsNullOrEmpty(width))
#pragma warning disable CS0618 // Type or member is obsolete
                {
                    width = column.Size?.ToLower();
                }
#pragma warning restore CS0618 // Type or member is obsolete
                if (width == null || width == AdaptiveColumnWidth.Stretch.ToLower())
                {
                    uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition()
                    {
                        Width = new GridLength(1, GridUnitType.Star)
                    });
                }
                else if (width == AdaptiveColumnWidth.Auto.ToLower())
                {
                    uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition()
                    {
                        Width = GridLength.Auto
                    });
                }
                else
                {
                    double val;
                    if (double.TryParse(width, out val))
                    {
                        uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition()
                        {
                            Width = new GridLength(val, GridUnitType.Star)
                        });
                    }
                    else
                    {
                        uiColumnSet.ColumnDefinitions.Add(new ColumnDefinition()
                        {
                            Width = GridLength.Auto
                        });
                    }
                }

                Grid.SetColumn(uiContainer, uiColumnSet.ColumnDefinitions.Count - 1);
                uiColumnSet.Children.Add(uiContainer);
            }

            if (columnSet.SelectAction != null)
            {
                var uiButton = (Button)context.Render(columnSet.SelectAction);
                if (uiButton != null)
                {
                    uiButton.Content = uiColumnSet;
                    uiButton.Style   = context.GetStyle("Adaptive.Action.Tap");
                    return(uiButton);
                }
            }
            return(uiColumnSet);
        }