Make builder template factories use a subscope

This commit is contained in:
James Westman 2023-04-28 20:42:46 -05:00
parent a2eaaa26fe
commit 9dcd06de51
6 changed files with 46 additions and 0 deletions

View file

@ -1,6 +1,8 @@
from .gobject_object import ObjectContent, validate_parent_type from .gobject_object import ObjectContent, validate_parent_type
from ..parse_tree import Keyword from ..parse_tree import Keyword
from ..ast_utils import AstNode, validate from ..ast_utils import AstNode, validate
from .common import *
from .contexts import ScopeCtx
class ListItemFactory(AstNode): class ListItemFactory(AstNode):
@ -19,6 +21,14 @@ class ListItemFactory(AstNode):
"sub-templates", "sub-templates",
) )
@context(ScopeCtx)
def scope_ctx(self) -> ScopeCtx:
return ScopeCtx(node=self)
@validate()
def unique_ids(self):
self.context[ScopeCtx].validate_unique_ids()
@property @property
def content(self) -> ObjectContent: def content(self) -> ObjectContent:
return self.children[ObjectContent][0] return self.children[ObjectContent][0]

View file

@ -0,0 +1,9 @@
using Gtk 4.0;
BuilderListItemFactory {
template {
child: label;
}
}
Label label {}

View file

@ -0,0 +1 @@
5,12,5,Could not find object with ID label

View file

@ -0,0 +1,9 @@
using Gtk 4.0;
Gtk.BuilderListItemFactory {
template {
child: Gtk.Label label {};
}
}
Gtk.Label label {}

15
tests/samples/subscope.ui Normal file
View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk" version="4.0"/>
<object class="GtkBuilderListItemFactory">
<property name="bytes"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="GtkListItem">
<property name="child">
<object class="GtkLabel" id="label"></object>
</property>
</template>
</interface>]]></property>
</object>
<object class="GtkLabel" id="label"></object>
</interface>

View file

@ -184,6 +184,7 @@ class TestSamples(unittest.TestCase):
self.assert_sample("string_list") self.assert_sample("string_list")
self.assert_sample("strings") self.assert_sample("strings")
self.assert_sample("style") self.assert_sample("style")
self.assert_sample("subscope")
self.assert_sample( self.assert_sample(
"template", skip_run=True "template", skip_run=True
) # The template class doesn't exist ) # The template class doesn't exist
@ -258,6 +259,7 @@ class TestSamples(unittest.TestCase):
self.assert_sample_error("size_group_obj_dne") self.assert_sample_error("size_group_obj_dne")
self.assert_sample_error("strv") self.assert_sample_error("strv")
self.assert_sample_error("styles_in_non_widget") self.assert_sample_error("styles_in_non_widget")
self.assert_sample_error("subscope")
self.assert_sample_error("two_templates") self.assert_sample_error("two_templates")
self.assert_sample_error("uint") self.assert_sample_error("uint")
self.assert_sample_error("using_invalid_namespace") self.assert_sample_error("using_invalid_namespace")