MobileMenuButton.xaml.cs 4.6 KB

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