[gnome-characters/bilelmoussaoui/ui-cleanup: 7/8] sidebar: finish the recent items changes




commit 3000d2cd5a1f9dee3d8b5cd77ff578d7bd87eb45
Author: Bilal Elmoussaoui <bil elmoussaoui gmail com>
Date:   Fri Feb 12 02:40:03 2021 +0100

    sidebar: finish the recent items changes

 src/categoryList.js | 93 ++++++++++++++++++++++++++++++++++++++---------------
 src/window.js       | 38 +++++++++++-----------
 2 files changed, 86 insertions(+), 45 deletions(-)
---
diff --git a/src/categoryList.js b/src/categoryList.js
index a1348e1..22e7387 100644
--- a/src/categoryList.js
+++ b/src/categoryList.js
@@ -159,12 +159,6 @@ const CategoryListRowWidget = GObject.registerClass({
         hbox.pack_start(label, true, true, 0);
 
     }
-
-    select() {
-        let toplevel = this.get_toplevel();
-        let action = toplevel.lookup_action('category');
-        action.activate(new GLib.Variant('s', this.category.name));
-    }
 });
 
 const CategoryListWidget = GObject.registerClass({
@@ -189,7 +183,10 @@ const CategoryListWidget = GObject.registerClass({
 
     vfunc_row_selected(row) {
         if (row != null && row.selectable) {
-            row.select();
+            let toplevel = row.get_toplevel();
+            let action = toplevel.lookup_action('category');
+            action.activate(new GLib.Variant('s', row.category.name));
+            this._lastSelectedRow = row;
         }
     }
     populateCategoryList() {
@@ -207,6 +204,18 @@ const CategoryListWidget = GObject.registerClass({
         }
         return null;
     }
+
+    restorePreviousSelection() {
+        if (this._lastSelectedRow) {
+            this.select_row(this._lastSelectedRow)
+        }
+    }
+
+    unselect() {
+        let selected = this.get_selected_row()
+        if (selected)
+            this.unselect_row(selected)
+    }
 });
 
 const LetterCategoryListWidget = GObject.registerClass({
@@ -343,6 +352,28 @@ const EmojiCategoryListWidget = GObject.registerClass({
     }
 });
 
+const RecentCategoryListWidget = GObject.registerClass({
+
+}, class RecentCategoryListWidget extends CategoryListWidget {
+    _init(params) {
+        super._init(params);
+        this.recentCategory = {
+            name: 'recent',
+            category: Gc.Category.NONE,
+            title: N_('Recently Used'),
+            icon_name: 'document-open-recent-symbolic',
+        };
+        this.recentRow = new CategoryListRowWidget({}, this.recentCategory);
+        this.recentRow.get_style_context().add_class('category');
+        this.recentRow.get_style_context().add_class('recent-category');
+        this.add(this.recentRow)
+    }
+
+    getCategory(name) {
+        return this.recentCategory;
+    }
+});
+
 var CategoryListView = GObject.registerClass({
 }, class CategoryListView extends Gtk.Box {
     _init(params) {
@@ -350,18 +381,18 @@ var CategoryListView = GObject.registerClass({
             hexpand: true, vexpand: true,
             orientation: Gtk.Orientation.VERTICAL,
         });
+        this._lastSelectedList = null;
         super._init(params);
         this.get_style_context().add_class('categories-list');
 
-        let category = {
-            name: 'recent',
-            category: Gc.Category.NONE,
-            title: N_('Recently Used'),
-            icon_name: 'document-open-recent-symbolic',
-        };
-        let recentRow = new CategoryListRowWidget({}, category);
-        recentRow.get_style_context().add_class('category');
-        this.add(recentRow);
+        this._recentCategoryList = new RecentCategoryListWidget();
+        this._recentCategoryList.connect('row-selected', (list, row) => {
+            this._letterCategoryList.unselect();
+            this._emojiCategoryList.unselect();
+            this._lastSelectedList = list;
+            list.select_row(row);
+        });
+        this.add(this._recentCategoryList)
         this.add(new Gtk.Separator({orientation: Gtk.Orientation.HORIZONTAL}));
         
         let emojis_label = new Gtk.Label ({
@@ -375,11 +406,9 @@ var CategoryListView = GObject.registerClass({
             categoryList: EmojiCategoryList
         });
         this._emojiCategoryList.connect('row-selected', (list, row) => {
-            let selected = this._letterCategoryList.get_selected_row();
-            if (selected)
-                this._letterCategoryList.unselect_row(selected);
-
-            recentRow.get_style_context().remove_class('selected');
+            this._letterCategoryList.unselect();
+            this._recentCategoryList.unselect();
+            this._lastSelectedList = list;
             list.select_row(row);
         });
         this.add(this._emojiCategoryList);
@@ -389,16 +418,15 @@ var CategoryListView = GObject.registerClass({
             halign: Gtk.Align.START,
         });
         letters_label.get_style_context().add_class("category-title");
-
         this.add(letters_label);
+
         this._letterCategoryList = new LetterCategoryListWidget({
             categoryList: LetterCategoryList
         });
         this._letterCategoryList.connect('row-selected', (list, row) => {
-            let selected = this._emojiCategoryList.get_selected_row();
-            if (selected)
-                this._emojiCategoryList.unselect_row(selected);
-            recentRow.get_style_context().remove_class('selected');
+            this._emojiCategoryList.unselect();
+            this._recentCategoryList.unselect();
+            this._lastSelectedList = list;
             list.select_row(row);
         });
         this.add(this._letterCategoryList);
@@ -410,6 +438,8 @@ var CategoryListView = GObject.registerClass({
         switch (name) {
             case 'emojis':
                 return this._emojiCategoryList
+            case 'recent':
+                return this._recentCategoryList
             default:
                 return this._letterCategoryList
         }
@@ -418,4 +448,15 @@ var CategoryListView = GObject.registerClass({
     getCategoryList() {
         return this._categoryList;
     }
+
+    get selectedList() {
+        return this._lastSelectedList
+    }
+    
+
+    restorePreviousSelection() {
+        if (this._lastSelectedList) {
+            this._lastSelectedList.restorePreviousSelection()
+        }
+    }
 });
diff --git a/src/window.js b/src/window.js
index e8767ad..de728e4 100644
--- a/src/window.js
+++ b/src/window.js
@@ -128,14 +128,13 @@ var MainWindow = GObject.registerClass({
 
     // Select the first subcategory which contains at least one character.
     _selectFirstSubcategory() {
-        /*let categoryList = this._categoryListView.get_visible_child();
-        let index = 0;
-        let row = categoryList.get_row_at_index(index);
-        if (row.category.name == 'recent' &&
-            this._mainView.recentCharacters.length == 0)
-            index++;
-        categoryList.select_row(categoryList.get_row_at_index(index));
-        */
+        let categoryList;
+        if (this._mainView.recentCharacters.length !== 0) {
+            categoryList = this._categoryListView.getCategoryByName('recent');
+        } else {
+            categoryList = this._categoryListView.getCategoryByName('emojis');
+        }
+        categoryList.select_row(categoryList.get_row_at_index(0));
     }
 
     get search_active() {
@@ -148,14 +147,13 @@ var MainWindow = GObject.registerClass({
 
         this._searchActive = v;
 
-        /*if (this._searchActive) {
-            let categoryList = this._categoryListView.get_visible_child();
+        if (this._searchActive) {
+            let categoryList = this._categoryListView.selectedList;
             categoryList.unselect_all();
             this._updateTitle(_("Search Result"));
         } else {
-            let categoryList = this._categoryListView.get_visible_child();
-            categoryList.restorePreviousSelection();
-        }*/
+            this._categoryListView.restorePreviousSelection();
+        }
 
         this.notify('search-active');
     }
@@ -222,11 +220,12 @@ var MainWindow = GObject.registerClass({
         let categoryName;
         if(name.startsWith("emoji")) {
             categoryName = "emojis";
-        } else {    
+        } else if(name === "recent") {
+            categoryName = "recent";
+        } else {
             categoryName = "letters";
-        }
+        } 
         let categoryList = this._categoryListView.getCategoryByName(categoryName);
-        
         let category = categoryList.getCategory(name);
         if (category) {
             this._mainView.setPage(category);
@@ -235,7 +234,6 @@ var MainWindow = GObject.registerClass({
         }
     }
 
-
     _character(action, v) {
         const [uc, length] = v.get_string();
         this._mainView.addToRecent(uc);
@@ -338,6 +336,8 @@ const MainView = GObject.registerClass({
             hexpand: false,
         });
         scroll.add(recentBox);
+        recentBox.show_all();
+        scroll.show_all();
         // FIXME: Can't use GtkContainer.child_get_property.
         scroll.title = _('Recently Used');
         this.add_titled(scroll, 'recent', scroll.title);
@@ -391,8 +391,8 @@ const MainView = GObject.registerClass({
     }
 
     setPage(category) {
-        if (category.name == 'recent') {
-            if (this.recentCharacters.length == 0)
+        if (category.name === 'recent') {
+            if (this.recentCharacters.length === 0)
                 this.visible_child_name = 'empty-recent';
             else {
                 let categories = this._categoryListView.getCategoryList();


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