[gnome-maps/wip/mlundblad/osm-add-location: 5/7] Introduce an abstract base class for search result popovers



commit 48695d38f6693da22fa0a6fb4d2919a041de2341
Author: Marcus Lundblad <ml update uu se>
Date:   Sun Jan 3 23:08:48 2016 +0100

    Introduce an abstract base class for search result popovers
    
    Refactor-out logic connecting the search entry to the search popover's
    result list.
    This is in preparation for a similar component in the search for location
    types view when adding/editing locations in OpenStreetMap.

 data/ui/search-popup.ui              |    2 +-
 src/org.gnome.Maps.src.gresource.xml |    1 +
 src/propagatingSearchPopover.js      |  112 ++++++++++++++++++++++++++++++++++
 src/searchPopup.js                   |   74 +----------------------
 4 files changed, 116 insertions(+), 73 deletions(-)
---
diff --git a/data/ui/search-popup.ui b/data/ui/search-popup.ui
index ae7d839..f122bc0 100644
--- a/data/ui/search-popup.ui
+++ b/data/ui/search-popup.ui
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <!-- interface-requires gtk+ 3.10 -->
-  <template class="Gjs_SearchPopup" parent="GtkPopover">
+  <template class="Gjs_SearchPopup" parent="Gjs_PropagatingSearchPopover">
     <property name="visible">False</property>
     <property name="hexpand">False</property>
     <property name="modal">False</property>
diff --git a/src/org.gnome.Maps.src.gresource.xml b/src/org.gnome.Maps.src.gresource.xml
index 676ea96..ec6d5d6 100644
--- a/src/org.gnome.Maps.src.gresource.xml
+++ b/src/org.gnome.Maps.src.gresource.xml
@@ -46,6 +46,7 @@
     <file>placeListRow.js</file>
     <file>placeMarker.js</file>
     <file>placeStore.js</file>
+    <file>propagatingSearchPopover.js</file>
     <file>route.js</file>
     <file>routeEntry.js</file>
     <file>routeQuery.js</file>
diff --git a/src/propagatingSearchPopover.js b/src/propagatingSearchPopover.js
new file mode 100644
index 0000000..8c0573c
--- /dev/null
+++ b/src/propagatingSearchPopover.js
@@ -0,0 +1,112 @@
+/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
+/* vim: set et ts=4 sw=4: */
+/*
+ * Copyright (c) 2016 Marcus Lundblad.
+ *
+ * GNOME Maps is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * GNOME Maps is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with GNOME Maps; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Marcus Lundblad <ml update uu se>
+ *         Jonas Danielsson <jonas threetimestwo org>
+ */
+
+const Gdk = imports.gi.Gdk;
+const GObject = imports.gi.GObject;
+const Gtk = imports.gi.Gtk;
+const Lang = imports.lang;
+
+/* Abstract search result popover that progagates keypress events from a
+   focus-taking internal widget to the spawning search entry widget */
+const PropagatingSearchPopover = new Lang.Class({
+    Name: 'PropagatingSearchPopover',
+    Extends: Gtk.Popover,
+    Abstract: true,
+
+    _init: function(props) {
+        this.parent(props);
+
+        this._entry = this.relative_to;
+
+        // We need to propagate events to the listbox so that we can
+        // keep typing while selecting a place. But we do not want to
+        // propagate the 'enter' key press if there is a selection.
+        this._entry.connect('key-press-event',
+                            this._propagateKeys.bind(this));
+        this._entry.connect('button-press-event',
+                            this._list.unselect_all.bind(this._list));
+    },
+
+    _propagateKeys: function(entry, event) {
+        let row;
+
+        if (this.visible) {
+            row = this._list.get_selected_row();
+            if (!row)
+                row = this._list.get_row_at_index(0);
+        } else
+            row = this._list.get_row_at_index(0);
+
+        if (!row)
+            return false;
+
+        let length = this._list.get_children().length;
+        let keyval = event.get_keyval()[1];
+
+        if (keyval === Gdk.KEY_Escape) {
+            this._list.unselect_all();
+            this.hide();
+            return false;
+        }
+
+        // If we get an 'enter' keypress and we have a selected
+        // row, we do not want to propagate the event.
+        if ((this.visible && row.is_selected()) &&
+            keyval === Gdk.KEY_Return ||
+            keyval === Gdk.KEY_KP_ENTER ||
+            keyval === Gdk.KEY_ISO_Enter) {
+            row.activate();
+
+            return true;
+        } else if (keyval === Gdk.KEY_KP_Up || keyval === Gdk.KEY_Up) {
+            this.show();
+
+            if (!row.is_selected()) {
+                let pRow = this._list.get_row_at_index(length - 1);
+                this._list.select_row(pRow);
+                return false;
+            }
+
+            if (row.get_index() > 0) {
+                let pRow = this._list.get_row_at_index(row.get_index() - 1);
+                this._list.select_row(pRow);
+            } else
+                this._list.unselect_all();
+        } else if (keyval === Gdk.KEY_KP_Down || keyval === Gdk.KEY_Down) {
+            this.show();
+
+            if (!row.is_selected()) {
+                this._list.select_row(row);
+                return true;
+            }
+
+            if (row.get_index() !== (length - 1)) {
+                let nRow = this._list.get_row_at_index(row.get_index() + 1);
+                this._list.select_row(nRow);
+            } else
+                this._list.unselect_all();
+            return true;
+        }
+        return false;
+    }
+});
+
diff --git a/src/searchPopup.js b/src/searchPopup.js
index dd33ae1..fa5d1f0 100644
--- a/src/searchPopup.js
+++ b/src/searchPopup.js
@@ -17,7 +17,6 @@
  * Author: Jonas Danielsson <jonas threetimestwo org>
  */
 
-const Gdk = imports.gi.Gdk;
 const GObject = imports.gi.GObject;
 const Gtk = imports.gi.Gtk;
 const Lang = imports.lang;
@@ -25,6 +24,7 @@ const Lang = imports.lang;
 const Application = imports.application;
 const PlaceListRow = imports.placeListRow;
 const PlaceStore = imports.placeStore;
+const PropagatingSearchPopover = imports.propagatingSearchPopover;
 
 const _PLACE_ICON_SIZE = 20;
 
@@ -37,7 +37,7 @@ const Mode = {
 
 const SearchPopup = new Lang.Class({
     Name: 'SearchPopup',
-    Extends: Gtk.Popover,
+    Extends: PropagatingSearchPopover.PropagatingSearchPopover,
     Signals : {
         'selected' : { param_types: [ GObject.TYPE_OBJECT ] }
     },
@@ -81,14 +81,6 @@ const SearchPopup = new Lang.Class({
         this._list.connect('selected-rows-changed',
                            this._updateHint.bind(this));
 
-        // We need to propagate events to the listbox so that we can
-        // keep typing while selecting a place. But we do not want to
-        // propagate the 'enter' key press if there is a selection.
-        this._entry.connect('key-press-event',
-                            this._propagateKeys.bind(this));
-        this._entry.connect('button-press-event',
-                            this._list.unselect_all.bind(this._list));
-
         this._list.set_header_func(function(row, before) {
             let header = new Gtk.Separator();
             if (before)
@@ -193,67 +185,5 @@ const SearchPopup = new Lang.Class({
             this._hintRevealer.reveal_child = false;
         else
             this._hintRevealer.reveal_child = true;
-    },
-
-    _propagateKeys: function(entry, event) {
-        let row;
-
-        if (this.visible) {
-            row = this._list.get_selected_row();
-            if (!row)
-                row = this._list.get_row_at_index(0);
-        } else
-            row = this._list.get_row_at_index(0);
-
-        if (!row)
-            return false;
-
-        let length = this._list.get_children().length;
-        let keyval = event.get_keyval()[1];
-
-        if (keyval === Gdk.KEY_Escape) {
-            this._list.unselect_all();
-            this.hide();
-            return false;
-        }
-
-        // If we get an 'enter' keypress and we have a selected
-        // row, we do not want to propagate the event.
-        if ((this.visible && row.is_selected()) &&
-            keyval === Gdk.KEY_Return ||
-            keyval === Gdk.KEY_KP_ENTER ||
-            keyval === Gdk.KEY_ISO_Enter) {
-            row.activate();
-
-            return true;
-        } else if (keyval === Gdk.KEY_KP_Up || keyval === Gdk.KEY_Up) {
-            this.show();
-
-            if (!row.is_selected()) {
-                let pRow = this._list.get_row_at_index(length - 1);
-                this._list.select_row(pRow);
-                return false;
-            }
-
-            if (row.get_index() > 0) {
-                let pRow = this._list.get_row_at_index(row.get_index() - 1);
-                this._list.select_row(pRow);
-            } else
-                this._list.unselect_all();
-        } else if (keyval === Gdk.KEY_KP_Down || keyval === Gdk.KEY_Down) {
-            this.show();
-
-            if (!row.is_selected()) {
-                this._list.select_row(row);
-                return false;
-            }
-
-            if (row.get_index() !== (length - 1)) {
-                let nRow = this._list.get_row_at_index(row.get_index() + 1);
-                this._list.select_row(nRow);
-            } else
-                this._list.unselect_all();
-        }
-        return false;
     }
 });


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