瀏覽代碼

Fixed Broken Popup Menus

Frank van den Bos 1 年之前
父節點
當前提交
d0aca24e44

+ 1 - 3
InABox.Client.RPC/Transports/Socket/RPCClientSocketTransport.cs

@@ -249,9 +249,7 @@ namespace InABox.Rpc
                         childCts.Cancel();
 
                         if(_socket != null)
-                        {
-                            throw new Exception("Socket already exists!");
-                        }
+                            Logger.Send(LogType.Error,"","Socket already exists!");
 
                         _socket = result.Result;
                         _connected = true;

+ 1 - 1
InABox.Core/DigitalForms/DigitalFormDocumentFactory.cs

@@ -95,7 +95,7 @@ namespace InABox.Core
                 () =>
                 {
                     bool? previouslyActive = null;
-                    while (!_cancel.IsCancellationRequested)
+                    while (_cancel?.IsCancellationRequested != true)
                     {
                         var file = Directory.EnumerateFiles(CachePath, "*.formdocument")
                             .FirstOrDefault();

+ 13 - 22
InABox.Mobile/InABox.Mobile.Shared/Components/MobileDateSelector/MobileDateButton.xaml.cs

@@ -56,8 +56,6 @@ namespace InABox.Mobile
     [XamlCompilation(XamlCompilationOptions.Compile)]
     public partial class MobileDateButton
     {
-        private SfPopupLayout popup;
-        
         public event DateButtonChanged Changed;
         
         public static readonly BindableProperty PromptProperty = BindableProperty.Create(
@@ -171,38 +169,35 @@ namespace InABox.Mobile
         public MobileDateButton()
         {
             InitializeComponent();
-            popup = new SfPopupLayout();
         }
 
         private void _frame_OnClicked(object sender, EventArgs e)
         {
-            popup.PopupView.WidthRequest = 300;
-            popup.PopupView.HeightRequest = 500;
-            popup.PopupView.ShowHeader = false;
-            popup.PopupView.ShowFooter = false;
-            //popup.PopupView.PopupStyle.HasShadow = false;
-            popup.PopupView.PopupStyle.CornerRadius = 5;
-            //popup.PopupView.Background = new SolidColorBrush(Color.White);
             MobileDateSelector popupContent = new MobileDateSelector();
             popupContent.Date = Date;
             popupContent.Changed += (o, args) =>
             {
                 Date = args.Date;
                 DoChanged();
-                popup.Closing -= RestrictClose;
-                popup.Dismiss();
+                PopupManager.DismissPopup();
             };
             popupContent.Cancelled += (o, args) =>
             {
-                popup.Closing -= RestrictClose;
-                popup.Dismiss();
+                PopupManager.DismissPopup();
             };
-            popup.Closing += RestrictClose;
-            popupContent.Margin = new Thickness(10);
             popupContent.HorizontalOptions = LayoutOptions.Fill;
             popupContent.VerticalOptions = LayoutOptions.Fill;
-            popup.PopupView.ContentTemplate = new DataTemplate(() => popupContent);
-            popup.Show();
+            
+            PopupManager.ShowPopup(
+                this, 
+                () => popupContent, 
+                new PopupManagerConfiguration()
+                {
+                    Modal = true,
+                    RequestedHeight = 500,
+                    RequestedWidth = 350
+                }
+            );   
         }
 
         protected virtual void DoChanged()
@@ -210,10 +205,6 @@ namespace InABox.Mobile
             Changed?.Invoke(this,new DateButtonChangedArgs(Date));
         }
 
-        private void RestrictClose(object sender, CancelEventArgs e)
-        {
-            e.Cancel = true;
-        }
 
         private void DateTimeFormatter_OnPropertyChanged(object sender, PropertyChangedEventArgs e)
         {

+ 12 - 73
InABox.Mobile/InABox.Mobile.Shared/Components/MobileMenuButton/MobileMenuButton.xaml.cs

@@ -13,8 +13,6 @@ namespace InABox.Mobile
     [XamlCompilation(XamlCompilationOptions.Compile)]
     public partial class MobileMenuButton
     {
-        
-        private SfPopupLayout _popup;
 
         private MobileMenuButtonMenu _menu;
 
@@ -59,10 +57,9 @@ namespace InABox.Mobile
         public MobileMenuButton()
         {
             _menu = new MobileMenuButtonMenu();
-            _menu.ItemClicked += (sender,args) => _popup.Dismiss();
+            _menu.ItemClicked += (sender,args) => PopupManager.DismissPopup();
             InitializeComponent();
             ShowImages = false;
-            _popup = new SfPopupLayout();
             Position = RelativePosition.AlignToLeftOf;
         }
 
@@ -71,80 +68,22 @@ namespace InABox.Mobile
             Appearing?.Invoke(this, EventArgs.Empty);
             if (_menu.Items.Any())
             {
-                _popup.PopupView.ContentTemplate = new DataTemplate(() => _menu);
-                _popup.PopupView.ShowFooter = false;
-                _popup.PopupView.ShowHeader = false;
-                _popup.PopupView.AutoSizeMode = AutoSizeMode.Both;
-                _popup.PopupView.PopupStyle.CornerRadius = 5;
-                _popup.Padding = new Thickness(5);
-                GetOffset(out double x, out double y);
-                
-                _popup.ShowRelativeToView(this, Position, x, y);
+                PopupManager.ShowPopup(
+                    this,
+                    () => _menu, 
+                    new PopupManagerConfiguration()
+                    {
+                        SizeMode = AutoSizeMode.Both,
+                        Padding = 5,
+                        Position = this.Position
+                    }
+                );
             }
             else
                 Clicked?.Invoke(this, new MobileMenuButtonClickedEventArgs(null));
         }
 
-        /// <summary>
-        /// Calculates the offest of the Menu to position it at the center of the Button
-        ///  Let's not presume that all the calculations are correct - its only been tested
-        /// against AlignToLeftOf (for top-right-hand side menu options 
-        /// </summary>
-        /// <param name="x"></param>
-        /// <param name="y"></param>
-        private void GetOffset(out double x, out double y)
-        {
-            x = 0F;
-            y = 0F;
-            // Displays the popup at the top of the given view.
-            if (Position == RelativePosition.AlignTop)
-            {
-                x = Width / 2F;
-                y = Height / 2F;
-            }
-            // Displays the popup to the left of the given view.</summary>
-            else if (Position == RelativePosition.AlignToLeftOf)
-            {
-                x = Width / 2F;
-                y = Height / 2F;
-            }
-            // Displays the popup to the right of the given view.</summary>
-            else if (Position == RelativePosition.AlignToRightOf)
-            {
-                x = 0F - (Width / 2F);
-                y = Height / 2F;
-            }
-            // Displays the popup at the bottom of the given view.</summary>
-            else if (Position == RelativePosition.AlignBottom)
-            {
-                x = Width / 2F;
-                y = 0F - (Height / 2F);
-            }
-            // Displays the popup at the top left position of the given view.
-            else if (Position == RelativePosition.AlignTopLeft)
-            {
-                x = Width / 2F;
-                y = Height / 2F;
-            }
-            // Displays the popup at the top right position of the given view.
-            else if (Position == RelativePosition.AlignTopRight)
-            {
-                x = 0F - (Width / 2F);
-                y = Height / 2F;
-            }
-            // Displays the popup at the bottom left position of the given view.
-            else if (Position == RelativePosition.AlignBottomLeft)
-            {
-                x = 0F - (Width / 2F);
-                y = Height / 2F;
-            }
-            // Displays the popup at the bottom right position of the given view.
-            else if (Position == RelativePosition.AlignBottomRight)
-            {
-                x = 0F - (Width / 2F);
-                y = 0F - (Height / 2F);
-            }
-        }
+ 
     }
     
 }

+ 0 - 4
InABox.Mobile/InABox.Mobile.Shared/Components/MobilePage/MobilePage.xaml.cs

@@ -35,10 +35,6 @@ namespace InABox.Mobile
             get => _backButton.IsVisible;
             set => _backButton.IsVisible = value;
         }
-
-        //public event BackButtonClickedEvent BackButtonClicked;
-
-        private SfPopupLayout _popup = null;
         
         public MobilePage()
         {

+ 12 - 0
InABox.Mobile/InABox.Mobile.Shared/Components/MobileTabStrip/MobileTabStripItem.cs

@@ -53,6 +53,18 @@ namespace InABox.Mobile
             set => SetValue(IndexProperty, value);
         }
         
+        // private readonly BindableProperty IsVisibleProperty = BindableProperty.Create(
+        //     nameof(IsVisible),
+        //     typeof(bool),
+        //     typeof(MobileTabStripItem),
+        //     true);
+        //
+        // public bool IsVisible
+        // {
+        //     get => (bool)GetValue(IsVisibleProperty);
+        //     set => SetValue(IsVisibleProperty, value);
+        // }
+        
         
     }
 }

+ 1 - 8
InABox.Mobile/InABox.Mobile.Shared/Components/MobileTimeSelector/MobileTimeButton.xaml.cs

@@ -212,14 +212,7 @@ namespace InABox.Mobile
                     RequestedWidth = 350
                 }
             );            
-            // popup.PopupView.WidthRequest = 300;
-            // popup.PopupView.HeightRequest = 500;
-            // popup.PopupView.ShowHeader = false;
-            // popup.PopupView.ShowFooter = false;
-            // popup.PopupView.PopupStyle.CornerRadius = 5;
-            // popup.Closing += RestrictClose;
-            // popup.PopupView.ContentTemplate = new DataTemplate(() => popupContent);
-            // popup.Show();
+
         }
 
         protected virtual void DoChanged()

+ 96 - 14
InABox.Mobile/InABox.Mobile.Shared/PopupManager.cs

@@ -12,21 +12,45 @@ namespace InABox.Mobile
 
     public class PopupManagerConfiguration
     {
-        public int RequestedHeight { get; set; } = 700;
-        public int RequestedWidth { get; set; } = 350;
+
+        public static double? DefaultHeight => 700;
+        public static double? DefaultWidth => 350;
+        public static double? AutoSize => null;
+        
+        public static RelativePosition? CenterOnScreen => null;
+        
+        public double? RequestedHeight { get; set; } = DefaultHeight;
+        public double? RequestedWidth { get; set; } = DefaultWidth;
+        
+        public AutoSizeMode SizeMode
+        {
+            get => (Equals(RequestedHeight,AutoSize) && Equals(RequestedWidth,AutoSize))
+                ? AutoSizeMode.Both
+                : Equals(RequestedHeight,AutoSize)
+                    ? AutoSizeMode.Height
+                    : Equals(RequestedWidth,AutoSize)
+                        ? AutoSizeMode.Width
+                        : AutoSizeMode.None;
+            set
+            {
+                RequestedHeight = value is AutoSizeMode.Both or AutoSizeMode.Height ? AutoSize : DefaultHeight;
+                RequestedWidth = value is AutoSizeMode.Both or AutoSizeMode.Width ? AutoSize : DefaultWidth;
+            }
+        }
+        
         public bool ShowHeader { get; set; } = false;
         public bool ShowFooter { get; set; } = false;
         public int Padding { get; set; } = 10;
-
         public int CornerRadius { get; set; } = 5;
         public bool Modal { get; set; } = false;
+
+        public RelativePosition? Position { get; set; } = CenterOnScreen;
     }
     
     public static class PopupManager
     {
         
         private static SfPopupLayout _popup = null;
-
         
         private static ContentPage FindParentPage(Element view)
         {
@@ -45,15 +69,17 @@ namespace InABox.Mobile
             }); 
         }
 
-        public static void ShowPopup(Element parent, Func<View> view, PopupManagerConfiguration config = null)
+        public static bool Initialise()
         {
+            var service = DependencyService.Get<IPopupManager>();
+            return service?.Initialise() == true;
+        }
 
-            if (_popup == null)
-            {
-                var service = DependencyService.Get<IPopupManager>();
-                if (service.Initialise())
-                    _popup = new SfPopupLayout();
-            }
+        public static void ShowPopup(VisualElement parent, Func<View> view, PopupManagerConfiguration config = null)
+        {
+            
+            if (_popup == null && Initialise())
+                _popup = new SfPopupLayout();
 
             if (_popup == null)
             {
@@ -63,8 +89,18 @@ namespace InABox.Mobile
 
             var cfg = config ?? new PopupManagerConfiguration();
             
-            _popup.PopupView.HeightRequest = cfg.RequestedHeight;
-            _popup.PopupView.WidthRequest = cfg.RequestedWidth;
+            _popup.PopupView.AutoSizeMode = cfg.SizeMode;
+
+            if (Equals(cfg.RequestedHeight, PopupManagerConfiguration.AutoSize))
+                _popup.PopupView.ClearValue(VisualElement.HeightRequestProperty);
+            else
+                _popup.PopupView.HeightRequest = cfg.RequestedHeight!.Value;
+            
+            if (Equals(cfg.RequestedWidth, PopupManagerConfiguration.AutoSize))
+                _popup.PopupView.ClearValue(VisualElement.WidthRequestProperty);
+            else
+                _popup.PopupView.WidthRequest = cfg.RequestedWidth!.Value;
+            
             _popup.PopupView.ShowHeader = cfg.ShowHeader;
             _popup.PopupView.ShowFooter = cfg.ShowFooter;
             _popup.PopupView.PopupStyle.CornerRadius = cfg.CornerRadius;
@@ -77,7 +113,53 @@ namespace InABox.Mobile
                 return grid;
             });
             
-            _popup.Show();
+            if (parent is View pView && cfg.Position != PopupManagerConfiguration.CenterOnScreen)
+            {
+                var offset = GetOffset(parent, cfg.Position!.Value); 
+                _popup.ShowRelativeToView(pView, cfg.Position!.Value, offset.X, offset.Y);
+            }
+            else
+                _popup.Show();
+        }
+        
+        /// <summary>
+        /// Calculates the offest of the Menu to position it at the center of the Button
+        ///  Let's not presume that all the calculations are correct - its only been tested
+        /// against AlignToLeftOf (for top-right-hand side menu options 
+        /// </summary>
+        /// <param name="x"></param>
+        /// <param name="y"></param>
+        private static Point GetOffset(VisualElement parent, RelativePosition Position)
+        {
+            return Position switch
+            {
+
+                // Displays the popup at the top of the given view.
+                RelativePosition.AlignTop => new Point(parent.Width / 2F, parent.Height / 2F),
+
+                // Displays the popup to the left of the given view.</summary>
+                RelativePosition.AlignToLeftOf => new Point(parent.Width / 2F, parent.Height / 2F),
+
+                // Displays the popup to the right of the given view.</summary>
+                RelativePosition.AlignToRightOf => new Point(0F - (parent.Width / 2F), parent.Height / 2F),
+
+                // Displays the popup at the bottom of the given view.</summary>
+                RelativePosition.AlignBottom => new Point(parent.Width / 2F, 0F - (parent.Height / 2F)),
+
+                // Displays the popup at the top left position of the given view.
+                RelativePosition.AlignTopLeft => new Point(parent.Width / 2F, parent.Height / 2F),
+
+                // Displays the popup at the top right position of the given view.
+                RelativePosition.AlignTopRight => new Point(0F - (parent.Width / 2F), parent.Height / 2F),
+
+                // Displays the popup at the bottom left position of the given view.
+                RelativePosition.AlignBottomLeft => new Point(0F - (parent.Width / 2F), parent.Height / 2F),
+
+                // Displays the popup at the bottom right position of the given view.
+                RelativePosition.AlignBottomRight => new Point(0F - (parent.Width / 2F), 0F - (parent.Height / 2F)),
+
+                _ => new Point()
+            };
         }
 
         public static void DismissPopup()