public bool ShouldSerializePositionY() => SerializeDefaults || !PositionY.Equals(ZPLForgeDefaults.Elements.PositionY);
// Calculate menu's position based on Anchor and parent's positions. // If there is not enought space, then menu's size can be also resized. private void CalculateMenuPosition() { if ((Anchor == null) || (Content == null)) { return; } if (Items == null) { return; } if (SizeWidth.Equals(0) && SizeHeight.Equals(0)) { return; } float menuScreenPosX = 0; float menuScreenPosY = 0; if (HorizontalPositionToAnchor == RelativePosition.Start) { if (GetRootView().LayoutDirection == ViewLayoutDirectionType.LTR) { menuScreenPosX = Anchor.ScreenPosition.X - SizeWidth; } else { menuScreenPosX = Anchor.ScreenPosition.X + Anchor.Margin.Start + Anchor.SizeWidth + Anchor.Margin.End; } } else if (HorizontalPositionToAnchor == RelativePosition.Center) { menuScreenPosX = Anchor.ScreenPosition.X + Anchor.Margin.Start + (Anchor.SizeWidth / 2) - (SizeWidth / 2); } else { if (GetRootView().LayoutDirection == ViewLayoutDirectionType.LTR) { menuScreenPosX = Anchor.ScreenPosition.X + Anchor.Margin.Start + Anchor.SizeWidth + Anchor.Margin.End; } else { menuScreenPosX = Anchor.ScreenPosition.X - SizeWidth; } } if (VerticalPositionToAnchor == RelativePosition.Start) { menuScreenPosY = Anchor.ScreenPosition.Y - SizeHeight; } else if (VerticalPositionToAnchor == RelativePosition.Center) { menuScreenPosY = Anchor.ScreenPosition.Y + Anchor.Margin.Top + (Anchor.SizeHeight / 2) - (SizeHeight / 2); } else { menuScreenPosY = Anchor.ScreenPosition.Y + Anchor.Margin.Top + Anchor.SizeHeight + Anchor.Margin.Bottom; } float menuSizeW = SizeWidth; float menuSizeH = SizeHeight; // Check if menu is not inside parent's boundary in x coordinate system. if (menuScreenPosX + SizeWidth > Window.Size.Width) { if (HorizontalPositionToAnchor == RelativePosition.Center) { menuScreenPosX = Window.Size.Width - SizeWidth; } else { menuSizeW = Window.Size.Width - menuScreenPosX; } } if (menuScreenPosX < 0) { menuScreenPosX = 0; menuSizeW = Window.Size.Width; } // Check if menu is not inside parent's boundary in y coordinate system. if (menuScreenPosY + SizeHeight > Window.Size.Height) { if (VerticalPositionToAnchor == RelativePosition.Center) { menuScreenPosY = Window.Size.Height - SizeHeight; } else { menuSizeH = Window.Size.Height - menuScreenPosY; } } if (menuScreenPosY < 0) { menuScreenPosY = 0; menuSizeH = Window.Size.Height; } // Position is relative to parent's coordinate system. var menuPosX = menuScreenPosX; var menuPosY = menuScreenPosY; if (!PositionX.Equals(menuPosX) || !PositionY.Equals(menuPosY) || !SizeWidth.Equals(menuSizeW) || !SizeHeight.Equals(menuSizeH)) { Position = new Position(menuPosX, menuPosY); Size = new Size(menuSizeW, menuSizeH); } }