[NUI] Apply focusable features and Automatic Focus algorithm
authorEverLEEst(SangHyeon Lee) <sh10233.lee@samsung.com>
Tue, 15 Feb 2022 08:03:33 +0000 (17:03 +0900)
committerSangHyeon Jade Lee <dltkdgus1764@gmail.com>
Wed, 16 Feb 2022 08:40:57 +0000 (00:40 -0800)
test/Tizen.NUI.StyleGuide/Examples/ButtonExample.cs
test/Tizen.NUI.StyleGuide/Examples/ScrollableBase/ScrollableBaseDirectionExample.cs
test/Tizen.NUI.StyleGuide/Examples/ScrollableBase/ScrollableBaseExample.cs
test/Tizen.NUI.StyleGuide/Tizen.NUI.StyleGuide.cs

index 08e030e..a73ffd8 100644 (file)
@@ -71,6 +71,7 @@ namespace Tizen.NUI.StyleGuide
             {
                 Text = "Enabled"
             };
+            enabledButton.EnableFocus();
             enabledButton.Clicked += (object obj, ClickedEventArgs ev) =>
             {
                 Log.Info(this.GetType().Name, "Enabled Button Clicked\n");
@@ -95,6 +96,7 @@ namespace Tizen.NUI.StyleGuide
                 Text = "Unselected",
                 IsSelectable = true,
             };
+            selectableButton.EnableFocus();
             selectableButton.Clicked += (object obj, ClickedEventArgs ev) =>
             {
                 Log.Info(this.GetType().Name, "Selected Button Clicked\n");
index eb065b2..2ab5934 100644 (file)
@@ -49,6 +49,8 @@ namespace Tizen.NUI.StyleGuide
                 HeightSpecification = LayoutParamPolicies.MatchParent,
                 HideScrollbar = false,
             };
+            // FIXME: should work focus on ScrollableBase.
+            // scrollView.EnableFocus();
 
             if (isHorizontal)
             {
@@ -79,6 +81,7 @@ namespace Tizen.NUI.StyleGuide
                 colorView.WidthSpecification = (isHorizontal? 200 : LayoutParamPolicies.MatchParent);
                 colorView.HeightSpecification = (isHorizontal? LayoutParamPolicies.MatchParent : 200);
                 colorView.BackgroundColor = new Color((float)rnd.Next(256)/256f, (float)rnd.Next(256)/256f, (float)rnd.Next(256)/256f, 1);
+                colorView.EnableFocus();
                 scrollView.Add(colorView);
             }
 
index c0913e7..6db39d0 100644 (file)
@@ -73,6 +73,7 @@ namespace Tizen.NUI.StyleGuide
                     };
                     item.Label.SetBinding(TextLabel.TextProperty, "Direction");
                     item.Label.HorizontalAlignment = HorizontalAlignment.Begin;
+                    item.EnableFocus();
                     return item;
                 }),
                 ScrollingDirection = ScrollableBase.Direction.Vertical,
@@ -87,6 +88,7 @@ namespace Tizen.NUI.StyleGuide
                     Page scrollDirPage = new ScrollableBaseDirectionExample(directionItem.Direction);
                     window = NUIApplication.GetDefaultWindow();
                     window.GetDefaultNavigator().Push(scrollDirPage);
+                    FocusableExtension.SetFocusOnPage(scrollDirPage);
                 }
                 directionListView.SelectedItem = null;
             };
index 63dd4ec..ffa0663 100644 (file)
@@ -26,6 +26,67 @@ using System.Reflection;
 
 namespace Tizen.NUI.StyleGuide
 {
+
+    /// Helder static extension class for Focusable.
+    /// NUI default behavior is unfocusable in key or touch,
+    /// this class help to setting focusable features easily.
+    public static class FocusableExtension
+    {
+        public static FocusManager FocusManager;
+        public static void EnableFocus(this View view)
+        {
+            view.Focusable = true;
+            view.FocusableInTouch = true;
+        }
+
+        public static void EnableAutoFocusable()
+        {
+            FocusManager = FocusManager.Instance;
+            FocusManager.EnableDefaultAlgorithm(true);
+            FocusManager.FocusIndicator = new View()
+            {
+                PositionUsesPivotPoint = true,
+                PivotPoint = new Position(0, 0, 0),
+                WidthResizePolicy = ResizePolicyType.FillToParent,
+                HeightResizePolicy = ResizePolicyType.FillToParent,
+                BorderlineColor = Color.Orange,
+                BorderlineWidth = 4.0f,
+                BorderlineOffset = -1f,
+                BackgroundColor = new Color(0.2f, 0.2f, 0.2f, 0.2f),
+            };
+        }
+
+        public static void SetFocusOnPage(Page page)
+        {
+            View focusCandidate = null;
+            if (page == null) return;
+
+            if (page is ContentPage contentPage)
+            {
+                focusCandidate = contentPage.AppBar?.NavigationContent;
+                focusCandidate.Focusable = true;
+            }
+
+            if (focusCandidate == null)
+            {
+                foreach (View child in page.Children)
+                {
+                    if (child.Focusable)
+                    {
+                        focusCandidate = child;
+                    }
+                }
+            }
+
+            Log.Info("FocusableExtension", $"Focus candidate {focusCandidate}\n");
+
+            if (focusCandidate != null)
+            {
+                FocusManager.SetCurrentFocusView(focusCandidate);
+            }
+        }
+    }
+
     public class SearchField : View
     {
         public TextField SearchTextField;
@@ -47,12 +108,14 @@ namespace Tizen.NUI.StyleGuide
 
             var searchTextBox = CreateSearchText();
             SearchTextField = CreateTextField();
+            SearchTextField.EnableFocus();
             var underline = CreateUnderline();
 
             searchTextBox.Add(SearchTextField);
             searchTextBox.Add(underline);
 
             SearchButton = CreateSearchButton();
+            SearchButton.EnableFocus();
 
             Add(searchTextBox);
             Add(SearchButton);
@@ -173,6 +236,7 @@ namespace Tizen.NUI.StyleGuide
         private ContentPage page;
         private SearchField field;
         private List<ControlMenu> testSource;
+        private FocusManager focusManager;
 
         public void OnKeyEvent(object sender, Window.KeyEventArgs e)
         {
@@ -220,7 +284,14 @@ namespace Tizen.NUI.StyleGuide
             window.Title = "NUI Style Guide";
             window.KeyEvent += OnKeyEvent;
 
+            FocusableExtension.EnableAutoFocusable();
+
             navigator = window.GetDefaultNavigator();
+            navigator.Popped += (object obj, PoppedEventArgs ev) =>
+            {
+                Page top = navigator.Peek();
+                FocusableExtension.SetFocusOnPage(top);
+            };
         }
 
         void OnSearchBtnClicked(object sender, ClickedEventArgs e)
@@ -248,6 +319,7 @@ namespace Tizen.NUI.StyleGuide
             var appBarStyle = ThemeManager.GetStyle("Tizen.NUI.Components.AppBar");
             var moreButton = new Button(((AppBarStyle)appBarStyle).BackButton);
             moreButton.Icon.ResourceUrl = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "menu.png";
+            moreButton.EnableFocus();
             appBar.NavigationContent = moreButton;
 
 
@@ -287,6 +359,7 @@ namespace Tizen.NUI.StyleGuide
                     };
                     item.Label.SetBinding(TextLabel.TextProperty, "ViewLabel");
                     item.Label.HorizontalAlignment = HorizontalAlignment.Begin;
+                    item.EnableFocus();
                     return item;
                 }),
                 Header = myTitle,
@@ -305,13 +378,17 @@ namespace Tizen.NUI.StyleGuide
                 AppBar = appBar,
                 Content = pageContent,
             };
+            page.Focusable = true;
+
             navigator.Push(page);
+            FocusableExtension.SetFocusOnPage(page);
         }
 
         private void RunSample(string name)
         {
             IExample example = typeof(Program).Assembly?.CreateInstance(name) as IExample;
 
+
             Console.WriteLine($"@@@ typeof(Program).Assembly={typeof(Program).Assembly}, name={name}");
 
             if (example != null)
@@ -319,7 +396,9 @@ namespace Tizen.NUI.StyleGuide
                 example.Activate();
                 if (example is Page examplePage)
                 {
-                    navigator.Push((examplePage));
+                    examplePage.Focusable = true;
+                    navigator.Push(examplePage);
+                    FocusableExtension.SetFocusOnPage(examplePage);
                 }
             }
             else