[gnome-maps/wip/mlundblad/search-as-you-type: 9/9] WIP: placeEntry: Auto-complete searches



commit d914fd48115f2de3901a33a545f13ebb69125325
Author: Marcus Lundblad <ml update uu se>
Date:   Sun May 5 21:38:14 2019 +0200

    WIP: placeEntry: Auto-complete searches

 src/placeEntry.js | 73 ++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 53 insertions(+), 20 deletions(-)
---
diff --git a/src/placeEntry.js b/src/placeEntry.js
index 8fc86f5..baa0754 100644
--- a/src/placeEntry.js
+++ b/src/placeEntry.js
@@ -34,6 +34,9 @@ const PlaceStore = imports.placeStore;
 const PlacePopover = imports.placePopover;
 const Utils = imports.utils;
 
+// minimum number of characters to start completion, TODO: how to handle ideographs?
+const MIN_CHARS_COMPLETION = 3;
+
 var PlaceEntry = GObject.registerClass({
     Properties: {
         'place': GObject.ParamSpec.object('place',
@@ -54,11 +57,13 @@ var PlaceEntry = GObject.registerClass({
 
         if (p) {
             if (p.name) {
-                this.text = p.name;
+                this._placeText = p.name;
             } else
-                this.text = p.location.latitude + ', ' + p.location.longitude;
+                this._placeText = p.location.latitude + ', ' + p.location.longitude;
         } else
-            this.text = '';
+            this._placeText = '';
+
+        this.text = this._placeText;
 
         this._place = p;
         this.notify('place');
@@ -99,23 +104,7 @@ var PlaceEntry = GObject.registerClass({
         this._popover = this._createPopover(numVisible, maxChars);
 
         this.connect('activate', this._onActivate.bind(this));
-        this.connect('search-changed', () => {
-            if (this._cancellable)
-                this._cancellable.cancel();
-
-            this._refreshFilter();
-
-            if (this.text.length === 0) {
-                this._popover.hide();
-                this.place = null;
-                return;
-            }
-
-            if (this._filter.iter_n_children(null) > 0)
-                this._popover.showCompletion();
-            else
-                this._popover.hide();
-        });
+        this.connect('search-changed', this._onSearchChanged.bind(this));
 
         if (parseOnFocusOut) {
             this.connect('focus-out-event', () => {
@@ -125,6 +114,39 @@ var PlaceEntry = GObject.registerClass({
         }
     }
 
+    _onSearchChanged() {
+        // wait for an ongoing search
+        if (this._cancellable)
+            return;
+
+        //this._refreshFilter();
+
+        if (this.text.length < MIN_CHARS_COMPLETION) {
+            this._popover.hide();
+            this.place = null;
+            this._previousSearch = null;
+            return;
+        } else if (this.text.length >= MIN_CHARS_COMPLETION &&
+                   this.text !== this._placeText) {
+            // if no previous search has been performed, show spinner
+            if (!this._previousSearch ||
+                this._previousSearch.length < MIN_CHARS_COMPLETION ||
+                this._placeText) {
+                this._popover.showSpinner();
+            }
+            this._placeText = '';
+            this._doSearch();
+            return;
+        }
+
+        /*
+        if (this._filter.iter_n_children(null) > 0)
+            this._popover.showCompletion();
+        else
+            this._popover.hide();
+        */
+    }
+
     _locEquals(placeA, placeB) {
         if (!placeA.location || !placeB.location)
             return false;
@@ -207,14 +229,21 @@ var PlaceEntry = GObject.registerClass({
         let bbox = this._mapView.view.get_bounding_box();
 
         this._popover.showSpinner();
+        this._doSearch();
+    }
 
+    _doSearch() {
+        if (this._cancellable)
+            this._cancellable.cancel();
         this._cancellable = new Gio.Cancellable();
+        this._previousSearch = this.text;
         Application.photonGeocode.search(this.text,
                                          this._mapView.view.latitude,
                                          this._mapView.view.longitude,
                                          this._cancellable,
                                          (places, error) => {
             Utils.debug('places: ' + places);
+            this._cancellable = null;
             if (!places) {
                 this.place = null;
                 this._popover.showNoResult();
@@ -222,6 +251,10 @@ var PlaceEntry = GObject.registerClass({
             }
             this._popover.updateResult(places, this.text);
             this._popover.showResult();
+
+            // if search input has been updated, trigger a refresh
+            if (this.text !== this._previousSearch)
+                this._onSearchChanged();
         });
     }
 });


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