From 988e69ab25140eb1773cf0eabfaa52b62b21d93c Mon Sep 17 00:00:00 2001 From: James Westman Date: Thu, 2 May 2024 20:10:40 -0500 Subject: [PATCH] lang: Allow ColumnView widgets to be built Allow BuilderListItemFactory to contain Gtk.ColumnViewRow or Gtk.ColumnViewCell templates, in addition to Gtk.ListItem templates. This is necessary for people to use Gtk.ColumnView idiomatically in Blueprint. Fixes #157. --- .../language/gtk_list_item_factory.py | 15 ++++-- blueprintcompiler/outputs/xml/__init__.py | 2 +- docs/reference/extensions.rst | 2 +- tests/sample_errors/list_factory.err | 2 +- tests/samples/gtkcolumnview.blp | 17 ++++++ tests/samples/gtkcolumnview.ui | 53 +++++++++++++++++++ 6 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 tests/samples/gtkcolumnview.blp create mode 100644 tests/samples/gtkcolumnview.ui diff --git a/blueprintcompiler/language/gtk_list_item_factory.py b/blueprintcompiler/language/gtk_list_item_factory.py index 7d05422..5b6177f 100644 --- a/blueprintcompiler/language/gtk_list_item_factory.py +++ b/blueprintcompiler/language/gtk_list_item_factory.py @@ -39,7 +39,10 @@ class ExtListItemFactory(AstNode): @property def gir_class(self): - return self.root.gir.get_type("ListItem", "Gtk") + if self.type_name is not None: + return self.type_name.gir_type + else: + return self.root.gir.get_type("ListItem", "Gtk") @validate("template") def container_is_builder_list(self): @@ -57,8 +60,14 @@ class ExtListItemFactory(AstNode): @validate() def type_is_list_item(self): if self.type_name is not None: - if self.type_name.glib_type_name != "GtkListItem": - raise CompileError(f"Only Gtk.ListItem is allowed as a type here") + if self.type_name.glib_type_name not in ( + "GtkListItem", + "GtkColumnViewRow", + "GtkColumnViewCell", + ): + raise CompileError( + f"Only Gtk.ListItem, Gtk.ColumnViewRow, or Gtk.ColumnViewCell is allowed as a type here" + ) @validate("template") def type_name_upgrade(self): diff --git a/blueprintcompiler/outputs/xml/__init__.py b/blueprintcompiler/outputs/xml/__init__.py index 8707ee6..f23dacb 100644 --- a/blueprintcompiler/outputs/xml/__init__.py +++ b/blueprintcompiler/outputs/xml/__init__.py @@ -377,7 +377,7 @@ class XmlOutput(OutputFormat): elif isinstance(extension, ExtListItemFactory): child_xml = XmlEmitter() child_xml.start_tag("interface") - child_xml.start_tag("template", **{"class": "GtkListItem"}) + child_xml.start_tag("template", **{"class": extension.gir_class}) self._emit_object_or_template(extension, child_xml) child_xml.end_tag() child_xml.end_tag() diff --git a/docs/reference/extensions.rst b/docs/reference/extensions.rst index 744326f..a7b7b93 100644 --- a/docs/reference/extensions.rst +++ b/docs/reference/extensions.rst @@ -224,7 +224,7 @@ Valid in `Gtk.BuilderListItemFactory `_. The template object can be referenced with the ``template`` keyword. +The template type must be `Gtk.ListItem `_, `Gtk.ColumnViewRow `_, or `Gtk.ColumnViewCell `_ The template object can be referenced with the ``template`` keyword. .. code-block:: blueprint diff --git a/tests/sample_errors/list_factory.err b/tests/sample_errors/list_factory.err index adfd57b..1d4dfaa 100644 --- a/tests/sample_errors/list_factory.err +++ b/tests/sample_errors/list_factory.err @@ -1 +1 @@ -4,3,17,Only Gtk.ListItem is allowed as a type here \ No newline at end of file +4,3,17,Only Gtk.ListItem, Gtk.ColumnViewRow, or Gtk.ColumnViewCell is allowed as a type here \ No newline at end of file diff --git a/tests/samples/gtkcolumnview.blp b/tests/samples/gtkcolumnview.blp new file mode 100644 index 0000000..9f11dd3 --- /dev/null +++ b/tests/samples/gtkcolumnview.blp @@ -0,0 +1,17 @@ +using Gtk 4.0; + +ColumnView { + row-factory: BuilderListItemFactory { + template ColumnViewRow {} + }; + + ColumnViewColumn { + factory: BuilderListItemFactory { + template ColumnViewCell { + child: Label { + label: bind template.item as <$MyObject>.name; + }; + } + }; + } +} diff --git a/tests/samples/gtkcolumnview.ui b/tests/samples/gtkcolumnview.ui new file mode 100644 index 0000000..53126b0 --- /dev/null +++ b/tests/samples/gtkcolumnview.ui @@ -0,0 +1,53 @@ + + + + + + + + + + + +]]> + + + + + + + + + + +]]> + + + + + + \ No newline at end of file