[NUI] fix button IsEnabled and remove return value of OnEnabled
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Button.Internal.cs
1 /*
2  * Copyright(c) 2021 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
18 using System;
19 using System.ComponentModel;
20 using System.Diagnostics;
21 using Tizen.NUI.BaseComponents;
22 using Tizen.NUI.Components.Extension;
23 using Tizen.NUI.Accessibility; // To use AccessibilityManager
24
25 namespace Tizen.NUI.Components
26 {
27     public partial class Button
28     {
29         private ImageView overlayImage;
30         private TextLabel buttonText;
31         private ImageView buttonIcon;
32         private Vector2 size;
33
34         private EventHandler<StateChangedEventArgs> stateChangeHandler;
35
36         private bool isPressed = false;
37         private bool styleApplied = false;
38
39         /// <summary>
40         /// Get accessibility name.
41         /// </summary>
42         [EditorBrowsable(EditorBrowsableState.Never)]
43         protected override string AccessibilityGetName()
44         {
45             return Text;
46         }
47
48         /// <summary>
49         /// Prevents from showing child widgets in AT-SPI tree.
50         /// </summary>
51         [EditorBrowsable(EditorBrowsableState.Never)]
52         protected override bool AccessibilityShouldReportZeroChildren()
53         {
54             return true;
55         }
56
57         /// <summary>
58         /// The ButtonExtension instance that is injected by ButtonStyle.
59         /// </summary>
60         [EditorBrowsable(EditorBrowsableState.Never)]
61         protected ButtonExtension Extension { get; set; }
62
63         /// <summary>
64         /// Creates Button's text part.
65         /// </summary>
66         /// <return>The created Button's text part.</return>
67         [EditorBrowsable(EditorBrowsableState.Never)]
68         protected virtual TextLabel CreateText()
69         {
70             return new TextLabel(new TextLabelStyle())
71             {
72                 HorizontalAlignment = HorizontalAlignment.Center,
73                 VerticalAlignment = VerticalAlignment.Center,
74                 AccessibilityHighlightable = false,
75             };
76         }
77
78         /// <summary>
79         /// Creates Button's icon part.
80         /// </summary>
81         /// <return>The created Button's icon part.</return>
82         [EditorBrowsable(EditorBrowsableState.Never)]
83         protected virtual ImageView CreateIcon()
84         {
85             return new ImageView()
86             {
87                 AccessibilityHighlightable = false
88             };
89         }
90
91         /// <summary>
92         /// Creates Button's overlay image part.
93         /// </summary>
94         /// <return>The created Button's overlay image part.</return>
95         [EditorBrowsable(EditorBrowsableState.Never)]
96         protected virtual ImageView CreateOverlayImage()
97         {
98             return new ImageView
99             {
100                 PositionUsesPivotPoint = true,
101                 ParentOrigin = NUI.ParentOrigin.Center,
102                 PivotPoint = NUI.PivotPoint.Center,
103                 WidthResizePolicy = ResizePolicyType.FillToParent,
104                 HeightResizePolicy = ResizePolicyType.FillToParent,
105                 AccessibilityHighlightable = false
106             };
107         }
108
109         /// <summary>
110         /// Called when the Button is Clicked by a user
111         /// </summary>
112         /// <param name="eventArgs">The click information.</param>
113         [EditorBrowsable(EditorBrowsableState.Never)]
114         protected virtual void OnClicked(ClickedEventArgs eventArgs)
115         {
116         }
117
118         /// <summary>
119         /// Get Button style.
120         /// </summary>
121         /// <returns>The default button style.</returns>
122         /// <since_tizen> 8 </since_tizen>
123         protected override ViewStyle CreateViewStyle()
124         {
125             return new ButtonStyle();
126         }
127
128         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
129         [EditorBrowsable(EditorBrowsableState.Never)]
130         protected override void OnUpdate()
131         {
132             base.OnUpdate();
133             Extension?.OnRelayout(this);
134         }
135
136         /// <inheritdoc/>
137         [EditorBrowsable(EditorBrowsableState.Never)]
138         protected override bool HandleControlStateOnTouch(Touch touch)
139         {
140             if (!IsEnabled || null == touch)
141             {
142                 return false;
143             }
144
145             PointStateType state = touch.GetState(0);
146
147             switch (state)
148             {
149                 case PointStateType.Down:
150                     isPressed = true;
151                     Extension?.SetTouchInfo(touch);
152                     UpdateState();
153                     return true;
154                 case PointStateType.Interrupted:
155                     isPressed = false;
156                     UpdateState();
157                     return true;
158                 case PointStateType.Up:
159                     {
160                         if (!isPressed)
161                         {
162                             return false;
163                         }
164
165                         isPressed = false;
166
167                         if (IsSelectable)
168                         {
169                             Extension?.SetTouchInfo(touch);
170                             IsSelected = !IsSelected;
171                         }
172                         else
173                         {
174                             Extension?.SetTouchInfo(touch);
175                             UpdateState();
176                         }
177
178                         ClickedEventArgs eventArgs = new ClickedEventArgs();
179                         OnClickedInternal(eventArgs);
180
181                         return true;
182                     }
183                 default:
184                     break;
185             }
186             return base.HandleControlStateOnTouch(touch);
187         }
188
189
190         /// <inheritdoc/>
191         [EditorBrowsable(EditorBrowsableState.Never)]
192         protected override void OnEnabled(bool enabled)
193         {
194             base.OnEnabled(enabled);
195             //Sensitive = false;
196             UpdateState();
197         }
198
199         /// <summary>
200         /// Update Button State.
201         /// </summary>
202         /// <since_tizen> 6 </since_tizen>
203         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
204         [EditorBrowsable(EditorBrowsableState.Never)]
205         protected void UpdateState()
206         {
207             if (!styleApplied) return;
208
209             ControlState sourceState = ControlState;
210             ControlState targetState;
211
212             // Normal, Disabled
213             targetState = IsEnabled ? ControlState.Normal : ControlState.Disabled;
214
215             // Selected, DisabledSelected
216             if (IsSelected) targetState += ControlState.Selected;
217
218             // Pressed, PressedSelected
219             if (isPressed) targetState += ControlState.Pressed;
220
221             // Focused, FocusedPressed, FocusedPressedSelected, DisabledFocused, DisabledSelectedFocused
222             if (IsFocused) targetState += ControlState.Focused;
223
224             if (sourceState != targetState)
225             {
226                 ControlState = targetState;
227                 OnUpdate();
228
229                 StateChangedEventArgs e = new StateChangedEventArgs
230                 {
231                     PreviousState = ControlStatesExtension.FromControlStateClass(sourceState),
232                     CurrentState = ControlStatesExtension.FromControlStateClass(targetState)
233                 };
234                 stateChangeHandler?.Invoke(this, e);
235
236                 Extension?.OnControlStateChanged(this, new ControlStateChangedEventArgs(sourceState, targetState));
237             }
238         }
239
240         /// <summary>
241         /// Dispose Button and all children on it.
242         /// </summary>
243         /// <param name="type">Dispose type.</param>
244         /// <since_tizen> 6 </since_tizen>
245         protected override void Dispose(DisposeTypes type)
246         {
247             if (disposed)
248             {
249                 return;
250             }
251
252             if (type == DisposeTypes.Explicit)
253             {
254                 Extension?.OnDispose(this);
255
256                 if (buttonIcon != null)
257                 {
258                     Utility.Dispose(buttonIcon);
259                 }
260                 if (buttonText != null)
261                 {
262                     Utility.Dispose(buttonText);
263                 }
264                 if (overlayImage != null)
265                 {
266                     Utility.Dispose(overlayImage);
267                 }
268             }
269
270             base.Dispose(type);
271         }
272
273         /// <summary>
274         /// Initializes AT-SPI object.
275         /// </summary>
276         [EditorBrowsable(EditorBrowsableState.Never)]
277         public override void OnInitialize()
278         {
279             base.OnInitialize();
280             SetAccessibilityConstructor(Role.PushButton);
281
282             AccessibilityHighlightable = true;
283             EnableControlStatePropagation = true;
284
285             AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Trait, "Button");
286
287             buttonText = CreateText();
288             buttonIcon = CreateIcon();
289             LayoutItems();
290
291 #if PROFILE_MOBILE
292             Feedback = true;
293 #endif
294         }
295
296         /// <inheritdoc/>
297         [EditorBrowsable(EditorBrowsableState.Never)]
298         public override void OnRelayout(Vector2 size, RelayoutContainer container)
299         {
300             if (size == null) return;
301
302             if (size.Equals(this.size))
303             {
304                 return;
305             }
306
307             this.size = new Vector2(size);
308
309             UpdateSizeAndSpacing();
310         }
311
312         /// <inheritdoc/>
313         [EditorBrowsable(EditorBrowsableState.Never)]
314         protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
315         {
316             base.OnControlStateChanged(controlStateChangedInfo);
317
318             var stateEnabled = !controlStateChangedInfo.CurrentState.Contains(ControlState.Disabled);
319
320             if (IsEnabled != stateEnabled)
321             {
322                 IsEnabled = stateEnabled;
323             }
324
325             var statePressed = controlStateChangedInfo.CurrentState.Contains(ControlState.Pressed);
326
327             if (isPressed != statePressed)
328             {
329                 isPressed = statePressed;
330             }
331         }
332
333         /// <summary>
334         /// Put sub items (e.g. buttonText, buttonIcon) to the right place.
335         /// </summary>
336         [EditorBrowsable(EditorBrowsableState.Never)]
337         protected virtual void LayoutItems()
338         {
339             if (buttonIcon == null || buttonText == null)
340             {
341                 return;
342             }
343
344             buttonIcon.Unparent();
345             buttonText.Unparent();
346             overlayImage?.Unparent();
347
348 #pragma warning disable CA2000
349             Size2D cellPadding = String.IsNullOrEmpty(buttonText.Text) ? new Size2D(0, 0) : itemSpacing;
350 #pragma warning restore CA2000
351
352             if (IconRelativeOrientation == IconOrientation.Left)
353             {
354                 Layout = new LinearLayout()
355                 {
356                     LinearOrientation = LinearLayout.Orientation.Horizontal,
357                     LinearAlignment = itemAlignment,
358                     CellPadding = cellPadding
359                 };
360
361                 Add(buttonIcon);
362                 Add(buttonText);
363             }
364             else if (IconRelativeOrientation == IconOrientation.Right)
365             {
366                 Layout = new LinearLayout()
367                 {
368                     LinearOrientation = LinearLayout.Orientation.Horizontal,
369                     LinearAlignment = itemAlignment,
370                     CellPadding = cellPadding
371                 };
372
373                 Add(buttonText);
374                 Add(buttonIcon);
375             }
376             else if (IconRelativeOrientation == IconOrientation.Top)
377             {
378                 Layout = new LinearLayout()
379                 {
380                     LinearOrientation = LinearLayout.Orientation.Vertical,
381                     LinearAlignment = itemAlignment,
382                     CellPadding = cellPadding
383                 };
384
385                 Add(buttonIcon);
386                 Add(buttonText);
387             }
388             else if (IconRelativeOrientation == IconOrientation.Bottom)
389             {
390                 Layout = new LinearLayout()
391                 {
392                     LinearOrientation = LinearLayout.Orientation.Vertical,
393                     LinearAlignment = itemAlignment,
394                     CellPadding = cellPadding
395                 };
396
397                 Add(buttonText);
398                 Add(buttonIcon);
399             }
400
401             if (overlayImage != null)
402             {
403                 overlayImage.ExcludeLayouting = true;
404                 Add(overlayImage);
405             }
406         }
407
408         private void UpdateSizeAndSpacing()
409         {
410             if (size == null || buttonIcon == null || buttonText == null)
411             {
412                 return;
413             }
414
415             LinearLayout layout = Layout as LinearLayout;
416
417             if (layout == null)
418             {
419                 return;
420             }
421
422             float lengthWithoutText = 0;
423             Size2D cellPadding = null;
424             Extents iconMargin = buttonIcon.Margin ?? new Extents(0);
425             Extents textMargin = buttonText.Margin ?? new Extents(0);
426
427             if (buttonIcon.Size.Width != 0 && buttonIcon.Size.Height != 0)
428             {
429                 lengthWithoutText = buttonIcon.Size.Width;
430
431                 if (!String.IsNullOrEmpty(buttonText.Text))
432                 {
433                     cellPadding = itemSpacing;
434
435                     if (iconRelativeOrientation == IconOrientation.Left || iconRelativeOrientation == IconOrientation.Right)
436                     {
437                         lengthWithoutText += (itemSpacing?.Width ?? 0) + iconMargin.Start + iconMargin.End + textMargin.Start + textMargin.End;
438                     }
439                     else
440                     {
441                         lengthWithoutText += (itemSpacing?.Height ?? 0) + iconMargin.Top + iconMargin.Bottom + textMargin.Top + textMargin.Bottom;
442                     }
443                 }
444             }
445
446             layout.CellPadding = cellPadding ?? new Size2D(0, 0);
447
448             // If the button has fixed width and the text is not empty, the text should not exceed button boundary.
449             if (WidthSpecification != LayoutParamPolicies.WrapContent && !String.IsNullOrEmpty(buttonText.Text))
450             {
451                 buttonText.MaximumSize = new Size2D((int)Math.Max(size.Width - lengthWithoutText, Math.Max(buttonText.MinimumSize.Width, 1)), (int)size.Height);
452             }
453         }
454
455         private void OnClickedInternal(ClickedEventArgs eventArgs)
456         {
457             Command?.Execute(CommandParameter);
458             OnClicked(eventArgs);
459             Extension?.OnClicked(this, eventArgs);
460
461             ClickEventArgs nestedEventArgs = new ClickEventArgs();
462             ClickEvent?.Invoke(this, nestedEventArgs);
463             Clicked?.Invoke(this, eventArgs);
464         }
465     }
466 }