[gnome-music/wip/mschraal/move-searchbar-handling: 1/3] headerbar: Move Searchbar to Window



commit b056349ac1623280807bb82c9cb2e815624963d9
Author: Marinus Schraal <mschraal gnome org>
Date:   Tue Sep 4 15:00:57 2018 +0200

    headerbar: Move Searchbar to Window
    
    Makes more sense to control this from the main window, including the back
    button logic.

 gnomemusic/views/searchview.py  | 11 ++++++---
 gnomemusic/widgets/headerbar.py | 38 ++++++++----------------------
 gnomemusic/widgets/searchbar.py | 16 +++++--------
 gnomemusic/window.py            | 51 +++++++++++++++++++++++++++++++----------
 4 files changed, 63 insertions(+), 53 deletions(-)
---
diff --git a/gnomemusic/views/searchview.py b/gnomemusic/views/searchview.py
index 380c1300..8dfcd4a1 100644
--- a/gnomemusic/views/searchview.py
+++ b/gnomemusic/views/searchview.py
@@ -23,6 +23,8 @@
 # delete this exception statement from your version.
 
 from gettext import gettext as _
+import gi
+gi.require_version('Gd', '1.0')
 from gi.repository import Gd, Gdk, GdkPixbuf, GObject, Grl, Gtk, Pango
 
 from gnomemusic.albumartcache import Art
@@ -54,6 +56,9 @@ class SearchView(BaseView):
     def __init__(self, window, player):
         super().__init__('search', None, window)
 
+        # FIXME: Searchbar handling does not belong here.
+        self._searchbar = window._searchbar
+
         self._add_list_renderers()
         self.player = player
         self._head_iters = [None, None, None, None]
@@ -102,7 +107,7 @@ class SearchView(BaseView):
 
     @log
     def _back_button_clicked(self, widget, data=None):
-        self._headerbar.searchbar.reveal(True, False)
+        self._searchbar.reveal(True, False)
 
         if self.get_visible_child() == self._artist_albums_widget:
             self._artist_albums_widget.destroy()
@@ -140,7 +145,7 @@ class SearchView(BaseView):
             self._headerbar.props.title = title
             self._headerbar.props.subtitle = artist
             self.set_visible_child(self._album_widget)
-            self._headerbar.searchbar.reveal(False)
+            self._searchbar.reveal(False)
         elif self.model[_iter][12] == 'artist':
             artist = self.model[_iter][2]
             albums = self._artists[artist.casefold()]['albums']
@@ -159,7 +164,7 @@ class SearchView(BaseView):
             self._headerbar.props.state = HeaderBar.State.SEARCH
             self._headerbar.props.title = artist
             self.set_visible_child(self._artist_albums_widget)
-            self._headerbar.searchbar.reveal(False)
+            self._searchbar.reveal(False)
         elif self.model[_iter][12] == 'song':
             if self.model[_iter][11] != ValidationStatus.FAILED:
                 c_iter = self._songs_model.convert_child_iter_to_iter(_iter)[1]
diff --git a/gnomemusic/widgets/headerbar.py b/gnomemusic/widgets/headerbar.py
index 1398acde..5772fb3b 100644
--- a/gnomemusic/widgets/headerbar.py
+++ b/gnomemusic/widgets/headerbar.py
@@ -28,8 +28,6 @@ from gettext import gettext as _, ngettext
 from gi.repository import GObject, Gtk
 
 from gnomemusic import log
-from gnomemusic.widgets.searchbar import Searchbar
-from gnomemusic.utils import View
 
 
 @Gtk.Template(resource_path="/org/gnome/Music/SelectionBarMenuButton.ui")
@@ -89,11 +87,16 @@ class HeaderBar(Gtk.HeaderBar):
 
     __gtype_name__ = "HeaderBar"
 
+    __gsignals__ = {
+        'back-button-clicked': (GObject.SignalFlags.RUN_FIRST, None, ()),
+    }
+
     _search_button = Gtk.Template.Child()
     _select_button = Gtk.Template.Child()
     _cancel_button = Gtk.Template.Child()
     _back_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)
@@ -111,14 +114,8 @@ class HeaderBar(Gtk.HeaderBar):
             can_focus=False, halign="center")
         self._stack_switcher.show()
 
-        self.searchbar = Searchbar(self._stack_switcher)
-
         self._selection_menu = SelectionBarMenuButton()
 
-        self._search_button.bind_property(
-            "active", self.searchbar, "search-mode-enabled",
-            GObject.BindingFlags.BIDIRECTIONAL |
-            GObject.BindingFlags.SYNC_CREATE)
         self.bind_property(
             "selection-mode", self, "show-close-button",
             GObject.BindingFlags.INVERT_BOOLEAN |
@@ -138,6 +135,10 @@ class HeaderBar(Gtk.HeaderBar):
         self.bind_property(
             "selected-items-count", self._selection_menu,
             "selected-items-count")
+        self.bind_property(
+            "search-mode-enabled", self._search_button, "active",
+            GObject.BindingFlags.BIDIRECTIONAL |
+            GObject.BindingFlags.SYNC_CREATE)
 
         self.connect(
             "notify::selection-mode-allowed",
@@ -203,26 +204,7 @@ class HeaderBar(Gtk.HeaderBar):
     @Gtk.Template.Callback()
     @log
     def _on_back_button_clicked(self, widget=None):
-        if self.props.selection_mode:
-            return
-
-        window = self.get_toplevel()
-
-        visible_child = window.curr_view.props.visible_child
-
-        # FIXME: Stack switch logic should not be here.
-        view = self._stack_switcher.props.stack.props.visible_child
-        view._back_button_clicked(view)
-
-        current_view = window.curr_view
-        if not ((current_view == window.views[View.SEARCH]
-                 or current_view == window.views[View.EMPTY])
-                and visible_child != current_view._grid):
-            self.props.state = HeaderBar.State.MAIN
-        else:
-            self._search_button.props.visible = True
-
-        self.searchbar.reveal(False)
+        self.emit('back-button-clicked')
 
     @Gtk.Template.Callback()
     @log
diff --git a/gnomemusic/widgets/searchbar.py b/gnomemusic/widgets/searchbar.py
index 1cd661df..95fb7758 100644
--- a/gnomemusic/widgets/searchbar.py
+++ b/gnomemusic/widgets/searchbar.py
@@ -324,20 +324,17 @@ class Searchbar(Gtk.SearchBar):
     _search_entry = Gtk.Template.Child()
     _drop_down_button = Gtk.Template.Child()
 
+    stack = GObject.Property(type=Gtk.Stack)
+
     def __repr__(self):
         return '<Searchbar>'
 
     @log
-    def __init__(self, stack_switcher):
-        """Initialize the Searchbar
-
-        :param Gtk.Stack stack_switcher: The stack switcher containing
-          the search view.
-        """
+    def __init__(self):
+        """Initialize the Searchbar"""
         super().__init__()
 
         self._timeout = None
-        self._stack_switcher = stack_switcher
 
         self._dropdown = DropDown()
         self._dropdown.initialize_filters(self)
@@ -371,10 +368,9 @@ class Searchbar(Gtk.SearchBar):
         else:
             fields_filter = 'search_all'
 
-        stack = self._stack_switcher.props.stack
         if search_term != "":
-            stack.set_visible_child_name('search')
-            view = stack.get_visible_child()
+            self.props.stack.set_visible_child_name('search')
+            view = self.props.stack.get_visible_child()
             view.set_search_text(search_term, fields_filter)
 
         self._drop_down_button.set_active(False)
diff --git a/gnomemusic/window.py b/gnomemusic/window.py
index ccfc4c0b..907e76a8 100644
--- a/gnomemusic/window.py
+++ b/gnomemusic/window.py
@@ -47,6 +47,7 @@ from gnomemusic.widgets.headerbar import HeaderBar
 from gnomemusic.widgets.notificationspopup import NotificationsPopup
 from gnomemusic.widgets.playertoolbar import PlayerToolbar
 from gnomemusic.widgets.playlistdialog import PlaylistDialog
+from gnomemusic.widgets.searchbar import Searchbar
 from gnomemusic.widgets.selectiontoolbar import SelectionToolbar
 from gnomemusic.playlists import Playlists
 from gnomemusic.grilo import grilo
@@ -153,6 +154,7 @@ class Window(Gtk.ApplicationWindow):
     def _setup_view(self):
         self._box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
         self._headerbar = HeaderBar()
+        self._searchbar = Searchbar()
         self.player = Player(self)
         self.player_toolbar = PlayerToolbar(self.player, self)
         self.selection_toolbar = SelectionToolbar()
@@ -164,6 +166,14 @@ 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)
+
         self.connect('notify::selection-mode', self._on_selection_mode_changed)
         self.bind_property(
             'selected-items-count', self._headerbar, 'selected-items-count')
@@ -191,9 +201,9 @@ class Window(Gtk.ApplicationWindow):
         self._overlay = Gtk.Overlay()
         self._overlay.add(self._stack)
         # FIXME: Need to find a proper way to do this.
-        self._overlay.add_overlay(self._headerbar.searchbar._dropdown)
+        self._overlay.add_overlay(self._searchbar._dropdown)
         self.set_titlebar(self._headerbar)
-        self._box.pack_start(self._headerbar.searchbar, False, False, 0)
+        self._box.pack_start(self._searchbar, False, False, 0)
         self._box.pack_start(self._overlay, True, True, 0)
         self._box.pack_start(self.player_toolbar, False, False, 0)
         self._box.pack_start(self.selection_toolbar, False, False, 0)
@@ -261,7 +271,7 @@ class Window(Gtk.ApplicationWindow):
         self.views[View.EMPTY].props.state = EmptyView.State.SEARCH
         self._headerbar.props.state = HeaderBar.State.MAIN
         self._headerbar.props.stack = self._stack
-        self._headerbar.searchbar.show()
+        self._searchbar.show()
 
         self.views[View.ALBUM] = AlbumsView(self, self.player)
         self.views[View.ARTIST] = ArtistsView(self, self.player)
@@ -326,7 +336,7 @@ 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._headerbar.searchbar.toggle()
+                self._searchbar.toggle()
             # Play / Pause on Ctrl + SPACE
             if keyval == Gdk.KEY_space:
                 self.player.play_pause()
@@ -354,9 +364,9 @@ class Window(Gtk.ApplicationWindow):
                 self._select_none()
         # Alt+<KEY>
         elif modifiers == mod1_mask:
-            # Go back from Album view on Alt + Left
+            # Go back from child view on Alt + Left
             if keyval == Gdk.KEY_Left:
-                self._headerbar._on_back_button_clicked()
+                self._switch_back_from_childview()
             # Headerbar switching
             if keyval in [Gdk.KEY_1, Gdk.KEY_KP_1]:
                 self._toggle_view(View.ALBUM)
@@ -390,18 +400,18 @@ class Window(Gtk.ApplicationWindow):
                 if self.props.selection_mode:
                     self.props.selection_mode = False
                 else:
-                    self._headerbar.searchbar.reveal(False)
+                    self._searchbar.reveal(False)
 
         # Open the search bar when typing printable chars.
         key_unic = Gdk.keyval_to_unicode(keyval)
-        if ((not self._headerbar.searchbar.get_search_mode()
+        if ((not self._searchbar.get_search_mode()
                 and not keyval == Gdk.KEY_space)
                 and GLib.unichar_isprint(chr(key_unic))
                 and (modifiers == shift_mask
                      or modifiers == 0)
                 and not self.views[View.PLAYLIST].rename_active
                 and self._headerbar.props.state != HeaderBar.State.SEARCH):
-            self._headerbar.searchbar.reveal(True)
+            self._searchbar.reveal(True)
 
     @log
     def do_button_release_event(self, event):
@@ -412,7 +422,7 @@ class Window(Gtk.ApplicationWindow):
         __, code = event.get_button()
         # Mouse button 8 is the navigation button
         if code == 8:
-            self._headerbar._on_back_button_clicked()
+            self._switch_back_from_childview()
 
     @log
     def _notify_mode_disconnect(self, data=None):
@@ -433,7 +443,7 @@ class Window(Gtk.ApplicationWindow):
 
         if (self.curr_view != self.views[View.SEARCH]
                 and self.curr_view != self.views[View.EMPTY]):
-            self._headerbar.searchbar.reveal(False)
+            self._searchbar.reveal(False)
 
         # Disable the selection button for the EmptySearch and Playlist
         # view
@@ -463,7 +473,7 @@ class Window(Gtk.ApplicationWindow):
 
     @log
     def _on_search_toggled(self, button, data=None):
-        self._headerbar.searchbar.reveal(
+        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]
@@ -481,6 +491,23 @@ class Window(Gtk.ApplicationWindow):
             if self.props.selection_mode:
                 self.props.selection_mode = False
 
+    @log
+    def _switch_back_from_childview(self, klass=None):
+        if self.props.selection_mode:
+            return
+
+        visible_child = self.curr_view.props.visible_child
+
+        view = self._stack.props.visible_child
+        view._back_button_clicked(view)
+
+        if not ((self.curr_view == self.views[View.SEARCH]
+                 or self.curr_view == self.views[View.EMPTY])
+                and visible_child != self.curr_view._grid):
+            self._headerbar.props.state = HeaderBar.State.MAIN
+
+        self._searchbar.reveal(False)
+
     @log
     def _on_selection_mode_changed(self, widget, data=None):
         if self.props.selection_mode:


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