WIP: List item factory syntax

This commit is contained in:
James Westman 2022-07-18 16:37:15 -05:00
parent 3416546eac
commit fdf91dc08e
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
10 changed files with 145 additions and 9 deletions

View file

@ -10,6 +10,7 @@ from .gtk_a11y import A11y
from .gtk_combo_box_text import Items
from .gtk_file_filter import mime_types, patterns, suffixes
from .gtk_layout import Layout
from .gtk_list_item_factory import ListItemFactory
from .gtk_menu import menu
from .gtk_size_group import Widgets
from .gtk_string_list import Strings
@ -26,6 +27,7 @@ from .common import *
OBJECT_HOOKS.children = [
menu,
Object,
ListItemFactory,
]
OBJECT_CONTENT_HOOKS.children = [

View file

@ -21,6 +21,7 @@
from .expression import Expr
from .gobject_object import Object
from .gtkbuilder_template import Template
from .gtk_list_item_factory import ListItemFactory
from .values import Value, TranslatedStringValue
from .common import *
@ -30,7 +31,7 @@ class Property(AstNode):
[
UseIdent("name"),
":",
Keyword("bind"),
Keyword("bind-prop", "bind"),
UseIdent("bind_source"),
".",
UseIdent("bind_property"),
@ -46,7 +47,7 @@ class Property(AstNode):
UseIdent("name"),
UseLiteral("binding", True),
":",
"bind",
Keyword("bind"),
Expr,
),
Statement(
@ -162,6 +163,10 @@ class Property(AstNode):
xml.start_tag("property", **props)
self.children[Object][0].emit_xml(xml)
xml.end_tag()
elif len(self.children[ListItemFactory]) == 1:
xml.start_tag("property", **props)
self.children[ListItemFactory][0].emit_xml(xml)
xml.end_tag()
elif value is None:
if self.tokens["binding"]:
xml.start_tag("binding", **props)

View file

@ -0,0 +1,87 @@
# gtk_list_item_factory.py
#
# Copyright 2022 James Westman <james@jwestman.net>
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
from .types import ClassName
from .gobject_object import ObjectContent
from .common import *
class ListItemFactoryContent(AstNode):
grammar = ObjectContent
@property
def gir_class(self):
return self.root.gir.namespaces["Gtk"].lookup_type("Gtk.ListItem")
def emit_xml(self, xml: XmlEmitter):
self.children[ObjectContent][0].emit_xml(xml)
class ListItemFactory(AstNode, Scope):
grammar = [
"list_item_factory",
"(",
ClassName,
")",
Optional(UseIdent("id")),
ListItemFactoryContent,
]
@property
def variables(self) -> T.Dict[str, ScopeVariable]:
def emit_xml(xml: XmlEmitter, id: str):
xml.start_tag("constant")
xml.put_text(id)
xml.end_tag()
def emit_item_xml(xml: XmlEmitter):
xml.start_tag("lookup", name="item")
xml.put_text("GtkListItem")
xml.end_tag()
return {
**{
obj.tokens["id"]: ScopeVariable(obj.tokens["id"], obj.gir_class, lambda xml: emit_xml(xml, obj.tokens["id"]))
for obj in self.iterate_children_recursive()
if obj.tokens["id"] is not None
},
"item": ScopeVariable("item", self.gir_class, emit_item_xml),
}
@property
def gir_class(self):
return self.root.gir.namespaces["Gtk"].lookup_type("Gtk.ListItemFactory")
@property
def item_type(self):
return self.children[ClassName][0].gir_type
def emit_xml(self, xml: XmlEmitter):
sub = XmlEmitter()
sub.start_tag("interface")
sub.put_self_closing("requires", lib="gtk", version="4.0")
sub.start_tag("template", **{"class": "GtkListItem"})
self.children[ListItemFactoryContent][0].emit_xml(sub)
sub.end_tag()
sub.end_tag()
xml.start_tag("object", **{"class": "GtkBuilderListItemFactory"}, id=self.tokens["id"])
xml.start_tag("property", name="bytes")
xml.put_cdata("\n" + sub.result)
xml.end_tag()
xml.end_tag()