[NUI] Fix TabButton not to invoke duplicate SelectedChanged (#2775)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / TabButton.cs
1 /*
2  * Copyright(c) 2020 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.ComponentModel;
18 using System.Diagnostics.CodeAnalysis;
19 using Tizen.NUI.BaseComponents;
20
21 namespace Tizen.NUI.Components
22 {
23     /// <summary>
24     /// TabButton is a class which is used for selecting one content in a TabView.
25     /// </summary>
26     [EditorBrowsable(EditorBrowsableState.Never)]
27     public class TabButton : SelectButton
28     {
29         private bool selectedAgain = false;
30
31         /// <summary>
32         /// Creates a new instance of TabButton.
33         /// </summary>
34         [EditorBrowsable(EditorBrowsableState.Never)]
35         public TabButton()
36         {
37             GridLayout.SetHorizontalStretch(this, GridLayout.StretchFlags.ExpandAndFill);
38         }
39
40         /// <inheritdoc/>
41         [EditorBrowsable(EditorBrowsableState.Never)]
42         public override bool OnKey(Key key)
43         {
44             if ((IsEnabled == false) || (key == null))
45             {
46                 return false;
47             }
48
49             if (key.State == Key.StateType.Up)
50             {
51                 if (key.KeyPressedName == "Return")
52                 {
53                     if (IsSelected == true)
54                     {
55                         selectedAgain = true;
56                     }
57                 }
58             }
59
60             bool ret = base.OnKey(key);
61
62             if (selectedAgain == true)
63             {
64                 IsSelected = true;
65                 selectedAgain = false;
66             }
67
68             return ret;
69         }
70
71         /// <inheritdoc/>
72         [EditorBrowsable(EditorBrowsableState.Never)]
73         protected override bool HandleControlStateOnTouch(Touch touch)
74         {
75             if ((IsEnabled == false) || (touch == null))
76             {
77                 return false;
78             }
79
80             PointStateType state = touch.GetState(0);
81             switch (state)
82             {
83                 case PointStateType.Up:
84                     if (IsSelected == true)
85                     {
86                         selectedAgain = true;
87                     }
88                     break;
89                 default:
90                     break;
91             }
92
93             bool ret = base.HandleControlStateOnTouch(touch);
94
95             if (selectedAgain == true)
96             {
97                 IsSelected = true;
98                 selectedAgain = false;
99             }
100
101             return ret;
102         }
103
104         /// <inheritdoc/>
105         [SuppressMessage("Microsoft.Design",
106                          "CA1062: Validate arguments of public methods",
107                          MessageId = "controlStateChangedInfo",
108                          Justification = "OnControlStateChanged is called when controlState is changed so controlStateChangedInfo cannot be null.")]
109         [EditorBrowsable(EditorBrowsableState.Never)]
110         protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
111         {
112             if (controlStateChangedInfo.PreviousState.Contains(ControlState.Selected) != controlStateChangedInfo.CurrentState.Contains(ControlState.Selected))
113             {
114                 // TabButton does not invoke SelectedChanged if button or key is
115                 // unpressed while its state is selected.
116                 if (selectedAgain == true)
117                 {
118                     return;
119                 }
120
121                 base.OnControlStateChanged(controlStateChangedInfo);
122             }
123         }
124     }
125 }