Add Gtk.SizeGroup

This commit is contained in:
James Westman 2021-11-12 16:50:35 -06:00
parent b4d4877e07
commit 2224f0958c
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
9 changed files with 130 additions and 4 deletions

View file

@ -2,11 +2,12 @@
templates. """ templates. """
from .gtk_a11y import a11y from .gtk_a11y import a11y
from .gtk_menu import menu
from .gtk_styles import styles
from .gtk_layout import layout
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_menu import menu
from .gtk_size_group import widgets
from .gtk_styles import styles
OBJECT_HOOKS = [menu] OBJECT_HOOKS = [menu]
OBJECT_CONTENT_HOOKS = [a11y, styles, layout, mime_types, patterns, suffixes] OBJECT_CONTENT_HOOKS = [a11y, styles, layout, mime_types, patterns, suffixes, widgets]

View file

@ -0,0 +1,82 @@
# gtk_size_group.py
#
# Copyright 2021 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 .. import ast
from ..ast_utils import AstNode, validate
from ..completions_utils import *
from ..lsp_utils import Completion, CompletionItemKind
from ..parse_tree import *
from ..parser_utils import *
from ..xml_emitter import XmlEmitter
class Widgets(AstNode):
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("widgets")
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
class Widget(AstNode):
@validate("name")
def obj_widget(self):
object = self.root.objects_by_id.get(self.tokens["name"])
type = self.root.gir.get_type("Widget", "Gtk")
if object is None:
raise CompileError(
f"Could not find object with ID {self.tokens['name']}",
did_you_mean=(self.tokens['name'], self.root.objects_by_id.keys()),
)
elif object.gir_class and not object.gir_class.assignable_to(type):
raise CompileError(
f"Cannot assign {object.gir_class.full_name} to {type.full_name}"
)
def emit_xml(self, xml: XmlEmitter):
xml.put_self_closing("widget", name=self.tokens["name"])
widgets = Group(
Widgets,
Statement(
Keyword("widgets"),
Op(":"),
OpenBracket(),
Delimited(
Group(
Widget,
UseIdent("name"),
),
Comma(),
),
CloseBracket(),
)
)
@completer(
applies_in=[ast.ObjectContent],
matches=new_statement_patterns,
)
def file_filter_completer(ast_node, match_variables):
file_filter = ast_node.root.gir.get_type("SizeGroup", "Gtk")
if ast_node.gir_class and ast_node.gir_class.assignable_to(file_filter):
yield Completion("widgets", CompletionItemKind.Snippet, snippet="mime-types: [$0];")

View file

@ -0,0 +1,9 @@
using Gtk 4.0;
using GObject 2.0;
SizeGroup {
mode: horizontal;
widgets: [object];
}
GObject.Object object {}

View file

@ -0,0 +1 @@
6,13,6,Cannot assign GObject.Object to Gtk.Widget

View file

@ -0,0 +1,7 @@
using Gtk 4.0;
using GObject 2.0;
SizeGroup {
mode: horizontal;
widgets: [object];
}

View file

@ -0,0 +1 @@
6,13,6,Could not find object with ID object

View file

@ -0,0 +1,9 @@
using Gtk 4.0;
SizeGroup {
mode: horizontal;
widgets: [label, button];
}
Label label {}
Button button {}

View file

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk" version="4.0"/>
<object class="GtkSizeGroup">
<property name="mode">horizontal</property>
<widgets>
<widget name="label"/>
<widget name="button"/>
</widgets>
</object>
<object class="GtkLabel" id="label"></object>
<object class="GtkButton" id="button"></object>
</interface>

View file

@ -103,6 +103,7 @@ class TestSamples(unittest.TestCase):
self.assert_sample("object_prop") self.assert_sample("object_prop")
self.assert_sample("property") self.assert_sample("property")
self.assert_sample("signal") self.assert_sample("signal")
self.assert_sample("size_group")
self.assert_sample("strings") self.assert_sample("strings")
self.assert_sample("style") self.assert_sample("style")
self.assert_sample("template") self.assert_sample("template")
@ -125,5 +126,7 @@ class TestSamples(unittest.TestCase):
self.assert_sample_error("obj_prop_type") self.assert_sample_error("obj_prop_type")
self.assert_sample_error("property_dne") self.assert_sample_error("property_dne")
self.assert_sample_error("signal_dne") self.assert_sample_error("signal_dne")
self.assert_sample_error("size_group_non_widget")
self.assert_sample_error("size_group_obj_dne")
self.assert_sample_error("two_templates") self.assert_sample_error("two_templates")
self.assert_sample_error("using_invalid_namespace") self.assert_sample_error("using_invalid_namespace")