[meld] ui.listselectors: Move template setup into a class decorator



commit c435c07c02f4d06083b7400cd2ef9f27c25aefd6
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Sun Nov 26 07:32:28 2017 +1000

    ui.listselectors: Move template setup into a class decorator

 meld/ui/bufferselectors.py |   24 ++++++++----------------
 meld/ui/listselector.py    |   16 ++++++++++++++++
 2 files changed, 24 insertions(+), 16 deletions(-)
---
diff --git a/meld/ui/bufferselectors.py b/meld/ui/bufferselectors.py
index 57ea4ab..8bd5a22 100644
--- a/meld/ui/bufferselectors.py
+++ b/meld/ui/bufferselectors.py
@@ -1,13 +1,13 @@
 
-from gi.repository import GLib
 from gi.repository import GObject
 from gi.repository import Gtk
 from gi.repository import GtkSource
 
-from meld.conf import _, ui_file
-from meld.ui.listselector import FilteredListSelector
+from meld.conf import _
+from meld.ui.listselector import FilteredListSelector, with_template_file
 
 
+@with_template_file('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.
@@ -33,11 +33,6 @@ class EncodingSelector(FilteredListSelector, Gtk.Grid):
             name=enc.get_name(), charset=enc.get_charset())
 
 
-template = open(ui_file('encoding-selector.ui'), 'rb').read()
-template_bytes = GLib.Bytes.new(template)
-EncodingSelector.set_template(template_bytes)
-
-
 # SourceLangSelector was intially based on gedit's
 # GeditHighlightModeSelector
 # Copyright (C) 2013 - Ignacio Casal Quinteiro
@@ -45,6 +40,11 @@ EncodingSelector.set_template(template_bytes)
 # 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')
 class SourceLangSelector(FilteredListSelector, Gtk.Grid):
     # The subclassing here is weird; the Selector must directly
     # subclass Gtk.Grid, or the template building explodes.
@@ -72,11 +72,3 @@ class SourceLangSelector(FilteredListSelector, Gtk.Grid):
         if not lang:
             return _("Plain Text")
         return lang.get_name()
-
-
-# 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.
-template = open(ui_file('language-selector.ui'), 'rb').read()
-template_bytes = GLib.Bytes.new(template)
-SourceLangSelector.set_template(template_bytes)
diff --git a/meld/ui/listselector.py b/meld/ui/listselector.py
index 14577d5..fa4d3d3 100644
--- a/meld/ui/listselector.py
+++ b/meld/ui/listselector.py
@@ -1,6 +1,22 @@
 
+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(object):
 


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