[gnome-tweak-tool/wip/pwood/app-chooser: 4/11] AppChooser : Desensitize ‘Add’ button when no app is sel ected



commit 5ef5c1f085b649315ad873ad695e140f6717dd11
Author: Phillip Wood <phillip wood dunelm org uk>
Date:   Wed Apr 1 18:37:49 2015 +0100

    AppChooser: Desensitize ‘Add’ button when no app is selected
    
    The ‘Add Application’ button shouldn't be sensitive if there is no
    application currently selected. This is complicated be the fact that
    when Gtk.Listbox filters the list it does not clear the selection if the
    selected row is filtered out so we need to watch for the selected row
    being umapped.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=747983

 configure.ac                         |    2 +-
 gtweak/tweaks/tweak_group_startup.py |   48 ++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 731839c..ca15ff8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -8,7 +8,7 @@ AM_PATH_PYTHON([2.6])
 GLIB_GSETTINGS
 
 DESKTOP_SCHEMAS_REQUIRED_VERSION=3.4.0
-GTK_REQUIRED_VERSION=3.12.0
+GTK_REQUIRED_VERSION=3.14.0
 PYGOBJECT_REQUIRED_VERSION=3.2.1
 
 PKG_CHECK_MODULES([GSETTINGS_DESKTOP_SCHEMAS], [gsettings-desktop-schemas >= 
$DESKTOP_SCHEMAS_REQUIRED_VERSION])
diff --git a/gtweak/tweaks/tweak_group_startup.py b/gtweak/tweaks/tweak_group_startup.py
index ef74892..b1f58c5 100644
--- a/gtweak/tweaks/tweak_group_startup.py
+++ b/gtweak/tweaks/tweak_group_startup.py
@@ -36,6 +36,9 @@ class _AppChooser(Gtk.Dialog):
 
         self._running = {}
         self._all = {}
+        self._selected_row = None
+        self._map_id = 0
+        self._unmap_id = 0
 
         self.entry = Gtk.SearchEntry(
                 placeholder_text=_("Search Applications..."))
@@ -52,6 +55,8 @@ class _AppChooser(Gtk.Dialog):
         lb.set_filter_func(self._list_filter_func, None)
         self.entry.connect("search-changed", lambda e: lb.invalidate_filter())
 
+        lb.connect("row-selected", self._on_row_selected)
+
         apps = Gio.app_info_get_all()
         for a in apps:
             if a.get_id() not in startup_apps:
@@ -136,6 +141,49 @@ class _AppChooser(Gtk.Dialog):
                   return True
       return False
 
+# Gtk.ListBox does not deselect rows when they are filtered out, it
+# just hides them so we need to watch for them being mapped and
+# umapped and change the 'Add' button sensitivity appropriately.
+    def _on_row_mapped(self, row):
+        self.set_response_sensitive(Gtk.ResponseType.OK, True)
+        self._selected_row.disconnect(self._map_id)
+        self._map_id = 0
+        self._unmap_id = row.connect("unmap", self._on_row_unmapped)
+
+    def _on_row_unmapped(self, row):
+        self.set_response_sensitive(Gtk.ResponseType.OK, False)
+        self._selected_row.disconnect(self._unmap_id)
+        self._unmap_id = 0
+        self._map_id = row.connect("map", self._on_row_mapped)
+
+# When the selected row is changed we need to take care to disconnect
+# the 'map' and 'unmap' signal handlers for the old row and connect
+# the handlers to the new row so we can detect if it gets hidden by
+# the filter.
+    def _on_row_selected(self, box, row):
+        if row:
+            if row == self._selected_row:
+                return
+            elif not row.get_mapped():
+                box.select_row(self._selected_row)
+            elif self._map_id:
+                self._selected_row.disconnect(self._map_id)
+                self._map_id = 0
+                self.set_response_sensitive(Gtk.ResponseType.OK, True)
+            elif self._unmap_id:
+                self._selected_row.disconnect(self._unmap_id)
+            self._selected_row = row
+            self._unmap_id = row.connect("unmap", self._on_row_unmapped)
+        else:
+            if self._map_id:
+                self._selected_row.disconnect(self._map_id)
+                self._map_id = 0
+            elif self._unmap_id:
+                self._selected_row.disconnect(self._unmap_id)
+                self._unmap_id = 0
+            self._selected_row = None
+            self.set_response_sensitive(Gtk.ResponseType.OK, False)
+
     def _on_key_press(self, widget, event):
       keyname = Gdk.keyval_name(event.keyval)
       if keyname == 'Escape':


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