mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
WIP: List item factory syntax
This commit is contained in:
parent
3416546eac
commit
fdf91dc08e
10 changed files with 145 additions and 9 deletions
|
@ -260,7 +260,7 @@ def decompile_property(ctx, gir, name, cdata, bind_source=None, bind_property=No
|
||||||
flags += " inverted"
|
flags += " inverted"
|
||||||
if "bidirectional" in bind_flags:
|
if "bidirectional" in bind_flags:
|
||||||
flags += " bidirectional"
|
flags += " bidirectional"
|
||||||
ctx.print(f"{name}: bind {bind_source}.{bind_property}{flags};")
|
ctx.print(f"{name}: bind-prop {bind_source}.{bind_property}{flags};")
|
||||||
elif truthy(translatable):
|
elif truthy(translatable):
|
||||||
if context is not None:
|
if context is not None:
|
||||||
ctx.print(f"{name}: C_(\"{escape_quote(context)}\", \"{escape_quote(cdata)}\");")
|
ctx.print(f"{name}: C_(\"{escape_quote(context)}\", \"{escape_quote(cdata)}\");")
|
||||||
|
|
|
@ -10,6 +10,7 @@ from .gtk_a11y import A11y
|
||||||
from .gtk_combo_box_text import Items
|
from .gtk_combo_box_text import Items
|
||||||
from .gtk_file_filter import mime_types, patterns, suffixes
|
from .gtk_file_filter import mime_types, patterns, suffixes
|
||||||
from .gtk_layout import Layout
|
from .gtk_layout import Layout
|
||||||
|
from .gtk_list_item_factory import ListItemFactory
|
||||||
from .gtk_menu import menu
|
from .gtk_menu import menu
|
||||||
from .gtk_size_group import Widgets
|
from .gtk_size_group import Widgets
|
||||||
from .gtk_string_list import Strings
|
from .gtk_string_list import Strings
|
||||||
|
@ -26,6 +27,7 @@ from .common import *
|
||||||
OBJECT_HOOKS.children = [
|
OBJECT_HOOKS.children = [
|
||||||
menu,
|
menu,
|
||||||
Object,
|
Object,
|
||||||
|
ListItemFactory,
|
||||||
]
|
]
|
||||||
|
|
||||||
OBJECT_CONTENT_HOOKS.children = [
|
OBJECT_CONTENT_HOOKS.children = [
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
from .expression import Expr
|
from .expression import Expr
|
||||||
from .gobject_object import Object
|
from .gobject_object import Object
|
||||||
from .gtkbuilder_template import Template
|
from .gtkbuilder_template import Template
|
||||||
|
from .gtk_list_item_factory import ListItemFactory
|
||||||
from .values import Value, TranslatedStringValue
|
from .values import Value, TranslatedStringValue
|
||||||
from .common import *
|
from .common import *
|
||||||
|
|
||||||
|
@ -30,7 +31,7 @@ class Property(AstNode):
|
||||||
[
|
[
|
||||||
UseIdent("name"),
|
UseIdent("name"),
|
||||||
":",
|
":",
|
||||||
Keyword("bind"),
|
Keyword("bind-prop", "bind"),
|
||||||
UseIdent("bind_source"),
|
UseIdent("bind_source"),
|
||||||
".",
|
".",
|
||||||
UseIdent("bind_property"),
|
UseIdent("bind_property"),
|
||||||
|
@ -46,7 +47,7 @@ class Property(AstNode):
|
||||||
UseIdent("name"),
|
UseIdent("name"),
|
||||||
UseLiteral("binding", True),
|
UseLiteral("binding", True),
|
||||||
":",
|
":",
|
||||||
"bind",
|
Keyword("bind"),
|
||||||
Expr,
|
Expr,
|
||||||
),
|
),
|
||||||
Statement(
|
Statement(
|
||||||
|
@ -162,6 +163,10 @@ class Property(AstNode):
|
||||||
xml.start_tag("property", **props)
|
xml.start_tag("property", **props)
|
||||||
self.children[Object][0].emit_xml(xml)
|
self.children[Object][0].emit_xml(xml)
|
||||||
xml.end_tag()
|
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:
|
elif value is None:
|
||||||
if self.tokens["binding"]:
|
if self.tokens["binding"]:
|
||||||
xml.start_tag("binding", **props)
|
xml.start_tag("binding", **props)
|
||||||
|
|
87
blueprintcompiler/language/gtk_list_item_factory.py
Normal file
87
blueprintcompiler/language/gtk_list_item_factory.py
Normal 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()
|
|
@ -536,13 +536,13 @@ class UseLiteral(ParseNode):
|
||||||
class Keyword(ParseNode):
|
class Keyword(ParseNode):
|
||||||
""" Matches the given identifier and sets it as a named token, with the name
|
""" Matches the given identifier and sets it as a named token, with the name
|
||||||
being the identifier itself. """
|
being the identifier itself. """
|
||||||
def __init__(self, kw):
|
def __init__(self, kw, token=None):
|
||||||
self.kw = kw
|
self.kw = kw
|
||||||
self.set_token = True
|
self.token = token or kw
|
||||||
|
|
||||||
def _parse(self, ctx: ParseContext):
|
def _parse(self, ctx: ParseContext):
|
||||||
token = ctx.next_token()
|
token = ctx.next_token()
|
||||||
ctx.set_group_val(self.kw, True, token)
|
ctx.set_group_val(self.token, True, token)
|
||||||
return str(token) == self.kw
|
return str(token) == self.kw
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -59,6 +59,10 @@ class XmlEmitter:
|
||||||
self.result += saxutils.escape(str(text))
|
self.result += saxutils.escape(str(text))
|
||||||
self._needs_newline = False
|
self._needs_newline = False
|
||||||
|
|
||||||
|
def put_cdata(self, text):
|
||||||
|
self.result += f"<![CDATA[{text}]]>"
|
||||||
|
self._needs_newline = False
|
||||||
|
|
||||||
def _indent(self):
|
def _indent(self):
|
||||||
if self.indent is not None:
|
if self.indent is not None:
|
||||||
self.result += "\n" + " " * (self.indent * len(self._tag_stack))
|
self.result += "\n" + " " * (self.indent * len(self._tag_stack))
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
using Gtk 4.0;
|
using Gtk 4.0;
|
||||||
|
|
||||||
Box {
|
Box {
|
||||||
visible: bind box2.visible inverted;
|
visible: bind-prop box2.visible inverted;
|
||||||
orientation: bind box2.orientation;
|
orientation: bind-prop box2.orientation;
|
||||||
spacing: bind box2.spacing no-sync-create;
|
spacing: bind-prop box2.spacing no-sync-create;
|
||||||
}
|
}
|
||||||
|
|
||||||
Box box2 {
|
Box box2 {
|
||||||
|
|
9
tests/samples/list_item_factory.blp
Normal file
9
tests/samples/list_item_factory.blp
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
using Gtk 4.0;
|
||||||
|
|
||||||
|
ListView {
|
||||||
|
factory: list_item_factory(CheckButton) {
|
||||||
|
child: Label {
|
||||||
|
label: bind item.group.label;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
28
tests/samples/list_item_factory.ui
Normal file
28
tests/samples/list_item_factory.ui
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<interface>
|
||||||
|
<requires lib="gtk" version="4.0"/>
|
||||||
|
<object class="GtkListView">
|
||||||
|
<property name="factory">
|
||||||
|
<object class="GtkBuilderListItemFactory">
|
||||||
|
<property name="bytes"><![CDATA[
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<interface>
|
||||||
|
<requires lib="gtk" version="4.0"/>
|
||||||
|
<template class="GtkListItem">
|
||||||
|
<property name="child">
|
||||||
|
<object class="GtkLabel">
|
||||||
|
<binding name="label">
|
||||||
|
<lookup name="label">
|
||||||
|
<lookup name="group">
|
||||||
|
<lookup name="item">GtkListItem</lookup>
|
||||||
|
</lookup>
|
||||||
|
</lookup>
|
||||||
|
</binding>
|
||||||
|
</object>
|
||||||
|
</property>
|
||||||
|
</template>
|
||||||
|
</interface>]]></property>
|
||||||
|
</object>
|
||||||
|
</property>
|
||||||
|
</object>
|
||||||
|
</interface>
|
|
@ -145,6 +145,7 @@ class TestSamples(unittest.TestCase):
|
||||||
self.assert_sample("inline_menu")
|
self.assert_sample("inline_menu")
|
||||||
self.assert_sample("lambda")
|
self.assert_sample("lambda")
|
||||||
self.assert_sample("layout")
|
self.assert_sample("layout")
|
||||||
|
self.assert_sample("list_item_factory")
|
||||||
self.assert_sample("menu")
|
self.assert_sample("menu")
|
||||||
self.assert_sample("numbers")
|
self.assert_sample("numbers")
|
||||||
self.assert_sample("object_prop")
|
self.assert_sample("object_prop")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue