Skip to content

Commit

Permalink
Add gtk generics to fix #209 and #208
Browse files Browse the repository at this point in the history
  • Loading branch information
JumpLink committed Nov 5, 2024
1 parent d3644fd commit a25b49c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/lib/src/generics/generify.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -31,7 +32,7 @@ export function generify(registry: NSRegistry, inferGenerics: boolean) {

$(gio);
$(glib);

$(gtk);
const $_ = generifyDefinitions(registry, inferGenerics, false);

$_(clutter10);
Expand Down
77 changes: 77 additions & 0 deletions packages/lib/src/generics/gtk.ts
Original file line number Diff line number Diff line change
@@ -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");
}
};

0 comments on commit a25b49c

Please sign in to comment.