MobileMenuButton.xaml.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using Xamarin.Forms;
  3. using Xamarin.Forms.Xaml;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using Syncfusion.XForms.PopupLayout;
  8. using XF.Material.Forms.UI;
  9. namespace InABox.Mobile
  10. {
  11. [XamlCompilation(XamlCompilationOptions.Compile)]
  12. public partial class MobileMenuButton
  13. {
  14. private SfPopupLayout _popup;
  15. private MobileMenuButtonMenu _menu;
  16. public IList<MobileMenuEntry> Items => _menu.Items;
  17. public RelativePosition Position { get; set; }
  18. public event EventHandler Appearing;
  19. public ImageSource Image
  20. {
  21. get => _image.Source;
  22. set => _image.Source = value;
  23. }
  24. public event MobileMenuButtonClickedEvent Clicked;
  25. public MobileMenuButton()
  26. {
  27. _menu = new MobileMenuButtonMenu();
  28. _menu.ItemClicked += (sender,args) => _popup.Dismiss();
  29. InitializeComponent();
  30. _popup = new SfPopupLayout();
  31. Position = RelativePosition.AlignToLeftOf;
  32. }
  33. private void _image_OnClicked(object sender, EventArgs e)
  34. {
  35. Appearing?.Invoke(this, EventArgs.Empty);
  36. if (_menu.Items.Any())
  37. {
  38. _popup.PopupView.ContentTemplate = new DataTemplate(() => _menu);
  39. _popup.PopupView.ShowFooter = false;
  40. _popup.PopupView.ShowHeader = false;
  41. _popup.PopupView.AutoSizeMode = AutoSizeMode.Both;
  42. _popup.PopupView.PopupStyle.CornerRadius = 5;
  43. _popup.Padding = new Thickness(5);
  44. GetOffset(out double x, out double y);
  45. _popup.ShowRelativeToView(this, Position, x, y);
  46. }
  47. else
  48. Clicked?.Invoke(this, new MobileMenuButtonClickedEventArgs(null));
  49. }
  50. /// <summary>
  51. /// Calculates the offest of the Menu to position it at the center of the Button
  52. /// Let's not presume that all the calculations are correct - its only been tested
  53. /// against AlignToLeftOf (for top-right-hand side menu options
  54. /// </summary>
  55. /// <param name="x"></param>
  56. /// <param name="y"></param>
  57. private void GetOffset(out double x, out double y)
  58. {
  59. x = 0F;
  60. y = 0F;
  61. // Displays the popup at the top of the given view.
  62. if (Position == RelativePosition.AlignTop)
  63. {
  64. x = Width / 2F;
  65. y = Height / 2F;
  66. }
  67. // Displays the popup to the left of the given view.</summary>
  68. else if (Position == RelativePosition.AlignToLeftOf)
  69. {
  70. x = Width / 2F;
  71. y = Height / 2F;
  72. }
  73. // Displays the popup to the right of the given view.</summary>
  74. else if (Position == RelativePosition.AlignToRightOf)
  75. {
  76. x = 0F - (Width / 2F);
  77. y = Height / 2F;
  78. }
  79. // Displays the popup at the bottom of the given view.</summary>
  80. else if (Position == RelativePosition.AlignBottom)
  81. {
  82. x = Width / 2F;
  83. y = 0F - (Height / 2F);
  84. }
  85. // Displays the popup at the top left position of the given view.
  86. else if (Position == RelativePosition.AlignTopLeft)
  87. {
  88. x = Width / 2F;
  89. y = Height / 2F;
  90. }
  91. // Displays the popup at the top right position of the given view.
  92. else if (Position == RelativePosition.AlignTopRight)
  93. {
  94. x = 0F - (Width / 2F);
  95. y = Height / 2F;
  96. }
  97. // Displays the popup at the bottom left position of the given view.
  98. else if (Position == RelativePosition.AlignBottomLeft)
  99. {
  100. x = 0F - (Width / 2F);
  101. y = Height / 2F;
  102. }
  103. // Displays the popup at the bottom right position of the given view.
  104. else if (Position == RelativePosition.AlignBottomRight)
  105. {
  106. x = 0F - (Width / 2F);
  107. y = 0F - (Height / 2F);
  108. }
  109. }
  110. }
  111. }