WIP: Support GSettings bindings

Fixes #66.
This commit is contained in:
James Westman 2022-07-09 15:58:49 -05:00
parent 012fc61926
commit 05f65a86a5
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
4 changed files with 41 additions and 2 deletions

View file

@ -24,7 +24,6 @@ from .gtkbuilder_template import Template
from .values import Value, TranslatedStringValue from .values import Value, TranslatedStringValue
from .common import * from .common import *
class Property(AstNode): class Property(AstNode):
grammar = AnyOf( grammar = AnyOf(
[ [
@ -42,6 +41,20 @@ class Property(AstNode):
)), )),
";", ";",
], ],
Statement(
UseIdent("name"),
":",
Keyword("bind-settings"),
UseQuoted("bind_settings_schema"),
UseQuoted("bind_settings_key"),
ZeroOrMore(AnyOf(
Keyword("get"),
Keyword("set"),
Keyword("no-sensitivity"),
Keyword("get-no-changes"),
["inverted", UseLiteral("inverted", True)],
)),
),
Statement( Statement(
UseIdent("name"), UseIdent("name"),
UseLiteral("binding", True), UseLiteral("binding", True),
@ -96,7 +109,10 @@ class Property(AstNode):
@validate("bind") @validate("bind")
def property_bindable(self): def property_bindable(self):
if self.tokens["bind"] and self.gir_property is not None and self.gir_property.construct_only: if ((self.tokens["bind"] or self.tokens["bind-setting"])
and self.gir_property is not None
and self.gir_property.construct_only):
raise CompileError( raise CompileError(
f"{self.gir_property.full_name} can't be bound because it is construct-only", f"{self.gir_property.full_name} can't be bound because it is construct-only",
hints=["construct-only properties may only be set to a static value"] hints=["construct-only properties may only be set to a static value"]
@ -146,11 +162,21 @@ class Property(AstNode):
bind_flags.append("invert-boolean") bind_flags.append("invert-boolean")
if self.tokens["bidirectional"]: if self.tokens["bidirectional"]:
bind_flags.append("bidirectional") bind_flags.append("bidirectional")
if self.tokens["get"]:
bind_flags.append("get")
if self.tokens["set"]:
bind_flags.append("set")
if self.tokens["no-sensitivity"]:
bind_flags.append("no-sensitivity")
if self.tokens["get-no-changes"]:
bind_flags.append("get-no-changes")
bind_flags_str = "|".join(bind_flags) or None bind_flags_str = "|".join(bind_flags) or None
props = { props = {
"name": self.tokens["name"], "name": self.tokens["name"],
"bind-source": self.tokens["bind_source"], "bind-source": self.tokens["bind_source"],
"bind-settings-schema": self.tokens["bind_settings_schema"],
"bind-settings-key": self.tokens["bind_settings_key"],
"bind-property": self.tokens["bind_property"], "bind-property": self.tokens["bind_property"],
"bind-flags": bind_flags_str, "bind-flags": bind_flags_str,
} }

View file

@ -0,0 +1,5 @@
using Gtk 4.0;
Label {
label: bind-settings "org.example.Settings" "my-label" inverted get-no-changes;
}

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk" version="4.0"/>
<object class="GtkLabel">
<property name="label" bind-settings-schema="org.example.Settings" bind-settings-key="my-label" bind-flags="invert-boolean|get-no-changes"/>
</object>
</interface>

View file

@ -132,6 +132,7 @@ class TestSamples(unittest.TestCase):
self.assert_sample("accessibility") self.assert_sample("accessibility")
self.assert_sample("action_widgets") self.assert_sample("action_widgets")
self.assert_sample("binding") self.assert_sample("binding")
self.assert_sample("bind_settings")
self.assert_sample("child_type") self.assert_sample("child_type")
self.assert_sample("combo_box_text") self.assert_sample("combo_box_text")
self.assert_sample("comments") self.assert_sample("comments")