[meld/deprecation-cleanup: 45/48] ui: Migrate status bar selectors from template hack to real templates



commit 1029ce2c093f4eabf57cc526bd09e8611a54dc56
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Sun Dec 2 10:56:18 2018 +1000

    ui: Migrate status bar selectors from template hack to real templates

 meld/resources/meld.gresource.xml                |  2 +
 {data => meld/resources}/ui/encoding-selector.ui |  0
 {data => meld/resources}/ui/language-selector.ui |  0
 meld/ui/bufferselectors.py                       | 29 ++++++++++-----
 meld/ui/listselector.py                          | 47 +-----------------------
 5 files changed, 24 insertions(+), 54 deletions(-)
---
diff --git a/meld/resources/meld.gresource.xml b/meld/resources/meld.gresource.xml
index fe4739c4..2b140cc5 100644
--- a/meld/resources/meld.gresource.xml
+++ b/meld/resources/meld.gresource.xml
@@ -10,6 +10,8 @@
     <file>ui/about-dialog.ui</file>
     <file>ui/appwindow.ui</file>
     <file>ui/commit-dialog.ui</file>
+    <file>ui/encoding-selector.ui</file>
+    <file>ui/language-selector.ui</file>
     <file>ui/patch-dialog.ui</file>
     <file>ui/push-dialog.ui</file>
     <file>ui/revert-dialog.ui</file>
diff --git a/data/ui/encoding-selector.ui b/meld/resources/ui/encoding-selector.ui
similarity index 100%
rename from data/ui/encoding-selector.ui
rename to meld/resources/ui/encoding-selector.ui
diff --git a/data/ui/language-selector.ui b/meld/resources/ui/language-selector.ui
similarity index 100%
rename from data/ui/language-selector.ui
rename to meld/resources/ui/language-selector.ui
diff --git a/meld/ui/bufferselectors.py b/meld/ui/bufferselectors.py
index 8bd5a229..2e9b8deb 100644
--- a/meld/ui/bufferselectors.py
+++ b/meld/ui/bufferselectors.py
@@ -4,10 +4,21 @@ from gi.repository import Gtk
 from gi.repository import GtkSource
 
 from meld.conf import _
-from meld.ui.listselector import FilteredListSelector, with_template_file
+from meld.ui._gtktemplate import Template
+from meld.ui.listselector import FilteredListSelector
 
+# TODO: Current pygobject support for templates excludes subclassing of
+# templated classes, which is why we have two near-identical UI files
+# here, and why we can't subclass Gtk.Grid directly in
+# FilteredListSelector.
 
-@with_template_file('encoding-selector.ui')
+# The subclassing here is weird; the Selector must directly subclass
+# Gtk.Grid; we can't do this on the FilteredListSelector. Likewise, the
+# Template.Child attributes must be per-class, because of how they're
+# registered by the templating engine.
+
+
+@Template(resource_path='/org/gnome/meld/ui/encoding-selector.ui')
 class EncodingSelector(FilteredListSelector, Gtk.Grid):
     # The subclassing here is weird; the Selector must directly
     # subclass Gtk.Grid, or the template building explodes.
@@ -24,6 +35,9 @@ class EncodingSelector(FilteredListSelector, Gtk.Grid):
     value_accessor = 'get_charset'
     change_signal_name = 'encoding-selected'
 
+    entry = Template.Child('entry')
+    treeview = Template.Child('treeview')
+
     def populate_model(self):
         for enc in GtkSource.Encoding.get_all():
             self.liststore.append((self.get_value_label(enc), enc))
@@ -40,14 +54,8 @@ class EncodingSelector(FilteredListSelector, Gtk.Grid):
 # Copyright (C) 2015, 2017 Kai Willadsen <kai willadsen gmail com>
 
 
-# TODO: When there's proper pygobject support for widget templates,
-# make both selectors here use a generic UI file. We can't do this
-# currently due to subclassing issues.
-
-@with_template_file('language-selector.ui')
+@Template(resource_path='/org/gnome/meld/ui/language-selector.ui')
 class SourceLangSelector(FilteredListSelector, Gtk.Grid):
-    # The subclassing here is weird; the Selector must directly
-    # subclass Gtk.Grid, or the template building explodes.
 
     __gtype_name__ = "SourceLangSelector"
 
@@ -61,6 +69,9 @@ class SourceLangSelector(FilteredListSelector, Gtk.Grid):
     value_accessor = 'get_id'
     change_signal_name = 'language-selected'
 
+    entry = Template.Child('entry')
+    treeview = Template.Child('treeview')
+
     def populate_model(self):
         self.liststore.append((_("Plain Text"), None))
         manager = GtkSource.LanguageManager.get_default()
diff --git a/meld/ui/listselector.py b/meld/ui/listselector.py
index af74b4dd..9c10af56 100644
--- a/meld/ui/listselector.py
+++ b/meld/ui/listselector.py
@@ -1,46 +1,5 @@
 
-from gi.repository import GLib
-from gi.repository import Gtk
-
-from meld.conf import ui_file
-
-
-def with_template_file(template_file):
-    """Class decorator for setting a widget template"""
-
-    def add_template(cls):
-        template_path = ui_file(template_file)
-        template = open(template_path, 'rb').read()
-        template_bytes = GLib.Bytes.new(template)
-        cls.set_template(template_bytes)
-        return cls
-
-    return add_template
-
-
-class TemplateHackMixin:
-
-    def get_template_child(self, widget_type, name):
-        # Taken from an in-progress patch on bgo#701843
-
-        def get_template_child(widget, widget_type, name):
-            # Explicitly use gtk_buildable_get_name() because it is masked by
-            # gtk_widget_get_name() in GI.
-            if isinstance(widget, widget_type) and \
-                    isinstance(widget, Gtk.Buildable) and \
-                    Gtk.Buildable.get_name(widget) == name:
-                return widget
-
-            if isinstance(widget, Gtk.Container):
-                for child in widget.get_children():
-                    result = get_template_child(child, widget_type, name)
-                    if result is not None:
-                        return result
-
-        return get_template_child(self, widget_type, name)
-
-
-class FilteredListSelector(TemplateHackMixin):
+class FilteredListSelector:
 
     # FilteredListSelector was intially based on gedit's
     # GeditHighlightModeSelector
@@ -53,11 +12,9 @@ class FilteredListSelector(TemplateHackMixin):
     NAME_COLUMN, VALUE_COLUMN = 0, 1
 
     def __init__(self):
-        Gtk.Grid.__init__(self)
+        super().__init__()
         self.init_template()
 
-        self.entry = self.get_template_child(Gtk.SearchEntry, 'entry')
-        self.treeview = self.get_template_child(Gtk.TreeView, 'treeview')
         self.treeview_selection = self.treeview.get_selection()
         # FIXME: Should be able to access as a template child, but can't.
         self.listfilter = self.treeview.get_model()


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