Skip to content

Commit c435c07

Browse files
committed
ui.listselectors: Move template setup into a class decorator
1 parent 523f1d5 commit c435c07

2 files changed

Lines changed: 24 additions & 16 deletions

File tree

meld/ui/bufferselectors.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

2-
from gi.repository import GLib
32
from gi.repository import GObject
43
from gi.repository import Gtk
54
from gi.repository import GtkSource
65

7-
from meld.conf import _, ui_file
8-
from meld.ui.listselector import FilteredListSelector
6+
from meld.conf import _
7+
from meld.ui.listselector import FilteredListSelector, with_template_file
98

109

10+
@with_template_file('encoding-selector.ui')
1111
class EncodingSelector(FilteredListSelector, Gtk.Grid):
1212
# The subclassing here is weird; the Selector must directly
1313
# subclass Gtk.Grid, or the template building explodes.
@@ -33,18 +33,18 @@ def get_value_label(self, enc):
3333
name=enc.get_name(), charset=enc.get_charset())
3434

3535

36-
template = open(ui_file('encoding-selector.ui'), 'rb').read()
37-
template_bytes = GLib.Bytes.new(template)
38-
EncodingSelector.set_template(template_bytes)
39-
40-
4136
# SourceLangSelector was intially based on gedit's
4237
# GeditHighlightModeSelector
4338
# Copyright (C) 2013 - Ignacio Casal Quinteiro
4439
# Python translation and adaptations
4540
# Copyright (C) 2015, 2017 Kai Willadsen <kai.willadsen@gmail.com>
4641

4742

43+
# TODO: When there's proper pygobject support for widget templates,
44+
# make both selectors here use a generic UI file. We can't do this
45+
# currently due to subclassing issues.
46+
47+
@with_template_file('language-selector.ui')
4848
class SourceLangSelector(FilteredListSelector, Gtk.Grid):
4949
# The subclassing here is weird; the Selector must directly
5050
# subclass Gtk.Grid, or the template building explodes.
@@ -72,11 +72,3 @@ def get_value_label(self, lang):
7272
if not lang:
7373
return _("Plain Text")
7474
return lang.get_name()
75-
76-
77-
# TODO: When there's proper pygobject support for widget templates,
78-
# make both selectors here use a generic UI file. We can't do this
79-
# currently due to subclassing issues.
80-
template = open(ui_file('language-selector.ui'), 'rb').read()
81-
template_bytes = GLib.Bytes.new(template)
82-
SourceLangSelector.set_template(template_bytes)

meld/ui/listselector.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11

2+
from gi.repository import GLib
23
from gi.repository import Gtk
34

5+
from meld.conf import ui_file
6+
7+
8+
def with_template_file(template_file):
9+
"""Class decorator for setting a widget template"""
10+
11+
def add_template(cls):
12+
template_path = ui_file(template_file)
13+
template = open(template_path, 'rb').read()
14+
template_bytes = GLib.Bytes.new(template)
15+
cls.set_template(template_bytes)
16+
return cls
17+
18+
return add_template
19+
420

521
class TemplateHackMixin(object):
622

0 commit comments

Comments
 (0)