[gnome-music/wip/jfelder/search-selection-reset: 2/7] search: Add state GObject Property



commit e9e5a5517ff514ba03326630f203b36504785924
Author: Jean Felder <jfelder src gnome org>
Date:   Tue Feb 26 23:29:03 2019 +0100

    search: Add state GObject Property

 gnomemusic/search.py            | 11 +++++++++++
 gnomemusic/views/searchview.py  | 20 +++++++++++++++-----
 gnomemusic/widgets/searchbar.py | 16 ++++++----------
 gnomemusic/window.py            |  5 +----
 4 files changed, 33 insertions(+), 19 deletions(-)
---
diff --git a/gnomemusic/search.py b/gnomemusic/search.py
index 2c77646a..b93e8108 100644
--- a/gnomemusic/search.py
+++ b/gnomemusic/search.py
@@ -50,3 +50,14 @@ class Search(GObject.GObject):
     @log
     def __init__(self):
         super().__init__()
+        self._state = Search.State.NONE
+
+    @GObject.Property(type=int, default=State.NONE)
+    def state(self):
+        return self._state
+
+    @state.setter
+    def state(self, state):
+        self._state = state
+        none_state = (self._state == Search.State.NONE)
+        self.props.search_mode_enabled = not none_state
diff --git a/gnomemusic/views/searchview.py b/gnomemusic/views/searchview.py
index a088a515..0bfc6384 100644
--- a/gnomemusic/views/searchview.py
+++ b/gnomemusic/views/searchview.py
@@ -52,11 +52,20 @@ class SearchView(BaseView):
         return '<SearchView>'
 
     @log
-    def __init__(self, window, player):
+    def __init__(self, window, player, search):
+        """Initialize SearchView.
+
+        :param Window window: Window object
+        :param Player player: Player object
+        :param Search search: Search object
+        """
         super().__init__('search', None, window)
 
-        # FIXME: Searchbar handling does not belong here.
-        self._searchbar = window._searchbar
+        self._search = search
+        self.bind_property(
+            "search-state", self._search, "state",
+            GObject.BindingFlags.SYNC_CREATE)
+        self._previous_search_state = Search.State.NONE
 
         self._add_list_renderers()
         self.player = player
@@ -103,8 +112,6 @@ class SearchView(BaseView):
 
     @log
     def _back_button_clicked(self, widget, data=None):
-        self._searchbar.props.search_mode_enabled = True
-
         if self.get_visible_child() == self._artist_albums_widget:
             self._artist_albums_widget.destroy()
             self._artist_albums_widget = None
@@ -113,6 +120,7 @@ class SearchView(BaseView):
                 self._window.views[View.ALBUM]._grid)
 
         self.set_visible_child(self._grid)
+        self.props.search_state = self._previous_search_state
         self._headerbar.props.state = HeaderBar.State.MAIN
 
     @log
@@ -141,6 +149,7 @@ class SearchView(BaseView):
             self._headerbar.props.title = title
             self._headerbar.props.subtitle = artist
             self.set_visible_child(self._album_widget)
+            self._previous_search_state = self.props.search_state
             self.props.search_state = Search.State.NONE
 
         elif self.model[_iter][12] == 'artist':
@@ -162,6 +171,7 @@ class SearchView(BaseView):
             self._headerbar.props.title = artist
             self._headerbar.props.subtitle = None
             self.set_visible_child(self._artist_albums_widget)
+            self._previous_search_state = self.props.search_state
             self.props.search_state = Search.State.NONE
         elif self.model[_iter][12] == 'song':
             if self.model[_iter][11] != ValidationStatus.FAILED:
diff --git a/gnomemusic/widgets/searchbar.py b/gnomemusic/widgets/searchbar.py
index 17ce4486..79c58f8a 100644
--- a/gnomemusic/widgets/searchbar.py
+++ b/gnomemusic/widgets/searchbar.py
@@ -330,7 +330,6 @@ class Searchbar(Gtk.SearchBar):
     _search_entry = Gtk.Template.Child()
     _drop_down_button = Gtk.Template.Child()
 
-    search_state = GObject.Property(type=int, default=Search.State.NONE)
     stack = GObject.Property(type=Gtk.Stack)
 
     def __repr__(self):
@@ -352,12 +351,11 @@ class Searchbar(Gtk.SearchBar):
         self._search = search
         self._search.bind_property(
             "search-mode-enabled", self, "search-mode-enabled",
-            GObject.BindingFlags.BIDIRECTIONAL
-            | GObject.BindingFlags.SYNC_CREATE)
+            GObject.BindingFlags.SYNC_CREATE)
         self.connect(
             "notify::search-mode-enabled", self._search_mode_changed)
-
-        self.connect('notify::search-state', self._search_state_changed)
+        self._search.connect(
+            'notify::state', self._search_state_changed)
 
     @Gtk.Template.Callback()
     @log
@@ -411,14 +409,12 @@ class Searchbar(Gtk.SearchBar):
 
     @log
     def _search_state_changed(self, klass, data):
-        search_state = self.props.search_state
+        search_state = self._search.props.state
 
-        if search_state == Search.State.NONE:
-            self.props.search_mode_enabled = False
-        elif search_state == Search.State.NO_RESULT:
+        if search_state == Search.State.NO_RESULT:
             self._set_error_style(True)
             self.props.stack.props.visible_child_name = 'emptyview'
-        else:
+        elif search_state == Search.State.RESULT:
             self._set_error_style(False)
             self.props.stack.props.visible_child_name = 'search'
 
diff --git a/gnomemusic/window.py b/gnomemusic/window.py
index a941e0ad..2060ec1b 100644
--- a/gnomemusic/window.py
+++ b/gnomemusic/window.py
@@ -247,7 +247,7 @@ class Window(Gtk.ApplicationWindow):
         self.views[View.ARTIST] = ArtistsView(self, self._player)
         self.views[View.SONG] = SongsView(self, self._player)
         self.views[View.PLAYLIST] = PlaylistView(self, self._player)
-        self.views[View.SEARCH] = SearchView(self, self._player)
+        self.views[View.SEARCH] = SearchView(self, self._player, self._search)
 
         selectable_views = [View.ALBUM, View.ARTIST, View.SONG, View.SEARCH]
         for view in selectable_views:
@@ -266,9 +266,6 @@ class Window(Gtk.ApplicationWindow):
 
         self._stack.set_visible_child(self.views[View.ALBUM])
 
-        self.views[View.SEARCH].bind_property(
-            'search-state', self._searchbar, 'search-state')
-
     @log
     def _select_all(self, action=None, param=None):
         if not self.props.selection_mode:


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