MobileMenuButton.xaml.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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<MobileMenuButtonMenuItem> Items => _menu.Items;
  17. public RelativePosition Position { get; set; }
  18. public ImageSource Image
  19. {
  20. get => _image.Source;
  21. set => _image.Source = value;
  22. }
  23. public event MobileMenuButtonClickedEvent Clicked;
  24. public MobileMenuButton()
  25. {
  26. _menu = new MobileMenuButtonMenu();
  27. InitializeComponent();
  28. _popup = new SfPopupLayout();
  29. Position = RelativePosition.AlignToLeftOf;
  30. }
  31. private void _image_OnClicked(object sender, EventArgs e)
  32. {
  33. if (Items.Any())
  34. {
  35. _popup.PopupView.ContentTemplate = new DataTemplate(() => _menu);
  36. _popup.PopupView.ShowFooter = false;
  37. _popup.PopupView.ShowHeader = false;
  38. _popup.PopupView.AutoSizeMode = AutoSizeMode.Both;
  39. _popup.PopupView.PopupStyle.CornerRadius = 5;
  40. _popup.Padding = new Thickness(5);
  41. GetOffset(out double x, out double y);
  42. _popup.ShowRelativeToView(this, Position, x, y);
  43. }
  44. else
  45. Clicked?.Invoke(this, new MobileMenuButtonClickedEventArgs(null));
  46. }
  47. /// <summary>
  48. /// Calculates the offest of the Menu to position it at the center of the Button
  49. /// Let's not presume that all the calculations are correct - its only been tested
  50. /// against AlignToLeftOf (for top-right-hand side menu options
  51. /// </summary>
  52. /// <param name="x"></param>
  53. /// <param name="y"></param>
  54. private void GetOffset(out double x, out double y)
  55. {
  56. x = 0F;
  57. y = 0F;
  58. // Displays the popup at the top of the given view.
  59. if (Position == RelativePosition.AlignTop)
  60. {
  61. x = Width / 2F;
  62. y = Height / 2F;
  63. }
  64. // Displays the popup to the left of the given view.</summary>
  65. else if (Position == RelativePosition.AlignToLeftOf)
  66. {
  67. x = Width / 2F;
  68. y = Height / 2F;
  69. }
  70. // Displays the popup to the right of the given view.</summary>
  71. else if (Position == RelativePosition.AlignToRightOf)
  72. {
  73. x = 0F - (Width / 2F);
  74. y = Height / 2F;
  75. }
  76. // Displays the popup at the bottom of the given view.</summary>
  77. else if (Position == RelativePosition.AlignBottom)
  78. {
  79. x = Width / 2F;
  80. y = 0F - (Height / 2F);
  81. }
  82. // Displays the popup at the top left position of the given view.
  83. else if (Position == RelativePosition.AlignTopLeft)
  84. {
  85. x = Width / 2F;
  86. y = Height / 2F;
  87. }
  88. // Displays the popup at the top right position of the given view.
  89. else if (Position == RelativePosition.AlignTopRight)
  90. {
  91. x = 0F - (Width / 2F);
  92. y = Height / 2F;
  93. }
  94. // Displays the popup at the bottom left position of the given view.
  95. else if (Position == RelativePosition.AlignBottomLeft)
  96. {
  97. x = 0F - (Width / 2F);
  98. y = Height / 2F;
  99. }
  100. // Displays the popup at the bottom right position of the given view.
  101. else if (Position == RelativePosition.AlignBottomRight)
  102. {
  103. x = 0F - (Width / 2F);
  104. y = 0F - (Height / 2F);
  105. }
  106. }
  107. }
  108. }