MobileMenuButton.xaml.cs 4.9 KB

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