forked from IeuanWalker/Maui.StateButton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateButton.xaml.cs
223 lines (182 loc) · 6.7 KB
/
StateButton.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System.Windows.Input;
using IeuanWalker.Maui.StateButton.Enums;
namespace IeuanWalker.Maui.StateButton;
public partial class StateButton : Border
{
#region Bindable Properties
/// <summary>
/// Backing BindableProperty for the <see cref="State"/> property.
/// </summary>
public static readonly BindableProperty StateProperty = BindableProperty.Create(nameof(State), typeof(ButtonState), typeof(StateButton), ButtonState.NotPressed, BindingMode.OneWayToSource);
/// <summary>
/// Property that gets updated depending on the button state. This is a bindable property.
/// </summary>
public ButtonState State
{
get => (ButtonState)GetValue(StateProperty);
set => SetValue(StateProperty, value);
}
#endregion Bindable Properties
#region Commands
/// <summary>
/// Backing BindableProperty for the <see cref="ClickedCommand"/> property.
/// </summary>
public static readonly BindableProperty ClickedCommandProperty = BindableProperty.Create(nameof(ClickedCommand), typeof(ICommand), typeof(StateButton));
/// <summary>
/// Command that is triggered when the button is clicked. This is a bindable property.
/// </summary>
public ICommand ClickedCommand
{
get => (ICommand)GetValue(ClickedCommandProperty);
set => SetValue(ClickedCommandProperty, value);
}
/// <summary>
/// Backing BindableProperty for the <see cref="ClickedCommandParameter"/> property.
/// </summary>
public static readonly BindableProperty ClickedCommandParameterProperty = BindableProperty.Create(nameof(ClickedCommandParameter), typeof(object), typeof(StateButton));
/// <summary>
/// Property that gets returned when <see cref="ClickedCommand" /> is executed. This is a bindable property.
/// </summary>
public object ClickedCommandParameter
{
get => GetValue(ClickedCommandParameterProperty);
set => SetValue(ClickedCommandParameterProperty, value);
}
/// <summary>
/// Backing BindableProperty for the <see cref="PressedCommand"/> property.
/// </summary>
public static readonly BindableProperty PressedCommandProperty = BindableProperty.Create(nameof(PressedCommand), typeof(ICommand), typeof(StateButton));
/// <summary>
/// Command that is triggered when the button is pressed. This is a bindable property.
/// </summary>
public ICommand PressedCommand
{
get => (ICommand)GetValue(PressedCommandProperty);
set => SetValue(PressedCommandProperty, value);
}
/// <summary>
/// Backing BindableProperty for the <see cref="PressedCommandParameter"/> property.
/// </summary>
public static readonly BindableProperty PressedCommandParameterProperty = BindableProperty.Create(nameof(PressedCommandParameter), typeof(object), typeof(StateButton));
/// <summary>
/// Property that gets returned when <see cref="PressedCommand" /> is executed. This is a bindable property.
/// </summary>
public object PressedCommandParameter
{
get => GetValue(PressedCommandParameterProperty);
set => SetValue(PressedCommandParameterProperty, value);
}
/// <summary>
/// Backing BindableProperty for the <see cref="ReleasedCommand"/> property.
/// </summary>
public static readonly BindableProperty ReleasedCommandProperty = BindableProperty.Create(nameof(ReleasedCommand), typeof(ICommand), typeof(StateButton));
/// <summary>
/// Command that is triggered when the button is released. This is a bindable property.
/// </summary>
public ICommand ReleasedCommand
{
get => (ICommand)GetValue(ReleasedCommandProperty);
set => SetValue(ReleasedCommandProperty, value);
}
/// <summary>
/// Backing BindableProperty for the <see cref="ReleasedCommandParameter"/> property.
/// </summary>
public static readonly BindableProperty ReleasedCommandParameterProperty = BindableProperty.Create(nameof(ReleasedCommandParameter), typeof(object), typeof(StateButton));
/// <summary>
/// Property that gets returned when <see cref="ReleasedCommand" /> is executed. This is a bindable property.
/// </summary>
public object ReleasedCommandParameter
{
get => GetValue(ReleasedCommandParameterProperty);
set => SetValue(ReleasedCommandParameterProperty, value);
}
/// <summary>
/// Backing BindableProperty for the <see cref="LongPressCommand"/> property.
/// </summary>
public static readonly BindableProperty LongPressCommandProperty = BindableProperty.Create(nameof(LongPressCommand), typeof(ICommand), typeof(StateButton));
/// <summary>
/// Command that is triggered when the button is long pressed. This is a bindable property.
/// </summary>
public ICommand LongPressCommand
{
get => (ICommand)GetValue(LongPressCommandProperty);
set => SetValue(LongPressCommandProperty, value);
}
/// <summary>
/// Backing BindableProperty for the <see cref="LongPressCommandParameter"/> property.
/// </summary>
public static readonly BindableProperty LongPressCommandParameterProperty = BindableProperty.Create(nameof(LongPressCommandParameter), typeof(object), typeof(StateButton));
/// <summary>
/// Property that gets returned when <see cref="LongPressCommand" /> is executed. This is a bindable property.
/// </summary>
public object LongPressCommandParameter
{
get => GetValue(LongPressCommandParameterProperty);
set => SetValue(LongPressCommandParameterProperty, value);
}
#endregion Commands
#region Events
/// <summary>
/// Event that is triggered when button is pressed. This is a bindable property.
/// </summary>
public event EventHandler<EventArgs>? Pressed;
/// <summary>
/// Event that is triggered when button is released. This is a bindable property.
/// </summary>
public event EventHandler<EventArgs>? Released;
/// <summary>
/// Event that is triggered when button is clicked. This is a bindable property.
/// </summary>
public event EventHandler<EventArgs>? Clicked;
/// <summary>
/// Event that is triggered when the button is long pressed. This is a bindable property.
/// </summary>
public event EventHandler<EventArgs>? LongPressed;
#endregion Events
public StateButton()
{
InitializeComponent();
}
internal void InvokePressed()
{
if(!IsEnabled)
{
return;
}
Pressed?.Invoke(this, EventArgs.Empty);
PressedCommand?.Execute(PressedCommandParameter);
State = ButtonState.Pressed;
}
internal void InvokeReleased()
{
if(!IsEnabled)
{
return;
}
if(State.Equals(ButtonState.NotPressed))
{
return;
}
Released?.Invoke(this, EventArgs.Empty);
ReleasedCommand?.Execute(ReleasedCommandParameter);
State = ButtonState.NotPressed;
}
internal void InvokeClicked()
{
if(!IsEnabled)
{
return;
}
Clicked?.Invoke(this, EventArgs.Empty);
ClickedCommand?.Execute(ClickedCommandParameter);
}
internal void InvokeLongPressed()
{
if(!IsEnabled)
{
return;
}
LongPressed?.Invoke(this, EventArgs.Empty);
LongPressCommand?.Execute(LongPressCommandParameter);
}
}