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



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

    search: Add search-mode-enabled GObject property

 gnomemusic/search.py            | 15 ++++++++++++++-
 gnomemusic/views/searchview.py  |  2 +-
 gnomemusic/widgets/headerbar.py | 11 ++++++++---
 gnomemusic/widgets/searchbar.py | 37 ++++++++++++++++++++-----------------
 gnomemusic/window.py            | 31 ++++++++++++++++---------------
 5 files changed, 59 insertions(+), 37 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/headerbar.py b/gnomemusic/widgets/headerbar.py
index 3b362982..60d4df4c 100644
--- a/gnomemusic/widgets/headerbar.py
+++ b/gnomemusic/widgets/headerbar.py
@@ -98,7 +98,6 @@ class HeaderBar(Gtk.HeaderBar):
     _back_button = Gtk.Template.Child()
     _menu_button = Gtk.Template.Child()
 
-    search_mode_enabled = GObject.Property(type=bool, default=False)
     selected_items_count = GObject.Property(type=int, default=0, minimum=0)
     selection_mode_allowed = GObject.Property(type=bool, default=True)
     stack = GObject.Property(type=Gtk.Stack)
@@ -107,7 +106,11 @@ class HeaderBar(Gtk.HeaderBar):
         return "<HeaderBar>"
 
     @log
-    def __init__(self):
+    def __init__(self, search):
+        """Initialize the headerbar
+
+        :param Search search: Search object
+        """
         super().__init__()
 
         self._selection_mode = False
@@ -139,7 +142,9 @@ class HeaderBar(Gtk.HeaderBar):
         self.bind_property(
             "selected-items-count", self._selection_menu,
             "selected-items-count")
-        self.bind_property(
+
+        self._search = search
+        self._search.bind_property(
             "search-mode-enabled", self._search_button, "active",
             GObject.BindingFlags.BIDIRECTIONAL
             | GObject.BindingFlags.SYNC_CREATE)
diff --git a/gnomemusic/widgets/searchbar.py b/gnomemusic/widgets/searchbar.py
index 87b2490b..17ce4486 100644
--- a/gnomemusic/widgets/searchbar.py
+++ b/gnomemusic/widgets/searchbar.py
@@ -337,14 +337,26 @@ class Searchbar(Gtk.SearchBar):
         return '<Searchbar>'
 
     @log
-    def __init__(self):
-        """Initialize the Searchbar"""
+    def __init__(self, search):
+        """Initialize the Searchbar
+
+        :param Search search: Search object
+        """
         super().__init__()
 
         self._timeout = None
 
         self._dropdown = DropDown()
         self._dropdown.initialize_filters(self)
+
+        self._search = search
+        self._search.bind_property(
+            "search-mode-enabled", self, "search-mode-enabled",
+            GObject.BindingFlags.BIDIRECTIONAL
+            | GObject.BindingFlags.SYNC_CREATE)
+        self.connect(
+            "notify::search-mode-enabled", self._search_mode_changed)
+
         self.connect('notify::search-state', self._search_state_changed)
 
     @Gtk.Template.Callback()
@@ -390,18 +402,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 +414,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 +435,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 5f0cbd42..a941e0ad 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,9 @@ class Window(Gtk.ApplicationWindow):
     @log
     def _setup_view(self):
         self._box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
-        self._headerbar = HeaderBar()
-        self._searchbar = Searchbar()
+        self._search = Search()
+        self._headerbar = HeaderBar(self._search)
+        self._searchbar = Searchbar(self._search)
         self._player = Player(self)
         self._player_toolbar = PlayerToolbar(self._player, self)
         selection_toolbar = SelectionToolbar()
@@ -131,10 +133,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 +309,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 +381,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 +393,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 +410,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 +444,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 +452,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]