[dconf-editor] Add some utilities to OverlayedList.



commit da47cc0c273d902f1f118d6ea8e6736a34dfa66a
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Mon Nov 26 11:59:11 2018 +0100

    Add some utilities to OverlayedList.

 editor/bookmarks-list.vala | 46 ++++++++++++----------------------------------
 editor/overlayed-list.vala | 28 ++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 34 deletions(-)
---
diff --git a/editor/bookmarks-list.vala b/editor/bookmarks-list.vala
index 50ae56b..9daba94 100644
--- a/editor/bookmarks-list.vala
+++ b/editor/bookmarks-list.vala
@@ -265,13 +265,7 @@ private class BookmarksList : OverlayedList
 
     internal void move_top ()
     {
-        int [] indices = {};
-        main_list_box.selected_foreach ((_list_box, selected_row) => {
-                int index = selected_row.get_index ();
-                if (index < 0)
-                    assert_not_reached ();
-                indices += index;
-            });
+        int [] indices = get_selected_rows_indices ();
 
         string [] old_bookmarks = settings.get_strv ("bookmarks");
         string [] new_bookmarks = new string [0];
@@ -287,23 +281,18 @@ private class BookmarksList : OverlayedList
         }
 
         set_new_bookmarks (new_bookmarks);
-
-        Adjustment adjustment = main_list_box.get_adjustment ();
-        adjustment.set_value (adjustment.get_lower ());
+        scroll_top ();
     }
 
     internal void move_up ()
     {
-        ListBoxRow? row = main_list_box.get_selected_row ();
-        if (row == null)
+        int [] indices = get_selected_rows_indices ();
+        if (indices.length != 1)
             return; // TODO assert_not_reached?
-
-        int index = ((!) row).get_index ();
-        if (index < 0)
-            assert_not_reached ();
+        int index = indices [0];
 
         if (index == 0)
-            return;
+            return; // TODO assert_not_reached?
 
         ListBoxRow? prev_row = main_list_box.get_row_at_index (index - 1);
         if (prev_row == null)
@@ -353,17 +342,14 @@ private class BookmarksList : OverlayedList
 
     internal void move_down ()
     {
-        ListBoxRow? row = main_list_box.get_selected_row ();
-        if (row == null)
+        int [] indices = get_selected_rows_indices ();
+        if (indices.length != 1)
             return; // TODO assert_not_reached?
-
-        int index = ((!) row).get_index ();
-        if (index < 0)
-            assert_not_reached ();
+        int index = indices [0];
 
         ListBoxRow? next_row = main_list_box.get_row_at_index (index + 1);
         if (next_row == null)
-            return;
+            return; // TODO assert_not_reached?
 
         Allocation list_allocation, row_allocation;
         scrolled.get_allocation (out list_allocation);
@@ -409,13 +395,7 @@ private class BookmarksList : OverlayedList
 
     internal void move_bottom ()
     {
-        int [] indices = {};
-        main_list_box.selected_foreach ((_list_box, selected_row) => {
-                int index = selected_row.get_index ();
-                if (index < 0)
-                    assert_not_reached ();
-                indices += index;
-            });
+        int [] indices = get_selected_rows_indices ();
 
         string [] old_bookmarks = settings.get_strv ("bookmarks");
         string [] new_bookmarks = new string [0];
@@ -431,9 +411,7 @@ private class BookmarksList : OverlayedList
             new_bookmarks += old_bookmarks [index];
 
         set_new_bookmarks (new_bookmarks);
-
-        Adjustment adjustment = main_list_box.get_adjustment ();
-        adjustment.set_value (adjustment.get_upper ());
+        scroll_bottom ();
     }
 
     private void set_new_bookmarks (string [] new_bookmarks)
diff --git a/editor/overlayed-list.vala b/editor/overlayed-list.vala
index 9aa6c50..b310541 100644
--- a/editor/overlayed-list.vala
+++ b/editor/overlayed-list.vala
@@ -163,6 +163,34 @@ private abstract class OverlayedList : Overlay, AdaptativeWidget
         main_list_box.select_row (row);
     }
 
+    /*\
+    * * utilities
+    \*/
+
+    protected int [] get_selected_rows_indices ()
+    {
+        int [] indices = new int [0];
+        main_list_box.selected_foreach ((_list_box, selected_row) => {
+                int index = selected_row.get_index ();
+                if (index < 0)
+                    assert_not_reached ();
+                indices += index;
+            });
+        return indices;
+    }
+
+    protected void scroll_top ()
+    {
+        Adjustment adjustment = main_list_box.get_adjustment ();
+        adjustment.set_value (adjustment.get_lower ());
+    }
+
+    protected void scroll_bottom ()
+    {
+        Adjustment adjustment = main_list_box.get_adjustment ();
+        adjustment.set_value (adjustment.get_upper ());
+    }
+
     /*\
     * * selection state
     \*/


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