/
ButtonBase.cs
209 lines (172 loc) · 4.88 KB
/
ButtonBase.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
using Uno.Client;
using Uno.Extensions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Uno.Disposables;
using System.Text;
using System.Windows.Input;
using Windows.UI.Input;
using Microsoft.UI.Xaml.Input;
using Uno.Extensions.Specialized;
using Uno.Foundation.Logging;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Windows.System;
#if __IOS__
using View = UIKit.UIView;
#elif __MACOS__
using View = AppKit.NSView;
#elif __ANDROID__
using View = Android.Views.View;
#else
using View = Microsoft.UI.Xaml.UIElement;
#endif
namespace Microsoft.UI.Xaml.Controls.Primitives
{
public partial class ButtonBase : ContentControl
{
public
#if __ANDROID__
new
#endif
event RoutedEventHandler Click;
public ButtonBase()
{
Initialize();
InitializeProperties();
Unloaded += (s, e) =>
IsPressed = false;
DefaultStyleKey = typeof(ButtonBase);
}
private protected override void OnLoaded()
{
base.OnLoaded();
OnLoadedPartial();
RegisterEvents();
}
partial void OnLoadedPartial();
private protected override void OnUnloaded()
{
base.OnUnloaded();
OnUnloadedPartial();
}
partial void OnUnloadedPartial();
public new bool IsPointerOver
{
get => base.IsPointerOver;
set => base.IsPointerOver = value;
}
private void InitializeProperties()
{
PartialInitializeProperties();
}
partial void PartialInitializeProperties();
#region Command (DP)
public static DependencyProperty CommandProperty { get; } = DependencyProperty.Register(
nameof(Command), typeof(ICommand), typeof(ButtonBase), new FrameworkPropertyMetadata(default(ICommand)));
public ICommand Command
{
get => (ICommand)GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
#endregion
#region CommandParameter
public static DependencyProperty CommandParameterProperty { get; } =
DependencyProperty.Register(
nameof(CommandParameter),
typeof(object),
typeof(ButtonBase),
new FrameworkPropertyMetadata(default(object), OnCommandParameterChanged));
public object CommandParameter
{
get => GetValue(CommandParameterProperty);
set => SetValue(CommandParameterProperty, value);
}
private static void OnCommandParameterChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) =>
((ButtonBase)dependencyObject)?.CoerceValue(IsEnabledProperty);
#endregion
public ClickMode ClickMode
{
get => (ClickMode)this.GetValue(ClickModeProperty);
set => this.SetValue(ClickModeProperty, value);
}
public new bool IsPressed
{
get => (bool)GetValue(IsPressedProperty);
internal set => SetValue(IsPressedProperty, value);
}
public static DependencyProperty ClickModeProperty { get; } =
DependencyProperty.Register(
name: nameof(ClickMode),
propertyType: typeof(ClickMode),
ownerType: typeof(ButtonBase),
typeMetadata: new FrameworkPropertyMetadata(ClickMode.Release));
public static DependencyProperty IsPointerOverProperty { get; } =
DependencyProperty.Register(
name: nameof(IsPointerOver),
propertyType: typeof(bool),
ownerType: typeof(ButtonBase),
typeMetadata: new FrameworkPropertyMetadata(default(bool)));
public static DependencyProperty IsPressedProperty { get; } =
DependencyProperty.Register(
name: nameof(IsPressed),
propertyType: typeof(bool),
ownerType: typeof(ButtonBase),
typeMetadata: new FrameworkPropertyMetadata(default(bool)));
partial void RegisterEvents();
#if __ANDROID__ || __IOS__
private void OnCanExecuteChanged()
{
this.CoerceValue(IsEnabledProperty);
}
#endif
private protected override object CoerceIsEnabled(object baseValue, DependencyPropertyValuePrecedences precedence)
{
if (Command != null
&& !Command.CanExecute(CommandParameter))
{
return false;
}
return base.CoerceIsEnabled(baseValue, precedence);
}
private protected override void OnContentTemplateRootSet() => RegisterEvents();
// Might be changed if the method does not conflict in UnoViewGroup.
internal override bool IsViewHit()
{
// Overrides the need for a non-null Background (required by base.IsViewHit)
return true;
}
// Allows native buttons (e.g., UIBarButtonItem, IMenuItem) to raise clicks on their associated AppBarButton.
internal void RaiseClick(PointerRoutedEventArgs args = null)
{
OnClick();
}
internal void AutomationPeerClick()
{
OnClick();
}
#if false
private void OnClick(PointerRoutedEventArgs args = null)
{
Click?.Invoke(this, new RoutedEventArgs(args?.OriginalSource ?? this));
InvokeCommand();
}
#endif
internal void InvokeCommand()
{
try
{
if (this.Log().IsEnabled(Uno.Foundation.Logging.LogLevel.Debug))
{
this.Log().Debug("Executing command");
}
Command.ExecuteIfPossible(CommandParameter);
}
catch (Exception e)
{
this.Log().Error("Failed to execute command", e);
}
}
}
}