fd3acd69cec49789ca1d22b6638e0bce0741a429
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / XamlBinding / MenuItem.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Windows.Input;
5
6 namespace Tizen.NUI.Binding
7 {
8
9     internal class MenuItem : BaseMenuItem, IMenuItemController
10     {
11         public static readonly BindableProperty AcceleratorProperty = BindableProperty.CreateAttached(nameof(Accelerator), typeof(Accelerator), typeof(MenuItem), null);
12
13         public static Accelerator GetAccelerator(BindableObject bindable) => (Accelerator)bindable.GetValue(AcceleratorProperty);
14
15         public static void SetAccelerator(BindableObject bindable, Accelerator value) => bindable.SetValue(AcceleratorProperty, value);
16
17         public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(MenuItem), null);
18
19         public static readonly BindableProperty CommandProperty = BindableProperty.Create("Command", typeof(ICommand), typeof(MenuItem), null,
20             propertyChanging: (bo, o, n) => ((MenuItem)bo).OnCommandChanging(), propertyChanged: (bo, o, n) => ((MenuItem)bo).OnCommandChanged());
21
22         public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create("CommandParameter", typeof(object), typeof(MenuItem), null,
23             propertyChanged: (bo, o, n) => ((MenuItem)bo).OnCommandParameterChanged());
24
25         public static readonly BindableProperty IsDestructiveProperty = BindableProperty.Create("IsDestructive", typeof(bool), typeof(MenuItem), false);
26
27         public static readonly BindableProperty IconProperty = BindableProperty.Create("Icon", typeof(FileImageSource), typeof(MenuItem), default(FileImageSource));
28
29         [EditorBrowsable(EditorBrowsableState.Never)]
30         public static readonly BindableProperty IsEnabledProperty = BindableProperty.Create("IsEnabled", typeof(bool), typeof(ToolbarItem), true);
31
32         [EditorBrowsable(EditorBrowsableState.Never)]
33         public string IsEnabledPropertyName
34         {
35             get
36             {
37                 return IsEnabledProperty.PropertyName;
38             }
39         }
40
41         public ICommand Command
42         {
43             get { return (ICommand)GetValue(CommandProperty); }
44             set { SetValue(CommandProperty, value); }
45         }
46
47         public object CommandParameter
48         {
49             get { return GetValue(CommandParameterProperty); }
50             set { SetValue(CommandParameterProperty, value); }
51         }
52
53         public FileImageSource Icon
54         {
55             get { return (FileImageSource)GetValue(IconProperty); }
56             set { SetValue(IconProperty, value); }
57         }
58
59         public bool IsDestructive
60         {
61             get { return (bool)GetValue(IsDestructiveProperty); }
62             set { SetValue(IsDestructiveProperty, value); }
63         }
64
65         public string Text
66         {
67             get { return (string)GetValue(TextProperty); }
68             set { SetValue(TextProperty, value); }
69         }
70
71         [EditorBrowsable(EditorBrowsableState.Never)]
72         public bool IsEnabled
73         {
74             get { return (bool)GetValue(IsEnabledProperty); }
75             set { SetValue(IsEnabledProperty, value); }
76         }
77
78         bool IsEnabledCore
79         {
80             set { SetValueCore(IsEnabledProperty, value); }
81         }
82
83         public event EventHandler Clicked;
84
85         protected virtual void OnClicked()
86             => Clicked?.Invoke(this, EventArgs.Empty);
87
88         [EditorBrowsable(EditorBrowsableState.Never)]
89         public void Activate()
90         {
91             if (Command != null)
92             {
93                 if (IsEnabled)
94                     Command.Execute(CommandParameter);
95             }
96
97             OnClicked();
98         }
99
100         void OnCommandCanExecuteChanged(object sender, EventArgs eventArgs)
101         {
102             IsEnabledCore = Command.CanExecute(CommandParameter);
103         }
104
105         void OnCommandChanged()
106         {
107             if (Command == null)
108             {
109                 IsEnabledCore = true;
110                 return;
111             }
112
113             IsEnabledCore = Command.CanExecute(CommandParameter);
114
115             Command.CanExecuteChanged += OnCommandCanExecuteChanged;
116         }
117
118         void OnCommandChanging()
119         {
120             if (Command == null)
121                 return;
122
123             Command.CanExecuteChanged -= OnCommandCanExecuteChanged;
124         }
125
126         void OnCommandParameterChanged()
127         {
128             if (Command == null)
129                 return;
130
131             IsEnabledCore = Command.CanExecute(CommandParameter);
132         }
133     }
134 }