Support new features of Tizen.CircularUI (#188)
[platform/core/csapi/xsf.git] / src / XSF / Tizen.Wearable.CircularUI.Forms / ContentButton.cs
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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.Windows.Input;
20 using Xamarin.Forms;
21
22 namespace Tizen.Wearable.CircularUI.Forms
23 {
24     /// <summary>
25     /// The ContentButton is a Button, which allows you to customize the View to be displayed.
26     /// </summary>
27     /// <since_tizen> 4 </since_tizen>
28     public class ContentButton : ContentView, IButtonController
29     {
30         /// <summary>
31         /// BindableProperty. Identifies the Command bindable property.
32         /// </summary>
33         /// <since_tizen> 4 </since_tizen>
34         public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(ContentButton), null, 
35             propertyChanging: OnCommandChanging, propertyChanged: OnCommandChanged);
36
37         /// <summary>
38         /// BindableProperty. Identifies the CommandParameter bindable property.
39         /// </summary>
40         /// <since_tizen> 4 </since_tizen>
41         public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create(nameof(CommandParameter), typeof(object), typeof(ContentButton), null, 
42             propertyChanged: (bindable, oldvalue, newvalue) => CommandCanExcuteChanged(bindable, EventArgs.Empty));
43
44         /// <summary>
45         /// Gets or sets command that is executed when the button is clicked.
46         /// </summary>
47         /// <since_tizen> 4 </since_tizen>
48         public ICommand Command
49         {
50             get => (ICommand)GetValue(CommandProperty);
51             set => SetValue(CommandProperty, value);
52         }
53
54         /// <summary>
55         /// Gets or sets command paramter that is executed when the button is clicked.
56         /// </summary>
57         /// <since_tizen> 4 </since_tizen>
58         public object CommandParameter
59         {
60             get => GetValue(CommandParameterProperty);
61             set => SetValue(CommandParameterProperty, value);
62         }
63
64         /// <summary>
65         /// Occurs when the button is clicked.
66         /// </summary>
67         /// <since_tizen> 4 </since_tizen>
68         public event EventHandler Clicked;
69
70         /// <summary>
71         /// Occurs when the button is pressed.
72         /// </summary>
73         /// <since_tizen> 4 </since_tizen>
74         public event EventHandler Pressed;
75
76         /// <summary>
77         /// Occurs when the button is released.
78         /// </summary>
79         /// <since_tizen> 4 </since_tizen>
80         public event EventHandler Released;
81
82         bool IsEnabledCore
83         {
84             set => SetValueCore(IsEnabledProperty, value);
85         }
86
87         /// <summary>
88         /// For internal use.
89         /// </summary>
90         [EditorBrowsable(EditorBrowsableState.Never)]
91         public void SendClicked()
92         {
93             if (IsEnabled)
94             {
95                 Command?.Execute(CommandParameter);
96                 Clicked?.Invoke(this, EventArgs.Empty);
97             }
98         }
99
100         /// <summary>
101         /// For internal use.
102         /// </summary>
103         [EditorBrowsable(EditorBrowsableState.Never)]
104         public void SendPressed()
105         {
106             if (IsEnabled)
107             {
108                 Pressed?.Invoke(this, EventArgs.Empty);
109             }
110         }
111
112         /// <summary>
113         /// For internal use.
114         /// </summary>
115         [EditorBrowsable(EditorBrowsableState.Never)]
116         public void SendReleased()
117         {
118             if (IsEnabled)
119             {
120                 Released?.Invoke(this, EventArgs.Empty);
121             }
122         }
123
124         protected override void OnBindingContextChanged()
125         {
126             base.OnBindingContextChanged();
127
128             View content = Content;
129             if (content != null)
130             {
131                 SetInheritedBindingContext(content, BindingContext);
132             }
133         }
134
135         static void OnCommandChanged(BindableObject bindable, object oldCommand, object newCommand)
136         {
137             ContentButton button = (ContentButton)bindable;
138             if (newCommand is ICommand command)
139             {
140                 command.CanExecuteChanged += button.OnCommandCanExecuteChanged;
141             }
142             CommandChanged(button);
143         }
144
145         static void CommandChanged(ContentButton button)
146         {
147             if(button.Command != null)
148             {
149                 CommandCanExcuteChanged(button, EventArgs.Empty);
150             }
151             else
152             {
153                 button.IsEnabledCore = true;
154             }
155         }
156
157         static void OnCommandChanging(BindableObject bindable, object oldCommand, object newCommand)
158         {
159             ContentButton button = (ContentButton)bindable;
160             if (oldCommand != null)
161             {
162                 (oldCommand as ICommand).CanExecuteChanged -= button.OnCommandCanExecuteChanged;
163             }
164         }
165
166         static void CommandCanExcuteChanged(object sender, EventArgs e)
167         {
168             var button = (ContentButton)sender;
169             if (button.Command != null)
170             {
171                 button.IsEnabledCore = button.Command.CanExecute(button.CommandParameter);
172             }
173         }
174
175         void OnCommandCanExecuteChanged(object sender, EventArgs e)
176         {
177             ContentButton button = (ContentButton)sender;
178             if (button.Command != null)
179             {
180                 button.IsEnabledCore = button.Command.CanExecute(button.CommandParameter);
181             }
182         }
183     }
184 }