[gnome-music/wip/mschraal/minor-searchbar-cleanup: 6/6] searchbar: Use a template for Searchbar



commit 428aba201ce33ee4740d258e4c911ba27f6a2a20
Author: Marinus Schraal <mschraal gnome org>
Date:   Mon Aug 13 15:21:07 2018 +0200

    searchbar: Use a template for Searchbar

 data/Searchbar.ui                  | 40 ++++++++++++++++++++++++++++++++++++++
 data/org.gnome.Music.gresource.xml |  1 +
 gnomemusic/searchbar.py            | 34 ++++++++------------------------
 3 files changed, 49 insertions(+), 26 deletions(-)
---
diff --git a/data/Searchbar.ui b/data/Searchbar.ui
new file mode 100644
index 00000000..981f486f
--- /dev/null
+++ b/data/Searchbar.ui
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="Searchbar" parent="GtkSearchBar">
+    <child>
+      <object class="GtkBox">
+        <property name="halign">center</property>
+        <property name="orientation">horizontal</property>
+        <property name="visible">True</property>
+        <child>
+          <object class="GdTaggedEntry" id="_search_entry">
+            <property name="halign">center</property>
+            <property name="visible">True</property>
+            <property name="width-request">500</property>
+            <signal name="changed" handler="_search_entry_timeout"/>
+            <signal name="tag-button-clicked" handler="_tag_button_clicked"/>
+          </object>
+        </child>
+        <child>
+          <object class="GtkToggleButton" id="_drop_down_button">
+            <property name="visible">True</property>
+            <signal name="toggled" handler="_drop_down_button_toggled"/>
+            <style>
+              <class name="image-button"/>
+            </style>
+            <child>
+              <object class="GtkImage">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="icon-name">pan-down-symbolic</property>
+              </object>
+            </child>
+          </object>
+        </child>
+        <style>
+          <class name="linked"/>
+        </style>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/data/org.gnome.Music.gresource.xml b/data/org.gnome.Music.gresource.xml
index 7402e893..6054ed5d 100644
--- a/data/org.gnome.Music.gresource.xml
+++ b/data/org.gnome.Music.gresource.xml
@@ -13,6 +13,7 @@
     <file preprocess="xml-stripblanks">DiscBox.ui</file>
     <file preprocess="xml-stripblanks">EmptyView.ui</file>
     <file preprocess="xml-stripblanks">PlayerToolbar.ui</file>
+    <file preprocess="xml-stripblanks">Searchbar.ui</file>
     <file preprocess="xml-stripblanks">SelectionBarMenuButton.ui</file>
     <file preprocess="xml-stripblanks">SelectionToolbar.ui</file>
     <file preprocess="xml-stripblanks">SidebarRow.ui</file>
diff --git a/gnomemusic/searchbar.py b/gnomemusic/searchbar.py
index a49be8f7..8102002f 100644
--- a/gnomemusic/searchbar.py
+++ b/gnomemusic/searchbar.py
@@ -31,6 +31,7 @@ from gettext import gettext as _
 import gi
 gi.require_version('Gd', '1.0')
 from gi.repository import Gd, GLib, GObject, Gtk, Pango
+from gi.repository.Gd import TaggedEntry  # noqa: F401
 
 from gnomemusic import log
 from gnomemusic.grilo import grilo
@@ -282,12 +283,16 @@ class DropDown(Gtk.Revealer):
                 'search_all' if id_ != 'grl-tracker-source' else '')
 
 
+@Gtk.Template(resource_path="/org/gnome/Music/Searchbar.ui")
 class Searchbar(Gtk.SearchBar):
     """Widget containing the search entry
     """
 
     __gtype_name__ = 'Searchbar'
 
+    _search_entry = Gtk.Template.Child()
+    _drop_down_button = Gtk.Template.Child()
+
     def __repr__(self):
         return '<Searchbar>'
 
@@ -303,44 +308,21 @@ class Searchbar(Gtk.SearchBar):
         self._timeout = None
         self._stack_switcher = stack_switcher
 
-        self._search_box = Gtk.Box(
-            orientation=Gtk.Orientation.HORIZONTAL, halign=Gtk.Align.CENTER)
-        self._search_box.get_style_context().add_class('linked')
-
-        self._search_entry = Gd.TaggedEntry(
-            width_request=500, halign=Gtk.Align.CENTER)
-        self._search_entry.connect("changed", self._search_entry_timeout)
-        self._search_entry.show()
-        self._search_box.add(self._search_entry)
-
         self._dropdown = DropDown()
         self._dropdown.initialize_filters(self)
 
-        arrow = Gtk.Image.new_from_icon_name(
-            'pan-down-symbolic', Gtk.IconSize.BUTTON)
-        self._drop_down_button = Gtk.ToggleButton()
-        self._drop_down_button.add(arrow)
-        self._drop_down_button.get_style_context().add_class('image-button')
-        self._drop_down_button.connect(
-            "toggled", self._drop_down_button_toggled)
-        self._drop_down_button.show_all()
-        self._search_box.add(self._drop_down_button)
-
-        self._search_entry.connect(
-            "tag-button-clicked", self._tag_button_clicked)
-
-        self._search_box.show_all()
-        self.add(self._search_box)
-
+    @Gtk.Template.Callback()
     @log
     def _drop_down_button_toggled(self, *args):
         self._dropdown.set_reveal_child(self._drop_down_button.get_active())
 
+    @Gtk.Template.Callback()
     @log
     def _tag_button_clicked(self, entry, tag_):
         tag_.manager.active = tag_.manager.values[1][BaseModelColumns.ID]
         self._search_entry_changed(None)
 
+    @Gtk.Template.Callback()
     @log
     def _search_entry_timeout(self, widget):
         if self._timeout:


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