Skip to content

Commit 1029ce2

Browse files
committed
ui: Migrate status bar selectors from template hack to real templates
1 parent ca4c58f commit 1029ce2

5 files changed

Lines changed: 24 additions & 54 deletions

File tree

meld/resources/meld.gresource.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<file>ui/about-dialog.ui</file>
1111
<file>ui/appwindow.ui</file>
1212
<file>ui/commit-dialog.ui</file>
13+
<file>ui/encoding-selector.ui</file>
14+
<file>ui/language-selector.ui</file>
1315
<file>ui/patch-dialog.ui</file>
1416
<file>ui/push-dialog.ui</file>
1517
<file>ui/revert-dialog.ui</file>

meld/ui/bufferselectors.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@
44
from gi.repository import GtkSource
55

66
from meld.conf import _
7-
from meld.ui.listselector import FilteredListSelector, with_template_file
7+
from meld.ui._gtktemplate import Template
8+
from meld.ui.listselector import FilteredListSelector
89

10+
# TODO: Current pygobject support for templates excludes subclassing of
11+
# templated classes, which is why we have two near-identical UI files
12+
# here, and why we can't subclass Gtk.Grid directly in
13+
# FilteredListSelector.
914

10-
@with_template_file('encoding-selector.ui')
15+
# The subclassing here is weird; the Selector must directly subclass
16+
# Gtk.Grid; we can't do this on the FilteredListSelector. Likewise, the
17+
# Template.Child attributes must be per-class, because of how they're
18+
# registered by the templating engine.
19+
20+
21+
@Template(resource_path='/org/gnome/meld/ui/encoding-selector.ui')
1122
class EncodingSelector(FilteredListSelector, Gtk.Grid):
1223
# The subclassing here is weird; the Selector must directly
1324
# subclass Gtk.Grid, or the template building explodes.
@@ -24,6 +35,9 @@ class EncodingSelector(FilteredListSelector, Gtk.Grid):
2435
value_accessor = 'get_charset'
2536
change_signal_name = 'encoding-selected'
2637

38+
entry = Template.Child('entry')
39+
treeview = Template.Child('treeview')
40+
2741
def populate_model(self):
2842
for enc in GtkSource.Encoding.get_all():
2943
self.liststore.append((self.get_value_label(enc), enc))
@@ -40,14 +54,8 @@ def get_value_label(self, enc):
4054
# Copyright (C) 2015, 2017 Kai Willadsen <kai.willadsen@gmail.com>
4155

4256

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')
57+
@Template(resource_path='/org/gnome/meld/ui/language-selector.ui')
4858
class SourceLangSelector(FilteredListSelector, Gtk.Grid):
49-
# The subclassing here is weird; the Selector must directly
50-
# subclass Gtk.Grid, or the template building explodes.
5159

5260
__gtype_name__ = "SourceLangSelector"
5361

@@ -61,6 +69,9 @@ class SourceLangSelector(FilteredListSelector, Gtk.Grid):
6169
value_accessor = 'get_id'
6270
change_signal_name = 'language-selected'
6371

72+
entry = Template.Child('entry')
73+
treeview = Template.Child('treeview')
74+
6475
def populate_model(self):
6576
self.liststore.append((_("Plain Text"), None))
6677
manager = GtkSource.LanguageManager.get_default()

meld/ui/listselector.py

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,5 @@
11

2-
from gi.repository import GLib
3-
from gi.repository import Gtk
4-
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-
20-
21-
class TemplateHackMixin:
22-
23-
def get_template_child(self, widget_type, name):
24-
# Taken from an in-progress patch on bgo#701843
25-
26-
def get_template_child(widget, widget_type, name):
27-
# Explicitly use gtk_buildable_get_name() because it is masked by
28-
# gtk_widget_get_name() in GI.
29-
if isinstance(widget, widget_type) and \
30-
isinstance(widget, Gtk.Buildable) and \
31-
Gtk.Buildable.get_name(widget) == name:
32-
return widget
33-
34-
if isinstance(widget, Gtk.Container):
35-
for child in widget.get_children():
36-
result = get_template_child(child, widget_type, name)
37-
if result is not None:
38-
return result
39-
40-
return get_template_child(self, widget_type, name)
41-
42-
43-
class FilteredListSelector(TemplateHackMixin):
2+
class FilteredListSelector:
443

454
# FilteredListSelector was intially based on gedit's
465
# GeditHighlightModeSelector
@@ -53,11 +12,9 @@ class FilteredListSelector(TemplateHackMixin):
5312
NAME_COLUMN, VALUE_COLUMN = 0, 1
5413

5514
def __init__(self):
56-
Gtk.Grid.__init__(self)
15+
super().__init__()
5716
self.init_template()
5817

59-
self.entry = self.get_template_child(Gtk.SearchEntry, 'entry')
60-
self.treeview = self.get_template_child(Gtk.TreeView, 'treeview')
6118
self.treeview_selection = self.treeview.get_selection()
6219
# FIXME: Should be able to access as a template child, but can't.
6320
self.listfilter = self.treeview.get_model()

0 commit comments

Comments
 (0)