[Tizen] Enhancement EntryRenderer (#8686)
authorSeungkeun Lee <sngn.lee@samsung.com>
Tue, 3 Dec 2019 10:37:39 +0000 (19:37 +0900)
committerRui Marinho <me@ruimarinho.net>
Tue, 3 Dec 2019 10:37:39 +0000 (10:37 +0000)
* Enhancement EntryRenderer on Tizen

 - Support ClearButtonVisibility
 - Support ReturnType.Next
 - Limitation No support clearButtonVisibility on TV

* Update ClearButton color

* Remove redundant line

* Remove init value on IsTextBlockFoused

Xamarin.Forms.Platform.Tizen/Native/EditfieldEntry.cs
Xamarin.Forms.Platform.Tizen/Native/SearchBar.cs
Xamarin.Forms.Platform.Tizen/Renderers/EntryRenderer.cs
Xamarin.Forms.Platform.Tizen/Renderers/VisualElementRenderer.cs

index 7b239e1..25eadac 100644 (file)
@@ -1,22 +1,16 @@
 using System;
 using ElmSharp;
 using ELayout = ElmSharp.Layout;
+using EColor = ElmSharp.Color;
 
 namespace Xamarin.Forms.Platform.Tizen.Native
 {
        public class EditfieldEntry : Native.Entry
        {
-               public event EventHandler TextBlockFocused;
-               public event EventHandler TextBlockUnfocused;
-
-               public event EventHandler LayoutFocused;
-               public event EventHandler LayoutUnfocused;
-
-               public bool IsTextBlockFocused => _isTexstBlockFocused;
-
+               Button _clearButton;
                ELayout _editfieldLayout;
+               bool _enableClearButton;
                int _heightPadding = 0;
-               bool _isTexstBlockFocused = false;
 
                public EditfieldEntry(EvasObject parent) : base(parent)
                {
@@ -28,7 +22,15 @@ namespace Xamarin.Forms.Platform.Tizen.Native
                                _editfieldLayout.SetTheme("layout", "editfield", style);
                }
 
-               public override ElmSharp.Color BackgroundColor
+               public event EventHandler TextBlockFocused;
+               public event EventHandler TextBlockUnfocused;
+
+               public event EventHandler LayoutFocused;
+               public event EventHandler LayoutUnfocused;
+
+               public bool IsTextBlockFocused { get; private set; }
+
+               public override EColor BackgroundColor
                {
                        get
                        {
@@ -40,11 +42,33 @@ namespace Xamarin.Forms.Platform.Tizen.Native
                        }
                }
 
+               public bool EnableClearButton
+               {
+                       get => _enableClearButton;
+                       set
+                       {
+                               _enableClearButton = value;
+                               UpdateEnableClearButton();
+                       }
+               }
+
+               public EColor ClearButtonColor
+               {
+                       get => _clearButton?.GetPartColor("icon") ?? EColor.Default;
+                       set
+                       {
+                               if (_clearButton != null)
+                               {
+                                       _clearButton.SetPartColor("icon", value);
+                                       _clearButton.SetPartColor("icon_pressed", value);
+                               }
+                       }
+               }
+
                public void SetFocusOnTextBlock(bool isFocused)
                {
-                       AllowFocus(isFocused);
                        SetFocus(isFocused);
-                       _isTexstBlockFocused = isFocused;
+                       IsTextBlockFocused = isFocused;
 
                        if (isFocused)
                                TextBlockFocused?.Invoke(this, EventArgs.Empty);
@@ -72,7 +96,6 @@ namespace Xamarin.Forms.Platform.Tizen.Native
                protected override IntPtr CreateHandle(EvasObject parent)
                {
                        var handle = base.CreateHandle(parent);
-                       AllowFocus(false);
                        _editfieldLayout = CreateEditFieldLayout(parent);
 
                        // If true, It means, there is no extra layout on the widget handle
@@ -94,6 +117,16 @@ namespace Xamarin.Forms.Platform.Tizen.Native
                        return _editfieldLayout;
                }
 
+               protected override void OnTextChanged(string oldValue, string newValue)
+               {
+                       base.OnTextChanged(oldValue, newValue);
+                       if (EnableClearButton)
+                       {
+                               var emission = string.IsNullOrEmpty(newValue) ? "elm,action,hide,button" : "elm,action,show,button";
+                               _editfieldLayout.SignalEmit(emission, "");
+                       }
+               }
+
                protected virtual ELayout CreateEditFieldLayout(EvasObject parent)
                {
                        var layout = new ELayout(parent);
@@ -107,7 +140,6 @@ namespace Xamarin.Forms.Platform.Tizen.Native
                        };
                        layout.Focused += (s, e) =>
                        {
-                               AllowFocus(false);
                                layout.SignalEmit("elm,state,focused", "");
                                LayoutFocused?.Invoke(this, EventArgs.Empty);
                        };
@@ -116,7 +148,7 @@ namespace Xamarin.Forms.Platform.Tizen.Native
                        {
                                if (e.KeyName == "Return")
                                {
-                                       if (!_isTexstBlockFocused)
+                                       if (!IsTextBlockFocused)
                                        {
                                                SetFocusOnTextBlock(true);
                                                e.Flags |= EvasEventFlag.OnHold;
@@ -138,5 +170,31 @@ namespace Xamarin.Forms.Platform.Tizen.Native
 
                        return layout;
                }
+
+               protected virtual void UpdateEnableClearButton()
+               {
+                       if (EnableClearButton)
+                       {
+                               _clearButton = new Button(_editfieldLayout)
+                               {
+                                       Style = "editfield_clear"
+                               };
+                               _clearButton.AllowFocus(false);
+                               _clearButton.Clicked += OnClearButtonClicked;
+
+                               _editfieldLayout.SetPartContent("elm.swallow.button", _clearButton);
+                               _editfieldLayout.SignalEmit("elm,action,show,button", "");
+                       }
+                       else
+                       {
+                               _editfieldLayout.SetPartContent("elm.swallow.button", null);
+                               _clearButton = null;
+                       }
+               }
+
+               void OnClearButtonClicked(object sender, EventArgs e)
+               {
+                       Text = string.Empty;
+               }
        }
 }
\ No newline at end of file
index 0a4ac01..1495e25 100644 (file)
@@ -1,59 +1,18 @@
-using System;
 using ElmSharp;
-using EButton = ElmSharp.Button;
 using EColor = ElmSharp.Color;
-using ELayout = ElmSharp.Layout;
 
 namespace Xamarin.Forms.Platform.Tizen.Native
 {
        public class SearchBar : Native.EditfieldEntry
        {
-               EButton _clearButton;
-               ELayout _layout;
-
                public SearchBar(EvasObject parent) : base(parent)
                {
+                       EnableClearButton = true;
                }
 
                public void SetClearButtonColor(EColor color)
                {
-                       _clearButton.Color = color;
-               }
-
-               protected override ElmSharp.Layout CreateEditFieldLayout(EvasObject parent)
-               {
-                       _layout =  base.CreateEditFieldLayout(parent);
-
-                       _clearButton = new EButton(_layout)
-                       {
-                               Style = "editfield_clear"
-                       };
-                       _clearButton.AllowFocus(false);
-                       _clearButton.Clicked += ClearButtonClicked;
-
-                       _layout.SetPartContent("elm.swallow.button", _clearButton);
-                       _layout.SignalEmit("elm,action,show,button", "");
-
-                       return _layout;
-               }
-
-               protected override void OnTextChanged(string oldValue, string newValue)
-               {
-                       base.OnTextChanged(oldValue, newValue);
-
-                       if (String.IsNullOrEmpty(Text))
-                       {
-                               _layout.SignalEmit("elm,action,hide,button", "");
-                       }
-                       else
-                       {
-                               _layout.SignalEmit("elm,action,show,button", "");
-                       }
-               }
-
-               void ClearButtonClicked(object sender, EventArgs e)
-               {
-                       Text = string.Empty;
+                       ClearButtonColor = color;
                }
        }
 }
\ No newline at end of file
index 9d50426..4a0d88d 100644 (file)
@@ -26,6 +26,7 @@ namespace Xamarin.Forms.Platform.Tizen
                        RegisterPropertyHandler(Specific.FontWeightProperty, UpdateFontWeight);
                        RegisterPropertyHandler(Entry.SelectionLengthProperty, UpdateSelectionLength);
                        RegisterPropertyHandler(InputView.IsReadOnlyProperty, UpdateIsReadOnly);
+                       RegisterPropertyHandler(Entry.ClearButtonVisibilityProperty, UpdateClearButtonVisibility);
                }
 
                protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
@@ -99,9 +100,14 @@ namespace Xamarin.Forms.Platform.Tizen
 
                void OnCompleted(object sender, EventArgs e)
                {
-                       //TODO Consider if any other object should overtake focus
-                       Control.SetFocus(false);
-
+                       if (Element.ReturnType == ReturnType.Next)
+                       {
+                               FocusSearch(true)?.SetFocus(true);
+                       }
+                       else
+                       {
+                               Control.SetFocus(false);
+                       }
                        ((IEntryController)Element).SendCompleted();
                }
 
@@ -256,5 +262,23 @@ namespace Xamarin.Forms.Platform.Tizen
                {
                        Control.IsEditable = !Element.IsReadOnly;
                }
+
+               void UpdateClearButtonVisibility(bool init)
+               {
+                       if (Element.ClearButtonVisibility == ClearButtonVisibility.WhileEditing)
+                       {
+                               if (Control is Native.EditfieldEntry editfieldEntry)
+                               {
+                                       editfieldEntry.EnableClearButton = true;
+                               }
+                       }
+                       else if (!init)
+                       {
+                               if (Control is Native.EditfieldEntry editfieldEntry)
+                               {
+                                       editfieldEntry.EnableClearButton = false;
+                               }
+                       }
+               }
        }
 }
\ No newline at end of file
index 473a4d9..7d960c2 100644 (file)
@@ -494,6 +494,29 @@ namespace Xamarin.Forms.Platform.Tizen
                        }
                }
 
+               protected Widget FocusSearch(bool forwardDirection)
+               {
+                       VisualElement element = Element as VisualElement;
+                       int maxAttempts = 0;
+                       var tabIndexes = element?.GetTabIndexesOnParentPage(out maxAttempts);
+                       if (tabIndexes == null)
+                               return null;
+
+                       int tabIndex = Element.TabIndex;
+                       int attempt = 0;
+
+                       do
+                       {
+                               element = element.FindNextElement(forwardDirection, tabIndexes, ref tabIndex) as VisualElement;
+                               var renderer = Platform.GetRenderer(element);
+                               if (renderer?.NativeView is Widget widget && widget.IsFocusAllowed)
+                               {
+                                       return widget;
+                               }
+                       } while (!(element.IsFocused || ++attempt >= maxAttempts));
+                       return null;
+               }
+
                internal virtual void SendVisualElementInitialized(VisualElement element, EvasObject nativeView)
                {
                        element.SendViewInitialized(nativeView);