From a25b49c902a4db31f32b67c4318a7a6dcc25b718 Mon Sep 17 00:00:00 2001 From: Pascal Garber Date: Tue, 5 Nov 2024 21:16:27 +0100 Subject: [PATCH] Add gtk generics to fix #209 and #208 --- packages/lib/src/generics/generify.ts | 3 +- packages/lib/src/generics/gtk.ts | 77 +++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 packages/lib/src/generics/gtk.ts diff --git a/packages/lib/src/generics/generify.ts b/packages/lib/src/generics/generify.ts index f8dbead8..4c44de9d 100644 --- a/packages/lib/src/generics/generify.ts +++ b/packages/lib/src/generics/generify.ts @@ -1,5 +1,6 @@ import gio from "./gio.js"; import glib from "./glib.js"; +import gtk from "./gtk.js"; import { clutter10, clutter11, clutter12, clutter13, clutter14, clutter15 } from "./clutter.js"; import { st1, st12, st13, st14, st15 } from "./st.js"; import { meta10, meta11, meta12, meta13, meta14, meta15 } from "./meta.js"; @@ -31,7 +32,7 @@ export function generify(registry: NSRegistry, inferGenerics: boolean) { $(gio); $(glib); - + $(gtk); const $_ = generifyDefinitions(registry, inferGenerics, false); $_(clutter10); diff --git a/packages/lib/src/generics/gtk.ts b/packages/lib/src/generics/gtk.ts new file mode 100644 index 00000000..8cb0a566 --- /dev/null +++ b/packages/lib/src/generics/gtk.ts @@ -0,0 +1,77 @@ +import { IntrospectedNamespace } from "../gir/namespace.js"; +import { Generic, GenericType, GenerifiedTypeIdentifier } from "../gir.js"; +import { IntrospectedBaseClass } from "../gir/class.js"; + +export default { + namespace: "Gtk", + version: "4.0", + modifier: (namespace: IntrospectedNamespace) => { + const FlowBox = namespace.getClass("FlowBox"); + const ListBox = namespace.getClass("ListBox"); + const StringList = namespace.getClass("StringList"); + const StringObject = namespace.getClass("StringObject"); + const GObject = namespace.assertInstalledImport("GObject").assertClass("Object"); + + if (!FlowBox) { + throw new Error("Gtk.FlowBox not found."); + } + + if (!ListBox) { + throw new Error("Gtk.ListBox not found."); + } + + if (!StringList) { + throw new Error("Gtk.StringList not found."); + } + + if (!StringObject) { + throw new Error("Gtk.StringObject not found."); + } + + // Add generic support for StringList + StringList.addGeneric({ + default: StringObject.getType(), + constraint: GObject.getType() + }); + + // Add generic support for FlowBox + FlowBox.addGeneric({ + default: GObject.getType(), + constraint: GObject.getType() + }); + + // Add generic support for ListBox + ListBox.addGeneric({ + default: GObject.getType(), + constraint: GObject.getType() + }); + + // Update bind_model methods to use generics + const updateBindModel = (cls: IntrospectedBaseClass, widgetFuncName: string) => { + cls.members = cls.members.map(m => { + if (m.name === "bind_model") { + m.generics.push(new Generic(new GenericType("A"), GObject.getType(), undefined, GObject.getType())); + return m.copy({ + parameters: m.parameters.map(p => { + if (p.name === "model") { + return p.copy({ + type: new GenerifiedTypeIdentifier("ListModel", "Gio", [new GenericType("A")]) + }); + } + if (p.name === "create_widget_func") { + return p.copy({ + type: new GenerifiedTypeIdentifier(widgetFuncName, "Gtk", [new GenericType("A")]) + }); + } + return p; + }) + }); + } + return m; + }); + }; + + updateBindModel(FlowBox, "FlowBoxCreateWidgetFunc"); + updateBindModel(ListBox, "ListBoxCreateWidgetFunc"); + } +};