diff --git a/.ts-for-gir.packages-all.rc.js b/.ts-for-gir.packages-all.rc.js index e9cf56698..f0b7c77bb 100644 --- a/.ts-for-gir.packages-all.rc.js +++ b/.ts-for-gir.packages-all.rc.js @@ -3,6 +3,10 @@ export default { environments: ['gjs', 'node'], modules: ['*'], girDirectories: [ + // General gir files in this repository + './vala-girs/gir-1.0', + './girs', + // General gir files installed on the system "/usr/local/share/gir-1.0", "/usr/share/gir-1.0", @@ -23,6 +27,9 @@ export default { // GNOME Shell gir file dependencies on Fedora Workstation 39 (package: mutter) '/usr/lib64/mutter-13', + // Wait for release... + '/usr/lib64/mutter-14', + // GNOME Shell gir file dependencies on Ubuntu 22.04 (package: libmutter-10-dev) '/usr/lib/x86_64-linux-gnu/mutter-10', @@ -35,9 +42,8 @@ export default { // GNOME Shell gir file dependencies on Ubuntu 23.10 (package: libmutter-13-dev) '/usr/lib/x86_64-linux-gnu/mutter-13', - // General gir files in this repository - './vala-girs/gir-1.0', - './girs', + // Wait for release... + '/usr/lib/x86_64-linux-gnu/mutter-14', ], ignore: [ 'Colorhug-1.0', // Duplicate of ColorHug-1.0 diff --git a/NEWS.md b/NEWS.md index 379dc56de..742422278 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,7 +3,13 @@ - Create a Reporter to create a text or json file with warnings like unknown types, renaming, etc and a summary of all that (e.g. 5 type conflicts resolved, 8 unknown types of *gint) - Update types for GJS v1.75.2, see https://gitlab.gnome.org/GNOME/gjs/-/commit/666755b3b09d765e43d415e76105b828517b5509 -# 3.2.7 +# 3.2.9 +- Upgrade dependencies +- Update examples and removed deprecated function calles like `byteArray.toString()` +- Removed support for `underscore` properties in the generated types, if this causes problems please let us know, then we will undo it again +- node-gtk: Uses snake_case for property names at constructor, fixes #131 + +# 3.2.8 - Upgrade dependencies - Add type for import.meta.url, see 142 - C type `const` is readonly in typescript diff --git a/examples/gjs/adw-1-hello/main.ts b/examples/gjs/adw-1-hello/main.ts index 4702b2c1d..e76e53433 100644 --- a/examples/gjs/adw-1-hello/main.ts +++ b/examples/gjs/adw-1-hello/main.ts @@ -12,7 +12,7 @@ import Adw from './@types/adw-1.js'; const loop = GLib.MainLoop.new(null, false) const app = new Adw.Application({ - application_id: 'com.github.jumplink.gjs.adw-1-hello', + applicationId: 'com.github.jumplink.gjs.adw-1-hello', flags: Gio.ApplicationFlags.FLAGS_NONE }); @@ -26,16 +26,14 @@ const onActivate = (app: Adw.Application) => { const label = new Gtk.Label({ label: "Hello World", marginBottom: 11, - margin_top: 11, + marginTop: 11, }) label.marginTop = 12 - label.margin_bottom = 12 + label.marginBottom = 12 log(`label.marginTop: ${label.marginTop}`) - log(`label.margin_top: ${label.margin_top}`) log(`label.marginBottom: ${label.marginBottom}`) - log(`label.margin_bottom: ${label.margin_bottom}`) const window = new Gtk.ApplicationWindow(app) window.set_title('Hello') diff --git a/examples/gjs/gio-2-cat-alias/main.ts b/examples/gjs/gio-2-cat-alias/main.ts index 2db711f62..dba857379 100644 --- a/examples/gjs/gio-2-cat-alias/main.ts +++ b/examples/gjs/gio-2-cat-alias/main.ts @@ -10,7 +10,7 @@ import GLib from "gi://GLib?version=2.0"; import Gio from "gi://Gio?version=2.0"; -const ByteArray = imports.byteArray; +const textDecoder = new TextDecoder() const loop = GLib.MainLoop.new(null, false); function cat(filename: string) { @@ -24,7 +24,7 @@ function cat(filename: string) { loop.quit(); return; } - print(ByteArray.toString(contents)); + print(textDecoder.decode(contents)); loop.quit(); }); diff --git a/examples/gjs/gio-2-cat-alias/tsconfig.json b/examples/gjs/gio-2-cat-alias/tsconfig.json index ddeae7362..89d0f11a2 100644 --- a/examples/gjs/gio-2-cat-alias/tsconfig.json +++ b/examples/gjs/gio-2-cat-alias/tsconfig.json @@ -7,7 +7,7 @@ "module": "ESNext", "moduleResolution": "node", }, - "include": ["@types/gjs.d.ts"], + "include": ["@types/gjs.d.ts", "@types/dom.d.ts"], "files": [ "main.ts", ] diff --git a/examples/gjs/gio-2-cat-packages/main.ts b/examples/gjs/gio-2-cat-packages/main.ts index 6715e13a2..43d8d3ad2 100644 --- a/examples/gjs/gio-2-cat-packages/main.ts +++ b/examples/gjs/gio-2-cat-packages/main.ts @@ -9,11 +9,12 @@ */ import '@girs/gjs'; +import '@girs/gjs/dom'; import '@girs/gio-2.0'; import Gio from "gi://Gio?version=2.0"; -const ByteArray = imports.byteArray; +const textDecoder = new TextDecoder() Gio._promisify(Gio.File.prototype, 'load_contents_async', 'load_contents_finish'); @@ -21,7 +22,8 @@ async function cat(filename: string) { const file = Gio.file_new_for_path(filename); const [contents] = await file.load_contents_async(null); - print(ByteArray.toString(contents)); + + print(textDecoder.decode(contents)); } if (ARGV.length !== 1) diff --git a/examples/gjs/gio-2-cat-packages/tsconfig.json b/examples/gjs/gio-2-cat-packages/tsconfig.json index 5cc523f87..22851e93f 100644 --- a/examples/gjs/gio-2-cat-packages/tsconfig.json +++ b/examples/gjs/gio-2-cat-packages/tsconfig.json @@ -7,7 +7,7 @@ "module": "ESNext", "moduleResolution": "node" }, - "include": ["@girs/gjs", "@girs/gjs/ambient"], + "include": ["@girs/gjs", "@girs/gjs/dom", "@girs/gjs/ambient"], "files": [ "main.ts", ] diff --git a/examples/gjs/gio-2-cat-promisify/main.ts b/examples/gjs/gio-2-cat-promisify/main.ts index 111878957..0ea17133e 100644 --- a/examples/gjs/gio-2-cat-promisify/main.ts +++ b/examples/gjs/gio-2-cat-promisify/main.ts @@ -9,7 +9,7 @@ */ import Gio from "gi://Gio?version=2.0"; -const ByteArray = imports.byteArray; +const textDecoder = new TextDecoder(); Gio._promisify(Gio.File.prototype, 'load_contents_async', 'load_contents_finish'); @@ -17,7 +17,7 @@ async function cat(filename: string) { const file = Gio.file_new_for_path(filename); const [contents] = await file.load_contents_async(null); - print(ByteArray.toString(contents)); + print(textDecoder.decode(contents)); } if (ARGV.length !== 1) diff --git a/examples/gjs/gio-2-cat-promisify/tsconfig.json b/examples/gjs/gio-2-cat-promisify/tsconfig.json index e83c262cc..1d5d30cd2 100644 --- a/examples/gjs/gio-2-cat-promisify/tsconfig.json +++ b/examples/gjs/gio-2-cat-promisify/tsconfig.json @@ -6,7 +6,7 @@ "target": "ESNext", "module": "ESNext" }, - "include": ["@types/gjs.d.ts"], + "include": ["@types/gjs.d.ts", "@types/dom.d.ts"], "files": [ "main.ts", ] diff --git a/examples/gjs/gio-2-cat-types-only/main.ts b/examples/gjs/gio-2-cat-types-only/main.ts index 20ee51682..5f037780a 100644 --- a/examples/gjs/gio-2-cat-types-only/main.ts +++ b/examples/gjs/gio-2-cat-types-only/main.ts @@ -8,7 +8,7 @@ * the label should show a translation of 'Print help' */ -const ByteArray = imports.byteArray; +const textDecoder = new TextDecoder(); const Gio = imports.gi.Gio; const GLib = imports.gi.GLib; @@ -25,7 +25,7 @@ function cat(filename: string) { loop.quit(); return; } - print(ByteArray.toString(contents)); + print(textDecoder.decode(contents)); loop.quit(); }); diff --git a/examples/gjs/gio-2-cat-types-only/tsconfig.json b/examples/gjs/gio-2-cat-types-only/tsconfig.json index 0301ff13f..dc12aae7e 100644 --- a/examples/gjs/gio-2-cat-types-only/tsconfig.json +++ b/examples/gjs/gio-2-cat-types-only/tsconfig.json @@ -10,7 +10,7 @@ "noImplicitThis": true, "alwaysStrict": true }, - "include": ["@types/gjs.d.ts", "@types/gio-2.0.d.ts", "@types/glib-2.0.d.ts"], + "include": ["@types/gjs.d.ts", "@types/dom.d.ts", "@types/gio-2.0.d.ts", "@types/glib-2.0.d.ts"], "files": [ "main.ts", ] diff --git a/examples/gjs/gio-2-cat/main.ts b/examples/gjs/gio-2-cat/main.ts index 8a1caf42e..55145cea8 100644 --- a/examples/gjs/gio-2-cat/main.ts +++ b/examples/gjs/gio-2-cat/main.ts @@ -8,11 +8,13 @@ * the label should show a translation of 'Print help' */ -import imports from './@types/gjs.js'; +import './@types/gjs.js'; +import './@types/dom.js'; import GLib from './@types/glib-2.0.js'; import Gio from './@types/gio-2.0.js'; const loop = GLib.MainLoop.new(null, false); +const textDecoder = new TextDecoder() function cat(filename: string) { const file = Gio.file_new_for_path(filename); @@ -25,7 +27,7 @@ function cat(filename: string) { loop.quit(); return; } - print(imports.byteArray.toString(contents)); + print(textDecoder.decode(contents)); loop.quit(); }); diff --git a/examples/gjs/glib-2-spawn-command/main.ts b/examples/gjs/glib-2-spawn-command/main.ts index 21f55d5f9..6b9683d03 100644 --- a/examples/gjs/glib-2-spawn-command/main.ts +++ b/examples/gjs/glib-2-spawn-command/main.ts @@ -1,18 +1,18 @@ // Example based on https://gist.github.com/buzztaiki/1487781/74ea93d3a30f20c7f094327db9cb263a6286f6d6 import * as GLib from './@types/glib-2.0.js'; -const { byteArray } = imports; +const textDecoder = new TextDecoder(); let [res, out, err, status] = GLib.spawn_command_line_sync('ls -la'); -if (out) print(byteArray.toString(out)); +if (out) print(textDecoder.decode(out)); [res, out] = GLib.spawn_command_line_sync('ls -la'); -if (out) print(byteArray.toString(out)); +if (out) print(textDecoder.decode(out)); [res, out] = GLib.spawn_sync(null, ['/bin/ls', '-la'], null, 0, null); -if (out) print(byteArray.toString(out)); +if (out) print(textDecoder.decode(out)); [res, out] = GLib.spawn_sync(GLib.getenv('HOME'), ['/bin/ls', '-la'], null, 0, null); -if (out) print(byteArray.toString(out)); +if (out) print(textDecoder.decode(out)); [res, out] = GLib.spawn_sync(null, ['ls', '-la'], null, GLib.SpawnFlags.SEARCH_PATH, null); -if (out) print(byteArray.toString(out)); \ No newline at end of file +if (out) print(textDecoder.decode(out)); \ No newline at end of file diff --git a/examples/gjs/glib-2-spawn-command/tsconfig.json b/examples/gjs/glib-2-spawn-command/tsconfig.json index c52240e68..9da618f9d 100644 --- a/examples/gjs/glib-2-spawn-command/tsconfig.json +++ b/examples/gjs/glib-2-spawn-command/tsconfig.json @@ -10,7 +10,7 @@ "noImplicitThis": true, "alwaysStrict": true, }, - "include": ["@types/gjs.d.ts", "@types/ambient.d.ts"], + "include": ["@types/gjs.d.ts", "@types/dom.d.ts", "@types/ambient.d.ts"], "files": [ "main.ts" ] diff --git a/examples/gjs/gtk-3-editor/main.ts b/examples/gjs/gtk-3-editor/main.ts index ece33cfad..db7b92248 100644 --- a/examples/gjs/gtk-3-editor/main.ts +++ b/examples/gjs/gtk-3-editor/main.ts @@ -5,13 +5,13 @@ const { gettext } = imports; Gtk.init(null) -const wnd = new Gtk.Window({ title: 'Editor Test', default_width: 600, default_height: 400 }) +const wnd = new Gtk.Window({ title: 'Editor Test', defaultWidth: 600, defaultHeight: 400 }) const box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0) const notebook = new Gtk.Notebook({}) const srcView = new GtkSource.View() -srcView.auto_indent = true -srcView.show_line_numbers = true +srcView.autoIndent = true +srcView.showLineNumbers = true srcView.monospace = true const buf = srcView.buffer as GtkSource.Buffer; diff --git a/examples/gjs/gtk-3-hello-2/main.ts b/examples/gjs/gtk-3-hello-2/main.ts index 160b9766f..9ffe02213 100644 --- a/examples/gjs/gtk-3-hello-2/main.ts +++ b/examples/gjs/gtk-3-hello-2/main.ts @@ -17,12 +17,12 @@ Gtk.init(null); let win = new Gtk.Window({ type: Gtk.WindowType.TOPLEVEL, title: 'A default title', - default_width: 300, - default_height: 250, + defaultWidth: 300, + defaultHeight: 250, // A decent example of how constants are mapped: // 'Gtk' and 'WindowPosition' from the enum name GtkWindowPosition, // 'CENTER' from the enum's constant GTK_WIN_POS_CENTER - window_position: Gtk.WindowPosition.CENTER, + windowPosition: Gtk.WindowPosition.CENTER, }); // Object properties can also be set or changed after construction, unless they diff --git a/examples/gjs/gtk-3-hello/main.ts b/examples/gjs/gtk-3-hello/main.ts index c19be4858..c30421a12 100644 --- a/examples/gjs/gtk-3-hello/main.ts +++ b/examples/gjs/gtk-3-hello/main.ts @@ -8,10 +8,10 @@ if (!settings) { throw new Error('Can\t load default settings!') } -settings.gtk_application_prefer_dark_theme = true -settings.gtk_theme_name = 'Adwaita' +settings.gtkApplicationPreferDarkTheme = true +settings.gtkThemeName = 'Adwaita' -print(settings.gtk_enable_accels) +print(settings.gtkEnableAccels) const win = new Gtk.Window() win.set_title('gjs-gtk') diff --git a/examples/gjs/gtk-3-application/esbuild.js b/examples/gjs/gtk-4-application/esbuild.js similarity index 100% rename from examples/gjs/gtk-3-application/esbuild.js rename to examples/gjs/gtk-4-application/esbuild.js diff --git a/examples/gjs/gtk-3-application/main.ts b/examples/gjs/gtk-4-application/main.ts similarity index 77% rename from examples/gjs/gtk-3-application/main.ts rename to examples/gjs/gtk-4-application/main.ts index fc2b852d6..fd57b93fc 100644 --- a/examples/gjs/gtk-3-application/main.ts +++ b/examples/gjs/gtk-4-application/main.ts @@ -1,23 +1,20 @@ -#!/usr/bin/env gjs -m -// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later -// SPDX-FileCopyrightText: 2017 Andy Holmes // Based on https://gitlab.gnome.org/GNOME/gjs/-/blob/master/examples/gtk-application.js -// Include this in case both GTK3 and GTK4 installed, otherwise an exception -// will be thrown -imports.gi.versions.Gtk = '3.0'; +// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later +// SPDX-FileCopyrightText: 2017 Andy Holmes // See the note about Application.run() at the bottom of the script -const System = imports.system; - -const Gio = imports.gi.Gio; -const GLib = imports.gi.GLib; -const GObject = imports.gi.GObject; -const Gtk = imports.gi.Gtk; +import System from 'system'; +import Gio from 'gi://Gio?version=2.0'; +import GLib from 'gi://GLib?version=2.0'; +import GObject from 'gi://GObject?version=2.0'; +// Include the version in case both GTK3 and GTK4 installed +// otherwise an exception will be thrown +import Gtk from 'gi://Gtk?version=4.0'; // An example GtkApplication with a few bells and whistles, see also: // https://wiki.gnome.org/HowDoI/GtkApplication -var ExampleApplication = GObject.registerClass({ +let ExampleApplication = GObject.registerClass({ Properties: { 'exampleprop': GObject.ParamSpec.string( 'exampleprop', // property name @@ -29,33 +26,31 @@ var ExampleApplication = GObject.registerClass({ }, Signals: {'examplesig': {param_types: [GObject.TYPE_INT]}}, }, class ExampleApplication extends Gtk.Application { - - exampleprop = ''; - constructor() { super({ - application_id: 'org.gnome.gjs.ExampleApplication', + applicationId: 'org.gnome.gjs.ExampleApplication', flags: Gio.ApplicationFlags.FLAGS_NONE, }); } + exampleprop: string = 'a default value'; + // Example signal emission - emitExamplesig(number: number) { + emitExamplesig(number: number): void { this.emit('examplesig', number); } vfunc_startup() { - print('called vfunc_startup'); super.vfunc_startup(); // An example GAction, see: https://wiki.gnome.org/HowDoI/GAction let exampleAction = new Gio.SimpleAction({ name: 'exampleAction', - parameter_type: new GLib.VariantType('s'), + parameterType: new GLib.VariantType('s'), }); exampleAction.connect('activate', (action, param) => { - const paramStr = param?.deepUnpack().toString(); + const paramStr = param?.deepUnpack()?.toString() ?? ''; if (paramStr === 'exampleParameter') log('Yes!'); @@ -65,27 +60,26 @@ var ExampleApplication = GObject.registerClass({ } vfunc_activate() { - print('called vfunc_activate'); super.vfunc_activate(); this.hold(); // Example ApplicationWindow - let win = new Gtk.ApplicationWindow({ + let window = new Gtk.ApplicationWindow({ application: this, title: 'Example Application Window', - default_width: 300, - default_height: 200, + defaultWidth: 300, + defaultHeight: 200, }); let label = new Gtk.Label({label: this.exampleprop}); - win.add(label); + window.set_child(label); - win.connect('delete-event', () => { + window.connect('close-request', () => { this.quit(); }); - win.show_all(); + window.present(); // Example GNotification, see: https://developer.gnome.org/GNotification/ let notif = new Gio.Notification(); diff --git a/examples/gjs/gtk-3-application/package.json b/examples/gjs/gtk-4-application/package.json similarity index 82% rename from examples/gjs/gtk-3-application/package.json rename to examples/gjs/gtk-4-application/package.json index d15d963a9..0fa534a04 100644 --- a/examples/gjs/gtk-3-application/package.json +++ b/examples/gjs/gtk-4-application/package.json @@ -1,13 +1,13 @@ { - "name": "ts-for-gir-gtk-3-application-example", + "name": "ts-for-gir-gtk-4-application-example", "version": "3.2.7", - "description": "Simple GJS Gtk 3 example app to demonstrate how you can use and extend Gtk.Application", + "description": "Simple GJS Gtk 4 example app to demonstrate how you can use and extend Gtk.Application", "main": "index.js", "type": "module", "private": true, "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "build:types": "yarn ts-for-gir generate Gtk-3.0 --generateAlias", + "build:types": "yarn ts-for-gir generate Gtk-4.0 --generateAlias", "build:app": "yarn node esbuild.js", "build": "yarn build:types && yarn build:app", "start:app": "gjs -m dist/main.js", diff --git a/examples/gjs/gtk-3-application/tsconfig.json b/examples/gjs/gtk-4-application/tsconfig.json similarity index 71% rename from examples/gjs/gtk-3-application/tsconfig.json rename to examples/gjs/gtk-4-application/tsconfig.json index f2fbc610f..18c6dbf89 100644 --- a/examples/gjs/gtk-3-application/tsconfig.json +++ b/examples/gjs/gtk-4-application/tsconfig.json @@ -11,7 +11,7 @@ "noImplicitThis": true, "alwaysStrict": true, }, - "include": ["@types/gjs.d.ts", "@types/gio-2.0.d.ts", "@types/glib-2.0.d.ts", "@types/gtk-3.0.d.ts"], + "include": ["@types/gjs.d.ts", "@types/ambient.d.ts", "@types/gio-2.0.d.ts", "@types/glib-2.0.d.ts", "@types/gtk-3.0.d.ts"], "files": [ "main.ts", ] diff --git a/examples/gjs/gtk-3-application/tsconfig.types.json b/examples/gjs/gtk-4-application/tsconfig.types.json similarity index 100% rename from examples/gjs/gtk-3-application/tsconfig.types.json rename to examples/gjs/gtk-4-application/tsconfig.types.json diff --git a/examples/gjs/gtk-4-custom-widget/index.ts b/examples/gjs/gtk-4-custom-widget/index.ts index 283b863eb..f79ae9419 100644 --- a/examples/gjs/gtk-4-custom-widget/index.ts +++ b/examples/gjs/gtk-4-custom-widget/index.ts @@ -47,7 +47,7 @@ const CustomWidget = GObject.registerClass({ const loop = GLib.MainLoop.new(null, false) const app = new Gtk.Application({ - application_id: 'com.github.jumplink.gjs.demo', + applicationId: 'com.github.jumplink.gjs.demo', flags: 0 }) app.connect('activate', onActivate) diff --git a/examples/gjs/gtk-4-list-store/main.ts b/examples/gjs/gtk-4-list-store/main.ts index 9d2acea40..2b70991f4 100644 --- a/examples/gjs/gtk-4-list-store/main.ts +++ b/examples/gjs/gtk-4-list-store/main.ts @@ -38,8 +38,8 @@ class App { buildUI() { this.window = new Gtk.ApplicationWindow({ application: this.application, - default_height: 300, - default_width: 720, + defaultHeight: 300, + defaultWidth: 720, }); this.window.set_titlebar(this.getHeader()); this.window.set_child(this.getBody()); @@ -62,7 +62,7 @@ class App { store.set(store.append(), [0, 1, 2, 3], [2, '2C', 'Name 2', false]); store.set(store.append(), [0, 1, 2, 3], [3, '3D', 'Name 3', false]); - tree = new Gtk.TreeView({ headers_visible: false, vexpand: true, hexpand: true }); + tree = new Gtk.TreeView({ headersVisible: false, vexpand: true, hexpand: true }); tree.set_model(store); scroll.set_child(tree); diff --git a/examples/gjs/soup-3-http/http-client.ts b/examples/gjs/soup-3-http/http-client.ts index fbeb5d312..a845fd418 100644 --- a/examples/gjs/soup-3-http/http-client.ts +++ b/examples/gjs/soup-3-http/http-client.ts @@ -11,7 +11,7 @@ import GLib from 'gi://GLib?version=2.0'; import Soup from 'gi://Soup?version=3.0'; const loop = GLib.MainLoop.new(null, false); -const byteArray = imports.byteArray; +const textDecoder = new TextDecoder(); const session = new Soup.Session(); const message = new Soup.Message({ @@ -32,8 +32,9 @@ const readBytesAsyncCallback: Gio.AsyncReadyCallback = (inputStream, res) => { loop.quit(); return; } + - log(`body:\n${byteArray.toString(byteArray.fromGBytes(data))}`); + log(`body:\n${textDecoder.decode(data.toArray())}`); loop.quit(); } @@ -52,12 +53,12 @@ const send_async_callback: Gio.AsyncReadyCallback = (self, res) => { return; } - log(`status: ${message.status_code} - ${message.reason_phrase}`); - message.response_headers.foreach((name, value) => { + log(`status: ${message.statusCode} - ${message.reasonPhrase}`); + message.responseHeaders.foreach((name, value) => { log(`${name}: ${value}`); }); - inputStream.read_bytes_async(message.response_headers.get_content_length(), 0, null, readBytesAsyncCallback); + inputStream.read_bytes_async(message.responseHeaders.get_content_length(), 0, null, readBytesAsyncCallback); } session.send_async(message, 0, null, send_async_callback); diff --git a/examples/gjs/soup-3-http/http-server.ts b/examples/gjs/soup-3-http/http-server.ts index eead1f69a..86ef03f3e 100644 --- a/examples/gjs/soup-3-http/http-server.ts +++ b/examples/gjs/soup-3-http/http-server.ts @@ -15,7 +15,7 @@ interface GjsHashTable { } const loop = GLib.MainLoop.new(null, false); -const byteArray = imports.byteArray; +const textEncoder = new TextEncoder(); const handler: Soup.ServerCallback = (server, msg, path, query) => { if(!msg) { @@ -24,7 +24,7 @@ const handler: Soup.ServerCallback = (server, msg, path, query) => { msg.set_status(200, null); - const body = byteArray.fromString(` + const body = textEncoder.encode(` Greetings, visitor from ${msg.get_remote_host()}
@@ -53,7 +53,7 @@ const helloHandler: Soup.ServerCallback = (server, msg, path, query) => { print("query", JSON.stringify(query)) - const body = byteArray.fromString(` + const body = textEncoder.encode(` Hello, ${(query as GjsHashTable).myname}! ☺
diff --git a/examples/gjs/soup-3-http/tsconfig.json b/examples/gjs/soup-3-http/tsconfig.json index 28d873d99..516172fba 100644 --- a/examples/gjs/soup-3-http/tsconfig.json +++ b/examples/gjs/soup-3-http/tsconfig.json @@ -14,7 +14,7 @@ "noImplicitThis": true, "alwaysStrict": true, }, - "include": ["@types/gjs.d.ts"], + "include": ["@types/gjs.d.ts", "@types/dom.d.ts"], "files": [ "http-client.ts", "http-server.ts" diff --git a/examples/gjs/soup-3-websocket/client.ts b/examples/gjs/soup-3-websocket/client.ts index 46a56e3f0..d78b8a89a 100644 --- a/examples/gjs/soup-3-websocket/client.ts +++ b/examples/gjs/soup-3-websocket/client.ts @@ -11,9 +11,9 @@ import imports from './@types/gjs.js'; import GLib from 'gi://GLib?version=2.0'; import Soup from 'gi://Soup?version=3.0'; import Gio from 'gi://Gio?version=2.0'; -const { byteArray } = imports; const loop = GLib.MainLoop.new(null, false); +const textDecoder = new TextDecoder(); const session = new Soup.Session(); const message = new Soup.Message({ @@ -48,7 +48,7 @@ function websocket_connect_async_callback(_session: Soup.Session, res: Gio.Async if (type !== Soup.WebsocketDataType.TEXT) return; - const str = byteArray.toString(byteArray.fromGBytes(data)); + const str = textDecoder.decode(data.toArray()); log(`message: ${str}`); connection.close(Soup.WebsocketCloseCode.NORMAL, null); }); diff --git a/examples/gjs/soup-3-websocket/tsconfig.json b/examples/gjs/soup-3-websocket/tsconfig.json index 28f57b07d..c48863475 100644 --- a/examples/gjs/soup-3-websocket/tsconfig.json +++ b/examples/gjs/soup-3-websocket/tsconfig.json @@ -11,7 +11,7 @@ "noImplicitThis": true, "alwaysStrict": true, }, - "include": ["@types/gjs.d.ts"], + "include": ["@types/gjs.d.ts", "@types/dom.d.ts"], "files": [ "client.ts", ] diff --git a/examples/node-gtk/gtk-4-application/esbuild.mjs b/examples/node-gtk/gtk-4-application/esbuild.mjs new file mode 100644 index 000000000..48d5cbca5 --- /dev/null +++ b/examples/node-gtk/gtk-4-application/esbuild.mjs @@ -0,0 +1,14 @@ +import { build } from "esbuild"; + +await build({ + entryPoints: ['main.ts'], + outdir: 'dist', + bundle: true, + // target: "firefox60", // Since GJS 1.53.90 + // target: "firefox68", // Since GJS 1.63.90 + target: "firefox78", // Since GJS 1.65.90 + // target: "firefox91", // Since GJS 1.71.1 + format: 'cjs', + platform: 'node', + external: ['node-gtk', 'gi://*', 'resource://*', 'gettext', 'system', 'cairo'], +}) diff --git a/examples/node-gtk/gtk-4-application/main.ts b/examples/node-gtk/gtk-4-application/main.ts new file mode 100644 index 000000000..7f20b07b5 --- /dev/null +++ b/examples/node-gtk/gtk-4-application/main.ts @@ -0,0 +1,71 @@ +// Based on https://gitlab.gnome.org/GNOME/gjs/-/blob/master/examples/gtk-application.js + +import gi from 'node-gtk'; +import GLib from './@types/node-glib-2.0.js'; +import Gtk from './@types/node-gtk-4.0.js'; + +const printHello = () => console.log('Hello') + +const loop = GLib.MainLoop.new(null, false) +const app = new Gtk.Application('com.github.romgrk.node-gtk.demo', 0) +app.on('activate', onActivate) +const status = app.run([]) + +console.log('Finished with status:', status) + +function onActivate() { + const window = new Gtk.ApplicationWindow(app) + window.setTitle('Window') + window.setDefaultSize(200, 200) + window.on('close-request', onQuit) + + const ui = ` + + + + + vertical + + + 1 + Hello World! + + + + + Action + 1 + + + + + Close + 1 + + + + + ` + + const builder = Gtk.Builder.newFromString(ui, ui.length) + const root = builder.getObject('root') as Gtk.Box; + + const actionButton = builder.getObject('actionButton') + actionButton?.on('clicked', printHello) + + const closeButton = builder.getObject('closeButton') + closeButton?.on('clicked', () => window.close()) + + window.setChild(root) + window.show() + window.present() + + gi.startLoop() + loop.run() +} + +function onQuit() { + loop.quit() + app.quit() + return false +} \ No newline at end of file diff --git a/examples/node-gtk/gtk-4-application/package.json b/examples/node-gtk/gtk-4-application/package.json new file mode 100644 index 000000000..b18f9d38f --- /dev/null +++ b/examples/node-gtk/gtk-4-application/package.json @@ -0,0 +1,27 @@ +{ + "name": "ts-for-gir-node-gtk-gtk-4-application-example", + "version": "3.2.7", + "description": "Simple node-gtk Gtk 4 example app to demonstrate how you can use and extend Gtk.Application", + "main": "dist/main.js", + "private": true, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "build:types": "yarn ts-for-gir generate Gtk-4.0 -e node", + "build:app": "yarn node esbuild.mjs", + "build": "yarn build:types && yarn build:app", + "start:app": "yarn node dist/main.js", + "debug:app": "GTK_DEBUG=interactive yarn start:app", + "start": "yarn build && yarn start:app", + "validate": "yarn validate:types && yarn validate:app", + "validate:types": "tsc --project tsconfig.types.json", + "validate:app": "tsc --noEmit", + "clear": "rm -rf dist @types" + }, + "author": "Pascal Garber ", + "license": "MIT", + "devDependencies": { + "@ts-for-gir/cli": "workspace:^", + "esbuild": "^0.20.0", + "typescript": "5.2.2" + } +} diff --git a/examples/node-gtk/gtk-4-application/tsconfig.json b/examples/node-gtk/gtk-4-application/tsconfig.json new file mode 100644 index 000000000..0c8696d6c --- /dev/null +++ b/examples/node-gtk/gtk-4-application/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "lib": ["ESNext"], + "types": ["node"], + "target": "ESNext", + "module": "Node16", + "strict": true, + "allowSyntheticDefaultImports": true, + "esModuleInterop": true, + "noImplicitAny": true, + "strictNullChecks": true, + "noImplicitThis": true, + "alwaysStrict": true, + "moduleResolution":"Node16", + }, + "include": [ + "./@types/node-ambient.d.ts" + ], + "files": [ + "main.ts" + ] +} diff --git a/examples/node-gtk/gtk-4-application/tsconfig.types.json b/examples/node-gtk/gtk-4-application/tsconfig.types.json new file mode 100644 index 000000000..9f910a243 --- /dev/null +++ b/examples/node-gtk/gtk-4-application/tsconfig.types.json @@ -0,0 +1,6 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "noEmit": true + } +} diff --git a/packages/lib/src/gir-module.ts b/packages/lib/src/gir-module.ts index ec1097e82..6db7378fe 100644 --- a/packages/lib/src/gir-module.ts +++ b/packages/lib/src/gir-module.ts @@ -29,7 +29,7 @@ import { removeNamespace, addNamespace, girElementIsIntrospectable, - lowerCamelCase, + underscores, } from './utils.js' import { SymTable } from './symtable.js' import { LibraryVersion } from './library-version.js' @@ -1088,17 +1088,22 @@ export class GirModule { let name = girVar.$.name - switch (girTypeName) { - case 'property': - name = this.transformation.transformPropertyName(girVar.$.name, allowQuotes) - break - case 'constant': - name = this.transformation.transformConstantName(girVar.$.name, allowQuotes) - break - case 'field': - name = this.transformation.transformFieldName(girVar.$.name, allowQuotes) - break + if (tsTypeName === 'constructor-property') { + name = this.transformation.transformConstructorPropertyName(girVar.$.name, allowQuotes) + } else { + switch (girTypeName) { + case 'property': + name = this.transformation.transformPropertyName(girVar.$.name, allowQuotes) + break + case 'constant': + name = this.transformation.transformConstantName(girVar.$.name, allowQuotes) + break + case 'field': + name = this.transformation.transformFieldName(girVar.$.name, allowQuotes) + break + } } + // Use the out type because it can be a union which isn't appropriate // for a property const tsType = this.getTsType(girVar, tsClass, girTypeName, { optional, nullable, generics }) @@ -1786,6 +1791,8 @@ export class GirModule { return tsData } + // FIXME: https://github.com/gjsify/ts-for-gir/issues/145 + // FIXME: https://github.com/gjsify/ts-for-gir/issues/131 private getClassConstructPropsTsData( girClass: GirClassElement | GirUnionElement | GirInterfaceElement | GirRecordElement, constructPropNames: LocalNames, @@ -1830,12 +1837,12 @@ export class GirModule { } constructProps.push(girConstrProp) - // Add a new property with the name in upper camel case, see https://github.com/gjsify/ts-for-gir/issues/138 - const lowerCamelCaseProperty = this.clonePropertyForLowerCamelCase(girConstrProp) - if (lowerCamelCaseProperty && lowerCamelCaseProperty._fullSymName) { - girProperties.push(lowerCamelCaseProperty) - this.symTable.set(this.allDependencies, lowerCamelCaseProperty._fullSymName, lowerCamelCaseProperty) - } + // Clone property with the name in underscores, see https://github.com/gjsify/ts-for-gir/issues/138 + // const underscoresProperty = this.clonePropertyForUnderscores(girConstrProp) + // if (underscoresProperty && underscoresProperty._fullSymName) { + // girProperties.push(underscoresProperty) + // this.symTable.set(this.allDependencies, underscoresProperty._fullSymName, underscoresProperty) + // } } return constructProps @@ -2022,7 +2029,7 @@ export class GirModule { * @param girProperty * @returns */ - private clonePropertyForLowerCamelCase(girProperty: GirPropertyElement) { + private clonePropertyForUnderscores(girProperty: GirPropertyElement) { // Only for GJS if (this.config.environment === 'node') { return undefined @@ -2034,10 +2041,10 @@ export class GirModule { return undefined } - const lowerCamelCasePropertyName = lowerCamelCase(propertyName) + const underscoresPropertyName = underscores(propertyName) // Nothing has changed - if (lowerCamelCasePropertyName === propertyName) { + if (underscoresPropertyName === propertyName) { return undefined } @@ -2046,11 +2053,11 @@ export class GirModule { if (!upperCamelCaseGirProperty._tsData) { return undefined } - upperCamelCaseGirProperty._tsData.name = lowerCamelCasePropertyName + upperCamelCaseGirProperty._tsData.name = underscoresPropertyName upperCamelCaseGirProperty._fullSymName = upperCamelCaseGirProperty._fullSymName?.replace( propertyName, - lowerCamelCasePropertyName, + underscoresPropertyName, ) return upperCamelCaseGirProperty @@ -2080,25 +2087,25 @@ export class GirModule { } } - // Add a new property with the name in upper camel case, see https://github.com/gjsify/ts-for-gir/issues/138 - { - const lowerCamelCaseProperty = this.clonePropertyForLowerCamelCase(girProperty) - if (lowerCamelCaseProperty) { - const localName = this.checkOrSetLocalName(lowerCamelCaseProperty, localNames, 'property') - - if (localName?.added && localName.property) { - if (lowerCamelCaseProperty._fullSymName) { - this.symTable.set( - this.allDependencies, - lowerCamelCaseProperty._fullSymName, - lowerCamelCaseProperty, - ) - } - - girProperties.push(localName.property) - } - } - } + // Add a new property with the name in lower camel case, see https://github.com/gjsify/ts-for-gir/issues/138 + // { + // const underscoresProperty = this.clonePropertyForUnderscores(girProperty) + // if (underscoresProperty) { + // const localName = this.checkOrSetLocalName(underscoresProperty, localNames, 'property') + + // if (localName?.added && localName.property) { + // if (underscoresProperty._fullSymName) { + // this.symTable.set( + // this.allDependencies, + // underscoresProperty._fullSymName, + // underscoresProperty, + // ) + // } + + // girProperties.push(localName.property) + // } + // } + // } } } return girProperties diff --git a/packages/lib/src/transformation.ts b/packages/lib/src/transformation.ts index d5b9da3bd..b7e5706a7 100644 --- a/packages/lib/src/transformation.ts +++ b/packages/lib/src/transformation.ts @@ -291,14 +291,21 @@ export class Transformation { transformation: 'original', }, }, - // GJS always re-writes - to _ (I think?) propertyName: { node: { transformation: 'lowerCamelCase', }, gjs: { + transformation: 'lowerCamelCase', + }, + }, + constructorPropertyName: { + node: { transformation: 'underscores', }, + gjs: { + transformation: 'lowerCamelCase', + }, }, parameterName: { node: { @@ -466,6 +473,19 @@ export class Transformation { return name } + public transformConstructorPropertyName(name: string, allowQuotes: boolean): string { + name = this.transform('constructorPropertyName', name) + const originalName = `${name}` + + name = this.transformReservedVariableNames(name, allowQuotes) + name = this.transformNumericName(name, allowQuotes) + + if (originalName !== name) { + this.log.warn(WARN_RENAMED_PROPERTY(originalName, name)) + } + return name + } + public transformConstantName(name: string, allowQuotes: boolean): string { name = this.transform('constantName', name) const originalName = `${name}` diff --git a/packages/lib/src/types/construct-name.ts b/packages/lib/src/types/construct-name.ts index c658f5bed..98e55adf1 100644 --- a/packages/lib/src/types/construct-name.ts +++ b/packages/lib/src/types/construct-name.ts @@ -4,6 +4,7 @@ export type ConstructName = | 'signalName' | 'fieldName' | 'constantName' + | 'constructorPropertyName' | 'propertyName' | 'parameterName' | 'enumName' diff --git a/types b/types index 0c4a0fc4c..0037de12d 160000 --- a/types +++ b/types @@ -1 +1 @@ -Subproject commit 0c4a0fc4cb5db62b0dba5f18ab25b90b499df00c +Subproject commit 0037de12df188dacedef4c006617ea21a966df34 diff --git a/yarn.lock b/yarn.lock index 19394ab97..2a95c30c8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -21045,16 +21045,6 @@ __metadata: languageName: unknown linkType: soft -"ts-for-gir-gtk-3-application-example@workspace:examples/gjs/gtk-3-application": - version: 0.0.0-use.local - resolution: "ts-for-gir-gtk-3-application-example@workspace:examples/gjs/gtk-3-application" - dependencies: - "@ts-for-gir/cli": "workspace:^" - esbuild: "npm:^0.20.0" - typescript: "npm:5.2.2" - languageName: unknown - linkType: soft - "ts-for-gir-gtk-3-browser-example@workspace:examples/gjs/gtk-3-browser": version: 0.0.0-use.local resolution: "ts-for-gir-gtk-3-browser-example@workspace:examples/gjs/gtk-3-browser" @@ -21158,6 +21148,16 @@ __metadata: languageName: unknown linkType: soft +"ts-for-gir-gtk-4-application-example@workspace:examples/gjs/gtk-4-application": + version: 0.0.0-use.local + resolution: "ts-for-gir-gtk-4-application-example@workspace:examples/gjs/gtk-4-application" + dependencies: + "@ts-for-gir/cli": "workspace:^" + esbuild: "npm:^0.20.0" + typescript: "npm:5.2.2" + languageName: unknown + linkType: soft + "ts-for-gir-gtk-4-list-store-example@workspace:examples/gjs/gtk-4-list-store": version: 0.0.0-use.local resolution: "ts-for-gir-gtk-4-list-store-example@workspace:examples/gjs/gtk-4-list-store" @@ -21290,6 +21290,16 @@ __metadata: languageName: unknown linkType: soft +"ts-for-gir-node-gtk-gtk-4-application-example@workspace:examples/node-gtk/gtk-4-application": + version: 0.0.0-use.local + resolution: "ts-for-gir-node-gtk-gtk-4-application-example@workspace:examples/node-gtk/gtk-4-application" + dependencies: + "@ts-for-gir/cli": "workspace:^" + esbuild: "npm:^0.20.0" + typescript: "npm:5.2.2" + languageName: unknown + linkType: soft + "ts-for-gir-node-gtk-gtk-4-custom-widget-example@workspace:examples/node-gtk/gtk-4-custom-widget": version: 0.0.0-use.local resolution: "ts-for-gir-node-gtk-gtk-4-custom-widget-example@workspace:examples/node-gtk/gtk-4-custom-widget"