[gnome-music/wip/jfelder/search-selection-reset: 17/24] search: Add search-mode-enabled GObject property



commit 1d9e07e382487d03b948a7ab3ca5c4dfa23bd624
Author: Jean Felder <jfelder src gnome org>
Date:   Tue Feb 26 22:36:26 2019 +0100

    search: Add search-mode-enabled GObject property
    
    This should help simplify search-mode interactions between the main
    views and the searchbar.
    reveal and toggle method from Searchbar can be removed. The whole
    Searchbar visibility logic can be internally handled by listening to
    search-mode-enabled changes.

 gnomemusic/search.py            | 15 ++++++++++++++-
 gnomemusic/views/searchview.py  |  2 +-
 gnomemusic/widgets/searchbar.py | 24 +++++++++---------------
 gnomemusic/window.py            | 37 ++++++++++++++++++++++++-------------
 4 files changed, 48 insertions(+), 30 deletions(-)
---
diff --git a/gnomemusic/search.py b/gnomemusic/search.py
index 78000f76..2c77646a 100644
--- a/gnomemusic/search.py
+++ b/gnomemusic/search.py
@@ -24,8 +24,12 @@
 
 from enum import IntEnum
 
+from gi.repository import GObject
 
-class Search():
+from gnomemusic import log
+
+
+class Search(GObject.GObject):
 
     class State(IntEnum):
         """States the Search can have
@@ -37,3 +41,12 @@ class Search():
         NONE = 0
         RESULT = 1
         NO_RESULT = 2
+
+    search_mode_enabled = GObject.Property(type=bool, default=False)
+
+    def __repr__(self):
+        return "<Search>"
+
+    @log
+    def __init__(self):
+        super().__init__()
diff --git a/gnomemusic/views/searchview.py b/gnomemusic/views/searchview.py
index 010def73..a088a515 100644
--- a/gnomemusic/views/searchview.py
+++ b/gnomemusic/views/searchview.py
@@ -103,7 +103,7 @@ class SearchView(BaseView):
 
     @log
     def _back_button_clicked(self, widget, data=None):
-        self._searchbar.reveal(True, False)
+        self._searchbar.props.search_mode_enabled = True
 
         if self.get_visible_child() == self._artist_albums_widget:
             self._artist_albums_widget.destroy()
diff --git a/gnomemusic/widgets/searchbar.py b/gnomemusic/widgets/searchbar.py
index 87b2490b..b0930024 100644
--- a/gnomemusic/widgets/searchbar.py
+++ b/gnomemusic/widgets/searchbar.py
@@ -345,6 +345,9 @@ class Searchbar(Gtk.SearchBar):
 
         self._dropdown = DropDown()
         self._dropdown.initialize_filters(self)
+
+        self.connect(
+            "notify::search-mode-enabled", self._search_mode_changed)
         self.connect('notify::search-state', self._search_state_changed)
 
     @Gtk.Template.Callback()
@@ -390,18 +393,9 @@ class Searchbar(Gtk.SearchBar):
         return False
 
     @log
-    def reveal(self, show, clear=True):
-        """Hides or reveals the searchbar
-
-        :param bool show: Whether to show the searchbar
-        :param bool clear: Whether to clear the search entry
-        """
-        self.props.search_mode_enabled = show
-
-        if show:
+    def _search_mode_changed(self, klass, data):
+        if self.props.search_mode_enabled:
             self._search_entry.realize()
-            if clear:
-                self._search_entry.set_text('')
             self._search_entry.grab_focus()
         else:
             self._drop_down_button.set_active(False)
@@ -411,7 +405,7 @@ class Searchbar(Gtk.SearchBar):
         search_state = self.props.search_state
 
         if search_state == Search.State.NONE:
-            self.reveal(False)
+            self.props.search_mode_enabled = False
         elif search_state == Search.State.NO_RESULT:
             self._set_error_style(True)
             self.props.stack.props.visible_child_name = 'emptyview'
@@ -432,6 +426,6 @@ class Searchbar(Gtk.SearchBar):
             style_context.remove_class('error')
 
     @log
-    def toggle(self):
-        """Toggle the searchbar showing"""
-        self.reveal(not self.get_search_mode())
+    def clear(self):
+        """Clear the searchbar."""
+        self._search_entry.props.text = ""
diff --git a/gnomemusic/window.py b/gnomemusic/window.py
index a0087078..174c035a 100644
--- a/gnomemusic/window.py
+++ b/gnomemusic/window.py
@@ -36,6 +36,7 @@ from gnomemusic import log
 from gnomemusic.mediakeys import MediaKeys
 from gnomemusic.player import Player, RepeatMode
 from gnomemusic.query import Query
+from gnomemusic.search import Search
 from gnomemusic.utils import View
 from gnomemusic.views.albumsview import AlbumsView
 from gnomemusic.views.artistsview import ArtistsView
@@ -118,8 +119,19 @@ class Window(Gtk.ApplicationWindow):
     @log
     def _setup_view(self):
         self._box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
+
+        self._search = Search()
         self._headerbar = HeaderBar()
         self._searchbar = Searchbar()
+        self._search.bind_property(
+            "search-mode-enabled", self._headerbar, "search-mode-enabled",
+            GObject.BindingFlags.BIDIRECTIONAL
+            | GObject.BindingFlags.SYNC_CREATE)
+        self._search.bind_property(
+            "search-mode-enabled", self._searchbar, "search-mode-enabled",
+            GObject.BindingFlags.BIDIRECTIONAL
+            | GObject.BindingFlags.SYNC_CREATE)
+
         self._player = Player(self)
         self._player_toolbar = PlayerToolbar(self._player, self)
         selection_toolbar = SelectionToolbar()
@@ -131,10 +143,6 @@ class Window(Gtk.ApplicationWindow):
             visible=True,
             can_focus=False)
 
-        self._headerbar.bind_property(
-            "search-mode-enabled", self._searchbar, "search-mode-enabled",
-            GObject.BindingFlags.BIDIRECTIONAL |
-            GObject.BindingFlags.SYNC_CREATE)
         self._searchbar.props.stack = self._stack
         self._headerbar.connect(
             'back-button-clicked', self._switch_back_from_childview)
@@ -311,7 +319,8 @@ class Window(Gtk.ApplicationWindow):
             if (keyval == Gdk.KEY_f
                     and not self.views[View.PLAYLIST].rename_active
                     and self._headerbar.props.state != HeaderBar.State.SEARCH):
-                self._searchbar.toggle()
+                search_mode = self._search.props.search_mode_enabled
+                self._search.props.search_mode_enabled = not search_mode
             # Play / Pause on Ctrl + SPACE
             if keyval == Gdk.KEY_space:
                 self._player.play_pause()
@@ -382,8 +391,8 @@ class Window(Gtk.ApplicationWindow):
             if keyval == Gdk.KEY_Escape:
                 if self.props.selection_mode:
                     self.props.selection_mode = False
-                else:
-                    self._searchbar.reveal(False)
+                elif self._search.props.search_mode_enabled:
+                    self._search.props.search_mode_enabled = False
 
         # Open the search bar when typing printable chars.
         key_unic = Gdk.keyval_to_unicode(keyval)
@@ -394,7 +403,7 @@ class Window(Gtk.ApplicationWindow):
                      or modifiers == 0)
                 and not self.views[View.PLAYLIST].rename_active
                 and self._headerbar.props.state != HeaderBar.State.SEARCH):
-            self._searchbar.reveal(True)
+            self._search.props.search_mode_enabled = True
 
     @log
     def _on_back_button_pressed(self, gesture, n_press, x, y):
@@ -411,9 +420,11 @@ class Window(Gtk.ApplicationWindow):
         self.prev_view = self.curr_view
         self.curr_view = stack.get_visible_child()
 
-        if (self.curr_view != self.views[View.SEARCH]
-                and self.curr_view != self.views[View.EMPTY]):
-            self._searchbar.reveal(False)
+        # Disable search mode when switching view
+        search_views = [self.views[View.EMPTY], self.views[View.SEARCH]]
+        if (self.curr_view not in search_views
+                and self._search.props.search_mode_enabled is True):
+            self._search.props.search_mode_enabled = False
 
         # Disable the selection button for the EmptySearch and Playlist
         # view
@@ -443,8 +454,6 @@ class Window(Gtk.ApplicationWindow):
 
     @log
     def _on_search_toggled(self, button, data=None):
-        self._searchbar.reveal(
-            button.get_active(), self.curr_view != self.views[View.SEARCH])
         if (not button.get_active()
                 and (self.curr_view == self.views[View.SEARCH]
                     or self.curr_view == self.views[View.EMPTY])):
@@ -453,10 +462,12 @@ class Window(Gtk.ApplicationWindow):
                 # We should get back to the view before the search
                 self._stack.set_visible_child(
                     self.views[View.SEARCH].previous_view)
+                self._searchbar.clear()
             elif (self.views[View.SEARCH].previous_view == self.views[View.ALBUM]
                     and child != self.curr_view._album_widget
                     and child != self.curr_view._artist_albums_widget):
                 self._stack.set_visible_child(self.views[View.ALBUM])
+                self._searchbar.clear()
 
             if self.props.selection_mode:
                 self.props.selection_mode = False


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