Merge branch 'wip/velsinki/levelbar-support-offsets' into 'main'

language: Add Gtk.LevelBar offset syntax

See merge request GNOME/blueprint-compiler!247
This commit is contained in:
Matthijs Velsink 2025-06-15 09:52:52 +00:00
commit 7ce140e7b1
8 changed files with 198 additions and 0 deletions

View file

@ -28,6 +28,7 @@ from .gtk_file_filter import (
ext_file_filter_suffixes,
)
from .gtk_layout import ExtLayout
from .gtk_level_bar import ExtLevelBarOffsets
from .gtk_list_item_factory import ExtListItemFactory
from .gtk_menu import Menu, MenuAttribute, menu
from .gtk_scale import ExtScaleMarks
@ -67,6 +68,7 @@ OBJECT_CONTENT_HOOKS.children = [
ext_file_filter_patterns,
ext_file_filter_suffixes,
ExtLayout,
ExtLevelBarOffsets,
ExtListItemFactory,
ExtScaleMarks,
ExtSizeGroupWidgets,

View file

@ -0,0 +1,131 @@
# gtk_level_bar.py
#
# Copyright 2025 Matthijs Velsink <mvelsink@gnome.org>
#
# 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 .common import *
from .gobject_object import ObjectContent, validate_parent_type
from .values import StringValue
class ExtLevelBarOffset(AstNode):
grammar = [
Keyword("offset"),
Match("(").expected(),
[
UseQuoted("name"),
",",
Optional(AnyOf(UseExact("sign", "-"), UseExact("sign", "+"))),
UseNumber("value"),
],
Match(")").expected(),
]
@property
def name(self) -> str:
return self.tokens["name"]
@property
def value(self) -> float:
return self.tokens["value"]
@property
def document_symbol(self) -> DocumentSymbol:
return DocumentSymbol(
self.name,
SymbolKind.Field,
self.range,
self.group.tokens["offset"].range,
str(self.value),
)
@validate("value")
def validate_value(self):
if self.tokens["sign"] == "-":
raise CompileError(
"Offset value can't be negative",
Range.join(self.ranges["sign"], self.ranges["value"]),
)
@docs("offset")
def ref_docs(self):
return get_docs_section("Syntax ExtLevelBarOffsets")
class ExtLevelBarOffsets(AstNode):
grammar = [
Keyword("offsets"),
Match("[").expected(),
Until(ExtLevelBarOffset, "]", ","),
]
@property
def offsets(self) -> T.List[ExtLevelBarOffset]:
return self.children
@property
def document_symbol(self) -> DocumentSymbol:
return DocumentSymbol(
"offsets",
SymbolKind.Array,
self.range,
self.group.tokens["offsets"].range,
)
@validate("offsets")
def container_is_level_bar(self):
validate_parent_type(self, "Gtk", "LevelBar", "level bar offsets")
@validate("offsets")
def unique_in_parent(self):
self.validate_unique_in_parent("Duplicate 'offsets' block")
@docs("offsets")
def ref_docs(self):
return get_docs_section("Syntax ExtLevelBarOffsets")
@completer(
applies_in=[ObjectContent],
applies_in_subclass=("Gtk", "LevelBar"),
matches=new_statement_patterns,
)
def complete_offsets(lsp, ast_node, match_variables):
yield Completion(
"offsets", CompletionItemKind.Keyword, snippet="offsets [\n\t$0\n]"
)
@completer(
applies_in=[ExtLevelBarOffsets],
)
def complete_offset(lsp, ast_node, match_variables):
yield Completion(
"offset",
CompletionItemKind.Snippet,
snippet='offset ("${1:name}", ${2:value}),',
)
@decompiler("offsets")
def decompile_offsets(ctx, gir):
ctx.print("offsets [")
@decompiler("offset")
def decompile_offset(ctx: DecompileCtx, gir, name, value):
ctx.print(f'offset ("{name}", {value}),')

View file

@ -374,6 +374,12 @@ class XmlOutput(OutputFormat):
xml.end_tag()
xml.end_tag()
elif isinstance(extension, ExtLevelBarOffsets):
xml.start_tag("offsets")
for offset in extension.offsets:
xml.put_self_closing("offset", name=offset.name, value=offset.value)
xml.end_tag()
elif isinstance(extension, ExtScaleMarks):
xml.start_tag("marks")
for mark in extension.marks:

View file

@ -23,6 +23,7 @@ Properties are the main way to set values on objects, but they are limited by th
| :ref:`ExtFileFilterPatterns<Syntax ExtFileFilter>`
| :ref:`ExtFileFilterSuffixes<Syntax ExtFileFilter>`
| :ref:`ExtLayout<Syntax ExtLayout>`
| :ref:`ExtLevelBarOffsets<Syntax ExtLevelBarOffsets>`
| :ref:`ExtListItemFactory<Syntax ExtListItemFactory>`
| :ref:`ExtScaleMarks<Syntax ExtScaleMarks>`
| :ref:`ExtSizeGroupWidgets<Syntax ExtSizeGroupWidgets>`
@ -216,6 +217,31 @@ The ``layout`` block describes how the widget should be positioned within its pa
}
.. _Syntax ExtLevelBarOffsets:
Gtk.LevelBar Offsets
--------------------
.. rst-class:: grammar-block
ExtLevelBarOffsets = 'offsets' '[' (ExtLevelBarOffset),* ']'
ExtLevelBarOffset = 'offset' '(' <name::ref:`QUOTED<Syntax QUOTED>`> ',' <value::ref:`NUMBER<Syntax NUMBER>`> ')'
Valid in `Gtk.LevelBar <https://docs.gtk.org/gtk4/class.LevelBar.html>`_.
The ``offsets`` block defines the offsets on a level bar. A single ``offset`` has two arguments: a CSS class name and a (non-negative) value.
.. code-block:: blueprint
LevelBar {
offsets [
offset ("low-class-name", 0.3),
offset ("medium-class-name", 0.5),
offset ("high-class-name", 0.7),
]
}
.. _Syntax ExtListItemFactory:
Gtk.BuilderListItemFactory Templates

View file

@ -0,0 +1,7 @@
using Gtk 4.0;
LevelBar {
offsets [
offset ("negative-value", -0.3),
]
}

View file

@ -0,0 +1 @@
5,31,4,Offset value can't be negative

View file

@ -0,0 +1,9 @@
using Gtk 4.0;
LevelBar {
offsets [
offset ("low-class-name", 0.3),
offset ("medium-class-name", 0.5),
offset ("high-class-name", 0.7),
]
}

View file

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<object class="GtkLevelBar">
<offsets>
<offset name="low-class-name" value="0.3"/>
<offset name="medium-class-name" value="0.5"/>
<offset name="high-class-name" value="0.7"/>
</offsets>
</object>
</interface>