-
Notifications
You must be signed in to change notification settings - Fork 768
/
Copy pathSinglelineTextBoxView.iOS.cs
288 lines (241 loc) · 6.91 KB
/
SinglelineTextBoxView.iOS.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#nullable enable
using System;
using System.Runtime.InteropServices;
using CoreGraphics;
using Foundation;
using ObjCRuntime;
using UIKit;
using Uno.Extensions;
using Uno.UI.Controls;
using Uno.UI.Extensions;
using Microsoft.UI.Xaml.Media;
using Windows.UI;
using Uno.Disposables;
using Uno.Foundation.Logging;
using Uno.UI;
using Uno.UI.Xaml;
using static Uno.UI.FeatureConfiguration;
namespace Microsoft.UI.Xaml.Controls
{
public partial class SinglelineTextBoxView : UITextField, ITextBoxView, DependencyObject, IFontScalable
{
private SinglelineTextBoxDelegate _delegate = null!;
private readonly WeakReference<TextBox> _textBox;
private Action? _foregroundChanged;
private IDisposable? _foregroundBrushChangedSubscription;
public SinglelineTextBoxView(TextBox textBox)
{
_textBox = new WeakReference<TextBox>(textBox);
InitializeBinder();
Initialize();
}
public override void Paste(NSObject? sender) => HandlePaste(() => base.Paste(sender));
public override void PasteAndGo(NSObject? sender) => HandlePaste(() => base.PasteAndGo(sender));
public override void PasteAndMatchStyle(NSObject? sender) => HandlePaste(() => base.PasteAndMatchStyle(sender));
public override void PasteAndSearch(NSObject? sender) => HandlePaste(() => base.PasteAndSearch(sender));
public override void Paste(NSItemProvider[] itemProviders) => HandlePaste(() => base.Paste(itemProviders));
private void HandlePaste(Action baseAction)
{
var args = new TextControlPasteEventArgs();
TextBox?.RaisePaste(args);
if (!args.Handled)
{
baseAction.Invoke();
}
}
internal TextBox TextBox => _textBox.GetTarget();
public override string? Text
{
get => base.Text;
set
{
// The native control will ignore a value of null and retain an empty string. We coalesce the null to prevent a spurious empty string getting bounced back via two-way binding.
value ??= string.Empty;
if (base.Text != value)
{
base.Text = value;
OnTextChanged();
}
}
}
private void OnEditingChanged(object? sender, EventArgs e)
{
OnTextChanged();
}
private void OnTextChanged()
{
if (TextBox is { } textBox)
{
var text = textBox.ProcessTextInput(Text);
SetTextNative(text);
}
}
public void SetTextNative(string text) => Text = text;
private void Initialize()
{
//Set native VerticalAlignment to top-aligned (default is center) to match Windows text placement
base.VerticalAlignment = UIControlContentVerticalAlignment.Top;
Delegate = _delegate = new SinglelineTextBoxDelegate(_textBox)
{
IsKeyboardHiddenOnEnter = true
};
}
partial void OnLoadedPartial()
{
SubscribeEditingEvents();
}
partial void OnUnloadedPartial()
{
UnsubscribeEditingEvents();
}
//Forces the secure UITextField to maintain its current value upon regaining focus
public override bool BecomeFirstResponder()
{
var result = base.BecomeFirstResponder();
if (SecureTextEntry && !string.IsNullOrEmpty(Text))
{
UnsubscribeEditingEvents();
var text = Text;
UpdatePasswordText(string.Empty);
InsertText(text);
UpdatePasswordText(text);
SubscribeEditingEvents();
}
return result;
void UpdatePasswordText(string text)
{
if (TextBox is PasswordBox passwordBox)
{
passwordBox.Password = text;
}
}
}
public override CGSize SizeThatFits(CGSize size)
{
return IFrameworkElementHelper.SizeThatFits(this, base.SizeThatFits(size));
}
public override CGRect TextRect(CGRect forBounds)
{
return GetTextRect(forBounds);
}
public override CGRect PlaceholderRect(CGRect forBounds)
{
return GetTextRect(forBounds);
}
public override CGRect EditingRect(CGRect forBounds)
{
return GetTextRect(forBounds);
}
private CGRect GetTextRect(CGRect forBounds)
{
if (IsStoreInitialized)
{
// This test is present because most virtual methods are
// called before the ctor has finished executing.
return new CGRect(
forBounds.X,
forBounds.Y,
forBounds.Width,
forBounds.Height
);
}
else
{
return CGRect.Empty;
}
}
public void UpdateFont()
{
var textBox = _textBox.GetTarget();
if (textBox != null)
{
var newFont = FontHelper.TryGetFont(new((float)textBox.FontSize, textBox.FontWeight, textBox.FontStyle, textBox.FontStretch), textBox.FontFamily);
if (newFont != null)
{
base.Font = newFont;
this.InvalidateMeasure();
}
}
}
public Brush Foreground
{
get { return (Brush)GetValue(ForegroundProperty); }
set { SetValue(ForegroundProperty, value); }
}
public static DependencyProperty ForegroundProperty { get; } =
DependencyProperty.Register(
"Foreground",
typeof(Brush),
typeof(SinglelineTextBoxView),
new FrameworkPropertyMetadata(
defaultValue: SolidColorBrushHelper.Black,
options: FrameworkPropertyMetadataOptions.Inherits,
propertyChangedCallback: (s, e) => ((SinglelineTextBoxView)s).OnForegroundChanged((Brush)e.OldValue, (Brush)e.NewValue)
)
);
public void OnForegroundChanged(Brush oldValue, Brush newValue)
{
var textBox = _textBox.GetTarget();
if (textBox != null)
{
if (newValue is SolidColorBrush scb)
{
_foregroundBrushChangedSubscription?.Dispose();
_foregroundBrushChangedSubscription = Brush.SetupBrushChanged(newValue, ref _foregroundChanged, () => ApplyColor());
void ApplyColor()
{
TextColor = scb.Color;
}
}
}
}
public void UpdateTextAlignment()
{
var textBox = _textBox.GetTarget();
if (textBox != null)
{
TextAlignment = textBox.TextAlignment.ToNativeTextAlignment();
}
}
public void RefreshFont()
{
UpdateFont();
}
public void Select(int start, int length)
=> SelectedTextRange = this.GetTextRange(start: start, end: start + length).GetHandle();
/// <summary>
/// Workaround for https://github.com/unoplatform/uno/issues/9430
/// </summary>
[DllImport(Constants.ObjectiveCLibrary, EntryPoint = "objc_msgSendSuper")]
static internal extern IntPtr IntPtr_objc_msgSendSuper(IntPtr receiver, IntPtr selector);
[DllImport(Constants.ObjectiveCLibrary, EntryPoint = "objc_msgSendSuper")]
static internal extern void void_objc_msgSendSuper(IntPtr receiver, IntPtr selector, IntPtr arg);
[Export("selectedTextRange")]
public new IntPtr SelectedTextRange
{
get
{
return IntPtr_objc_msgSendSuper(SuperHandle, Selector.GetHandle("selectedTextRange"));
}
set
{
var textBox = TextBox;
if (textBox != null && SelectedTextRange != value)
{
void_objc_msgSendSuper(SuperHandle, Selector.GetHandle("setSelectedTextRange:"), value);
textBox.OnSelectionChanged();
}
}
}
private void SubscribeEditingEvents()
{
EditingChanged += OnEditingChanged;
EditingDidEnd += OnEditingChanged;
}
private void UnsubscribeEditingEvents()
{
EditingChanged -= OnEditingChanged;
EditingDidEnd -= OnEditingChanged;
}
}
}