[NUI]Fix build warnings (#254)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / XamlBinding / Command.cs
1 using System;
2 using System.Reflection;
3 using System.Windows.Input;
4
5 namespace Tizen.NUI.Binding
6 {
7         internal sealed class Command<T> : Command 
8         {
9                 public Command(Action<T> execute) 
10                         : base(o =>
11                         {
12                                 if (IsValidParameter(o))
13                                 {
14                                         execute((T)o);
15                                 }
16                         })
17                 {
18                         if (execute == null)
19                         {
20                                 throw new ArgumentNullException(nameof(execute));
21                         }
22                 }
23
24                 public Command(Action<T> execute, Func<T, bool> canExecute) 
25                         : base(o =>
26                         {
27                                 if (IsValidParameter(o))
28                                 {
29                                         execute((T)o);
30                                 }
31                         }, o => IsValidParameter(o) && canExecute((T)o))
32                 {
33                         if (execute == null)
34                                 throw new ArgumentNullException(nameof(execute));
35                         if (canExecute == null)
36                                 throw new ArgumentNullException(nameof(canExecute));
37                 }
38
39                 static bool IsValidParameter(object o)
40                 {
41                         if (o != null)
42                         {
43                                 // The parameter isn't null, so we don't have to worry whether null is a valid option
44                                 return o is T;
45                         }
46
47                         var t = typeof(T);
48
49                         // The parameter is null. Is T Nullable?
50                         if (Nullable.GetUnderlyingType(t) != null)
51                         {
52                                 return true;
53                         }
54
55                         // Not a Nullable, if it's a value type then null is not valid
56                         return !t.GetTypeInfo().IsValueType;
57                 }
58         }
59
60     /// <summary>
61     /// Defines an ICommand implementation that wraps a Action.
62     /// </summary>
63         public class Command : ICommand
64         {
65                 readonly Func<object, bool> _canExecute;
66                 readonly Action<object> _execute;
67
68         /// <summary>
69         /// Initializes a new instance of the Command class.
70         /// </summary>
71         /// <param name="execute">An instance to execute when the Command is executed.</param>
72                 public Command(Action<object> execute)
73                 {
74                         if (execute == null)
75                                 throw new ArgumentNullException(nameof(execute));
76
77                         _execute = execute;
78                 }
79
80         /// <summary>
81         /// Initializes a new instance of the Command class.
82         /// </summary>
83         /// <param name="execute">An Action to execute when the Command is executed.</param>
84                 public Command(Action execute) : this(o => execute())
85                 {
86                         if (execute == null)
87                                 throw new ArgumentNullException(nameof(execute));
88                 }
89
90         /// <summary>
91         /// Initializes a new instance of the Command class.
92         /// </summary>
93         /// <param name="execute">An Action to execute when the Command is executed.</param>
94         /// <param name="canExecute">A instance indicating if the Command can be executed.</param>
95                 public Command(Action<object> execute, Func<object, bool> canExecute) : this(execute)
96                 {
97                         if (canExecute == null)
98                                 throw new ArgumentNullException(nameof(canExecute));
99
100                         _canExecute = canExecute;
101                 }
102
103         /// <summary>
104         /// Initializes a new instance of the Command class.
105         /// </summary>
106         /// <param name="execute">An Action to execute when the Command is executed.</param>
107         /// <param name="canExecute">A instance indicating if the Command can be executed.</param>
108                 public Command(Action execute, Func<bool> canExecute) : this(o => execute(), o => canExecute())
109                 {
110                         if (execute == null)
111                                 throw new ArgumentNullException(nameof(execute));
112                         if (canExecute == null)
113                                 throw new ArgumentNullException(nameof(canExecute));
114                 }
115
116         /// <summary>
117         /// Returns a Boolean indicating if the Command can be exectued with the given parameter.
118         /// </summary>
119         /// <param name="parameter">An Object used as parameter to determine if the Command can be executed.</param>
120         /// <returns>true if the Command can be executed, false otherwise.</returns>
121                 public bool CanExecute(object parameter)
122                 {
123                         if (_canExecute != null)
124                                 return _canExecute(parameter);
125
126                         return true;
127                 }
128
129         /// <summary>
130         /// Occurs when the target of the Command should reevaluate whether or not the Command can be executed.
131         /// </summary>
132                 public event EventHandler CanExecuteChanged;
133
134         /// <summary>
135         /// Invokes the execute Action.
136         /// </summary>
137         /// <param name="parameter">An Object used as parameter for the execute Action.</param>
138         public void Execute(object parameter)
139                 {
140                         _execute(parameter);
141                 }
142
143         /// <summary>
144         /// Send a CanExecuteChanged.
145         /// </summary>
146                 public void ChangeCanExecute()
147                 {
148                         EventHandler changed = CanExecuteChanged;
149                         changed?.Invoke(this, EventArgs.Empty);
150                 }
151         }
152 }