From 10ecb92c4eaf2049a0a401d10e21f43637ee7deb Mon Sep 17 00:00:00 2001 From: Brian Runck Date: Tue, 9 Mar 2021 23:10:16 -0500 Subject: [PATCH 01/10] Editor placeholder in progress --- .../src/Android/Renderers/EditorRenderer.cs | 1 + .../Core/src/iOS/Renderers/EditorRenderer.cs | 1 + .../samples/Controls.Sample/Pages/MainPage.cs | 2 + .../src/Core/HandlerImpl/Editor.Impl.cs | 2 +- src/Core/src/Core/IEditor.cs | 4 +- .../Handlers/Editor/EditorHandler.Android.cs | 12 +++- .../Handlers/Editor/EditorHandler.Standard.cs | 4 +- src/Core/src/Handlers/Editor/EditorHandler.cs | 6 +- .../src/Handlers/Editor/EditorHandler.iOS.cs | 65 ++++++++++++++++++- .../src/Platform/Android/EditorExtensions.cs | 12 +++- .../src/Platform/Standard/EditorExtensions.cs | 10 +++ src/Core/src/Platform/iOS/EditorExtensions.cs | 9 ++- 12 files changed, 113 insertions(+), 15 deletions(-) create mode 100644 src/Core/src/Platform/Standard/EditorExtensions.cs diff --git a/src/Compatibility/Core/src/Android/Renderers/EditorRenderer.cs b/src/Compatibility/Core/src/Android/Renderers/EditorRenderer.cs index 0a9c3b842c24..6ef55f6eb0f2 100644 --- a/src/Compatibility/Core/src/Android/Renderers/EditorRenderer.cs +++ b/src/Compatibility/Core/src/Android/Renderers/EditorRenderer.cs @@ -266,6 +266,7 @@ void UpdateText() abstract protected void UpdateTextColor(); + [PortHandler] protected virtual void UpdatePlaceholderText() { if (EditText.Hint == Element.Placeholder) diff --git a/src/Compatibility/Core/src/iOS/Renderers/EditorRenderer.cs b/src/Compatibility/Core/src/iOS/Renderers/EditorRenderer.cs index f7bc0ad41f0e..f6189c83c515 100644 --- a/src/Compatibility/Core/src/iOS/Renderers/EditorRenderer.cs +++ b/src/Compatibility/Core/src/iOS/Renderers/EditorRenderer.cs @@ -63,6 +63,7 @@ protected internal override void UpdateFont() _placeholderLabel.Font = Element.ToUIFont(); } + [PortHandler] protected internal override void UpdatePlaceholderText() { _placeholderLabel.Text = Element.Placeholder; diff --git a/src/Controls/samples/Controls.Sample/Pages/MainPage.cs b/src/Controls/samples/Controls.Sample/Pages/MainPage.cs index 557c3c4baa86..e7e0d5e35383 100644 --- a/src/Controls/samples/Controls.Sample/Pages/MainPage.cs +++ b/src/Controls/samples/Controls.Sample/Pages/MainPage.cs @@ -93,6 +93,8 @@ void SetupMauiLayout() verticalStack.Add(new Entry { Placeholder = "This should be placeholder text" }); verticalStack.Add(new Entry { Text = "This should be read only property", IsReadOnly = true }); + verticalStack.Add(new Editor { Placeholder = "This is a placeholder."} ); + verticalStack.Add(new ProgressBar { Progress = 0.5 }); verticalStack.Add(new ProgressBar { Progress = 0.5, BackgroundColor = Color.LightCoral }); verticalStack.Add(new ProgressBar { Progress = 0.5, ProgressColor = Color.Purple }); diff --git a/src/Controls/src/Core/HandlerImpl/Editor.Impl.cs b/src/Controls/src/Core/HandlerImpl/Editor.Impl.cs index 62f659d845aa..ee2c40c1b86d 100644 --- a/src/Controls/src/Core/HandlerImpl/Editor.Impl.cs +++ b/src/Controls/src/Core/HandlerImpl/Editor.Impl.cs @@ -6,4 +6,4 @@ public partial class Editor : IEditor Font IText.Font => _font ??= Font.OfSize(FontFamily, FontSize).WithAttributes(FontAttributes); } -} \ No newline at end of file +} diff --git a/src/Core/src/Core/IEditor.cs b/src/Core/src/Core/IEditor.cs index f9dfe526edc6..b805cbf89f63 100644 --- a/src/Core/src/Core/IEditor.cs +++ b/src/Core/src/Core/IEditor.cs @@ -3,8 +3,8 @@ /// /// Represents a View used to accept multi-line input. /// - public interface IEditor : IView, IText + public interface IEditor : IView, ITextInput { } -} \ No newline at end of file +} diff --git a/src/Core/src/Handlers/Editor/EditorHandler.Android.cs b/src/Core/src/Handlers/Editor/EditorHandler.Android.cs index e831c804caad..f62c467b5167 100644 --- a/src/Core/src/Handlers/Editor/EditorHandler.Android.cs +++ b/src/Core/src/Handlers/Editor/EditorHandler.Android.cs @@ -1,6 +1,8 @@ -using Android.Views; +using Android.Views; using Android.Views.InputMethods; using AndroidX.AppCompat.Widget; +using Android.Content.Res; +using Android.Text; namespace Microsoft.Maui.Handlers { @@ -25,5 +27,11 @@ public static void MapText(EditorHandler handler, IEditor editor) { handler.TypedNativeView?.UpdateText(editor); } + + public static void MapPlaceholder(EditorHandler handler, IEditor editor) + { + handler.TypedNativeView?.UpdatePlaceholder(editor); + } } -} \ No newline at end of file +} + diff --git a/src/Core/src/Handlers/Editor/EditorHandler.Standard.cs b/src/Core/src/Handlers/Editor/EditorHandler.Standard.cs index f8247d149507..2b25db5fd2d7 100644 --- a/src/Core/src/Handlers/Editor/EditorHandler.Standard.cs +++ b/src/Core/src/Handlers/Editor/EditorHandler.Standard.cs @@ -7,5 +7,7 @@ public partial class EditorHandler : AbstractViewHandler protected override object CreateNativeView() => throw new NotImplementedException(); public static void MapText(IViewHandler handler, IEditor editor) { } + + public static void MapPlaceholder(IViewHandler handler, IEditor editor) { } } -} \ No newline at end of file +} diff --git a/src/Core/src/Handlers/Editor/EditorHandler.cs b/src/Core/src/Handlers/Editor/EditorHandler.cs index b64426da7ef1..6600d933adbb 100644 --- a/src/Core/src/Handlers/Editor/EditorHandler.cs +++ b/src/Core/src/Handlers/Editor/EditorHandler.cs @@ -4,12 +4,12 @@ public partial class EditorHandler { public static PropertyMapper EditorMapper = new PropertyMapper(ViewHandler.ViewMapper) { - [nameof(IEditor.Text)] = MapText + [nameof(IEditor.Text)] = MapText, + [nameof(IEditor.Placeholder)] = MapPlaceholder }; public EditorHandler() : base(EditorMapper) { - } public EditorHandler(PropertyMapper mapper) : base(mapper ?? EditorMapper) @@ -17,4 +17,4 @@ public EditorHandler(PropertyMapper mapper) : base(mapper ?? EditorMapper) } } -} \ No newline at end of file +} diff --git a/src/Core/src/Handlers/Editor/EditorHandler.iOS.cs b/src/Core/src/Handlers/Editor/EditorHandler.iOS.cs index 096bbeed9859..b0b50431382f 100644 --- a/src/Core/src/Handlers/Editor/EditorHandler.iOS.cs +++ b/src/Core/src/Handlers/Editor/EditorHandler.iOS.cs @@ -1,4 +1,4 @@ -using CoreGraphics; +using CoreGraphics; using UIKit; namespace Microsoft.Maui.Handlers @@ -6,12 +6,28 @@ namespace Microsoft.Maui.Handlers public partial class EditorHandler : AbstractViewHandler { static readonly int BaseHeight = 30; + UILabel PlaceholderLabel { get; set; } protected override UITextView CreateNativeView() { return new UITextView(CGRect.Empty); } + protected override void SetupDefaults(UITextField nativeView) + { + if (PlaceholderLabel == null) + { + PlaceholderLabel = new UILabel + { + BackgroundColor = UIColor.Clear, + Frame = new RectangleF(0, 0, Frame.Width, Frame.Height), + Lines = 0 + }; + } + + CreatePlaceholderLabel(); + } + public override Size GetDesiredSize(double widthConstraint, double heightConstraint) => new SizeRequest(new Size(widthConstraint, BaseHeight)); @@ -19,5 +35,50 @@ public static void MapText(EditorHandler handler, IEditor editor) { handler.TypedNativeView?.UpdateText(editor); } + + public static void MapPlaceholder(EditorHandler handler, IEditor editor) + { + PlaceholderLabel.Text = Element.Placeholder; + + PlaceholderLabel.SizeToFit(); + } + + void CreatePlaceholderLabel() + { + if (TypedNativeView == null) + { + return; + } + + if (VirtualView == null) + { + return; + } + + TypedNativeView.AddSubview(PlaceholderLabel); + + var edgeInsets = TypedNativeView.TextContainerInset; + var lineFragmentPadding = TypedNativeView.TextContainer.LineFragmentPadding; + + var vConstraints = NSLayoutConstraint.FromVisualFormat( + "V:|-" + edgeInsets.Top + "-[PlaceholderLabel]-" + edgeInsets.Bottom + "-|", 0, new NSDictionary(), + NSDictionary.FromObjectsAndKeys( + new NSObject[] {PlaceholderLabel}, new NSObject[] {new NSString("PlaceholderLabel")}) + ); + + var hConstraints = NSLayoutConstraint.FromVisualFormat( + "H:|-" + lineFragmentPadding + "-[PlaceholderLabel]-" + lineFragmentPadding + "-|", + 0, new NSDictionary(), + NSDictionary.FromObjectsAndKeys( + new NSObject[] { PlaceholderLabel }, new NSObject[] { new NSString("PlaceholderLabel") }) + ); + + PlaceholderLabel.TranslatesAutoresizingMaskIntoConstraints = false; + // TODO: Add when we have CharacterSpacing + //PlaceholderLabel.AttributedText = PlaceholderLabel.AttributedText.AddCharacterSpacing(VirtualView.Placeholder, VirtualView.CharacterSpacing); + + TypedNativeView.AddConstraints(hConstraints); + TypedNativeView.AddConstraints(vConstraints); + } } -} \ No newline at end of file +} diff --git a/src/Core/src/Platform/Android/EditorExtensions.cs b/src/Core/src/Platform/Android/EditorExtensions.cs index 9becc6ce9c63..b503d5c551bb 100644 --- a/src/Core/src/Platform/Android/EditorExtensions.cs +++ b/src/Core/src/Platform/Android/EditorExtensions.cs @@ -1,4 +1,4 @@ -using AndroidX.AppCompat.Widget; +using AndroidX.AppCompat.Widget; namespace Microsoft.Maui { @@ -18,5 +18,13 @@ public static void UpdateText(this AppCompatEditText editText, IEditor editor) editText.SetSelection(text.Length); } + + public static void UpdatePlaceholder(this AppCompatEditText editText, IEditor editor) + { + if (editText.Hint == editor.Placeholder) + return; + + editText.Hint = editor.Placeholder; + } } -} \ No newline at end of file +} diff --git a/src/Core/src/Platform/Standard/EditorExtensions.cs b/src/Core/src/Platform/Standard/EditorExtensions.cs new file mode 100644 index 000000000000..632e91c7d090 --- /dev/null +++ b/src/Core/src/Platform/Standard/EditorExtensions.cs @@ -0,0 +1,10 @@ +namespace Microsoft.Maui +{ + public static class EditorExtensions + { + public static void UpdatePlaceholder(this object nothing, IEditor editor) + { + + } + } +} diff --git a/src/Core/src/Platform/iOS/EditorExtensions.cs b/src/Core/src/Platform/iOS/EditorExtensions.cs index 47485559380c..f832243b3c9f 100644 --- a/src/Core/src/Platform/iOS/EditorExtensions.cs +++ b/src/Core/src/Platform/iOS/EditorExtensions.cs @@ -1,4 +1,4 @@ -using UIKit; +using UIKit; namespace Microsoft.Maui { @@ -13,5 +13,10 @@ public static void UpdateText(this UITextView textView, IEditor editor) textView.Text = text; } } + + public static void UpdatePlaceholder(UITextField text, IEditor editor) + { + + } } -} \ No newline at end of file +} From c463a8dc876ff60f83b2aafd2f4c85113d97577a Mon Sep 17 00:00:00 2001 From: Brian Runck Date: Mon, 15 Mar 2021 20:30:23 -0400 Subject: [PATCH 02/10] mostly iOS changes --- .../Core/src/AppHostBuilderExtensions.cs | 4 +- .../src/Handlers/Editor/EditorHandler.iOS.cs | 50 +++++++------------ src/Core/src/Platform/iOS/EditorExtensions.cs | 15 +++++- 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/Compatibility/Core/src/AppHostBuilderExtensions.cs b/src/Compatibility/Core/src/AppHostBuilderExtensions.cs index b6c9b9ed13bc..e3c3b1a3fb48 100644 --- a/src/Compatibility/Core/src/AppHostBuilderExtensions.cs +++ b/src/Compatibility/Core/src/AppHostBuilderExtensions.cs @@ -11,8 +11,8 @@ public static IAppHostBuilder RegisterCompatibilityRenderers(this IAppHostBuilde // This won't really be a thing once we have all the handlers built var defaultHandlers = new List { - typeof(Button) , - typeof(Editor), + typeof(Button) , + typeof(Editor), typeof(Entry) , typeof(ContentPage) , typeof(Page) , diff --git a/src/Core/src/Handlers/Editor/EditorHandler.iOS.cs b/src/Core/src/Handlers/Editor/EditorHandler.iOS.cs index b0b50431382f..eb04c608d8c5 100644 --- a/src/Core/src/Handlers/Editor/EditorHandler.iOS.cs +++ b/src/Core/src/Handlers/Editor/EditorHandler.iOS.cs @@ -1,4 +1,5 @@ using CoreGraphics; +using Foundation; using UIKit; namespace Microsoft.Maui.Handlers @@ -10,22 +11,18 @@ public partial class EditorHandler : AbstractViewHandler protected override UITextView CreateNativeView() { - return new UITextView(CGRect.Empty); - } + var textView = new UITextView(CGRect.Empty); - protected override void SetupDefaults(UITextField nativeView) - { - if (PlaceholderLabel == null) + PlaceholderLabel = new UILabel { - PlaceholderLabel = new UILabel - { - BackgroundColor = UIColor.Clear, - Frame = new RectangleF(0, 0, Frame.Width, Frame.Height), - Lines = 0 - }; - } + BackgroundColor = UIColor.Clear, + Frame = new CGRect(0, 0, textView.Frame.Width, textView.Frame.Height), + Lines = 0 + }; - CreatePlaceholderLabel(); + CreatePlaceholderLabel(textView); + + return textView; } public override Size GetDesiredSize(double widthConstraint, double heightConstraint) => @@ -38,27 +35,15 @@ public static void MapText(EditorHandler handler, IEditor editor) public static void MapPlaceholder(EditorHandler handler, IEditor editor) { - PlaceholderLabel.Text = Element.Placeholder; - - PlaceholderLabel.SizeToFit(); + handler.TypedNativeView?.UpdatePlaceholder(editor); } - void CreatePlaceholderLabel() + void CreatePlaceholderLabel(UITextView textView) { - if (TypedNativeView == null) - { - return; - } - - if (VirtualView == null) - { - return; - } - - TypedNativeView.AddSubview(PlaceholderLabel); + textView.AddSubview(PlaceholderLabel); - var edgeInsets = TypedNativeView.TextContainerInset; - var lineFragmentPadding = TypedNativeView.TextContainer.LineFragmentPadding; + var edgeInsets = textView.TextContainerInset; + var lineFragmentPadding = textView.TextContainer.LineFragmentPadding; var vConstraints = NSLayoutConstraint.FromVisualFormat( "V:|-" + edgeInsets.Top + "-[PlaceholderLabel]-" + edgeInsets.Bottom + "-|", 0, new NSDictionary(), @@ -75,10 +60,11 @@ void CreatePlaceholderLabel() PlaceholderLabel.TranslatesAutoresizingMaskIntoConstraints = false; // TODO: Add when we have CharacterSpacing + // TODO: maybe put in extension method for setting these properties //PlaceholderLabel.AttributedText = PlaceholderLabel.AttributedText.AddCharacterSpacing(VirtualView.Placeholder, VirtualView.CharacterSpacing); - TypedNativeView.AddConstraints(hConstraints); - TypedNativeView.AddConstraints(vConstraints); + textView.AddConstraints(hConstraints); + textView.AddConstraints(vConstraints); } } } diff --git a/src/Core/src/Platform/iOS/EditorExtensions.cs b/src/Core/src/Platform/iOS/EditorExtensions.cs index f832243b3c9f..e9f159de4f49 100644 --- a/src/Core/src/Platform/iOS/EditorExtensions.cs +++ b/src/Core/src/Platform/iOS/EditorExtensions.cs @@ -12,11 +12,24 @@ public static void UpdateText(this UITextView textView, IEditor editor) { textView.Text = text; } + + var placeholderLabel = textView.FindDescendantView(); + + if (placeholderLabel == null) + return; + + placeholderLabel.Hidden = !string.IsNullOrEmpty(textView.Text); } - public static void UpdatePlaceholder(UITextField text, IEditor editor) + public static void UpdatePlaceholder(this UITextView textView, IEditor editor) { + var placeholderLabel = textView.FindDescendantView(); + + if (placeholderLabel == null) + return; + placeholderLabel.Text = editor.Placeholder; + placeholderLabel.SizeToFit(); } } } From b119d3aa3d1c962f289ac27cd6d31ff19a6856fc Mon Sep 17 00:00:00 2001 From: Brian Runck Date: Wed, 17 Mar 2021 20:52:47 -0400 Subject: [PATCH 03/10] Placholder text and color properties; added tests --- .../src/Android/Renderers/EditorRenderer.cs | 2 + .../Core/src/iOS/Renderers/EditorRenderer.cs | 1 + .../samples/Controls.Sample/Pages/MainPage.cs | 3 +- src/Core/src/Core-net6.csproj | 6 +- src/Core/src/Core/IEditor.cs | 5 +- .../Handlers/Editor/EditorHandler.Android.cs | 17 ++++- .../Handlers/Editor/EditorHandler.Standard.cs | 2 + src/Core/src/Handlers/Editor/EditorHandler.cs | 3 +- .../src/Handlers/Editor/EditorHandler.iOS.cs | 73 ++++++++----------- .../src/Platform/Android/EditorExtensions.cs | 20 +++++ .../src/Platform/MaciOS/ColorExtensions.cs | 3 - src/Core/src/Platform/iOS/EditorExtensions.cs | 25 +++---- src/Core/src/Platform/iOS/MauiTextView.cs | 67 +++++++++++++++++ src/Core/src/Primitives/Color.cs | 1 - .../Editor/EditorHandlerTests.Android.cs | 6 ++ .../Handlers/Editor/EditorHandlerTests.cs | 70 +++++++++++++++++- .../Handlers/Editor/EditorHandlerTests.iOS.cs | 12 ++- .../tests/DeviceTests/Stubs/EditorStub.cs | 7 ++ 18 files changed, 250 insertions(+), 73 deletions(-) create mode 100644 src/Core/src/Platform/iOS/MauiTextView.cs diff --git a/src/Compatibility/Core/src/Android/Renderers/EditorRenderer.cs b/src/Compatibility/Core/src/Android/Renderers/EditorRenderer.cs index 6ef55f6eb0f2..2b69aba4ef1a 100644 --- a/src/Compatibility/Core/src/Android/Renderers/EditorRenderer.cs +++ b/src/Compatibility/Core/src/Android/Renderers/EditorRenderer.cs @@ -32,6 +32,7 @@ protected override FormsEditText CreateNativeControl() protected override EditText EditText => Control; + [PortHandler] protected override void UpdatePlaceholderColor() { _hintColorSwitcher = _hintColorSwitcher ?? new TextColorSwitcher(EditText.HintTextColors, Element.UseLegacyColorManagement()); @@ -275,6 +276,7 @@ protected virtual void UpdatePlaceholderText() EditText.Hint = Element.Placeholder; } + [PortHandler] abstract protected void UpdatePlaceholderColor(); void OnKeyboardBackPressed(object sender, EventArgs eventArgs) diff --git a/src/Compatibility/Core/src/iOS/Renderers/EditorRenderer.cs b/src/Compatibility/Core/src/iOS/Renderers/EditorRenderer.cs index f6189c83c515..1f44ed26ea58 100644 --- a/src/Compatibility/Core/src/iOS/Renderers/EditorRenderer.cs +++ b/src/Compatibility/Core/src/iOS/Renderers/EditorRenderer.cs @@ -84,6 +84,7 @@ protected internal override void UpdateCharacterSpacing() _placeholderLabel.AttributedText = placeHolder; } + [PortHandler] protected internal override void UpdatePlaceholderColor() { Color placeholderColor = Element.PlaceholderColor; diff --git a/src/Controls/samples/Controls.Sample/Pages/MainPage.cs b/src/Controls/samples/Controls.Sample/Pages/MainPage.cs index e707d8ec63e1..61b00addde07 100644 --- a/src/Controls/samples/Controls.Sample/Pages/MainPage.cs +++ b/src/Controls/samples/Controls.Sample/Pages/MainPage.cs @@ -35,6 +35,7 @@ void SetupMauiLayout() var verticalStack = new VerticalStackLayout() { Spacing = 5, BackgroundColor = Color.AntiqueWhite }; var horizontalStack = new HorizontalStackLayout() { Spacing = 2, BackgroundColor = Color.CornflowerBlue }; + verticalStack.Add(new Label { Text = " ", Padding = new Thickness(10) }); var label = new Label { Text = "centered text", BackgroundColor = Color.Fuchsia, HorizontalTextAlignment = TextAlignment.End }; @@ -50,6 +51,7 @@ void SetupMauiLayout() verticalStack.Add(new Label { Text = loremIpsum, LineBreakMode = LineBreakMode.TailTruncation }); verticalStack.Add(new Label { Text = loremIpsum, MaxLines = 2, LineBreakMode = LineBreakMode.TailTruncation }); + verticalStack.Add(new Editor { Placeholder = "This is an editor placeholder." } ); var paddingButton = new Button { @@ -95,7 +97,6 @@ void SetupMauiLayout() verticalStack.Add(new Entry { Placeholder = "This should be placeholder text" }); verticalStack.Add(new Entry { Text = "This should be read only property", IsReadOnly = true }); - verticalStack.Add(new Editor { Placeholder = "This is a placeholder."} ); verticalStack.Add(new ProgressBar { Progress = 0.5 }); verticalStack.Add(new ProgressBar { Progress = 0.5, BackgroundColor = Color.LightCoral }); diff --git a/src/Core/src/Core-net6.csproj b/src/Core/src/Core-net6.csproj index ba425f47c13c..b2be50d36c7f 100644 --- a/src/Core/src/Core-net6.csproj +++ b/src/Core/src/Core-net6.csproj @@ -19,9 +19,9 @@ - - - + + + diff --git a/src/Core/src/Core/IEditor.cs b/src/Core/src/Core/IEditor.cs index b805cbf89f63..65b1880e0595 100644 --- a/src/Core/src/Core/IEditor.cs +++ b/src/Core/src/Core/IEditor.cs @@ -5,6 +5,9 @@ /// public interface IEditor : IView, ITextInput { - + /// + /// Gets or sets the placeholder text color. + /// + Color PlaceholderColor { get; set; } } } diff --git a/src/Core/src/Handlers/Editor/EditorHandler.Android.cs b/src/Core/src/Handlers/Editor/EditorHandler.Android.cs index f62c467b5167..94f3e3e1de59 100644 --- a/src/Core/src/Handlers/Editor/EditorHandler.Android.cs +++ b/src/Core/src/Handlers/Editor/EditorHandler.Android.cs @@ -2,12 +2,13 @@ using Android.Views.InputMethods; using AndroidX.AppCompat.Widget; using Android.Content.Res; -using Android.Text; namespace Microsoft.Maui.Handlers { public partial class EditorHandler : AbstractViewHandler { + static ColorStateList? DefaultPlaceholderTextColors { get; set; } + protected override AppCompatEditText CreateNativeView() { var editText = new AppCompatEditText(Context) @@ -23,6 +24,12 @@ protected override AppCompatEditText CreateNativeView() return editText; } + protected override void SetupDefaults(AppCompatEditText nativeView) + { + base.SetupDefaults(nativeView); + DefaultPlaceholderTextColors = nativeView.HintTextColors; + } + public static void MapText(EditorHandler handler, IEditor editor) { handler.TypedNativeView?.UpdateText(editor); @@ -31,7 +38,11 @@ public static void MapText(EditorHandler handler, IEditor editor) public static void MapPlaceholder(EditorHandler handler, IEditor editor) { handler.TypedNativeView?.UpdatePlaceholder(editor); - } + } + + public static void MapPlaceholderColor(EditorHandler handler, IEditor editor) + { + handler.TypedNativeView?.UpdatePlaceholderColor(editor, DefaultPlaceholderTextColors); + } } } - diff --git a/src/Core/src/Handlers/Editor/EditorHandler.Standard.cs b/src/Core/src/Handlers/Editor/EditorHandler.Standard.cs index 2b25db5fd2d7..0fcf8d5dc360 100644 --- a/src/Core/src/Handlers/Editor/EditorHandler.Standard.cs +++ b/src/Core/src/Handlers/Editor/EditorHandler.Standard.cs @@ -9,5 +9,7 @@ public partial class EditorHandler : AbstractViewHandler public static void MapText(IViewHandler handler, IEditor editor) { } public static void MapPlaceholder(IViewHandler handler, IEditor editor) { } + + public static void MapPlaceholderColor(IViewHandler handler, IEditor editor) { } } } diff --git a/src/Core/src/Handlers/Editor/EditorHandler.cs b/src/Core/src/Handlers/Editor/EditorHandler.cs index 6600d933adbb..b1f7c6eaf2f8 100644 --- a/src/Core/src/Handlers/Editor/EditorHandler.cs +++ b/src/Core/src/Handlers/Editor/EditorHandler.cs @@ -5,7 +5,8 @@ public partial class EditorHandler public static PropertyMapper EditorMapper = new PropertyMapper(ViewHandler.ViewMapper) { [nameof(IEditor.Text)] = MapText, - [nameof(IEditor.Placeholder)] = MapPlaceholder + [nameof(IEditor.Placeholder)] = MapPlaceholder, + [nameof(IEditor.PlaceholderColor)] = MapPlaceholderColor }; public EditorHandler() : base(EditorMapper) diff --git a/src/Core/src/Handlers/Editor/EditorHandler.iOS.cs b/src/Core/src/Handlers/Editor/EditorHandler.iOS.cs index eb04c608d8c5..504d5ddbba71 100644 --- a/src/Core/src/Handlers/Editor/EditorHandler.iOS.cs +++ b/src/Core/src/Handlers/Editor/EditorHandler.iOS.cs @@ -1,70 +1,61 @@ using CoreGraphics; -using Foundation; using UIKit; +using Microsoft.Maui.Platform.iOS; namespace Microsoft.Maui.Handlers { - public partial class EditorHandler : AbstractViewHandler + public partial class EditorHandler : AbstractViewHandler { static readonly int BaseHeight = 30; - UILabel PlaceholderLabel { get; set; } + + static readonly UIColor DefaultPlaceholderColor = ColorExtensions.PlaceholderColor; - protected override UITextView CreateNativeView() + protected override MauiTextView CreateNativeView() { - var textView = new UITextView(CGRect.Empty); - - PlaceholderLabel = new UILabel - { - BackgroundColor = UIColor.Clear, - Frame = new CGRect(0, 0, textView.Frame.Width, textView.Frame.Height), - Lines = 0 - }; - - CreatePlaceholderLabel(textView); + return new MauiTextView(CGRect.Empty); + } - return textView; + protected override void SetupDefaults(MauiTextView nativeView) + { + nativeView.PlaceholderTextColor = DefaultPlaceholderColor; } public override Size GetDesiredSize(double widthConstraint, double heightConstraint) => new SizeRequest(new Size(widthConstraint, BaseHeight)); - public static void MapText(EditorHandler handler, IEditor editor) + protected override void ConnectHandler(MauiTextView nativeView) { - handler.TypedNativeView?.UpdateText(editor); + nativeView.Changed += OnChanged; } - public static void MapPlaceholder(EditorHandler handler, IEditor editor) + protected override void DisconnectHandler(MauiTextView nativeView) { - handler.TypedNativeView?.UpdatePlaceholder(editor); + nativeView.Changed -= OnChanged; } - void CreatePlaceholderLabel(UITextView textView) - { - textView.AddSubview(PlaceholderLabel); + void OnChanged(object? sender, System.EventArgs e) => OnTextChanged(); - var edgeInsets = textView.TextContainerInset; - var lineFragmentPadding = textView.TextContainer.LineFragmentPadding; + void OnTextChanged() + { + if (TypedNativeView == null) + return; - var vConstraints = NSLayoutConstraint.FromVisualFormat( - "V:|-" + edgeInsets.Top + "-[PlaceholderLabel]-" + edgeInsets.Bottom + "-|", 0, new NSDictionary(), - NSDictionary.FromObjectsAndKeys( - new NSObject[] {PlaceholderLabel}, new NSObject[] {new NSString("PlaceholderLabel")}) - ); + TypedNativeView.HidePlaceholder(!string.IsNullOrEmpty(TypedNativeView.Text)); + } - var hConstraints = NSLayoutConstraint.FromVisualFormat( - "H:|-" + lineFragmentPadding + "-[PlaceholderLabel]-" + lineFragmentPadding + "-|", - 0, new NSDictionary(), - NSDictionary.FromObjectsAndKeys( - new NSObject[] { PlaceholderLabel }, new NSObject[] { new NSString("PlaceholderLabel") }) - ); + public static void MapText(EditorHandler handler, IEditor editor) + { + handler.TypedNativeView?.UpdateText(editor); + } - PlaceholderLabel.TranslatesAutoresizingMaskIntoConstraints = false; - // TODO: Add when we have CharacterSpacing - // TODO: maybe put in extension method for setting these properties - //PlaceholderLabel.AttributedText = PlaceholderLabel.AttributedText.AddCharacterSpacing(VirtualView.Placeholder, VirtualView.CharacterSpacing); + public static void MapPlaceholder(EditorHandler handler, IEditor editor) + { + handler.TypedNativeView?.UpdatePlaceholder(editor); + } - textView.AddConstraints(hConstraints); - textView.AddConstraints(vConstraints); + public static void MapPlaceholderColor(EditorHandler handler, IEditor editor) + { + handler.TypedNativeView?.UpdatePlaceholderColor(editor, DefaultPlaceholderColor); } } } diff --git a/src/Core/src/Platform/Android/EditorExtensions.cs b/src/Core/src/Platform/Android/EditorExtensions.cs index b503d5c551bb..b5e2a6263f2e 100644 --- a/src/Core/src/Platform/Android/EditorExtensions.cs +++ b/src/Core/src/Platform/Android/EditorExtensions.cs @@ -1,3 +1,4 @@ +using Android.Content.Res; using AndroidX.AppCompat.Widget; namespace Microsoft.Maui @@ -26,5 +27,24 @@ public static void UpdatePlaceholder(this AppCompatEditText editText, IEditor ed editText.Hint = editor.Placeholder; } + + public static void UpdatePlaceholderColor(this AppCompatEditText editText, IEditor editor, ColorStateList? defaultColor) + { + var placeholderTextColor = editor.PlaceholderColor; + if (placeholderTextColor.IsDefault) + { + editText.SetHintTextColor(defaultColor); + } + else + { + var androidColor = placeholderTextColor.ToNative(); + + if (!editText.HintTextColors.IsOneColor(ColorExtensions.States, androidColor)) + { + var acolor = androidColor.ToArgb(); + editText.SetHintTextColor(new ColorStateList(ColorExtensions.States, new[] { acolor, acolor })); + } + } + } } } diff --git a/src/Core/src/Platform/MaciOS/ColorExtensions.cs b/src/Core/src/Platform/MaciOS/ColorExtensions.cs index f8c78faeaee8..da570b60cdd7 100644 --- a/src/Core/src/Platform/MaciOS/ColorExtensions.cs +++ b/src/Core/src/Platform/MaciOS/ColorExtensions.cs @@ -33,7 +33,6 @@ internal static UIColor PlaceholderColor { get { - if (NativeVersion.IsAtLeast(13)) return UIColor.PlaceholderTextColor; @@ -45,7 +44,6 @@ internal static UIColor SecondaryLabelColor { get { - if (NativeVersion.IsAtLeast(13)) return UIColor.SecondaryLabelColor; @@ -57,7 +55,6 @@ internal static UIColor BackgroundColor { get { - if (NativeVersion.IsAtLeast(13)) return UIColor.SystemBackgroundColor; diff --git a/src/Core/src/Platform/iOS/EditorExtensions.cs b/src/Core/src/Platform/iOS/EditorExtensions.cs index e9f159de4f49..0038b3b18082 100644 --- a/src/Core/src/Platform/iOS/EditorExtensions.cs +++ b/src/Core/src/Platform/iOS/EditorExtensions.cs @@ -1,4 +1,5 @@ using UIKit; +using Microsoft.Maui.Platform.iOS; namespace Microsoft.Maui { @@ -12,24 +13,20 @@ public static void UpdateText(this UITextView textView, IEditor editor) { textView.Text = text; } - - var placeholderLabel = textView.FindDescendantView(); - - if (placeholderLabel == null) - return; - - placeholderLabel.Hidden = !string.IsNullOrEmpty(textView.Text); } - public static void UpdatePlaceholder(this UITextView textView, IEditor editor) + public static void UpdatePlaceholder(this MauiTextView textView, IEditor editor) { - var placeholderLabel = textView.FindDescendantView(); - - if (placeholderLabel == null) - return; + textView.PlaceholderText = editor.Placeholder; + } - placeholderLabel.Text = editor.Placeholder; - placeholderLabel.SizeToFit(); + public static void UpdatePlaceholderColor(this MauiTextView textView, IEditor editor, UIColor? defaultPlaceholderColor) + { + Color placeholderColor = editor.PlaceholderColor; + if (placeholderColor.IsDefault) + textView.PlaceholderTextColor = defaultPlaceholderColor; + else + textView.PlaceholderTextColor = placeholderColor.ToNative(); } } } diff --git a/src/Core/src/Platform/iOS/MauiTextView.cs b/src/Core/src/Platform/iOS/MauiTextView.cs new file mode 100644 index 000000000000..3a07c9208afa --- /dev/null +++ b/src/Core/src/Platform/iOS/MauiTextView.cs @@ -0,0 +1,67 @@ +using UIKit; +using CoreGraphics; +using Foundation; + +namespace Microsoft.Maui.Platform.iOS +{ + public class MauiTextView : UITextView + { + UILabel PlaceholderLabel { get; } = new UILabel + { + BackgroundColor = UIColor.Clear, + Lines = 0 + }; + + public MauiTextView(CGRect frame) : base(frame) + { + InitPlaceholderLabel(); + } + + public string? PlaceholderText + { + get => PlaceholderLabel.Text; + set + { + PlaceholderLabel.Text = value; + PlaceholderLabel.SizeToFit(); + } + } + + public UIColor? PlaceholderTextColor + { + get => PlaceholderLabel.TextColor; + set => PlaceholderLabel.TextColor = value; + } + + public void HidePlaceholder(bool hide) + { + PlaceholderLabel.Hidden = hide; + } + + void InitPlaceholderLabel() + { + AddSubview(PlaceholderLabel); + + var edgeInsets = TextContainerInset; + var lineFragmentPadding = TextContainer.LineFragmentPadding; + + var vConstraints = NSLayoutConstraint.FromVisualFormat( + "V:|-" + edgeInsets.Top + "-[PlaceholderLabel]-" + edgeInsets.Bottom + "-|", 0, new NSDictionary(), + NSDictionary.FromObjectsAndKeys( + new NSObject[] { PlaceholderLabel }, new NSObject[] { new NSString("PlaceholderLabel") }) + ); + + var hConstraints = NSLayoutConstraint.FromVisualFormat( + "H:|-" + lineFragmentPadding + "-[PlaceholderLabel]-" + lineFragmentPadding + "-|", + 0, new NSDictionary(), + NSDictionary.FromObjectsAndKeys( + new NSObject[] { PlaceholderLabel }, new NSObject[] { new NSString("PlaceholderLabel") }) + ); + + PlaceholderLabel.TranslatesAutoresizingMaskIntoConstraints = false; + + AddConstraints(hConstraints); + AddConstraints(vConstraints); + } + } +} diff --git a/src/Core/src/Primitives/Color.cs b/src/Core/src/Primitives/Color.cs index 2e2f05bd1563..27432166af2b 100644 --- a/src/Core/src/Primitives/Color.cs +++ b/src/Core/src/Primitives/Color.cs @@ -2,7 +2,6 @@ using System.ComponentModel; using System.Diagnostics; using System.Globalization; -using Microsoft.Maui; namespace Microsoft.Maui { diff --git a/src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.Android.cs b/src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.Android.cs index 7972fc0aeae8..b4567ed5c046 100644 --- a/src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.Android.cs +++ b/src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.Android.cs @@ -10,5 +10,11 @@ AppCompatEditText GetNativeEditor(EditorHandler editorHandler) => string GetNativeText(EditorHandler editorHandler) => GetNativeEditor(editorHandler).Text; + + string GetNativePlaceholderText(EditorHandler editorHandler) => + GetNativeEditor(editorHandler).Hint; + + Color GetNativePlaceholderColor(EditorHandler editorHandler) => + ((uint)GetNativeEditor(editorHandler).CurrentHintTextColor).ToColor(); } } \ No newline at end of file diff --git a/src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.cs b/src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.cs index ef3766c2b5c0..9a06c2c3fbf5 100644 --- a/src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.cs +++ b/src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.cs @@ -1,5 +1,4 @@ -using System; -using System.Threading.Tasks; +using System.Threading.Tasks; using Microsoft.Maui.DeviceTests.Stubs; using Microsoft.Maui.Handlers; using Xunit; @@ -46,5 +45,72 @@ await ValidatePropertyUpdatesValue( setValue, unsetValue); } + + [Fact(DisplayName = "PlaceholderColor Initializes Correctly")] + public async Task PlaceholderColorInitializesCorrectly() + { + var editor = new EditorStub() + { + Placeholder = "Test", + PlaceholderColor = Color.Yellow + }; + + await ValidatePropertyInitValue(editor, () => editor.PlaceholderColor, GetNativePlaceholderColor, editor.PlaceholderColor); + } + + [Theory(DisplayName = "PlaceholderColor Updates Correctly")] + [InlineData(0xFF0000, 0x0000FF)] + [InlineData(0x0000FF, 0xFF0000)] + public async Task PlaceholderColorUpdatesCorrectly(uint setValue, uint unsetValue) + { + var editor = new EditorStub + { + Placeholder = "Placeholder" + }; + + var setColor = Color.FromUint(setValue); + var unsetColor = Color.FromUint(unsetValue); + + await ValidatePropertyUpdatesValue( + editor, + nameof(IEditor.PlaceholderColor), + GetNativePlaceholderColor, + setColor, + unsetColor); + } + + [Fact(DisplayName = "PlaceholderText Initializes Correctly")] + public async Task PlaceholderTextInitializesCorrectly() + { + var editor = new EditorStub() + { + Text = "Test" + }; + + await ValidatePropertyInitValue(editor, () => editor.Placeholder, GetNativePlaceholderText, editor.Placeholder); + } + + [Theory(DisplayName = "PlaceholderText Updates Correctly")] + [InlineData(null, null)] + [InlineData(null, "Hello")] + [InlineData("Hello", null)] + [InlineData("Hello", "Goodbye")] + public async Task PlaceholderTextUpdatesCorrectly(string setValue, string unsetValue) + { + var editor = new EditorStub(); + + await ValidatePropertyUpdatesValue( + editor, + nameof(IEditor.Placeholder), + h => + { + var n = GetNativePlaceholderText(h); + if (string.IsNullOrEmpty(n)) + n = null; // native platforms may not support null text + return n; + }, + setValue, + unsetValue); + } } } \ No newline at end of file diff --git a/src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.iOS.cs b/src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.iOS.cs index a99b4aa089c6..137854ccefcb 100644 --- a/src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.iOS.cs +++ b/src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.iOS.cs @@ -1,14 +1,20 @@ using Microsoft.Maui.Handlers; -using UIKit; +using Microsoft.Maui.Platform.iOS; namespace Microsoft.Maui.DeviceTests { public partial class EditorHandlerTests { - UITextView GetNativeEditor(EditorHandler editorHandler) => - (UITextView)editorHandler.View; + MauiTextView GetNativeEditor(EditorHandler editorHandler) => + (MauiTextView)editorHandler.View; string GetNativeText(EditorHandler editorHandler) => GetNativeEditor(editorHandler).Text; + + string GetNativePlaceholderText(EditorHandler editorHandler) => + GetNativeEditor(editorHandler).PlaceholderText; + + Color GetNativePlaceholderColor(EditorHandler editorHandler) => + GetNativeEditor(editorHandler).PlaceholderTextColor.ToColor(); } } \ No newline at end of file diff --git a/src/Core/tests/DeviceTests/Stubs/EditorStub.cs b/src/Core/tests/DeviceTests/Stubs/EditorStub.cs index 2f88d7829bff..fcdcb0bb2c1a 100644 --- a/src/Core/tests/DeviceTests/Stubs/EditorStub.cs +++ b/src/Core/tests/DeviceTests/Stubs/EditorStub.cs @@ -9,5 +9,12 @@ public partial class EditorStub : StubBase, IEditor public Font Font { get; set; } public double CharacterSpacing { get; set; } + + public bool IsReadOnly { get;set; } + + public string Placeholder { get;set; } + + public Color PlaceholderColor { get; set; } + } } \ No newline at end of file From 89afc30640c558ec4afadf6850893381bf211e81 Mon Sep 17 00:00:00 2001 From: Brian Runck Date: Sun, 21 Mar 2021 23:10:08 -0400 Subject: [PATCH 04/10] Added more [PortHandler] --- src/Compatibility/Core/src/iOS/Renderers/EditorRenderer.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Compatibility/Core/src/iOS/Renderers/EditorRenderer.cs b/src/Compatibility/Core/src/iOS/Renderers/EditorRenderer.cs index 1f44ed26ea58..5d2a5a9397d2 100644 --- a/src/Compatibility/Core/src/iOS/Renderers/EditorRenderer.cs +++ b/src/Compatibility/Core/src/iOS/Renderers/EditorRenderer.cs @@ -94,6 +94,7 @@ protected internal override void UpdatePlaceholderColor() _placeholderLabel.TextColor = placeholderColor.ToUIColor(); } + [PortHandler] void CreatePlaceholderLabel() { if (Control == null) @@ -341,7 +342,10 @@ protected internal virtual void UpdateText() } } + [PortHandler] protected internal abstract void UpdatePlaceholderText(); + + [PortHandler] protected internal abstract void UpdatePlaceholderColor(); protected internal abstract void UpdateCharacterSpacing(); From d591f35151f0677fce55ad7c9d5e611b6e0ace80 Mon Sep 17 00:00:00 2001 From: Brian Runck Date: Mon, 22 Mar 2021 20:02:00 -0400 Subject: [PATCH 05/10] cleanup --- src/Compatibility/Core/src/AppHostBuilderExtensions.cs | 4 ++-- src/Controls/src/Core/HandlerImpl/Editor.Impl.cs | 2 +- src/Core/src/Core-net6.csproj | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Compatibility/Core/src/AppHostBuilderExtensions.cs b/src/Compatibility/Core/src/AppHostBuilderExtensions.cs index e3c3b1a3fb48..b6c9b9ed13bc 100644 --- a/src/Compatibility/Core/src/AppHostBuilderExtensions.cs +++ b/src/Compatibility/Core/src/AppHostBuilderExtensions.cs @@ -11,8 +11,8 @@ public static IAppHostBuilder RegisterCompatibilityRenderers(this IAppHostBuilde // This won't really be a thing once we have all the handlers built var defaultHandlers = new List { - typeof(Button) , - typeof(Editor), + typeof(Button) , + typeof(Editor), typeof(Entry) , typeof(ContentPage) , typeof(Page) , diff --git a/src/Controls/src/Core/HandlerImpl/Editor.Impl.cs b/src/Controls/src/Core/HandlerImpl/Editor.Impl.cs index ee2c40c1b86d..62f659d845aa 100644 --- a/src/Controls/src/Core/HandlerImpl/Editor.Impl.cs +++ b/src/Controls/src/Core/HandlerImpl/Editor.Impl.cs @@ -6,4 +6,4 @@ public partial class Editor : IEditor Font IText.Font => _font ??= Font.OfSize(FontFamily, FontSize).WithAttributes(FontAttributes); } -} +} \ No newline at end of file diff --git a/src/Core/src/Core-net6.csproj b/src/Core/src/Core-net6.csproj index b2be50d36c7f..ba425f47c13c 100644 --- a/src/Core/src/Core-net6.csproj +++ b/src/Core/src/Core-net6.csproj @@ -19,9 +19,9 @@ - - - + + + From eac65a5b8ee5b49470030b8d6d52550d527edbcc Mon Sep 17 00:00:00 2001 From: Brian Runck Date: Mon, 22 Mar 2021 20:10:03 -0400 Subject: [PATCH 06/10] cleanup --- src/Core/src/Platform/MaciOS/ColorExtensions.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Core/src/Platform/MaciOS/ColorExtensions.cs b/src/Core/src/Platform/MaciOS/ColorExtensions.cs index da570b60cdd7..f8c78faeaee8 100644 --- a/src/Core/src/Platform/MaciOS/ColorExtensions.cs +++ b/src/Core/src/Platform/MaciOS/ColorExtensions.cs @@ -33,6 +33,7 @@ internal static UIColor PlaceholderColor { get { + if (NativeVersion.IsAtLeast(13)) return UIColor.PlaceholderTextColor; @@ -44,6 +45,7 @@ internal static UIColor SecondaryLabelColor { get { + if (NativeVersion.IsAtLeast(13)) return UIColor.SecondaryLabelColor; @@ -55,6 +57,7 @@ internal static UIColor BackgroundColor { get { + if (NativeVersion.IsAtLeast(13)) return UIColor.SystemBackgroundColor; From 807adacdcd2a5bcb776db8bd5ee843bd92982003 Mon Sep 17 00:00:00 2001 From: Brian Runck Date: Mon, 22 Mar 2021 20:15:36 -0400 Subject: [PATCH 07/10] cleanup --- src/Controls/samples/Controls.Sample/Pages/MainPage.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Controls/samples/Controls.Sample/Pages/MainPage.cs b/src/Controls/samples/Controls.Sample/Pages/MainPage.cs index 61b00addde07..50e05816392d 100644 --- a/src/Controls/samples/Controls.Sample/Pages/MainPage.cs +++ b/src/Controls/samples/Controls.Sample/Pages/MainPage.cs @@ -35,7 +35,6 @@ void SetupMauiLayout() var verticalStack = new VerticalStackLayout() { Spacing = 5, BackgroundColor = Color.AntiqueWhite }; var horizontalStack = new HorizontalStackLayout() { Spacing = 2, BackgroundColor = Color.CornflowerBlue }; - verticalStack.Add(new Label { Text = " ", Padding = new Thickness(10) }); var label = new Label { Text = "centered text", BackgroundColor = Color.Fuchsia, HorizontalTextAlignment = TextAlignment.End }; @@ -97,7 +96,6 @@ void SetupMauiLayout() verticalStack.Add(new Entry { Placeholder = "This should be placeholder text" }); verticalStack.Add(new Entry { Text = "This should be read only property", IsReadOnly = true }); - verticalStack.Add(new ProgressBar { Progress = 0.5 }); verticalStack.Add(new ProgressBar { Progress = 0.5, BackgroundColor = Color.LightCoral }); verticalStack.Add(new ProgressBar { Progress = 0.5, ProgressColor = Color.Purple }); From 0b356bbf96acd81a74c4e2cf6e2ae2e23cd8ffb7 Mon Sep 17 00:00:00 2001 From: Brian Runck Date: Mon, 22 Mar 2021 21:45:18 -0400 Subject: [PATCH 08/10] Fix merge --- src/Core/src/Platform/Android/EditorExtensions.cs | 15 --------------- src/Core/src/Platform/iOS/EditorExtensions.cs | 10 ---------- src/Core/tests/DeviceTests/Stubs/EditorStub.cs | 5 ++++- 3 files changed, 4 insertions(+), 26 deletions(-) diff --git a/src/Core/src/Platform/Android/EditorExtensions.cs b/src/Core/src/Platform/Android/EditorExtensions.cs index b5e2a6263f2e..30063d8a1400 100644 --- a/src/Core/src/Platform/Android/EditorExtensions.cs +++ b/src/Core/src/Platform/Android/EditorExtensions.cs @@ -5,21 +5,6 @@ namespace Microsoft.Maui { public static class EditorExtensions { - public static void UpdateText(this AppCompatEditText editText, IEditor editor) - { - string text = editor.Text; - - if (editText.Text == text) - return; - - editText.Text = text; - - if (string.IsNullOrEmpty(text)) - return; - - editText.SetSelection(text.Length); - } - public static void UpdatePlaceholder(this AppCompatEditText editText, IEditor editor) { if (editText.Hint == editor.Placeholder) diff --git a/src/Core/src/Platform/iOS/EditorExtensions.cs b/src/Core/src/Platform/iOS/EditorExtensions.cs index 0038b3b18082..6c2287d34fec 100644 --- a/src/Core/src/Platform/iOS/EditorExtensions.cs +++ b/src/Core/src/Platform/iOS/EditorExtensions.cs @@ -5,16 +5,6 @@ namespace Microsoft.Maui { public static class EditorExtensions { - public static void UpdateText(this UITextView textView, IEditor editor) - { - string text = editor.Text; - - if (textView.Text != text) - { - textView.Text = text; - } - } - public static void UpdatePlaceholder(this MauiTextView textView, IEditor editor) { textView.PlaceholderText = editor.Placeholder; diff --git a/src/Core/tests/DeviceTests/Stubs/EditorStub.cs b/src/Core/tests/DeviceTests/Stubs/EditorStub.cs index fcdcb0bb2c1a..a554e5eec744 100644 --- a/src/Core/tests/DeviceTests/Stubs/EditorStub.cs +++ b/src/Core/tests/DeviceTests/Stubs/EditorStub.cs @@ -15,6 +15,9 @@ public partial class EditorStub : StubBase, IEditor public string Placeholder { get;set; } public Color PlaceholderColor { get; set; } - + + public bool IsTextPredictionEnabled { get; set; } + + public int MaxLength { get; set; } } } \ No newline at end of file From 52b93ff4b60250db59f141e5b957163b43a02a43 Mon Sep 17 00:00:00 2001 From: Brian Runck Date: Tue, 23 Mar 2021 18:41:36 -0400 Subject: [PATCH 09/10] Changes after code review --- .../src/Handlers/Editor/EditorHandler.iOS.cs | 5 --- .../Platform/Android/EditTextExtensions.cs | 25 +++++++++++-- .../src/Platform/Android/EditorExtensions.cs | 35 ------------------- .../src/Platform/Standard/EditorExtensions.cs | 10 ------ 4 files changed, 22 insertions(+), 53 deletions(-) delete mode 100644 src/Core/src/Platform/Android/EditorExtensions.cs delete mode 100644 src/Core/src/Platform/Standard/EditorExtensions.cs diff --git a/src/Core/src/Handlers/Editor/EditorHandler.iOS.cs b/src/Core/src/Handlers/Editor/EditorHandler.iOS.cs index 9b273144b2e4..b9bfbc255a80 100644 --- a/src/Core/src/Handlers/Editor/EditorHandler.iOS.cs +++ b/src/Core/src/Handlers/Editor/EditorHandler.iOS.cs @@ -16,11 +16,6 @@ protected override MauiTextView CreateNativeView() return new MauiTextView(CGRect.Empty); } - protected override void SetupDefaults(MauiTextView nativeView) - { - nativeView.PlaceholderTextColor = DefaultPlaceholderColor; - } - protected override void ConnectHandler(MauiTextView nativeView) { nativeView.Changed += OnChanged; diff --git a/src/Core/src/Platform/Android/EditTextExtensions.cs b/src/Core/src/Platform/Android/EditTextExtensions.cs index 30a2d53db260..a8f4b6036c08 100644 --- a/src/Core/src/Platform/Android/EditTextExtensions.cs +++ b/src/Core/src/Platform/Android/EditTextExtensions.cs @@ -95,12 +95,31 @@ public static void UpdateMaxLength(this AppCompatEditText editText, int maxLengt editText.Text = TrimToMaxLength(editText.Text, maxLength); } - public static void UpdatePlaceholder(this AppCompatEditText editText, IEntry entry) + public static void UpdatePlaceholder(this AppCompatEditText editText, IPlaceholder textInput) { - if (editText.Hint == entry.Placeholder) + if (editText.Hint == textInput.Placeholder) return; - editText.Hint = entry.Placeholder; + editText.Hint = textInput.Placeholder; + } + + public static void UpdatePlaceholderColor(this AppCompatEditText editText, IEditor editor, ColorStateList? defaultColor) + { + var placeholderTextColor = editor.PlaceholderColor; + if (placeholderTextColor.IsDefault) + { + editText.SetHintTextColor(defaultColor); + } + else + { + var androidColor = placeholderTextColor.ToNative(); + + if (!editText.HintTextColors.IsOneColor(ColorExtensions.States, androidColor)) + { + var acolor = androidColor.ToArgb(); + editText.SetHintTextColor(new ColorStateList(ColorExtensions.States, new[] { acolor, acolor })); + } + } } public static void UpdateIsReadOnly(this AppCompatEditText editText, IEntry entry) diff --git a/src/Core/src/Platform/Android/EditorExtensions.cs b/src/Core/src/Platform/Android/EditorExtensions.cs deleted file mode 100644 index 30063d8a1400..000000000000 --- a/src/Core/src/Platform/Android/EditorExtensions.cs +++ /dev/null @@ -1,35 +0,0 @@ -using Android.Content.Res; -using AndroidX.AppCompat.Widget; - -namespace Microsoft.Maui -{ - public static class EditorExtensions - { - public static void UpdatePlaceholder(this AppCompatEditText editText, IEditor editor) - { - if (editText.Hint == editor.Placeholder) - return; - - editText.Hint = editor.Placeholder; - } - - public static void UpdatePlaceholderColor(this AppCompatEditText editText, IEditor editor, ColorStateList? defaultColor) - { - var placeholderTextColor = editor.PlaceholderColor; - if (placeholderTextColor.IsDefault) - { - editText.SetHintTextColor(defaultColor); - } - else - { - var androidColor = placeholderTextColor.ToNative(); - - if (!editText.HintTextColors.IsOneColor(ColorExtensions.States, androidColor)) - { - var acolor = androidColor.ToArgb(); - editText.SetHintTextColor(new ColorStateList(ColorExtensions.States, new[] { acolor, acolor })); - } - } - } - } -} diff --git a/src/Core/src/Platform/Standard/EditorExtensions.cs b/src/Core/src/Platform/Standard/EditorExtensions.cs deleted file mode 100644 index 632e91c7d090..000000000000 --- a/src/Core/src/Platform/Standard/EditorExtensions.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Microsoft.Maui -{ - public static class EditorExtensions - { - public static void UpdatePlaceholder(this object nothing, IEditor editor) - { - - } - } -} From 5a9b189d17df1bef0ccc525a64c9ece8c87ad198 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Wed, 24 Mar 2021 13:42:23 -0600 Subject: [PATCH 10/10] Fix merge issues --- src/Core/src/Handlers/Editor/EditorHandler.Android.cs | 4 +++- .../DeviceTests/Handlers/Editor/EditorHandlerTests.iOS.cs | 7 ++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Core/src/Handlers/Editor/EditorHandler.Android.cs b/src/Core/src/Handlers/Editor/EditorHandler.Android.cs index 21026decf7ea..1c678bb9110d 100644 --- a/src/Core/src/Handlers/Editor/EditorHandler.Android.cs +++ b/src/Core/src/Handlers/Editor/EditorHandler.Android.cs @@ -1,7 +1,9 @@ -using Android.Views; +using Android.Content.Res; +using Android.Views; using Android.Views.InputMethods; using AndroidX.AppCompat.Widget; using Microsoft.Extensions.DependencyInjection; +using System; namespace Microsoft.Maui.Handlers { diff --git a/src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.iOS.cs b/src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.iOS.cs index ecb1f40963a6..38c3ac910722 100644 --- a/src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.iOS.cs +++ b/src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.iOS.cs @@ -10,9 +10,6 @@ namespace Microsoft.Maui.DeviceTests { public partial class EditorHandlerTests { - MauiTextView GetNativeEditor(EditorHandler editorHandler) => - (MauiTextView)editorHandler.View; - [Fact(DisplayName = "CharacterSpacing Initializes Correctly")] public async Task CharacterSpacingInitializesCorrectly() { @@ -64,8 +61,8 @@ public async Task FontFamilyInitializesCorrectly(string family) Assert.NotEqual(fontManager.DefaultFont.FamilyName, nativeFont.FamilyName); } - UITextView GetNativeEditor(EditorHandler editorHandler) => - (UITextView)editorHandler.View; + MauiTextView GetNativeEditor(EditorHandler editorHandler) => + (MauiTextView)editorHandler.View; string GetNativeText(EditorHandler editorHandler) => GetNativeEditor(editorHandler).Text;