diff --git a/src/Compatibility/Core/src/Android/AppCompat/PickerRenderer.cs b/src/Compatibility/Core/src/Android/AppCompat/PickerRenderer.cs index ae7d4ce55c72..88cf7de238b1 100644 --- a/src/Compatibility/Core/src/Android/AppCompat/PickerRenderer.cs +++ b/src/Compatibility/Core/src/Android/AppCompat/PickerRenderer.cs @@ -109,6 +109,7 @@ protected override void OnFocusChangeRequested(object sender, VisualElement.Focu } } + [PortHandler("Partially ported, still missing code related to TitleColor, etc.")] void IPickerRenderer.OnClick() { Picker model = Element; @@ -168,6 +169,7 @@ protected void UpdateCharacterSpacing() } } + [PortHandler("Partially ported, still missing code related to TitleColor, etc.")] void UpdatePicker() { UpdatePlaceHolderText(); diff --git a/src/Compatibility/Core/src/iOS/Renderers/PickerRenderer.cs b/src/Compatibility/Core/src/iOS/Renderers/PickerRenderer.cs index cf16f9f2a0c7..4d75e7467778 100644 --- a/src/Compatibility/Core/src/iOS/Renderers/PickerRenderer.cs +++ b/src/Compatibility/Core/src/iOS/Renderers/PickerRenderer.cs @@ -11,6 +11,7 @@ namespace Microsoft.Maui.Controls.Compatibility.Platform.iOS { + [PortHandler] internal class ReadOnlyField : NoCaretField { readonly HashSet enableActions; @@ -32,6 +33,7 @@ public PickerRenderer() } + [PortHandler] protected override UITextField CreateNativeControl() { return new ReadOnlyField { BorderStyle = UITextBorderStyle.RoundedRect }; @@ -56,6 +58,8 @@ public PickerRendererBase() } protected abstract override TControl CreateNativeControl(); + + [PortHandler("Partially ported, still missing code related to TitleColor, etc.")] protected override void OnElementChanged(ElementChangedEventArgs e) { if (e.OldElement != null) @@ -231,6 +235,7 @@ protected internal virtual void UpdatePlaceholder() protected virtual void UpdateAttributedPlaceholder(NSAttributedString nsAttributedString) => Control.AttributedPlaceholder = nsAttributedString; + [PortHandler] void UpdatePicker() { var selectedIndex = Element.SelectedIndex; @@ -266,6 +271,7 @@ void UpdatePickerNativeSize(string oldText) ((IVisualElementController)Element).NativeSizeChanged(); } + [PortHandler] void UpdatePickerSelectedIndex(int formsIndex) { var source = (PickerSource)_picker.Model; @@ -334,6 +340,7 @@ protected override void Dispose(bool disposing) base.Dispose(disposing); } + [PortHandler] class PickerSource : UIPickerViewModel { PickerRendererBase _renderer; diff --git a/src/Controls/samples/Controls.Sample/Pages/MainPage.cs b/src/Controls/samples/Controls.Sample/Pages/MainPage.cs index e4765150b4b0..fc02ad767d46 100644 --- a/src/Controls/samples/Controls.Sample/Pages/MainPage.cs +++ b/src/Controls/samples/Controls.Sample/Pages/MainPage.cs @@ -1,3 +1,5 @@ +using System; +using System.Collections.Generic; using Maui.Controls.Sample.ViewModel; using Microsoft.Extensions.DependencyInjection; using Microsoft.Maui; @@ -113,6 +115,23 @@ void SetupMauiLayout() placeholderSearchBar.Placeholder = "Placeholder"; verticalStack.Add(placeholderSearchBar); + + var monkeyList = new List + { + "Baboon", + "Capuchin Monkey", + "Blue Monkey", + "Squirrel Monkey", + "Golden Lion Tamarin", + "Howler Monkey", + "Japanese Macaque" + }; + + var picker = new Picker { Title = "Select a monkey" }; + + picker.ItemsSource = monkeyList; + verticalStack.Add(picker); + verticalStack.Add(new Slider()); verticalStack.Add(new Stepper()); diff --git a/src/Controls/src/Core/HandlerImpl/Picker.Impl.cs b/src/Controls/src/Core/HandlerImpl/Picker.Impl.cs new file mode 100644 index 000000000000..b98e185c368d --- /dev/null +++ b/src/Controls/src/Core/HandlerImpl/Picker.Impl.cs @@ -0,0 +1,7 @@ +namespace Microsoft.Maui.Controls +{ + public partial class Picker : IPicker + { + + } +} \ No newline at end of file diff --git a/src/Controls/src/Core/Picker.cs b/src/Controls/src/Core/Picker.cs index a946602fab61..c50dedbf1a2b 100644 --- a/src/Controls/src/Core/Picker.cs +++ b/src/Controls/src/Core/Picker.cs @@ -9,7 +9,7 @@ namespace Microsoft.Maui.Controls { - public class Picker : View, IFontElement, ITextElement, ITextAlignmentElement, IElementConfiguration + public partial class Picker : View, IFontElement, ITextElement, ITextAlignmentElement, IElementConfiguration { public static readonly BindableProperty TextColorProperty = TextElement.TextColorProperty; diff --git a/src/Core/src/Core/IPicker.cs b/src/Core/src/Core/IPicker.cs new file mode 100644 index 000000000000..51552689368f --- /dev/null +++ b/src/Core/src/Core/IPicker.cs @@ -0,0 +1,14 @@ +using System.Collections; +using System.Collections.Generic; + +namespace Microsoft.Maui +{ + public interface IPicker : IView + { + string Title { get; } + IList Items { get; } + IList ItemsSource { get; } + int SelectedIndex { get; set; } + object? SelectedItem { get; set; } + } +} \ No newline at end of file diff --git a/src/Core/src/Handlers/Picker/PickerHandler.Android.cs b/src/Core/src/Handlers/Picker/PickerHandler.Android.cs new file mode 100644 index 000000000000..4d5ca267fe5c --- /dev/null +++ b/src/Core/src/Handlers/Picker/PickerHandler.Android.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Specialized; +using System.Linq; +using Android.App; +using AResource = Android.Resource; + +namespace Microsoft.Maui.Handlers +{ + public partial class PickerHandler : AbstractViewHandler + { + AlertDialog? _dialog; + + protected override MauiPicker CreateNativeView() => + new MauiPicker(Context); + + protected override void ConnectHandler(MauiPicker nativeView) + { + nativeView.FocusChange += OnFocusChange; + nativeView.Click += OnClick; + + if (VirtualView != null && VirtualView.Items is INotifyCollectionChanged notifyCollection) + notifyCollection.CollectionChanged += OnCollectionChanged; + + base.ConnectHandler(nativeView); + } + + protected override void DisconnectHandler(MauiPicker nativeView) + { + nativeView.FocusChange -= OnFocusChange; + nativeView.Click -= OnClick; + + if (VirtualView != null && VirtualView.Items is INotifyCollectionChanged notifyCollection) + notifyCollection.CollectionChanged -= OnCollectionChanged; + + base.DisconnectHandler(nativeView); + } + public static void MapTitle(PickerHandler handler, IPicker picker) + { + handler.TypedNativeView?.UpdateTitle(picker); + } + + public static void MapSelectedIndex(PickerHandler handler, IPicker picker) + { + handler.TypedNativeView?.UpdateSelectedIndex(picker); + } + + void OnFocusChange(object? sender, global::Android.Views.View.FocusChangeEventArgs e) + { + if (TypedNativeView == null) + return; + + if (e.HasFocus) + { + if (TypedNativeView.Clickable) + TypedNativeView.CallOnClick(); + else + OnClick(TypedNativeView, EventArgs.Empty); + } + else if (_dialog != null) + { + _dialog.Hide(); + TypedNativeView.ClearFocus(); + _dialog = null; + } + } + + void OnClick(object? sender, EventArgs e) + { + if (_dialog == null && VirtualView != null) + { + using (var builder = new AlertDialog.Builder(Context)) + { + builder.SetTitle(VirtualView.Title ?? string.Empty); + + string[] items = VirtualView.Items.ToArray(); + + builder.SetItems(items, (s, e) => + { + var selectedIndex = e.Which; + VirtualView.SelectedIndex = selectedIndex; + TypedNativeView?.UpdatePicker(VirtualView); + }); + + builder.SetNegativeButton(AResource.String.Cancel, (o, args) => { }); + + _dialog = builder.Create(); + } + + if (_dialog == null) + return; + + _dialog.SetCanceledOnTouchOutside(true); + + _dialog.DismissEvent += (sender, args) => + { + _dialog.Dispose(); + _dialog = null; + }; + + _dialog.Show(); + } + } + + void OnCollectionChanged(object? sender, EventArgs e) + { + if (VirtualView == null || TypedNativeView == null) + return; + + TypedNativeView.UpdatePicker(VirtualView); + } + } +} \ No newline at end of file diff --git a/src/Core/src/Handlers/Picker/PickerHandler.Standard.cs b/src/Core/src/Handlers/Picker/PickerHandler.Standard.cs new file mode 100644 index 000000000000..77a0294c3787 --- /dev/null +++ b/src/Core/src/Handlers/Picker/PickerHandler.Standard.cs @@ -0,0 +1,12 @@ +using System; + +namespace Microsoft.Maui.Handlers +{ + public partial class PickerHandler : AbstractViewHandler + { + protected override object CreateNativeView() => throw new NotImplementedException(); + + public static void MapTitle(PickerHandler handler, IPicker view) { } + public static void MapSelectedIndex(PickerHandler handler, IPicker view) { } + } +} \ No newline at end of file diff --git a/src/Core/src/Handlers/Picker/PickerHandler.cs b/src/Core/src/Handlers/Picker/PickerHandler.cs new file mode 100644 index 000000000000..2504f4a30c28 --- /dev/null +++ b/src/Core/src/Handlers/Picker/PickerHandler.cs @@ -0,0 +1,21 @@ +namespace Microsoft.Maui.Handlers +{ + public partial class PickerHandler + { + public static PropertyMapper PickerMapper = new PropertyMapper(ViewHandler.ViewMapper) + { + [nameof(IPicker.Title)] = MapTitle, + [nameof(IPicker.SelectedIndex)] = MapSelectedIndex + }; + + public PickerHandler() : base(PickerMapper) + { + + } + + public PickerHandler(PropertyMapper mapper) : base(mapper) + { + + } + } +} \ No newline at end of file diff --git a/src/Core/src/Handlers/Picker/PickerHandler.iOS.cs b/src/Core/src/Handlers/Picker/PickerHandler.iOS.cs new file mode 100644 index 000000000000..b7790c26e5f2 --- /dev/null +++ b/src/Core/src/Handlers/Picker/PickerHandler.iOS.cs @@ -0,0 +1,216 @@ +using System; +using System.Collections.Specialized; +using UIKit; +using RectangleF = CoreGraphics.CGRect; + +namespace Microsoft.Maui.Handlers +{ + public partial class PickerHandler : AbstractViewHandler + { + UIPickerView? _pickerView; + + protected override MauiPicker CreateNativeView() + { + _pickerView = new UIPickerView(); + + var nativePicker = new MauiPicker(_pickerView) { BorderStyle = UITextBorderStyle.RoundedRect }; + + var width = UIScreen.MainScreen.Bounds.Width; + var toolbar = new UIToolbar(new RectangleF(0, 0, width, 44)) { BarStyle = UIBarStyle.Default, Translucent = true }; + var spacer = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace); + + var doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, (o, a) => + { + var pickerSource = (PickerSource)_pickerView.Model; + + if (pickerSource.SelectedIndex == -1 && VirtualView?.Items != null && VirtualView.Items.Count > 0) + UpdatePickerSelectedIndex(0); + + if (VirtualView?.SelectedIndex == -1 && VirtualView.Items != null && VirtualView.Items.Count > 0) + { + TypedNativeView?.SetSelectedIndex(VirtualView, 0); + } + + UpdatePickerFromPickerSource(pickerSource); + nativePicker.ResignFirstResponder(); + }); + + toolbar.SetItems(new[] { spacer, doneButton }, false); + + nativePicker.InputView = _pickerView; + nativePicker.InputAccessoryView = toolbar; + + nativePicker.InputView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight; + nativePicker.InputAccessoryView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight; + + if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0)) + { + nativePicker.InputAssistantItem.LeadingBarButtonGroups = null; + nativePicker.InputAssistantItem.TrailingBarButtonGroups = null; + } + + nativePicker.AccessibilityTraits = UIAccessibilityTrait.Button; + + _pickerView.Model = new PickerSource(VirtualView); + + return nativePicker; + } + + protected override void ConnectHandler(MauiPicker nativeView) + { + nativeView.EditingDidEnd += OnEnded; + nativeView.EditingChanged += OnEditing; + + if (VirtualView != null && VirtualView.Items is INotifyCollectionChanged notifyCollectionChanged) + notifyCollectionChanged.CollectionChanged += OnCollectionChanged; + + base.ConnectHandler(nativeView); + } + + protected override void DisconnectHandler(MauiPicker nativeView) + { + nativeView.EditingDidEnd -= OnEnded; + nativeView.EditingChanged -= OnEditing; + + if (VirtualView != null && VirtualView.Items is INotifyCollectionChanged notifyCollectionChanged) + notifyCollectionChanged.CollectionChanged -= OnCollectionChanged; + + if (_pickerView != null) + { + if (_pickerView.Model != null) + { + _pickerView.Model.Dispose(); + _pickerView.Model = null; + } + + _pickerView.RemoveFromSuperview(); + _pickerView.Dispose(); + _pickerView = null; + } + + base.DisconnectHandler(nativeView); + } + + public static void MapTitle(PickerHandler handler, IPicker picker) + { + handler.TypedNativeView?.UpdateTitle(picker); + } + + public static void MapSelectedIndex(PickerHandler handler, IPicker picker) + { + handler.TypedNativeView?.UpdateSelectedIndex(picker); + } + + void OnCollectionChanged(object? sender, EventArgs e) + { + if (VirtualView == null || TypedNativeView == null) + return; + + TypedNativeView.UpdatePicker(VirtualView); + } + + void OnEnded(object? sender, EventArgs eventArgs) + { + if (_pickerView == null) + return; + + PickerSource? model = (PickerSource)_pickerView.Model; + + if (model.SelectedIndex != -1 && model.SelectedIndex != _pickerView.SelectedRowInComponent(0)) + { + _pickerView.Select(model.SelectedIndex, 0, false); + } + } + + void OnEditing(object? sender, EventArgs eventArgs) + { + if (VirtualView == null || TypedNativeView == null) + return; + + // Reset the TextField's Text so it appears as if typing with a keyboard does not work. + var selectedIndex = VirtualView.SelectedIndex; + var items = VirtualView.Items; + TypedNativeView.Text = selectedIndex == -1 || items == null ? string.Empty : items[selectedIndex]; + + // Also clears the undo stack (undo/redo possible on iPads) + TypedNativeView.UndoManager.RemoveAllActions(); + } + + void UpdatePickerFromPickerSource(PickerSource pickerSource) + { + if (VirtualView == null || TypedNativeView == null) + return; + + TypedNativeView.Text = pickerSource.SelectedItem; + VirtualView.SelectedIndex = pickerSource.SelectedIndex; + } + + void UpdatePickerSelectedIndex(int formsIndex) + { + if (VirtualView == null || _pickerView == null) + return; + + var source = (PickerSource)_pickerView.Model; + source.SelectedIndex = formsIndex; + source.SelectedItem = formsIndex >= 0 ? VirtualView.Items[formsIndex] : null; + _pickerView.Select(Math.Max(formsIndex, 0), 0, true); + } + } + + public class PickerSource : UIPickerViewModel + { + IPicker? _virtualView; + bool _disposed; + + public PickerSource(IPicker? virtualView) + { + _virtualView = virtualView; + } + + public int SelectedIndex { get; internal set; } + + public string? SelectedItem { get; internal set; } + + public override nint GetComponentCount(UIPickerView picker) + { + return 1; + } + + public override nint GetRowsInComponent(UIPickerView pickerView, nint component) + { + return _virtualView?.Items != null ? _virtualView.Items.Count : 0; + } + + public override string GetTitle(UIPickerView picker, nint row, nint component) + { + return _virtualView != null ? _virtualView.Items[(int)row] : string.Empty; + } + + public override void Selected(UIPickerView picker, nint row, nint component) + { + if (_virtualView?.Items.Count == 0) + { + SelectedItem = null; + SelectedIndex = -1; + } + else + { + SelectedItem = _virtualView?.Items[(int)row]; + SelectedIndex = (int)row; + } + } + + protected override void Dispose(bool disposing) + { + if (_disposed) + return; + + _disposed = true; + + if (disposing) + _virtualView = null; + + base.Dispose(disposing); + } + } +} \ No newline at end of file diff --git a/src/Core/src/Hosting/AppHostBuilderExtensions.cs b/src/Core/src/Hosting/AppHostBuilderExtensions.cs index b4868bbaa4f2..80048436d7d6 100644 --- a/src/Core/src/Hosting/AppHostBuilderExtensions.cs +++ b/src/Core/src/Hosting/AppHostBuilderExtensions.cs @@ -42,6 +42,7 @@ public static IAppHostBuilder UseMauiHandlers(this IAppHostBuilder builder) { typeof(IEntry), typeof(EntryHandler) }, { typeof(ILayout), typeof(LayoutHandler) }, { typeof(ILabel), typeof(LabelHandler) }, + { typeof(IPicker), typeof(PickerHandler) }, { typeof(IProgress), typeof(ProgressBarHandler) }, { typeof(ISlider), typeof(SliderHandler) }, { typeof(IStepper), typeof(StepperHandler) }, diff --git a/src/Core/src/Platform/Android/MauiPicker.cs b/src/Core/src/Platform/Android/MauiPicker.cs new file mode 100644 index 000000000000..392ad2c620dd --- /dev/null +++ b/src/Core/src/Platform/Android/MauiPicker.cs @@ -0,0 +1,47 @@ +using Android.Content; +using Android.Views; +using Android.Widget; +using Android.Runtime; +using AndroidX.Core.Graphics.Drawable; +using ARect = Android.Graphics.Rect; + +namespace Microsoft.Maui +{ + public class MauiPicker : MauiPickerBase + { + public bool ShowPopupOnFocus { get; set; } + + public MauiPicker(Context? context) : base(context) + { + PickerManager.Init(this); + } + + public override bool OnTouchEvent(MotionEvent? e) + { + PickerManager.OnTouchEvent(this, e); + return base.OnTouchEvent(e); // Raises the OnClick event if focus is already received + } + + protected override void OnFocusChanged(bool gainFocus, [GeneratedEnum] FocusSearchDirection direction, ARect? previouslyFocusedRect) + { + base.OnFocusChanged(gainFocus, direction, previouslyFocusedRect); + PickerManager.OnFocusChanged(gainFocus, this); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + PickerManager.Dispose(this); + + base.Dispose(disposing); + } + } + + public class MauiPickerBase : EditText + { + public MauiPickerBase(Context? context) : base(context) + { + DrawableCompat.Wrap(Background); + } + } +} \ No newline at end of file diff --git a/src/Core/src/Platform/Android/PickerExtensions.cs b/src/Core/src/Platform/Android/PickerExtensions.cs new file mode 100644 index 000000000000..7ce75900e60b --- /dev/null +++ b/src/Core/src/Platform/Android/PickerExtensions.cs @@ -0,0 +1,46 @@ +namespace Microsoft.Maui +{ + public static class PickerExtensions + { + public static void UpdateTitle(this MauiPicker nativePicker, IPicker picker) => + UpdatePicker(nativePicker, picker); + + public static void UpdateSelectedIndex(this MauiPicker nativePicker, IPicker picker) => + UpdatePicker(nativePicker, picker); + + internal static void UpdatePicker(this MauiPicker nativePicker, IPicker picker) + { + nativePicker.Hint = picker.Title; + + if (picker.SelectedIndex == -1 || picker.Items == null || picker.SelectedIndex >= picker.Items.Count) + nativePicker.Text = null; + else + nativePicker.Text = picker.Items[picker.SelectedIndex]; + + nativePicker.SetSelectedItem(picker); + } + + internal static void SetSelectedItem(this MauiPicker nativePicker, IPicker picker) + { + if (picker == null || nativePicker == null) + return; + + int index = picker.SelectedIndex; + + if (index == -1) + { + picker.SelectedItem = null; + return; + } + + if (picker.ItemsSource != null) + { + picker.SelectedItem = picker.ItemsSource[index]; + return; + } + + if (picker.Items != null) + picker.SelectedItem = picker.Items[index]; + } + } +} \ No newline at end of file diff --git a/src/Core/src/Platform/Android/PickerManager.cs b/src/Core/src/Platform/Android/PickerManager.cs new file mode 100644 index 000000000000..d8c2765837a8 --- /dev/null +++ b/src/Core/src/Platform/Android/PickerManager.cs @@ -0,0 +1,66 @@ +using System.Collections.Generic; +using Android.Text; +using Android.Text.Style; +using Android.Views; +using Android.Widget; +using AView = Android.Views.View; + +namespace Microsoft.Maui +{ + internal static class PickerManager + { + readonly static HashSet AvailableKeys = new HashSet(new[] { + Keycode.Tab, Keycode.Forward, Keycode.DpadDown, Keycode.DpadLeft, Keycode.DpadRight, Keycode.DpadUp + }); + + public static void Init(EditText editText) + { + editText.Focusable = true; + editText.Clickable = true; + editText.InputType = InputTypes.Null; + + editText.KeyPress += OnKeyPress; + } + + public static void OnTouchEvent(EditText sender, MotionEvent? e) + { + if (e != null && e.Action == MotionEventActions.Up && !sender.IsFocused) + { + sender.RequestFocus(); + } + } + + public static void OnFocusChanged(bool gainFocus, EditText sender) + { + if (gainFocus) + sender.CallOnClick(); + } + + static void OnKeyPress(object? sender, AView.KeyEventArgs e) + { + if (!AvailableKeys.Contains(e.KeyCode)) + { + e.Handled = false; + return; + } + e.Handled = true; + (sender as AView)?.CallOnClick(); + } + + public static void Dispose(EditText editText) + { + editText.KeyPress -= OnKeyPress; + editText.SetOnClickListener(null); + } + + public static Java.Lang.ICharSequence GetTitle(Color titleColor, string title) + { + if (titleColor == Color.Default) + return new Java.Lang.String(title); + + var spannableTitle = new SpannableString(title ?? string.Empty); + spannableTitle.SetSpan(new ForegroundColorSpan(titleColor.ToNative()), 0, spannableTitle.Length(), SpanTypes.ExclusiveExclusive); + return spannableTitle; + } + } +} \ No newline at end of file diff --git a/src/Core/src/Platform/Standard/PickerExtensions.cs b/src/Core/src/Platform/Standard/PickerExtensions.cs new file mode 100644 index 000000000000..753779c1a179 --- /dev/null +++ b/src/Core/src/Platform/Standard/PickerExtensions.cs @@ -0,0 +1,15 @@ +namespace Microsoft.Maui +{ + public static class PickerExtensions + { + public static void UpdateTitle(this object nothing, IPicker picker) + { + + } + + public static void UpdateSelectedIndex(this object nothing, IPicker picker) + { + + } + } +} \ No newline at end of file diff --git a/src/Core/src/Platform/iOS/MauiPicker.cs b/src/Core/src/Platform/iOS/MauiPicker.cs new file mode 100644 index 000000000000..40a03a902454 --- /dev/null +++ b/src/Core/src/Platform/iOS/MauiPicker.cs @@ -0,0 +1,25 @@ +using System.Collections.Generic; +using Foundation; +using UIKit; +using ObjCRuntime; + +namespace Microsoft.Maui +{ + public class MauiPicker : NoCaretField + { + readonly HashSet _enableActions; + + public MauiPicker(UIPickerView? uIPickerView) + { + UIPickerView = uIPickerView; + + string[] actions = { "copy:", "select:", "selectAll:" }; + _enableActions = new HashSet(actions); + } + + public UIPickerView? UIPickerView { get; set; } + + public override bool CanPerform(Selector action, NSObject? withSender) + => _enableActions.Contains(action.Name); + } +} diff --git a/src/Core/src/Platform/iOS/MauiTimePicker.cs b/src/Core/src/Platform/iOS/MauiTimePicker.cs index d6a2c5648f19..8a8a46d1e8dd 100644 --- a/src/Core/src/Platform/iOS/MauiTimePicker.cs +++ b/src/Core/src/Platform/iOS/MauiTimePicker.cs @@ -5,21 +5,6 @@ namespace Microsoft.Maui { - public class NoCaretField : UITextField - { - public NoCaretField() : base(new RectangleF()) - { - SpellCheckingType = UITextSpellCheckingType.No; - AutocorrectionType = UITextAutocorrectionType.No; - AutocapitalizationType = UITextAutocapitalizationType.None; - } - - public override RectangleF GetCaretRectForPosition(UITextPosition? position) - { - return new RectangleF(); - } - } - public class MauiTimePicker : NoCaretField { readonly Action _dateSelected; diff --git a/src/Core/src/Platform/iOS/NoCaretField.cs b/src/Core/src/Platform/iOS/NoCaretField.cs new file mode 100644 index 000000000000..5fdcb719eb90 --- /dev/null +++ b/src/Core/src/Platform/iOS/NoCaretField.cs @@ -0,0 +1,20 @@ +using UIKit; +using RectangleF = CoreGraphics.CGRect; + +namespace Microsoft.Maui +{ + public class NoCaretField : UITextField + { + public NoCaretField() : base(new RectangleF()) + { + SpellCheckingType = UITextSpellCheckingType.No; + AutocorrectionType = UITextAutocorrectionType.No; + AutocapitalizationType = UITextAutocapitalizationType.None; + } + + public override RectangleF GetCaretRectForPosition(UITextPosition? position) + { + return new RectangleF(); + } + } +} \ No newline at end of file diff --git a/src/Core/src/Platform/iOS/PickerExtensions.cs b/src/Core/src/Platform/iOS/PickerExtensions.cs new file mode 100644 index 000000000000..df2847b7a885 --- /dev/null +++ b/src/Core/src/Platform/iOS/PickerExtensions.cs @@ -0,0 +1,70 @@ +using System; +using Foundation; +using UIKit; +using Microsoft.Maui.Handlers; + +namespace Microsoft.Maui +{ + public static class PickerExtensions + { + public static void UpdateTitle(this MauiPicker nativePicker, IPicker picker) => + nativePicker.UpdatePicker(picker); + + public static void UpdateSelectedIndex(this MauiPicker nativePicker, IPicker picker) => + nativePicker.SetSelectedIndex(picker, picker.SelectedIndex); + + internal static void UpdatePicker(this MauiPicker nativePicker, IPicker picker) + { + var selectedIndex = picker.SelectedIndex; + var items = picker.Items; + + nativePicker.Text = selectedIndex == -1 || items == null || selectedIndex >= items.Count ? string.Empty : items[selectedIndex]; + + var pickerView = nativePicker.UIPickerView; + pickerView?.ReloadAllComponents(); + + if (items == null || items.Count == 0) + return; + + nativePicker.SetSelectedIndex(picker, selectedIndex); + nativePicker.SetSelectedItem(picker); + } + + internal static void SetSelectedIndex(this MauiPicker nativePicker, IPicker picker, int selectedIndex = 0) + { + picker.SelectedIndex = selectedIndex; + + var pickerView = nativePicker.UIPickerView; + + if (pickerView?.Model is PickerSource source) + { + source.SelectedIndex = selectedIndex; + source.SelectedItem = selectedIndex >= 0 ? picker.Items[selectedIndex] : null; + } + + pickerView?.Select(Math.Max(selectedIndex, 0), 0, true); + } + + internal static void SetSelectedItem(this MauiPicker nativePicker, IPicker picker) + { + if (nativePicker == null) + return; + + int index = picker.SelectedIndex; + + if (index == -1) + { + picker.SelectedItem = null; + return; + } + + if (picker.ItemsSource != null) + { + picker.SelectedItem = picker.ItemsSource[index]; + return; + } + + picker.SelectedItem = picker.Items[index]; + } + } +} \ No newline at end of file diff --git a/src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.Android.cs b/src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.Android.cs new file mode 100644 index 000000000000..825a3d7cb714 --- /dev/null +++ b/src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.Android.cs @@ -0,0 +1,26 @@ +using System.Threading.Tasks; +using Microsoft.Maui.DeviceTests.Stubs; +using Microsoft.Maui.Handlers; + +namespace Microsoft.Maui.DeviceTests +{ + public partial class PickerHandlerTests + { + [Fact(DisplayName = "Title Initializes Correctly")] + public async Task TitleInitializesCorrectly() + { + var picker = new PickerStub + { + Title = "Select an Item" + }; + + await ValidatePropertyInitValue(picker, () => picker.Title, GetNativeTitle, picker.Title); + } + + MauiPicker GetNativePicker(PickerHandler pickerHandler) => + (MauiPicker)pickerHandler.View; + + string GetNativeTitle(PickerHandler pickerHandler) => + GetNativePicker(pickerHandler).Hint; + } +} \ No newline at end of file diff --git a/src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.cs b/src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.cs new file mode 100644 index 000000000000..9a898609461f --- /dev/null +++ b/src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.cs @@ -0,0 +1,13 @@ +using Microsoft.Maui.DeviceTests.Stubs; +using Microsoft.Maui.Handlers; + +namespace Microsoft.Maui.DeviceTests +{ + [Category(TestCategory.Picker)] + public partial class PickerHandlerTests : HandlerTestBase + { + public PickerHandlerTests(HandlerTestFixture fixture) : base(fixture) + { + } + } +} \ No newline at end of file diff --git a/src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.iOS.cs b/src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.iOS.cs new file mode 100644 index 000000000000..b632b3da9f44 --- /dev/null +++ b/src/Core/tests/DeviceTests/Handlers/Picker/PickerHandlerTests.iOS.cs @@ -0,0 +1,40 @@ +using System.Threading.Tasks; +using Microsoft.Maui.Handlers; +using Xunit; + +namespace Microsoft.Maui.DeviceTests +{ + public partial class PickerHandlerTests + { + MauiPicker GetNativePicker(PickerHandler pickerHandler) => + (MauiPicker)pickerHandler.View; + + string GetNativeTitle(PickerHandler pickerHandler) => + GetNativePicker(pickerHandler).Text; + + string GetNativeText(PickerHandler pickerHandler) => + GetNativePicker(pickerHandler).Text; + + async Task ValidateNativeItemsSource(IPicker picker, int itemsCount) + { + var expected = await GetValueAsync(picker, handler => + { + var pickerView = GetNativePicker(handler).UIPickerView; + var model = (PickerSource)pickerView.Model; + return model.GetRowsInComponent(pickerView, 0); + }); + Assert.Equal(expected, itemsCount); + } + + async Task ValidateNativeSelectedIndex(IPicker slider, int selectedIndex) + { + var expected = await GetValueAsync(slider, handler => + { + var pickerView = GetNativePicker(handler).UIPickerView; + var model = (PickerSource)pickerView.Model; + return model.SelectedIndex; + }); + Assert.Equal(expected, selectedIndex); + } + } +} \ No newline at end of file diff --git a/src/Core/tests/DeviceTests/Stubs/PickerStub.cs b/src/Core/tests/DeviceTests/Stubs/PickerStub.cs new file mode 100644 index 000000000000..d8dccb5e9b6f --- /dev/null +++ b/src/Core/tests/DeviceTests/Stubs/PickerStub.cs @@ -0,0 +1,18 @@ +using System.Collections; +using System.Collections.Generic; + +namespace Microsoft.Maui.DeviceTests.Stubs +{ + public partial class PickerStub : StubBase, IPicker + { + public string Title { get; set; } + + public IList Items { get; set; } = new List(); + + public IList ItemsSource { get; set; } + + public int SelectedIndex { get; set; } = -1; + + public object SelectedItem { get; set; } + } +} \ No newline at end of file diff --git a/src/Core/tests/DeviceTests/TestCategory.cs b/src/Core/tests/DeviceTests/TestCategory.cs index 5494b608e9bb..2ed3e3928aad 100644 --- a/src/Core/tests/DeviceTests/TestCategory.cs +++ b/src/Core/tests/DeviceTests/TestCategory.cs @@ -8,6 +8,7 @@ public static class TestCategory public const string Entry = "Entry"; public const string Label = "Label"; public const string Layout = "Layout"; + public const string Picker = "Picker"; public const string SearchBar = "SearchBar"; public const string Slider = "Slider"; public const string Stepper = "Stepper";