[gnome-games/wip/exalm/tnum: 14/24] collection-view: Add support for renaming user collections




commit 1fbdb64c4be85e42d92a2788ab3130ebc9377807
Author: Neville <nevilleantony98 gmail com>
Date:   Sun Aug 9 15:58:06 2020 +0530

    collection-view: Add support for renaming user collections
    
    Adds a rename action in the menu when viewing a collection. Renaming
    can done by entering a valid name and pressing enter/return key.
    It can also be cancelled by pressing Escape key.

 data/ui/collection-view.ui  | 79 +++++++++++++++++++++++++++++++++++++++------
 src/ui/collection-view.vala | 62 ++++++++++++++++++++++++++++++++++-
 2 files changed, 131 insertions(+), 10 deletions(-)
---
diff --git a/data/ui/collection-view.ui b/data/ui/collection-view.ui
index 7fed118ba..97d3658ce 100644
--- a/data/ui/collection-view.ui
+++ b/data/ui/collection-view.ui
@@ -217,6 +217,7 @@
                 <object class="HdyHeaderBar" id="collection_subpage_header_bar">
                   <property name="visible">True</property>
                   <property name="show-close-button">True</property>
+                  <property name="title" bind-source="collections_page" bind-property="collection-title"/>
                   <child>
                     <object class="GtkButton">
                       <property name="visible">True</property>
@@ -239,15 +240,6 @@
                       </child>
                     </object>
                   </child>
-                  <child type="title">
-                    <object class="GtkLabel">
-                      <property name="visible">True</property>
-                      <property name="label" bind-source="collections_page" 
bind-property="collection-title"/>
-                      <style>
-                        <class name="title"/>
-                      </style>
-                    </object>
-                  </child>
                   <child>
                     <object class="GtkMenuButton">
                       <property name="menu-model">collection_menu</property>
@@ -559,6 +551,71 @@
       </object>
     </child>
   </template>
+  <object class="GtkPopover" id="rename_popover">
+    <property name="position">top</property>
+    <property name="relative-to">collection_subpage_header_bar</property>
+    <property name="width-request">360</property>
+    <child>
+      <object class="GtkBox">
+        <property name="visible">true</property>
+        <property name="orientation">vertical</property>
+        <property name="margin">12</property>
+        <property name="spacing">12</property>
+        <child>
+          <object class="GtkLabel">
+            <property name="visible">true</property>
+            <property name="label" translatable="yes">Rename Collection</property>
+            <attributes>
+              <attribute name="weight" value="bold"/>
+            </attributes>
+          </object>
+        </child>
+        <child>
+          <object class="GtkBox">
+            <property name="visible">true</property>
+            <property name="spacing">12</property>
+            <child>
+              <object class="GtkEntry" id="collection_rename_entry">
+                <property name="visible">true</property>
+                <property name="hexpand">true</property>
+                <signal name="notify::text" handler="update_collection_name_validity"/>
+                <signal name="activate" handler="on_collection_rename_activated"/>
+              </object>
+            </child>
+            <child>
+              <object class="GtkButton">
+                <property name="visible">true</property>
+                <property name="label" translatable="yes">_Rename</property>
+                <property name="use-underline">True</property>
+                <property name="sensitive" bind-source="GamesCollectionView" 
bind-property="is-collection-rename-valid"/>
+                <signal name="clicked" handler="on_collection_rename_activated"/>
+                <style>
+                  <class name="suggested-action"/>
+                </style>
+              </object>
+            </child>
+          </object>
+        </child>
+        <child>
+          <object class="GtkRevealer">
+            <property name="visible">True</property>
+            <property name="reveal-child" bind-source="GamesCollectionView" 
bind-property="is-collection-rename-valid" bind-flags="invert-boolean|sync-create"/>
+            <child>
+              <object class="GtkLabel" id="collection_rename_error_label">
+                <property name="visible">true</property>
+                <property name="halign">center</property>
+                <property name="wrap">true</property>
+                <property name="wrap-mode">word</property>
+                <style>
+                  <class name="dim-label"/>
+                </style>
+              </object>
+            </child>
+          </object>
+        </child>
+      </object>
+    </child>
+  </object>
   <object class="HdySwipeGroup" id="swipe_group"/>
   <object class="HdySwipeGroup" id="collections_swipe_group">
     <swipeables>
@@ -599,6 +656,10 @@
   </menu>
   <menu id="collection_menu">
     <section>
+      <item>
+        <attribute name="label" translatable="yes">_Rename</attribute>
+        <attribute name="action">view.rename-collection</attribute>
+      </item>
       <item>
         <attribute name="label" translatable="yes">R_emove</attribute>
         <attribute name="action">view.remove-collection</attribute>
diff --git a/src/ui/collection-view.vala b/src/ui/collection-view.vala
index 2e56ea0c1..683af72c9 100644
--- a/src/ui/collection-view.vala
+++ b/src/ui/collection-view.vala
@@ -50,6 +50,12 @@ private class Games.CollectionView : Gtk.Box, UiView {
        private Hdy.SwipeGroup collections_swipe_group;
        [GtkChild]
        private UndoNotification undo_notification;
+       [GtkChild]
+       private Gtk.Entry collection_rename_entry;
+       [GtkChild]
+       private Gtk.Popover rename_popover;
+       [GtkChild]
+       private Gtk.Label collection_rename_error_label;
 
        private bool _is_view_active;
        public bool is_view_active {
@@ -127,6 +133,7 @@ private class Games.CollectionView : Gtk.Box, UiView {
        public bool is_subview_open { get; set; }
        public bool is_selection_mode { get; set; }
        public bool is_selection_available { get; set; }
+       public bool is_collection_rename_valid { get; set; }
 
        private CollectionManager collection_manager;
        private KonamiCode konami_code;
@@ -137,7 +144,8 @@ private class Games.CollectionView : Gtk.Box, UiView {
                { "toggle-select",     toggle_select },
                { "favorite-action",   favorite_action },
                { "add-to-collection", add_to_collection },
-               { "remove-collection", remove_collection }
+               { "remove-collection", remove_collection },
+               { "rename-collection", rename_collection }
        };
 
        construct {
@@ -409,6 +417,58 @@ private class Games.CollectionView : Gtk.Box, UiView {
                undo_notification.show_notification ();
        }
 
+       public void rename_collection () {
+               assert (collections_page.current_collection is UserCollection);
+               collection_rename_entry.text = collections_page.collection_title;
+               rename_popover.popup ();
+               collection_rename_entry.grab_focus ();
+       }
+
+       [GtkCallback]
+       private void update_collection_name_validity () {
+               var name = collection_rename_entry.text.strip ();
+
+               if (name == collections_page.collection_title) {
+                       is_collection_rename_valid = true;
+                       collection_rename_error_label.label = "";
+               }
+               else if (name == "") {
+                       is_collection_rename_valid = false;
+                       collection_rename_error_label.label = _("Collection name cannot be empty");
+               }
+               else if (collection_manager.does_collection_title_exist (name)) {
+                       is_collection_rename_valid = false;
+                       collection_rename_error_label.label = _("A collection with this name already exists");
+               }
+               else {
+                       is_collection_rename_valid = true;
+                       collection_rename_error_label.label = "";
+               }
+
+               if (is_collection_rename_valid)
+                       collection_rename_entry.get_style_context ().remove_class ("error");
+               else
+                       collection_rename_entry.get_style_context ().add_class ("error");
+       }
+
+       [GtkCallback]
+       private void on_collection_rename_activated () {
+               assert (collections_page.current_collection is UserCollection);
+
+               if (!is_collection_rename_valid)
+                       return;
+
+               var name = collection_rename_entry.text.strip ();
+               var collection = collections_page.current_collection as UserCollection;
+               if (collection == null)
+                       return;
+
+               collection.set_title (name);
+               collections_page.collection_title = name;
+               rename_popover.popdown ();
+               collections_page.invalidate_sort ();
+       }
+
        [GtkCallback]
        private void update_selection_action_bar () {
                Game[] games = {};


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