Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Generics for LayoutManager #225

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions examples/st-15-layout-manager/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* 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';

export class GridLayoutWidget extends St.Widget<Clutter.GridLayout> {
static {
GObject.registerClass({
GTypeName: 'GridLayoutWidget',
}, this);
}

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
);
});
}
}

/**
* Example usage in your extension:
*
* class Extension {
* enable() {
* this._widget = new GridLayoutWidget();
* Main.uiGroup.add_child(this._widget);
* }
*
* disable() {
* this._widget.destroy();
* this._widget = null;
* }
* }
*/
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
Loading