Skip to content

Commit 83ce03d

Browse files
committed
Add new pure TypeScript compiler example, fixes #183
1 parent 51d36e2 commit 83ce03d

File tree

20 files changed

+262
-36
lines changed

20 files changed

+262
-36
lines changed

examples/README.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,24 +173,24 @@ cd /examples/gtk-4-custom-widget
173173
yarn start
174174
```
175175

176-
## Gtk4 Template
177-
![gtk-4-template](gtk-4-template/preview.png)
176+
## Gtk4 Template (esbuild)
177+
![gtk-4-template-esbuild](gtk-4-template-esbuild/preview.png)
178178

179-
This example shows how to use the Gtk4 template engine to create a custom widget.
179+
This example shows how to use the Gtk4 template engine to create a custom widget. It's also showing how to load the template `.ui` and style `.css` files from the file system using ESBuild.
180180

181-
Source: [gtk-4-template](gtk-4-template)
181+
Source: [gtk-4-template-esbuild](gtk-4-template-esbuild)
182182
Bundler: ESBuild
183183

184184
Build and run:
185185
```bash
186-
cd /examples/gtk-4-template
186+
cd /examples/gtk-4-template-esbuild
187187
yarn start
188188
```
189189

190190
## Gtk4 Template (Vite)
191191
![gtk-4-template-vite](gtk-4-template-vite/preview.png)
192192

193-
This example is largely identical to the example above, but uses Vite to inject the template `.ui` file into the source code.
193+
This example is largely identical to the example above, but uses Vite to inject the template `.ui` and style `.css` files into the source code.
194194

195195
Source: [gtk-4-template-vite](gtk-4-template-vite)
196196
Bundler: Vite
@@ -199,4 +199,18 @@ Build and run:
199199
```bash
200200
cd /examples/gtk-4-template-vite
201201
yarn start
202+
```
203+
204+
## Gtk4 Template (TSC)
205+
![gtk-4-template-tsc](gtk-4-template-tsc/preview.png)
206+
207+
This example is largely identical to the examples above, but uses the TypeScript compiler to compile the source code. No assets can be resolved with the TypeScript compiler, so this example shows how this can be done on runtime.
208+
209+
Source: [gtk-4-template-tsc](gtk-4-template-tsc)
210+
Bundler: TSC
211+
212+
Build and run:
213+
```bash
214+
cd /examples/gtk-4-template-tsc
215+
yarn start
202216
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
declare module '*.ui' {
2+
const content: string
3+
export default content
4+
}
5+
6+
declare module '*.css' {
7+
const content: string
8+
export default content
9+
}

examples/gtk-4-template/esbuild.js renamed to examples/gtk-4-template-esbuild/esbuild.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ await build({
1212
target: "firefox115", // Since GJS 1.77.2
1313
format: 'esm',
1414
external: ['gi://*', 'resource://*', 'gettext', 'system', 'cairo'],
15+
loader: {
16+
'.css': 'text',
17+
'.ui': 'text',
18+
},
1519
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
window {
2+
background-color: green;
3+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env gjs -m
2+
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
3+
// SPDX-FileCopyrightText: 2021 Andy Holmes <[email protected]>
4+
// Based on https://gitlab.gnome.org/GNOME/gjs/-/blob/master/examples/gtk4-template.js
5+
6+
import GObject from 'gi://GObject?version=2.0';
7+
import GLib from 'gi://GLib?version=2.0';
8+
import Gtk from 'gi://Gtk?version=4.0';
9+
import Gdk from 'gi://Gdk?version=4.0';
10+
11+
import Template from './gtk4-template.ui';
12+
import Style from './gtk4-template.css';
13+
14+
Gtk.init();
15+
16+
const ExampleWindow = GObject.registerClass({
17+
GTypeName: 'ExampleWindow',
18+
Template: Template,
19+
Children: [
20+
'box',
21+
],
22+
InternalChildren: [
23+
'button',
24+
],
25+
}, class ExampleWindow extends Gtk.Window {
26+
27+
box: Gtk.Box | null = null;
28+
_button: Gtk.Button | null = null;
29+
30+
constructor() {
31+
super();
32+
33+
// The template has been initialized and you can access the children
34+
if (this.box) this.box.visible = true;
35+
36+
// Internal children are set on the instance prefixed with a `_`
37+
if (this._button) this._button.visible = true;
38+
39+
this.initStyles()
40+
}
41+
42+
// The signal handler bound in the UI file
43+
_onButtonClicked(button: Gtk.Button) {
44+
if (this instanceof Gtk.Window)
45+
log('Callback scope is bound to `ExampleWindow`');
46+
47+
button.label = 'Button was clicked!';
48+
}
49+
50+
/** Load the stylesheet in a CssProvider and add it to the Gtk.StyleContext */
51+
protected initStyles() {
52+
const provider = new Gtk.CssProvider();
53+
console.log("Style", Style)
54+
provider.load_from_string(Style)
55+
const display = Gdk.Display.get_default()
56+
57+
if (!display) {
58+
console.error('No display found')
59+
return
60+
}
61+
62+
Gtk.StyleContext.add_provider_for_display(
63+
display,
64+
provider,
65+
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
66+
);
67+
}
68+
});
69+
70+
71+
// Create a window that stops the program when it is closed
72+
const loop = GLib.MainLoop.new(null, false);
73+
74+
const win = new ExampleWindow();
75+
win.connect('close-request', () => loop.quit());
76+
win.present();
77+
78+
loop.run();
79+

examples/gtk-4-template/package.json renamed to examples/gtk-4-template-esbuild/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@ts-for-gir-example/gtk-4-template",
2+
"name": "@ts-for-gir-example/gtk-4-template-esbuild",
33
"version": "4.0.0-beta.10",
44
"description": "Simple GJS Gtk 4 example app to demonstrate how you can use .ui template XML files over GJS itself",
55
"type": "module",
5.27 KB
Loading
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["ESNext"],
4+
"types": ["@girs/gjs", "@girs/gjs/dom", "@girs/gio-2.0", "@girs/glib-2.0", "@girs/gtk-4.0"],
5+
"target": "ESNext",
6+
"module": "ESNext",
7+
"moduleResolution": "bundler",
8+
"strict": true,
9+
"noImplicitAny": true,
10+
"strictNullChecks": true,
11+
"noImplicitThis": true,
12+
"alwaysStrict": true,
13+
},
14+
"files": [
15+
"main.ts",
16+
],
17+
"include": ["env.d.ts"]
18+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
window {
2+
background-color: yellow;
3+
}

0 commit comments

Comments
 (0)