diff --git a/examples/st-15-layout-manager/main.ts b/examples/st-15-layout-manager/main.ts new file mode 100644 index 00000000..7673c0d9 --- /dev/null +++ b/examples/st-15-layout-manager/main.ts @@ -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 { + 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; + * } + * } + */ \ No newline at end of file diff --git a/examples/st-15-layout-manager/package.json b/examples/st-15-layout-manager/package.json new file mode 100644 index 00000000..15f65433 --- /dev/null +++ b/examples/st-15-layout-manager/package.json @@ -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:^" + } +} diff --git a/examples/st-15-layout-manager/tsconfig.json b/examples/st-15-layout-manager/tsconfig.json new file mode 100644 index 00000000..f220dd80 --- /dev/null +++ b/examples/st-15-layout-manager/tsconfig.json @@ -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" + ] +} diff --git a/packages/lib/src/generics/clutter.ts b/packages/lib/src/generics/clutter.ts index cfb794ed..df44bb3d 100644 --- a/packages/lib/src/generics/clutter.ts +++ b/packages/lib/src/generics/clutter.ts @@ -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()); }); @@ -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()); }); } diff --git a/packages/lib/src/generics/st.ts b/packages/lib/src/generics/st.ts index 9646a599..198471c9 100644 --- a/packages/lib/src/generics/st.ts +++ b/packages/lib/src/generics/st.ts @@ -14,7 +14,6 @@ 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"); @@ -22,7 +21,6 @@ const stTemplate = (version: string) => ({ 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"); @@ -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(), @@ -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); } @@ -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()); }); } diff --git a/yarn.lock b/yarn.lock index 2de511fd..187b0487 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"