MobileMenuButton.xaml.cs 4.5 KB

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