AdaptiveSubgroup.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // ******************************************************************
  2. // Copyright (c) Microsoft. All rights reserved.
  3. // This code is licensed under the MIT License (MIT).
  4. // THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  5. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  6. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  7. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  8. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  9. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
  10. // THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
  11. // ******************************************************************
  12. using System;
  13. using System.Collections.Generic;
  14. using Microsoft.Toolkit.Uwp.Notifications.Adaptive.Elements;
  15. namespace Microsoft.Toolkit.Uwp.Notifications
  16. {
  17. /// <summary>
  18. /// Subgroups are vertical columns that can contain text and images. Supported on Tiles since RTM. Supported on Toasts since Anniversary Update.
  19. /// </summary>
  20. public sealed class AdaptiveSubgroup
  21. {
  22. /// <summary>
  23. /// <see cref="AdaptiveText"/> and <see cref="AdaptiveImage"/> are valid children of subgroups.
  24. /// </summary>
  25. public IList<IAdaptiveSubgroupChild> Children { get; private set; } = new List<IAdaptiveSubgroupChild>();
  26. private int? _hintWeight;
  27. /// <summary>
  28. /// Control the width of this subgroup column by specifying the weight, relative to the other subgroups.
  29. /// </summary>
  30. public int? HintWeight
  31. {
  32. get
  33. {
  34. return _hintWeight;
  35. }
  36. set
  37. {
  38. Element_AdaptiveSubgroup.CheckWeight(value);
  39. _hintWeight = value;
  40. }
  41. }
  42. /// <summary>
  43. /// Control the vertical alignment of this subgroup's content.
  44. /// </summary>
  45. public AdaptiveSubgroupTextStacking HintTextStacking { get; set; } = Element_AdaptiveSubgroup.DEFAULT_TEXT_STACKING;
  46. internal Element_AdaptiveSubgroup ConvertToElement()
  47. {
  48. var subgroup = new Element_AdaptiveSubgroup()
  49. {
  50. Weight = HintWeight,
  51. TextStacking = HintTextStacking
  52. };
  53. foreach (var child in Children)
  54. {
  55. subgroup.Children.Add(ConvertToSubgroupChildElement(child));
  56. }
  57. return subgroup;
  58. }
  59. private static IElement_AdaptiveSubgroupChild ConvertToSubgroupChildElement(IAdaptiveSubgroupChild child)
  60. {
  61. if (child is AdaptiveText)
  62. {
  63. return (child as AdaptiveText).ConvertToElement();
  64. }
  65. if (child is AdaptiveImage)
  66. {
  67. return (child as AdaptiveImage).ConvertToElement();
  68. }
  69. throw new NotImplementedException("Unknown child: " + child.GetType());
  70. }
  71. }
  72. }