Skip to content

Commit ff5c076

Browse files
committed
Improves MacCatalyst AutoCompleteEntry
Refactors the MacCatalyst AutoCompleteEntry to improve layout and border handling. Removes unnecessary size calculation and improves constraints for text input. Also, ensures the suggestion list is properly removed from the superview when closed.
1 parent 4d73b7b commit ff5c076

File tree

5 files changed

+23
-36
lines changed

5 files changed

+23
-36
lines changed

src/AutoCompleteEntry/Platforms/MacCatalyst/AutoCompleteEntryExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Foundation;
1+
using Foundation;
22
using Microsoft.Maui.Platform;
33
using UIKit;
44
using zoft.MauiExtensions.Core.Extensions;

src/AutoCompleteEntry/Platforms/MacCatalyst/AutoCompleteEntryHandler.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@ public partial class AutoCompleteEntryHandler : ViewHandler<AutoCompleteEntry, I
1010
/// <inheritdoc/>
1111
protected override IOSAutoCompleteEntry CreatePlatformView() => new();
1212

13-
/// <inheritdoc/>
14-
public override Size GetDesiredSize(double widthConstraint, double heightConstraint)
15-
{
16-
if (double.IsInfinity(widthConstraint) || double.IsInfinity(heightConstraint))
17-
{
18-
PlatformView.InputTextField.SizeToFit();
19-
return new Size(PlatformView.InputTextField.Frame.Width, PlatformView.InputTextField.Frame.Height);
20-
}
21-
22-
return base.GetDesiredSize(widthConstraint, heightConstraint);
23-
}
24-
2513
/// <inheritdoc/>
2614
protected override void ConnectHandler(IOSAutoCompleteEntry platformView)
2715
{

src/AutoCompleteEntry/Platforms/MacCatalyst/AutoCompleteEntryTableSource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Foundation;
1+
using Foundation;
22
using Microsoft.Maui.Controls.Internals;
33
using Microsoft.Maui.Platform;
44
using System.Collections;
@@ -142,4 +142,4 @@ private void OnTableRowSelected(NSIndexPath itemIndexPath)
142142
var item = _items[itemIndexPath.Row];
143143
TableRowSelected?.Invoke(this, new TableRowSelectedEventArgs<object>(item, itemIndexPath));
144144
}
145-
}
145+
}

src/AutoCompleteEntry/Platforms/MacCatalyst/IOSAutoCompleteEntry.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
using CoreAnimation;
12
using CoreGraphics;
23
using Foundation;
34
using ObjCRuntime;
45
using System.Collections;
5-
using System.Collections.Specialized;
66
using System.Drawing;
77
using UIKit;
88

@@ -17,7 +17,7 @@ public sealed class IOSAutoCompleteEntry : UIView
1717
/// Raised after the text content of the editable control component is updated.
1818
/// </summary>
1919
public event EventHandler<AutoCompleteEntryTextChangedEventArgs> TextChanged;
20-
20+
2121
/// <summary>
2222
/// Raised after the cursor position of the editable control component is updated.
2323
/// </summary>
@@ -39,7 +39,7 @@ public sealed class IOSAutoCompleteEntry : UIView
3939
private nfloat _keyboardHeight;
4040
private NSLayoutConstraint _bottomConstraint;
4141
private Func<object, string> _textFunc;
42-
private CoreAnimation.CALayer _border;
42+
private CoreAnimation.CALayer _border = null;
4343
private bool _showBottomBorder = true;
4444
private readonly NSObject _keyboardShownObserverToken;
4545
private readonly NSObject _keyboardHiddenObserverToken;
@@ -128,12 +128,15 @@ public IOSAutoCompleteEntry() : this(RectangleF.Empty)
128128
InputTextField.SelectedTextRangeChanged += InputText_OnTextRangeChanged;
129129
InputTextField.ShouldReturn += InputText_OnShouldReturn;
130130

131-
AddSubview(InputTextField);
131+
AddSubview(InputTextField);
132132

133-
InputTextField.TopAnchor.ConstraintEqualTo(TopAnchor).Active = true;
134-
InputTextField.LeftAnchor.ConstraintEqualTo(LeftAnchor).Active = true;
135-
InputTextField.WidthAnchor.ConstraintEqualTo(WidthAnchor).Active = true;
136-
InputTextField.HeightAnchor.ConstraintEqualTo(HeightAnchor).Active = true;
133+
NSLayoutConstraint.ActivateConstraints(new[]
134+
{
135+
InputTextField.TopAnchor.ConstraintEqualTo(TopAnchor),
136+
InputTextField.LeftAnchor.ConstraintEqualTo(LeftAnchor),
137+
InputTextField.RightAnchor.ConstraintEqualTo(RightAnchor),
138+
InputTextField.BottomAnchor.ConstraintEqualTo(BottomAnchor),
139+
});
137140

138141
SelectionList = new UITableView { TranslatesAutoresizingMaskIntoConstraints = false };
139142

@@ -219,14 +222,14 @@ private void InputText_OnEditingChanged(object sender, EventArgs e)
219222
TextChanged?.Invoke(this, new AutoCompleteEntryTextChangedEventArgs(AutoCompleteEntryTextChangeReason.UserInput));
220223

221224
InputText_OnTextRangeChanged(sender, e);
222-
225+
223226
IsSuggestionListOpen = true;
224227
}
225228

226229
private void InputText_OnTextRangeChanged(object sender, EventArgs e)
227230
{
228231
var cp = InputTextField.GetOffsetFromPosition(InputTextField.BeginningOfDocument, InputTextField.SelectedTextRange?.Start ?? InputTextField.EndOfDocument).ToInt32();
229-
232+
230233
CursorPositionChanged?.Invoke(this, new AutoCompleteEntryCursorPositionChangedEventArgs(cp));
231234
}
232235

@@ -235,11 +238,12 @@ private bool InputText_OnShouldReturn(UITextField view)
235238
ShouldReturn?.Invoke(this, EventArgs.Empty);
236239
return false;
237240
}
238-
241+
239242
/// <inheritdoc />
240243
public override void LayoutSubviews()
241244
{
242245
base.LayoutSubviews();
246+
243247
AddBottomBorder();
244248
}
245249

@@ -292,17 +296,10 @@ private void UpdateSuggestionListOpenState()
292296
return;
293297
}
294298

295-
#if MACCATALYST14_2_OR_GREATER
296299
if (viewController.PresentedViewController != null)
297300
{
298301
viewController = viewController.PresentedViewController;
299302
}
300-
#else
301-
if (viewController.ModalViewController != null)
302-
{
303-
viewController = viewController.ModalViewController;
304-
}
305-
#endif
306303

307304
if (SelectionList.Superview == null)
308305
{
@@ -319,7 +316,9 @@ private void UpdateSuggestionListOpenState()
319316
else
320317
{
321318
if (SelectionList.Superview != null)
319+
{
322320
SelectionList.RemoveFromSuperview();
321+
}
323322
}
324323
}
325324

@@ -376,7 +375,7 @@ private void SuggestionTableSource_TableRowSelected(object sender, TableRowSelec
376375
public class MyUITextField : UITextField
377376
{
378377
public event EventHandler<EventArgs> SelectedTextRangeChanged;
379-
378+
380379
public override UITextRange SelectedTextRange
381380
{
382381
get => base.SelectedTextRange;
@@ -385,7 +384,7 @@ public override UITextRange SelectedTextRange
385384
if (base.SelectedTextRange == null || !base.SelectedTextRange.Equals(value))
386385
{
387386
base.SelectedTextRange = value;
388-
387+
389388
SelectedTextRangeChanged?.Invoke(this, EventArgs.Empty);
390389
}
391390
}

src/AutoCompleteEntry/Platforms/MacCatalyst/TableRowSelectedEventArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Foundation;
1+
using Foundation;
22

33
namespace zoft.MauiExtensions.Controls.Platform;
44

0 commit comments

Comments
 (0)