[dconf-editor] Allow moving multiple bookmarks top or bottom.



commit 2a4ca2913300b9f83e86279ae536941f6e2eef3e
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Mon Nov 26 09:57:23 2018 +0100

    Allow moving multiple bookmarks top or bottom.

 editor/bookmarks-list.vala | 110 +++++++++++++++++++--------------------------
 editor/bookmarks.vala      |  43 +++++++-----------
 editor/dconf-window.vala   |  30 +------------
 editor/overlayed-list.vala |  26 ++++++++++-
 4 files changed, 90 insertions(+), 119 deletions(-)
---
diff --git a/editor/bookmarks-list.vala b/editor/bookmarks-list.vala
index 35cc008..50ae56b 100644
--- a/editor/bookmarks-list.vala
+++ b/editor/bookmarks-list.vala
@@ -77,6 +77,7 @@ private class BookmarksList : OverlayedList
         change_editability (is_writable);
     }
 
+    bool view_mode = true;
     internal void enter_edit_mode ()
     {
         main_list_box.grab_focus ();
@@ -84,11 +85,11 @@ private class BookmarksList : OverlayedList
         main_list_box.@foreach ((widget) => { ((Bookmark) widget).set_actionable (false); });
         main_list_box.set_activate_on_single_click (false);
         main_list_box.set_selection_mode (SelectionMode.MULTIPLE);
+        view_mode = false;
     }
 
     internal bool leave_edit_mode ()
     {
-
         ListBoxRow? row = (ListBoxRow?) main_list_box.get_focus_child ();  // broken, the child needs to 
have the global focus...
         bool give_focus_to_switch = row == null;
         if (give_focus_to_switch)
@@ -100,6 +101,7 @@ private class BookmarksList : OverlayedList
         main_list_box.@foreach ((widget) => { ((Bookmark) widget).set_actionable (true); });
         main_list_box.set_activate_on_single_click (true);
         main_list_box.set_selection_mode (SelectionMode.SINGLE);
+        view_mode = true;
 
         if (row != null)
             select_row_for_real ((!) row);
@@ -140,10 +142,10 @@ private class BookmarksList : OverlayedList
 
     internal bool create_bookmark_rows (Variant bookmarks_variant)
     {
-        _create_bookmark_rows (bookmarks_variant, ref main_list_store, ref main_list_box, ref 
bookmarks_hashtable);
+        _create_bookmark_rows (bookmarks_variant, view_mode, ref main_list_store, ref main_list_box, ref 
bookmarks_hashtable);
         return n_items == 0;
     }
-    private static void _create_bookmark_rows (Variant bookmarks_variant, ref GLib.ListStore 
main_list_store, ref ListBox main_list_box, ref HashTable<string, Bookmark> bookmarks_hashtable)
+    private static void _create_bookmark_rows (Variant bookmarks_variant, bool view_mode, ref GLib.ListStore 
main_list_store, ref ListBox main_list_box, ref HashTable<string, Bookmark> bookmarks_hashtable)
     {
         string saved_bookmark_name = "";
         ListBoxRow? selected_row = main_list_box.get_selected_row ();
@@ -164,7 +166,7 @@ private class BookmarksList : OverlayedList
                 continue;
             unduplicated_bookmarks += bookmark;
 
-            Bookmark bookmark_row = new Bookmark (bookmark);
+            Bookmark bookmark_row = new Bookmark (bookmark, view_mode);
             main_list_store.append (bookmark_row);
             bookmark_row.show ();
             bookmarks_hashtable.insert (bookmark, bookmark_row);
@@ -263,38 +265,28 @@ private class BookmarksList : OverlayedList
 
     internal void move_top ()
     {
-//        main_list_box.selected_foreach ((_list_box, selected_row) => {
-
-        ListBoxRow? row = main_list_box.get_selected_row ();
-        if (row == null)
-            return; // TODO assert_not_reached?
-
-        int index = ((!) row).get_index ();
-        if (index < 0)
-            assert_not_reached ();
-
-        if (index == 0)
-            return;
+        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;
+            });
 
         string [] old_bookmarks = settings.get_strv ("bookmarks");
         string [] new_bookmarks = new string [0];
-        uint position = 0;
 
-        new_bookmarks += old_bookmarks [index];
-        foreach (string bookmark in old_bookmarks)
+        foreach (int index in indices)
+            new_bookmarks += old_bookmarks [index];
+
+        for (int index = 0; index < old_bookmarks.length; index++)
         {
-            if (index != position)
-                new_bookmarks += bookmark;
-            position++;
+            if (index in indices)
+                continue;
+            new_bookmarks += old_bookmarks [index];
         }
 
-        SignalHandler.block (main_list_store, content_changed_handler);
-        SignalHandler.block (settings, bookmarks_changed_handler);
-        settings.set_strv ("bookmarks", new_bookmarks);
-        GLib.Settings.sync ();   // TODO better? really needed?
-        SignalHandler.unblock (settings, bookmarks_changed_handler);
-        SignalHandler.unblock (main_list_store, content_changed_handler);
-        on_bookmarks_changed (settings, "bookmarks");
+        set_new_bookmarks (new_bookmarks);
 
         Adjustment adjustment = main_list_box.get_adjustment ();
         adjustment.set_value (adjustment.get_lower ());
@@ -340,13 +332,7 @@ private class BookmarksList : OverlayedList
             position++;
         }
 
-        SignalHandler.block (main_list_store, content_changed_handler);
-        SignalHandler.block (settings, bookmarks_changed_handler);
-        settings.set_strv ("bookmarks", new_bookmarks);
-        GLib.Settings.sync ();   // TODO better? really needed?
-        SignalHandler.unblock (settings, bookmarks_changed_handler);
-        SignalHandler.unblock (main_list_store, content_changed_handler);
-        on_bookmarks_changed (settings, "bookmarks");
+        set_new_bookmarks (new_bookmarks);
 
 //        main_list_box.unselect_row ((!) row);
 
@@ -402,13 +388,7 @@ private class BookmarksList : OverlayedList
             position++;
         }
 
-        SignalHandler.block (main_list_store, content_changed_handler);
-        SignalHandler.block (settings, bookmarks_changed_handler);
-        settings.set_strv ("bookmarks", new_bookmarks);
-        GLib.Settings.sync ();   // TODO better? really needed?
-        SignalHandler.unblock (settings, bookmarks_changed_handler);
-        SignalHandler.unblock (main_list_store, content_changed_handler);
-        on_bookmarks_changed (settings, "bookmarks");
+        set_new_bookmarks (new_bookmarks);
 
 //        main_list_box.unselect_row ((!) row);
 
@@ -429,28 +409,35 @@ private class BookmarksList : OverlayedList
 
     internal void move_bottom ()
     {
-//        main_list_box.selected_foreach ((_list_box, selected_row) => {
-
-        ListBoxRow? row = main_list_box.get_selected_row ();
-        if (row == null)
-            return; // TODO assert_not_reached?
-
-        int index = ((!) row).get_index ();
-        if (index < 0)
-            assert_not_reached ();
+        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;
+            });
 
         string [] old_bookmarks = settings.get_strv ("bookmarks");
         string [] new_bookmarks = new string [0];
-        uint position = 0;
 
-        foreach (string bookmark in old_bookmarks)
+        for (int index = 0; index < old_bookmarks.length; index++)
         {
-            if (index != position)
-                new_bookmarks += bookmark;
-            position++;
+            if (index in indices)
+                continue;
+            new_bookmarks += old_bookmarks [index];
         }
-        new_bookmarks += old_bookmarks [index];
 
+        foreach (int index in indices)
+            new_bookmarks += old_bookmarks [index];
+
+        set_new_bookmarks (new_bookmarks);
+
+        Adjustment adjustment = main_list_box.get_adjustment ();
+        adjustment.set_value (adjustment.get_upper ());
+    }
+
+    private void set_new_bookmarks (string [] new_bookmarks)
+    {
         SignalHandler.block (main_list_store, content_changed_handler);
         SignalHandler.block (settings, bookmarks_changed_handler);
         settings.set_strv ("bookmarks", new_bookmarks);
@@ -458,9 +445,6 @@ private class BookmarksList : OverlayedList
         SignalHandler.unblock (settings, bookmarks_changed_handler);
         SignalHandler.unblock (main_list_store, content_changed_handler);
         on_bookmarks_changed (settings, "bookmarks");
-
-        Adjustment adjustment = main_list_box.get_adjustment ();
-        adjustment.set_value (adjustment.get_upper ());
     }
 
     /*\
@@ -553,14 +537,14 @@ private class Bookmark : ListBoxRow
         parse_bookmark_name (bookmark_name, out bookmark_text, out bookmark_type);
 
         construct_actions_names (bookmark_text, bookmark_type, out detailed_action_name, out 
inactive_action_name);
-        set_actionable (true);
 
         bookmark_label.set_label (bookmark_text);
     }
 
-    internal Bookmark (string bookmark_name)
+    internal Bookmark (string bookmark_name, bool view_mode)
     {
         Object (bookmark_name: bookmark_name);
+        set_actionable (view_mode);
     }
 
     internal void set_actionable (bool actionable)
diff --git a/editor/bookmarks.vala b/editor/bookmarks.vala
index 96c1bbf..7600920 100644
--- a/editor/bookmarks.vala
+++ b/editor/bookmarks.vala
@@ -243,35 +243,26 @@ private class Bookmarks : MenuButton
     private void update_actions ()
         requires (actions_init_done)
     {
-        OverlayedList.SelectionState selection_state = bookmarks_list.get_selection_state ();
-
-        bool has_selected_items = selection_state != OverlayedList.SelectionState.EMPTY;
-        bool has_one_selected_item = has_selected_items && (selection_state != 
OverlayedList.SelectionState.MULTIPLE);
+        _update_actions (bookmarks_list.get_selection_state (), ref move_top_action, ref move_up_action, ref 
move_down_action, ref move_bottom_action, ref trash_bookmark_action);
+    }
 
-        bool enable_move_top_action     = has_one_selected_item;    // TODO has_selected_items;
-        bool enable_move_up_action      = has_one_selected_item;
-        bool enable_move_down_action    = has_one_selected_item;
-        bool enable_move_bottom_action  = has_one_selected_item;    // TODO has_selected_items;
+    internal static void _update_actions (OverlayedList.SelectionState selection_state, ref SimpleAction 
move_top_action, ref SimpleAction move_up_action, ref SimpleAction move_down_action, ref SimpleAction 
move_bottom_action, ref SimpleAction trash_bookmark_action)
+    {
+        trash_bookmark_action.set_enabled (selection_state != OverlayedList.SelectionState.EMPTY);
 
-        if (has_one_selected_item)
-        {
-            if (selection_state == OverlayedList.SelectionState.UNIQUE || selection_state == 
OverlayedList.SelectionState.FIRST)
-            {
-                enable_move_top_action = false;
-                enable_move_up_action = false;
-            }
-            if (selection_state == OverlayedList.SelectionState.UNIQUE || selection_state == 
OverlayedList.SelectionState.LAST)
-            {
-                enable_move_down_action = false;
-                enable_move_bottom_action = false;
-            }
-        }
+        bool one_middle_selection = selection_state == OverlayedList.SelectionState.MIDDLE;
+        bool enable_move_up_action      = one_middle_selection || (selection_state == 
OverlayedList.SelectionState.LAST);
+        bool enable_move_down_action    = one_middle_selection || (selection_state == 
OverlayedList.SelectionState.FIRST);
+        move_up_action.set_enabled     (enable_move_up_action);
+        move_down_action.set_enabled   (enable_move_down_action);
 
-               move_up_action.set_enabled (enable_move_up_action);
-              move_top_action.set_enabled (enable_move_top_action);
-             move_down_action.set_enabled (enable_move_down_action);
-           move_bottom_action.set_enabled (enable_move_bottom_action);
-        trash_bookmark_action.set_enabled (has_selected_items);
+        bool multiple_middle_selections = selection_state == OverlayedList.SelectionState.MULTIPLE;
+        move_top_action.set_enabled    ((selection_state == OverlayedList.SelectionState.MULTIPLE_LAST)
+                                        || multiple_middle_selections
+                                        || enable_move_up_action);
+        move_bottom_action.set_enabled ((selection_state == OverlayedList.SelectionState.MULTIPLE_FIRST)
+                                        || multiple_middle_selections
+                                        || enable_move_down_action);
     }
 
     private void install_action_entries ()
diff --git a/editor/dconf-window.vala b/editor/dconf-window.vala
index 0128ad9..b1bc1f5 100644
--- a/editor/dconf-window.vala
+++ b/editor/dconf-window.vala
@@ -900,35 +900,7 @@ private class DConfWindow : AdaptativeWindow, AdaptativeWidget
     private void update_actions ()
         requires (actions_init_done)
     {
-        OverlayedList.SelectionState selection_state = browser_view.get_bookmarks_selection_state ();
-
-        bool has_selected_items = selection_state != OverlayedList.SelectionState.EMPTY;
-        bool has_one_selected_item = has_selected_items && (selection_state != 
OverlayedList.SelectionState.MULTIPLE);
-
-        bool enable_move_top_action     = has_one_selected_item;    // TODO has_selected_items;
-        bool enable_move_up_action      = has_one_selected_item;
-        bool enable_move_down_action    = has_one_selected_item;
-        bool enable_move_bottom_action  = has_one_selected_item;    // TODO has_selected_items;
-
-        if (has_one_selected_item)
-        {
-            if (selection_state == OverlayedList.SelectionState.UNIQUE || selection_state == 
OverlayedList.SelectionState.FIRST)
-            {
-                enable_move_top_action = false;
-                enable_move_up_action = false;
-            }
-            if (selection_state == OverlayedList.SelectionState.UNIQUE || selection_state == 
OverlayedList.SelectionState.LAST)
-            {
-                enable_move_down_action = false;
-                enable_move_bottom_action = false;
-            }
-        }
-
-               move_up_action.set_enabled (enable_move_up_action);
-              move_top_action.set_enabled (enable_move_top_action);
-             move_down_action.set_enabled (enable_move_down_action);
-           move_bottom_action.set_enabled (enable_move_bottom_action);
-        trash_bookmark_action.set_enabled (has_selected_items);
+        Bookmarks._update_actions (browser_view.get_bookmarks_selection_state (), ref move_top_action, ref 
move_up_action, ref move_down_action, ref move_bottom_action, ref trash_bookmark_action);
     }
 
     private void install_bmk_action_entries ()
diff --git a/editor/overlayed-list.vala b/editor/overlayed-list.vala
index a004a48..9aa6c50 100644
--- a/editor/overlayed-list.vala
+++ b/editor/overlayed-list.vala
@@ -177,11 +177,16 @@ private abstract class OverlayedList : Overlay, AdaptativeWidget
 
     internal enum SelectionState {
         EMPTY,
+        // one
         UNIQUE,
         FIRST,
         LAST,
         MIDDLE,
-        MULTIPLE
+        // multiple
+        MULTIPLE,
+        MULTIPLE_FIRST,
+        MULTIPLE_LAST,
+        ALL
     }
 
     internal SelectionState get_selection_state ()
@@ -192,7 +197,26 @@ private abstract class OverlayedList : Overlay, AdaptativeWidget
         if (n_selected_rows == 0)
             return SelectionState.EMPTY;
         if (n_selected_rows >= 2)
+        {
+            uint n_items = main_list_store.get_n_items ();
+            if (n_selected_rows == n_items)
+                return SelectionState.ALL;
+            uint first_items = 0;
+            uint last_items = 0;
+            uint first_of_the_last_items_index = n_items - n_selected_rows;
+            selected_rows.foreach ((row) => {
+                    uint index = row.get_index ();
+                    if (index < n_selected_rows)
+                        first_items++;
+                    if (index >= first_of_the_last_items_index)
+                        last_items++;
+                });
+            if (first_items == n_selected_rows)
+                return SelectionState.MULTIPLE_FIRST;
+            if (last_items == n_selected_rows)
+                return SelectionState.MULTIPLE_LAST;
             return SelectionState.MULTIPLE;
+        }
 
         int index = selected_rows.nth_data (0).get_index ();
         bool is_first = index == 0;


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