2 * Copyright(c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
19 using System.ComponentModel;
20 using Tizen.NUI.BaseComponents;
22 namespace Tizen.NUI.Components
25 /// Menu is a class which contains a set of MenuItems and has one of them selected.
27 [EditorBrowsable(EditorBrowsableState.Never)]
28 public class Menu : Control
30 private ScrollableBase scrollableBase = null;
31 private IEnumerable<MenuItem> menuItems = null;
32 private MenuItemGroup menuItemGroup = null;
33 private Position2D anchorPosition = new Position2D(0, 0);
36 /// Creates a new instance of Menu.
38 [EditorBrowsable(EditorBrowsableState.Never)]
39 public Menu() : base()
45 /// Dispose Menu and all children on it.
47 /// <param name="type">Dispose type.</param>
48 [EditorBrowsable(EditorBrowsableState.Never)]
49 protected override void Dispose(DisposeTypes type)
56 if (type == DisposeTypes.Explicit)
58 if (menuItems != null)
60 foreach (MenuItem menuItem in menuItems)
62 Utility.Dispose(menuItem);
68 if (anchorPosition != null)
70 anchorPosition.Dispose();
71 anchorPosition = null;
74 Utility.Dispose(scrollableBase);
81 /// Menu items in Menu.
83 public IEnumerable<MenuItem> Items
92 if (menuItems != null)
94 foreach (var oldItem in menuItems)
96 if (scrollableBase.Children?.Contains(oldItem) == true)
98 scrollableBase.Children.Remove(oldItem);
105 if (menuItems == null)
110 foreach (var item in menuItems)
112 scrollableBase.Add(item);
113 menuItemGroup.Add(item);
119 /// Anchor position of Menu.
120 /// Menu is displayed at the anchor position.
121 /// If there is no enough space to display menu at the anchor position,
122 /// then menu is displayed at the proper position near anchor position.
124 public Position2D AnchorPosition
128 return anchorPosition;
133 if (anchorPosition == value)
138 anchorPosition = value;
139 if (anchorPosition == null)
144 CalculateSizeAndPosition();
149 public override void OnRelayout(Vector2 size, RelayoutContainer container)
151 base.OnRelayout(size, container);
153 CalculateSizeAndPosition();
156 private void Initialize()
158 Layout = new AbsoluteLayout();
160 WidthSpecification = LayoutParamPolicies.WrapContent;
162 scrollableBase = new ScrollableBase()
164 Layout = new LinearLayout()
166 LinearOrientation = LinearLayout.Orientation.Vertical,
168 ScrollingDirection = ScrollableBase.Direction.Vertical,
169 ScrollEnabled = true,
170 HideScrollbar = false,
174 menuItemGroup = new MenuItemGroup();
177 private void CalculateSizeAndPosition()
179 var parent = GetParent() as View;
181 if ((AnchorPosition == null) || (parent == null))
191 if ((Size2D.Width == 0) && (Size2D.Height == 0))
196 var parentPosition = new Position2D((int)parent.ScreenPosition.X, (int)parent.ScreenPosition.Y);
197 var parentSize = new Size2D(parent.Size2D.Width, parent.Size2D.Height);
199 int menuPosX = AnchorPosition.X;
200 int menuPosY = AnchorPosition.Y;
201 int menuSizeW = Size2D.Width;
202 int menuSizeH = Size2D.Height;
204 // Check if menu is not inside parent's boundary in x coordinate system.
205 if (AnchorPosition.X + Size2D.Width > parentPosition.X + parentSize.Width)
207 menuPosX = parentPosition.X + parentSize.Width - Size2D.Width;
209 if (menuPosX < parentPosition.X)
211 menuPosX = parentPosition.X;
212 menuSizeW = parentSize.Width;
216 // Check if menu is not inside parent's boundary in y coordinate system.
217 if (AnchorPosition.Y + Size2D.Height > parentPosition.Y + parentSize.Height)
219 menuPosY = parentPosition.Y + parentSize.Height - Size2D.Height;
221 if (menuPosY < parentPosition.Y)
223 menuPosY = parentPosition.Y;
224 menuSizeH = parentSize.Height;
228 Position2D = new Position2D(menuPosX, menuPosY);
229 Size2D = new Size2D(menuSizeW, menuSizeH);
231 parentPosition.Dispose();
232 parentSize.Dispose();