ToastActionsCustom.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. namespace Microsoft.Toolkit.Uwp.Notifications
  15. {
  16. /// <summary>
  17. /// Create your own custom actions, using controls like <see cref="ToastButton"/>, <see cref="ToastTextBox"/>, and <see cref="ToastSelectionBox"/>.
  18. /// </summary>
  19. public sealed class ToastActionsCustom : IToastActions
  20. {
  21. /// <summary>
  22. /// Inputs like <see cref="ToastTextBox"/> and <see cref="ToastSelectionBox"/> can be added to the Toast. Only up to 5 inputs can be added; after that, an exception is thrown.
  23. /// </summary>
  24. public IList<IToastInput> Inputs { get; private set; } = new LimitedList<IToastInput>(5);
  25. /// <summary>
  26. /// Buttons are displayed after all the inputs (or adjacent to inputs if used as quick reply buttons). Only up to 5 buttons can be added (or fewer if you are also including context menu items). After that, an exception is thrown. You can add <see cref="ToastButton"/>, <see cref="ToastButtonSnooze"/>, or <see cref="ToastButtonDismiss"/>
  27. /// </summary>
  28. public IList<IToastButton> Buttons { get; private set; } = new LimitedList<IToastButton>(5);
  29. /// <summary>
  30. /// New in Anniversary Update: Custom context menu items, providing additional actions when the user right clicks the Toast notification. You can only have up to 5 buttons and context menu items *combined*. Thus, if you have one context menu item, you can only have four buttons, etc.
  31. /// </summary>
  32. public IList<ToastContextMenuItem> ContextMenuItems { get; private set; } = new List<ToastContextMenuItem>();
  33. internal Element_ToastActions ConvertToElement()
  34. {
  35. if (Buttons.Count + ContextMenuItems.Count > 5)
  36. {
  37. throw new InvalidOperationException("You have too many buttons/context menu items. You can only have up to 5 total.");
  38. }
  39. var el = new Element_ToastActions();
  40. foreach (var input in Inputs)
  41. {
  42. el.Children.Add(ConvertToInputElement(input));
  43. }
  44. foreach (var button in this.Buttons)
  45. {
  46. el.Children.Add(ConvertToActionElement(button));
  47. }
  48. foreach (var item in ContextMenuItems)
  49. {
  50. el.Children.Add(item.ConvertToElement());
  51. }
  52. return el;
  53. }
  54. private static Element_ToastAction ConvertToActionElement(IToastButton button)
  55. {
  56. if (button is ToastButton)
  57. {
  58. return (button as ToastButton).ConvertToElement();
  59. }
  60. if (button is ToastButtonDismiss)
  61. {
  62. return (button as ToastButtonDismiss).ConvertToElement();
  63. }
  64. if (button is ToastButtonSnooze)
  65. {
  66. return (button as ToastButtonSnooze).ConvertToElement();
  67. }
  68. throw new NotImplementedException("Unknown button child: " + button.GetType());
  69. }
  70. private static Element_ToastInput ConvertToInputElement(IToastInput input)
  71. {
  72. if (input is ToastTextBox)
  73. {
  74. return (input as ToastTextBox).ConvertToElement();
  75. }
  76. if (input is ToastSelectionBox)
  77. {
  78. return (input as ToastSelectionBox).ConvertToElement();
  79. }
  80. throw new NotImplementedException("Unknown input child: " + input.GetType());
  81. }
  82. }
  83. }