Skip to content

Commit

Permalink
Fix generics for LayoutManager, fixes #145
Browse files Browse the repository at this point in the history
  • Loading branch information
JumpLink committed Nov 13, 2024
1 parent f703cc1 commit 0e1e75d
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 8 deletions.
66 changes: 66 additions & 0 deletions examples/st-15-layout-manager/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* IMPORTANT NOTE:
* This is a demonstration of St.Widget with Layout Manager Generics for GNOME Shell Extensions.
* St (Shell Toolkit) can only be used within GNOME Shell Extensions and cannot be run as a standalone application.
*
* This example shows how to use the generic types in your extension code.
* To test this, you would need to integrate it into a proper GNOME Shell Extension.
*/

import Clutter from 'gi://Clutter';
import St from 'gi://St';
import GObject from 'gi://GObject';
import GLib from 'gi://GLib';

// Define a custom widget class with GridLayout
const GridLayoutWidget = GObject.registerClass(
class GridLayoutWidget extends St.Widget<Clutter.GridLayout> {
constructor() {
super({
layout_manager: new Clutter.GridLayout()
});

// Create and add labels in a grid pattern
const labels = [
'Top Left', 'Top Right',
'Bottom Left', 'Bottom Right'
];

labels.forEach((text, index) => {
const label = new St.Label({ text });
this.layout_manager.attach(
label,
index % 2, // column
Math.floor(index / 2), // row
1, 1
);
});
}
}
);

// Main loop
const loop = new GLib.MainLoop(null, false);

// Create main window
const stage = new Clutter.Stage({
width: 300,
height: 200,
});

// Add our widget
const widget = new GridLayoutWidget();
stage.add_child(widget);

// Center the widget
widget.set_position(
stage.width / 2 - widget.width / 2,
stage.height / 2 - widget.height / 2
);

// Connect signals
stage.connect('destroy', () => loop.quit());
stage.show();

// Start the main loop
loop.run();
26 changes: 26 additions & 0 deletions examples/st-15-layout-manager/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@ts-for-gir-example/st-15-layout-manager",
"version": "4.0.0-beta.19",
"description": "Example demonstrating St.Widget with Layout Manager Generics",
"type": "module",
"private": true,
"scripts": {
"build:app": "tsc",
"build": "yarn build:app",
"start:app": "gjs -m dist/main.js",
"start": "yarn build && yarn start:app",
"validate": "yarn validate:types",
"validate:types": "tsc --noEmit",
"clear": "rm -rf dist"
},
"devDependencies": {
"typescript": "^5.6.3"
},
"dependencies": {
"@girs/clutter-15": "workspace:^",
"@girs/gjs": "workspace:^",
"@girs/glib-2.0": "workspace:^",
"@girs/gobject-2.0": "workspace:^",
"@girs/st-15": "workspace:^"
}
}
18 changes: 18 additions & 0 deletions examples/st-15-layout-manager/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"types": ["@girs/gjs", "@girs/gjs/dom", "@girs/st-15", "@girs/clutter-15", "@girs/glib-2.0"],
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"outDir": "./dist"
},
"files": [
"main.ts"
]
}
5 changes: 1 addition & 4 deletions packages/lib/src/generics/clutter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ export const clutterTemplate = (version: string) => ({
Actor.props
.filter(p => p.name === "layout_manager" || p.name === "layoutManager")
.forEach(prop => {
// TODO Automatically infer such changes.
prop.type = new GenericType("A", Content.getType());
prop.type = new GenericType("A", LayoutManager.getType());
});

Actor.props
.filter(p => p.name === "content")
.forEach(prop => {
// TODO Automatically infer such changes.
prop.type = new GenericType("B", Content.getType());
});

Expand All @@ -47,7 +45,6 @@ export const clutterTemplate = (version: string) => ({
Clone.props
.filter(p => p.name === "source")
.forEach(prop => {
// TODO Automatically infer such changes.
prop.type = new GenericType("A", Content.getType());
});
}
Expand Down
10 changes: 6 additions & 4 deletions packages/lib/src/generics/st.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ const stTemplate = (version: string) => ({
const ScrollView = namespace.assertClass("ScrollView");
const ScrollBar = namespace.assertClass("ScrollBar");
const Widget = namespace.assertClass("Widget");
// TODO: Create a way to propagate this generic to child classes.
const Viewport = namespace.assertClass("Viewport");
const StBoxLayout = namespace.assertClass("BoxLayout");

const Clutter = namespace.assertInstalledImport("Clutter");

const Actor = Clutter.assertClass("Actor");
const Content = Clutter.assertClass("Content");
// Container was removed in Clutter-14
const Container = Number(version) < 14 ? Clutter.assertClass("Container") : null;
const LayoutManager = Clutter.assertClass("LayoutManager");
const ClutterBoxLayout = Clutter.assertClass("BoxLayout");
Expand All @@ -39,6 +37,12 @@ const stTemplate = (version: string) => ({
constraint: Content.getType()
});

Widget.props
.filter(p => p.name === "layout_manager")
.forEach(prop => {
prop.type = new GenericType("A", LayoutManager.getType());
});

Viewport.addGeneric({
deriveFrom: Widget.getType(),
default: LayoutManager.getType(),
Expand Down Expand Up @@ -88,7 +92,6 @@ const stTemplate = (version: string) => ({

if (get_hscroll_bar) {
const fixed_get_h = get_hscroll_bar?.copy({ returnType: ScrollBar.getType() });

const index = ScrollView.members.indexOf(get_hscroll_bar);
ScrollView.members.splice(index, 1, fixed_get_h);
}
Expand All @@ -108,7 +111,6 @@ const stTemplate = (version: string) => ({
Bin.props
.filter(p => p.name === "child")
.forEach(prop => {
// TODO Automatically infer such changes.
prop.type = new GenericType("A", Actor.getType());
});
}
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13077,6 +13077,19 @@ __metadata:
languageName: unknown
linkType: soft

"@ts-for-gir-example/st-15-layout-manager@workspace:examples/st-15-layout-manager":
version: 0.0.0-use.local
resolution: "@ts-for-gir-example/st-15-layout-manager@workspace:examples/st-15-layout-manager"
dependencies:
"@girs/clutter-15": "workspace:^"
"@girs/gjs": "workspace:^"
"@girs/glib-2.0": "workspace:^"
"@girs/gobject-2.0": "workspace:^"
"@girs/st-15": "workspace:^"
typescript: "npm:^5.6.3"
languageName: unknown
linkType: soft

"@ts-for-gir-example/timers-example@workspace:examples/timers":
version: 0.0.0-use.local
resolution: "@ts-for-gir-example/timers-example@workspace:examples/timers"
Expand Down

0 comments on commit 0e1e75d

Please sign in to comment.