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.
This commit is contained in:
James Westman 2024-05-02 20:10:40 -05:00
parent 84e529a4a8
commit 988e69ab25
6 changed files with 85 additions and 6 deletions

View file

@ -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):

View file

@ -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()

View file

@ -224,7 +224,7 @@ Valid in `Gtk.BuilderListItemFactory <https://docs.gtk.org/gtk4/class.BuilderLis
The ``template`` block defines the template that will be used to create list items. This block is unique within Blueprint because it defines a completely separate sub-blueprint which is used to create each list item. The sub-blueprint may not reference objects in the main blueprint or vice versa.
The template type must be `Gtk.ListItem <https://docs.gtk.org/gtk4/class.ListItem.html>`_. The template object can be referenced with the ``template`` keyword.
The template type must be `Gtk.ListItem <https://docs.gtk.org/gtk4/class.ListItem.html>`_, `Gtk.ColumnViewRow <https://docs.gtk.org/gtk4/class.ColumnViewRow.html>`_, or `Gtk.ColumnViewCell <https://docs.gtk.org/gtk4/class.ColumnViewCell.html>`_ The template object can be referenced with the ``template`` keyword.
.. code-block:: blueprint

View file

@ -1 +1 @@
4,3,17,Only Gtk.ListItem is allowed as a type here
4,3,17,Only Gtk.ListItem, Gtk.ColumnViewRow, or Gtk.ColumnViewCell is allowed as a type here

View file

@ -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;
};
}
};
}
}

View file

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<object class="GtkColumnView">
<property name="row-factory">
<object class="GtkBuilderListItemFactory">
<property name="bytes"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<template class="GtkColumnViewRow"></template>
</interface>]]></property>
</object>
</property>
<child>
<object class="GtkColumnViewColumn">
<property name="factory">
<object class="GtkBuilderListItemFactory">
<property name="bytes"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<template class="GtkColumnViewCell">
<property name="child">
<object class="GtkLabel">
<binding name="label">
<lookup name="name" type="MyObject">
<lookup name="item" type="GtkColumnViewCell">
<constant>GtkColumnViewCell</constant>
</lookup>
</lookup>
</binding>
</object>
</property>
</template>
</interface>]]></property>
</object>
</property>
</object>
</child>
</object>
</interface>