[NUI] Add constructor with style instance
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / MenuItem.cs
1 /*
2  * Copyright(c) 2022 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17 using System;
18 using System.ComponentModel;
19 using System.Diagnostics.CodeAnalysis;
20 using Tizen.NUI.BaseComponents;
21
22 namespace Tizen.NUI.Components
23 {
24     /// <summary>
25     /// MenuItem is a class which is used to show a list of items in Menu.
26     /// </summary>
27     /// <since_tizen> 9 </since_tizen>
28     public class MenuItem : SelectButton
29     {
30         private bool selectedAgain = false;
31
32         /// <summary>
33         /// Creates a new instance of MenuItem.
34         /// </summary>
35         /// <since_tizen> 9 </since_tizen>
36         public MenuItem()
37         {
38             Initialize();
39         }
40
41         /// <summary>
42         /// Creates a new instance of MenuItem.
43         /// </summary>
44         /// <param name="style">Creates MenuItem by special style defined in UX.</param>
45         [EditorBrowsable(EditorBrowsableState.Never)]
46         public MenuItem(string style) : base(style)
47         {
48             Initialize();
49         }
50
51         /// <summary>
52         /// Creates a new instance of a MenuItem with style.
53         /// </summary>
54         /// <param name="style">A style applied to the newly created MenuItem.</param>
55         [EditorBrowsable(EditorBrowsableState.Never)]
56         public MenuItem(ButtonStyle style) : base(style)
57         {
58             Initialize();
59         }
60
61         /// <inheritdoc/>
62         [EditorBrowsable(EditorBrowsableState.Never)]
63         protected override void Dispose(DisposeTypes type)
64         {
65             if (disposed)
66             {
67                 return;
68             }
69
70             if (type == DisposeTypes.Explicit)
71             {
72             }
73
74             base.Dispose(type);
75         }
76
77         /// <inheritdoc/>
78         [EditorBrowsable(EditorBrowsableState.Never)]
79         public override bool OnKey(Key key)
80         {
81             if ((IsEnabled == false) || (key == null))
82             {
83                 return false;
84             }
85
86             if (key.State == Key.StateType.Up)
87             {
88                 if (key.KeyPressedName == "Return")
89                 {
90                     if (IsSelected == true)
91                     {
92                         selectedAgain = true;
93                     }
94                 }
95             }
96
97             bool ret = base.OnKey(key);
98
99             if (selectedAgain == true)
100             {
101                 IsSelected = true;
102                 selectedAgain = false;
103             }
104
105             return ret;
106         }
107
108         /// <inheritdoc/>
109         [EditorBrowsable(EditorBrowsableState.Never)]
110         protected override bool HandleControlStateOnTouch(Touch touch)
111         {
112             if ((IsEnabled == false) || (touch == null))
113             {
114                 return false;
115             }
116
117             PointStateType state = touch.GetState(0);
118             switch (state)
119             {
120                 case PointStateType.Up:
121                     if (IsSelected == true)
122                     {
123                         selectedAgain = true;
124                     }
125                     break;
126                 default:
127                     break;
128             }
129
130             bool ret = base.HandleControlStateOnTouch(touch);
131
132             if (selectedAgain == true)
133             {
134                 IsSelected = true;
135                 selectedAgain = false;
136             }
137
138             return ret;
139         }
140
141         /// <inheritdoc/>
142         [SuppressMessage("Microsoft.Design",
143                          "CA1062: Validate arguments of public methods",
144                          MessageId = "controlStateChangedInfo",
145                          Justification = "OnControlStateChanged is called when controlState is changed so controlStateChangedInfo cannot be null.")]
146         [EditorBrowsable(EditorBrowsableState.Never)]
147         protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
148         {
149             if (controlStateChangedInfo.PreviousState.Contains(ControlState.Selected) != controlStateChangedInfo.CurrentState.Contains(ControlState.Selected))
150             {
151                 // MenuItem does not invoke SelectedChanged if button or key is
152                 // unpressed while its state is selected.
153                 if (selectedAgain == true)
154                 {
155                     return;
156                 }
157
158                 base.OnControlStateChanged(controlStateChangedInfo);
159             }
160         }
161
162         private void Initialize()
163         {
164             Layout = new LinearLayout()
165             {
166                 LinearOrientation = LinearLayout.Orientation.Horizontal,
167                 HorizontalAlignment = HorizontalAlignment.Begin,
168                 VerticalAlignment = VerticalAlignment.Center,
169             };
170         }
171
172         /// <summary>
173         /// Initialize AT-SPI object.
174         /// </summary>
175         [EditorBrowsable(EditorBrowsableState.Never)]
176         public override void OnInitialize()
177         {
178             base.OnInitialize();
179             AccessibilityRole = Role.MenuItem;
180         }
181     }
182 }