decompiler: Support list accessibility properties

This commit is contained in:
James Westman 2024-08-23 18:16:02 -05:00
parent 3dfce3bbe0
commit 4d3dc92448
5 changed files with 54 additions and 25 deletions

View file

@ -192,18 +192,21 @@ def decompile_element(
decompiler = decompilers[0] decompiler = decompilers[0]
args: T.Dict[str, T.Optional[str]] = { if decompiler._element:
canon(name): value for name, value in xml.attrs.items() args = [ctx, gir, xml]
} kwargs: T.Dict[str, T.Optional[str]] = {}
else:
args = [ctx, gir]
kwargs = {canon(name): value for name, value in xml.attrs.items()}
if decompiler._cdata: if decompiler._cdata:
if len(xml.children): if len(xml.children):
args["cdata"] = None kwargs["cdata"] = None
else: else:
args["cdata"] = xml.cdata kwargs["cdata"] = xml.cdata
ctx._node_stack.append(xml) ctx._node_stack.append(xml)
ctx.start_block() ctx.start_block()
gir = decompiler(ctx, gir, **args) gir = decompiler(*args, **kwargs)
if not decompiler._skip_children: if not decompiler._skip_children:
for child in xml.children: for child in xml.children:
@ -264,10 +267,12 @@ def decompiler(
parent_type: T.Optional[str] = None, parent_type: T.Optional[str] = None,
parent_tag: T.Optional[str] = None, parent_tag: T.Optional[str] = None,
skip_children=False, skip_children=False,
element=False,
): ):
def decorator(func): def decorator(func):
func._cdata = cdata func._cdata = cdata
func._skip_children = skip_children func._skip_children = skip_children
func._element = element
def filter(ctx): def filter(ctx):
if parent_type is not None: if parent_type is not None:

View file

@ -249,19 +249,35 @@ def a11y_name_completer(lsp, ast_node, match_variables):
) )
@decompiler("relation", cdata=True) def decompile_attr(ctx: DecompileCtx, attr):
def decompile_relation(ctx, gir, name, cdata): if attr["translatable"] is not None and decompile.truthy(attr["translatable"]):
ctx.print_attribute(name, cdata, get_types(ctx.gir).get(name)) ctx.print(f"_({escape_quote(attr.cdata)})")
@decompiler("state", cdata=True)
def decompile_state(ctx, gir, name, cdata, translatable="false"):
if decompile.truthy(translatable):
ctx.print(f"{name}: _({escape_quote(cdata)});")
else: else:
ctx.print_attribute(name, cdata, get_types(ctx.gir).get(name)) ctx.print_value(attr.cdata, get_types(ctx.gir).get(attr["name"]))
@decompiler("accessibility") @decompiler("accessibility", skip_children=True, element=True)
def decompile_accessibility(ctx, gir): def decompile_accessibility(ctx: DecompileCtx, _gir, element):
ctx.print("accessibility {") ctx.print("accessibility {")
already_printed = set()
for child in element.children:
name = child["name"]
if name in allow_duplicates:
if name in already_printed:
continue
ctx.print(f"{name}: [")
for value in element.children:
if value["name"] == name:
decompile_attr(ctx, value)
ctx.print(", ")
ctx.print("];")
else:
ctx.print(f"{name}:")
decompile_attr(ctx, child)
ctx.print(";")
already_printed.add(name)
ctx.print("}")
ctx.end_block_with("")

View file

@ -3,7 +3,11 @@ using Gtk 4.0;
Box { Box {
accessibility { accessibility {
label: _("Hello, world!"); label: _("Hello, world!");
labelled-by: my_label;
labelled-by: [
my_label,
];
checked: true; checked: true;
} }
} }

View file

@ -3,7 +3,13 @@ using Gtk 4.0;
Box { Box {
accessibility { accessibility {
label: _("Hello, world!"); label: _("Hello, world!");
labelled-by: [my_label1, my_label2, my_label3];
labelled-by: [
my_label1,
my_label2,
my_label3,
];
checked: true; checked: true;
} }
} }

View file

@ -221,8 +221,6 @@ class TestSamples(unittest.TestCase):
"list_factory", "list_factory",
# Not implemented yet # Not implemented yet
"subscope", "subscope",
# Not implemented yet
"accessibility_multiple_labelled_by",
] ]
if sample in REQUIRE_ADW_1_4 and not self.have_adw_1_4: if sample in REQUIRE_ADW_1_4 and not self.have_adw_1_4: