[dconf-editor] Selection in search view.



commit a21f6a7e6359c27e482cc51fe8d3fca6237f9d26
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sat Dec 2 11:32:02 2017 +0100

    Selection in search view.

 editor/browser-view.vala    |   22 ++++++++++++++++
 editor/dconf-window.vala    |    6 ++++
 editor/registry-search.vala |   59 ++++++++++++++++++++++++++++++++++++++++--
 editor/registry-view.vala   |   30 ++++++++++++++++++++++
 4 files changed, 114 insertions(+), 3 deletions(-)
---
diff --git a/editor/browser-view.vala b/editor/browser-view.vala
index 7b7c201..3a7e582 100644
--- a/editor/browser-view.vala
+++ b/editor/browser-view.vala
@@ -242,6 +242,28 @@ class BrowserView : Grid, PathElement
     }
 
     /*\
+    * * Keyboard calls
+    \*/
+
+    public bool up_pressed (bool grab_focus)
+    {
+        if (current_view_is_browse_view ())
+            return browse_view.up_or_down_pressed (grab_focus, false);
+        else if (current_view_is_search_results_view ())
+            return search_results_view.up_or_down_pressed (grab_focus, false);
+        return false;
+    }
+
+    public bool down_pressed (bool grab_focus)
+    {
+        if (current_view_is_browse_view ())
+            return browse_view.up_or_down_pressed (grab_focus, true);
+        else if (current_view_is_search_results_view ())
+            return search_results_view.up_or_down_pressed (grab_focus, true);
+        return false;
+    }
+
+    /*\
     * * Action entries
     \*/
 
diff --git a/editor/dconf-window.vala b/editor/dconf-window.vala
index 0b9f6c6..6f9ac9f 100644
--- a/editor/dconf-window.vala
+++ b/editor/dconf-window.vala
@@ -510,6 +510,12 @@ class DConfWindow : ApplicationWindow
                 bookmarks_button.active = false;
             return false;
         }
+
+        if (name == "Up")
+            return browser_view.up_pressed (!search_bar.get_search_mode ());
+        if (name == "Down")
+            return browser_view.down_pressed (!search_bar.get_search_mode ());
+
         if (name == "Menu")
         {
             if (browser_view.show_row_popover ())
diff --git a/editor/registry-search.vala b/editor/registry-search.vala
index a46dfac..49a5c8d 100644
--- a/editor/registry-search.vala
+++ b/editor/registry-search.vala
@@ -22,7 +22,7 @@ class RegistrySearch : Grid, PathElement, BrowsableView
 {
     public Behaviour behaviour { private get; set; }
 
-    //[GtkChild] private ScrolledWindow scrolled;
+    [GtkChild] private ScrolledWindow scrolled;
 
     [GtkChild] private ListBox key_list_box;
 
@@ -99,7 +99,7 @@ class RegistrySearch : Grid, PathElement, BrowsableView
             position++;
         }
         assert_not_reached ();
-    }
+    } */
     private void scroll_to_row (ListBoxRow row, bool grab_focus)
     {
         key_list_box.select_row (row);
@@ -111,7 +111,22 @@ class RegistrySearch : Grid, PathElement, BrowsableView
         row.get_allocation (out row_allocation);
         key_list_box.get_adjustment ().set_value (row_allocation.y + (int) ((row_allocation.height - 
list_allocation.height) / 2.0));
     }
-/*
+
+    private void ensure_selection ()
+    {
+        ListBoxRow? row = key_list_box.get_selected_row ();
+        if (row == null)
+            select_first_row ();
+    }
+
+    private void select_first_row ()
+    {
+        ListBoxRow? row = key_list_box.get_row_at_index (0);
+        if (row != null)
+            key_list_box.select_row ((!) row);
+        key_list_box.get_adjustment ().set_value (0);
+    }
+
     /*\
     * * Key ListBox
     \*/
@@ -228,6 +243,33 @@ class RegistrySearch : Grid, PathElement, BrowsableView
         return false;
     }
 
+    public bool up_or_down_pressed (bool grab_focus, bool is_down)
+    {
+        ListBoxRow? selected_row = key_list_box.get_selected_row ();
+        uint n_items = search_results_model.get_n_items ();
+
+        if (selected_row != null)
+        {
+            int position = ((!) selected_row).get_index ();
+            ListBoxRow? row = null;
+            if (!is_down && (position >= 1))
+                row = key_list_box.get_row_at_index (position - 1);
+            if (is_down && (position < n_items - 1))
+                row = key_list_box.get_row_at_index (position + 1);
+
+            if (row != null)
+                scroll_to_row ((!) row, grab_focus);
+
+            return true;
+        }
+        else if (n_items >= 1)
+        {
+            key_list_box.select_row (key_list_box.get_row_at_index (is_down ? 0 : (int) n_items - 1));
+            return true;
+        }
+        return false;
+    }
+
     [GtkCallback]
     private void row_activated_cb (ListBoxRow list_box_row)
     {
@@ -367,7 +409,10 @@ class RegistrySearch : Grid, PathElement, BrowsableView
     public void start_search (string term)
     {
         if (old_term != null && term == (!) old_term)
+        {
+            ensure_selection ();
             return;
+        }
 
         SettingsModel model = window.model;
         string current_path = window.current_path;
@@ -383,6 +428,8 @@ class RegistrySearch : Grid, PathElement, BrowsableView
                 refine_global_results (term);
                 resume_global_search (current_path, term); // update search term
             }
+
+            ensure_selection ();
         }
         else
         {
@@ -394,6 +441,9 @@ class RegistrySearch : Grid, PathElement, BrowsableView
             local_search (model, SettingsModel.get_base_path (current_path), term);
             bookmark_search (model, current_path, term);
             key_list_box.bind_model (search_results_model, new_list_box_row);
+
+            select_first_row ();
+
             if (term != "")
                 start_global_search (model, current_path, term);
         }
@@ -558,6 +608,9 @@ class RegistrySearch : Grid, PathElement, BrowsableView
                         search_results_model.append (item);
                 }
             }
+
+            ensure_selection ();
+
             return true;
         }
 
diff --git a/editor/registry-view.vala b/editor/registry-view.vala
index e091ab8..576aa64 100644
--- a/editor/registry-view.vala
+++ b/editor/registry-view.vala
@@ -226,6 +226,36 @@ class RegistryView : Grid, PathElement, BrowsableView
         return false;
     }
 
+    public bool up_or_down_pressed (bool grab_focus, bool is_down)
+    {
+        if (key_model == null)
+            return false;
+
+        ListBoxRow? selected_row = key_list_box.get_selected_row ();
+        uint n_items = ((!) key_model).get_n_items ();
+
+        if (selected_row != null)
+        {
+            int position = ((!) selected_row).get_index ();
+            ListBoxRow? row = null;
+            if (!is_down && (position >= 1))
+                row = key_list_box.get_row_at_index (position - 1);
+            if (is_down && (position < n_items - 1))
+                row = key_list_box.get_row_at_index (position + 1);
+
+            if (row != null)
+                scroll_to_row ((!) row, grab_focus);
+
+            return true;
+        }
+        else if (n_items >= 1)
+        {
+            key_list_box.select_row (key_list_box.get_row_at_index (is_down ? 0 : (int) n_items - 1));
+            return true;
+        }
+        return false;
+    }
+
     [GtkCallback]
     private void row_activated_cb (ListBoxRow list_box_row)
     {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]