Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make MauiAppCompatEditText public & adapt EntryHandler #26121

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions src/Core/src/Handlers/Editor/EditorHandler.Android.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
using System;
using Android.Views;
using Android.Views.InputMethods;
using AndroidX.AppCompat.Widget;
using Microsoft.Maui.Graphics;
using static Android.Views.View;

namespace Microsoft.Maui.Handlers
{
// TODO: NET8 issoto - Change the TPlatformView generic type to MauiAppCompatEditText
// This type adds support to the SelectionChanged event
public partial class EditorHandler : ViewHandler<IEditor, AppCompatEditText>
public partial class EditorHandler : ViewHandler<IEditor, MauiAppCompatEditText>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't change the generic type.

I think the only change here we can look at doing is making MauiAppCompatEditText public

I realize that makes the handler a little confusing :-/

But it'll be too breaking to change the generic type

{
bool _set;

// TODO: NET8 issoto - Change the return type to MauiAppCompatEditText
protected override AppCompatEditText CreatePlatformView()
protected override MauiAppCompatEditText CreatePlatformView()
{
var editText = new MauiAppCompatEditText(Context)
{
Expand All @@ -33,31 +29,31 @@ public override void SetVirtualView(IView view)
{
base.SetVirtualView(view);

// TODO: NET8 issoto - Remove the casting once we can set the TPlatformView generic type as MauiAppCompatEditText
if (!_set && PlatformView is MauiAppCompatEditText editText)
editText.SelectionChanged += OnSelectionChanged;
if (!_set)
{
PlatformView.SelectionChanged += OnSelectionChanged;
}

_set = true;
}

// TODO: NET8 issoto - Change the platformView type to MauiAppCompatEditText
protected override void ConnectHandler(AppCompatEditText platformView)
protected override void ConnectHandler(MauiAppCompatEditText platformView)
{
platformView.ViewAttachedToWindow += OnPlatformViewAttachedToWindow;
platformView.TextChanged += OnTextChanged;
platformView.FocusChange += OnFocusChange;
}

// TODO: NET8 issoto - Change the platformView type to MauiAppCompatEditText
protected override void DisconnectHandler(AppCompatEditText platformView)
protected override void DisconnectHandler(MauiAppCompatEditText platformView)
{
platformView.ViewAttachedToWindow -= OnPlatformViewAttachedToWindow;
platformView.TextChanged -= OnTextChanged;
platformView.FocusChange -= OnFocusChange;

// TODO: NET8 issoto - Remove the casting once we can set the TPlatformView generic type as MauiAppCompatEditText
if (_set && platformView is MauiAppCompatEditText editText)
editText.SelectionChanged -= OnSelectionChanged;
if (_set)
{
platformView.SelectionChanged -= OnSelectionChanged;
}

_set = false;
}
Expand All @@ -77,7 +73,9 @@ public static void MapPlaceholder(IEditorHandler handler, IEditor editor) =>
public static void MapPlaceholderColor(IEditorHandler handler, IEditor editor)
{
if (handler is EditorHandler platformHandler)
{
handler.PlatformView?.UpdatePlaceholderColor(editor);
}
}

public static void MapCharacterSpacing(IEditorHandler handler, IEditor editor) =>
Expand Down Expand Up @@ -120,7 +118,9 @@ public static void MapSelectionLength(IEditorHandler handler, ITextInput editor)
static void MapFocus(IEditorHandler handler, IEditor editor, object? args)
{
if (args is FocusRequest request)
{
handler.PlatformView.Focus(request);
}
}

void OnPlatformViewAttachedToWindow(object? sender, ViewAttachedToWindowEventArgs e)
Expand Down Expand Up @@ -149,10 +149,14 @@ private void OnSelectionChanged(object? sender, EventArgs e)
var selectedTextLength = PlatformView.GetSelectedTextLength();

if (VirtualView.CursorPosition != cursorPosition)
{
VirtualView.CursorPosition = cursorPosition;
}

if (VirtualView.SelectionLength != selectedTextLength)
{
VirtualView.SelectionLength = selectedTextLength;
}
}

private void OnFocusChange(object? sender, FocusChangeEventArgs e)
Expand Down
55 changes: 35 additions & 20 deletions src/Core/src/Handlers/Entry/EntryHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@
using Android.Text;
using Android.Views;
using Android.Views.InputMethods;
using AndroidX.AppCompat.Widget;
using AndroidX.Core.Content;
using static Android.Views.View;
using static Android.Widget.TextView;

namespace Microsoft.Maui.Handlers
{
// TODO: NET8 issoto - Change the TPlatformView generic type to MauiAppCompatEditText
// This type adds support to the SelectionChanged event
public partial class EntryHandler : ViewHandler<IEntry, AppCompatEditText>
public partial class EntryHandler : ViewHandler<IEntry, MauiAppCompatEditText>
{
Drawable? _clearButtonDrawable;
bool _clearButtonVisible;
bool _set;

protected override AppCompatEditText CreatePlatformView()
protected override MauiAppCompatEditText CreatePlatformView()
{
var nativeEntry = new MauiAppCompatEditText(Context);
return nativeEntry;
Expand All @@ -32,34 +29,34 @@ public override void SetVirtualView(IView view)
{
base.SetVirtualView(view);

// TODO: NET8 issoto - Remove the casting once we can set the TPlatformView generic type as MauiAppCompatEditText
if (!_set && PlatformView is MauiAppCompatEditText editText)
editText.SelectionChanged += OnSelectionChanged;
if (!_set)
{
PlatformView.SelectionChanged += OnSelectionChanged;
}

_set = true;
}

// TODO: NET8 issoto - Change the return type to MauiAppCompatEditText
protected override void ConnectHandler(AppCompatEditText platformView)
protected override void ConnectHandler(MauiAppCompatEditText platformView)
{
platformView.TextChanged += OnTextChanged;
platformView.FocusChange += OnFocusedChange;
platformView.Touch += OnTouch;
platformView.EditorAction += OnEditorAction;
}

// TODO: NET8 issoto - Change the return type to MauiAppCompatEditText
protected override void DisconnectHandler(AppCompatEditText platformView)
protected override void DisconnectHandler(MauiAppCompatEditText platformView)
{
_clearButtonDrawable = null;
platformView.TextChanged -= OnTextChanged;
platformView.FocusChange -= OnFocusedChange;
platformView.Touch -= OnTouch;
platformView.EditorAction -= OnEditorAction;

// TODO: NET8 issoto - Remove the casting once we can set the TPlatformView generic type as MauiAppCompatEditText
if (_set && platformView is MauiAppCompatEditText editText)
editText.SelectionChanged -= OnSelectionChanged;
if (_set)
{
platformView.SelectionChanged -= OnSelectionChanged;
}

_set = false;
}
Expand Down Expand Up @@ -100,8 +97,10 @@ public static void MapPlaceholder(IEntryHandler handler, IEntry entry) =>

public static void MapPlaceholderColor(IEntryHandler handler, IEntry entry)
{
if (handler is EntryHandler platformHandler)
if (handler is EntryHandler)
{
handler.PlatformView?.UpdatePlaceholderColor(entry);
}
}

public static void MapFont(IEntryHandler handler, IEntry entry) =>
Expand Down Expand Up @@ -136,18 +135,22 @@ public static void MapSelectionLength(IEntryHandler handler, IEntry entry) =>
public static void MapClearButtonVisibility(IEntryHandler handler, IEntry entry)
{
if (handler is EntryHandler platformHandler)
{
handler.PlatformView?.UpdateClearButtonVisibility(entry, platformHandler.GetClearButtonDrawable);
}
}

static void MapFocus(IEntryHandler handler, IEntry entry, object? args)
{
if (args is FocusRequest request)
{
handler.PlatformView.Focus(request);
}
}

void OnTextChanged(object? sender, TextChangedEventArgs e)
{
if (VirtualView == null)
if (VirtualView is null)
{
return;
}
Expand All @@ -165,7 +168,7 @@ void OnTextChanged(object? sender, TextChangedEventArgs e)

void OnFocusedChange(object? sender, FocusChangeEventArgs e)
{
if (VirtualView == null)
if (VirtualView is null)
{
return;
}
Expand All @@ -176,7 +179,7 @@ void OnFocusedChange(object? sender, FocusChangeEventArgs e)
// Check whether the touched position inbounds with clear button.
void OnTouch(object? sender, TouchEventArgs e) =>
e.Handled =
_clearButtonVisible && VirtualView != null &&
_clearButtonVisible && VirtualView is not null &&
PlatformView.HandleClearButtonTouched(e, GetClearButtonDrawable);

void OnEditorAction(object? sender, EditorActionEventArgs e)
Expand All @@ -187,7 +190,7 @@ void OnEditorAction(object? sender, EditorActionEventArgs e)
// This means, just by subscribing to EditorAction/KeyPressed/etc.. you change the behavior of the control
// So, we are setting handled to false here in order to maintain default behavior
bool handled = false;
if (returnType != null)
if (returnType is not null)
{
var actionId = e.ActionId;
var evt = e.Event;
Expand Down Expand Up @@ -225,10 +228,14 @@ private void OnSelectionChanged(object? sender, EventArgs e)
var selectedTextLength = PlatformView.GetSelectedTextLength();

if (VirtualView.CursorPosition != cursorPosition)
{
VirtualView.CursorPosition = cursorPosition;
}

if (VirtualView.SelectionLength != selectedTextLength)
{
VirtualView.SelectionLength = selectedTextLength;
}
}

internal void ShowClearButton()
Expand All @@ -241,14 +248,22 @@ internal void ShowClearButton()
var drawable = GetClearButtonDrawable();

if (VirtualView?.TextColor is not null)
{
drawable?.SetColorFilter(VirtualView.TextColor.ToPlatform(), FilterMode.SrcIn);
}
else
{
drawable?.ClearColorFilter();
}

if (PlatformView.LayoutDirection == LayoutDirection.Rtl)
{
PlatformView.SetCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
}
else
{
PlatformView.SetCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
}

_clearButtonVisible = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Platform/Android/MauiAppCompatEditText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Microsoft.Maui.Platform
{
internal class MauiAppCompatEditText : AppCompatEditText
public class MauiAppCompatEditText : AppCompatEditText
{
public event EventHandler? SelectionChanged;

Expand Down
7 changes: 7 additions & 0 deletions src/Core/src/PublicAPI/net-android/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ Microsoft.Maui.ITitleBar.Title.get -> string?
Microsoft.Maui.IWebView.ProcessTerminated(Microsoft.Maui.WebProcessTerminatedEventArgs! args) -> void
*REMOVED*Microsoft.Maui.IWindow.Content.get -> Microsoft.Maui.IView!
Microsoft.Maui.IWindow.Content.get -> Microsoft.Maui.IView?
Microsoft.Maui.Platform.MauiAppCompatEditText
Microsoft.Maui.Platform.MauiAppCompatEditText.MauiAppCompatEditText(Android.Content.Context! context) -> void
Microsoft.Maui.Platform.MauiAppCompatEditText.SelectionChanged -> System.EventHandler?
Microsoft.Maui.Platform.MauiHybridWebView
Microsoft.Maui.Platform.MauiHybridWebView.MauiHybridWebView(Microsoft.Maui.Handlers.HybridWebViewHandler! handler, Android.Content.Context! context) -> void
Microsoft.Maui.Platform.MauiHybridWebView.SendRawMessage(string! rawMessage) -> void
Expand All @@ -54,9 +57,13 @@ Microsoft.Maui.TextAlignment.Justify = 3 -> Microsoft.Maui.TextAlignment
Microsoft.Maui.WebProcessTerminatedEventArgs
Microsoft.Maui.WebProcessTerminatedEventArgs.RenderProcessGoneDetail.get -> Android.Webkit.RenderProcessGoneDetail?
Microsoft.Maui.WebProcessTerminatedEventArgs.Sender.get -> Android.Views.View?
override Microsoft.Maui.Handlers.EntryHandler.ConnectHandler(Microsoft.Maui.Platform.MauiAppCompatEditText! platformView) -> void
override Microsoft.Maui.Handlers.EntryHandler.CreatePlatformView() -> Microsoft.Maui.Platform.MauiAppCompatEditText!
override Microsoft.Maui.Handlers.EntryHandler.DisconnectHandler(Microsoft.Maui.Platform.MauiAppCompatEditText! platformView) -> void
override Microsoft.Maui.Handlers.HybridWebViewHandler.ConnectHandler(Android.Webkit.WebView! platformView) -> void
override Microsoft.Maui.Handlers.HybridWebViewHandler.CreatePlatformView() -> Android.Webkit.WebView!
override Microsoft.Maui.Handlers.HybridWebViewHandler.DisconnectHandler(Android.Webkit.WebView! platformView) -> void
override Microsoft.Maui.Platform.MauiAppCompatEditText.OnSelectionChanged(int selStart, int selEnd) -> void
override Microsoft.Maui.Platform.MauiHybridWebViewClient.Dispose(bool disposing) -> void
override Microsoft.Maui.Platform.MauiHybridWebViewClient.ShouldInterceptRequest(Android.Webkit.WebView? view, Android.Webkit.IWebResourceRequest? request) -> Android.Webkit.WebResourceResponse?
override Microsoft.Maui.Platform.MauiWebViewClient.OnRenderProcessGone(Android.Webkit.WebView? view, Android.Webkit.RenderProcessGoneDetail? detail) -> bool
Expand Down
Loading