Skip to content

Commit e4b515e

Browse files
sonnypvixalien
andauthored
Add TypeScript types and port Welcome demo (#227)
See also #201 --------- Co-authored-by: Angelo Verlain <[email protected]>
1 parent 3f500a2 commit e4b515e

File tree

131 files changed

+1297335
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+1297335
-0
lines changed

Diff for: .gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ __pycache__
1919
.idea
2020
.vscode
2121

22+
# compiled typescript
23+
out
24+
25+
# extra GIRs from Workbench
26+
workbench-girs
27+
2228
# Project files - sync with Makefile
2329
src/*/settings
2430
src/*/workbench.vala

Diff for: README.typescript.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Demos TypeScript
2+
3+
Here is how you generate TypeScript declaration files for the various platform
4+
GIRs (Gtk4, Adw, GObject, etc..) and other dependencies used by Workbench
5+
(Libportal/Xdp, Jsonrpc, Shumate, Vte, etc..).
6+
7+
Due to current limitations
8+
(https://github.com/workbenchdev/Workbench/issues/980), you cannot directly
9+
generate the types from within Workbench's sandbox automatically.
10+
11+
### 0. Copy the built GIRs from Workbench
12+
13+
First enter the Build Terminal. On VSCode you can easily do this by pressing
14+
`Ctrl+P` then selecting `Flatpak: Enter Build Terminal`.
15+
16+
You can also enter Workbench Terminal using the following command:
17+
18+
```bash
19+
flatpak run --command=bash --filesystem=$(pwd) re.sonny.Workbench.Devel
20+
```
21+
22+
Then you can copy all the GIRs build from Workbench
23+
24+
```bash
25+
mkdir -p workbench-girs
26+
cp /app/share/gir-1.0/* workbench-girs
27+
```
28+
29+
### 1. Enter the flatpak sandbox
30+
31+
```bash
32+
FLATPAK_ENABLE_SDK_EXT=node20,typescript flatpak run --share=network --command=bash --filesystem=$(pwd) org.gnome.Sdk//master
33+
34+
# Enable the node20 and typescript SDK extensions
35+
source /usr/lib/sdk/node20/enable.sh
36+
export PATH=/usr/lib/sdk/typescript/bin:$PATH
37+
```
38+
39+
### 2. Install ts-for-gir (in the flatpak sandbox)
40+
41+
```bash
42+
YARN_GLOBAL_DIR=/tmp/yarn-global
43+
export PATH="$YARN_GLOBAL_DIR/node_modules/.bin:$PATH"
44+
45+
yarn --global-folder $YARN_GLOBAL_DIR global add @ts-for-gir/[email protected]
46+
```
47+
48+
### Generate modules
49+
50+
```bash
51+
ts-for-gir generate -g workbench-girs/ -g /usr/share/gir-1.0/ -o workbench-types/ --ignoreVersionConflicts
52+
```

Diff for: ambient/workbench.d.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path="../workbench-types/adw-1.d.ts" />
2+
/// <reference path="../workbench-types/gtk-4.0.d.ts" />
3+
/// <reference path="../workbench-types/gobject-2.0.d.ts" />
4+
5+
import Gtk from "gi://Gtk?version=4.0";
6+
import Adw from "gi://Adw";
7+
import GObject from "gi://GObject";
8+
9+
declare global {
10+
// global workbench object
11+
declare const workbench: {
12+
window: Adw.ApplicationWindow;
13+
application: Adw.Application;
14+
builder: Gtk.Builder;
15+
template: string;
16+
resolve(path: string): string;
17+
preview(object: Gtk.Widget): void;
18+
build(params: Record<string, Function | GObject.Object>): void;
19+
};
20+
}

Diff for: src/Welcome/main.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Adw from "gi://Adw";
2+
import Gio from "gi://Gio";
3+
import Gtk from "gi://Gtk?version=4.0";
4+
5+
Gio._promisify(Adw.AlertDialog.prototype, "choose", "choose_finish");
6+
7+
const box = workbench.builder.get_object<Gtk.Box>("subtitle");
8+
9+
const button = new Gtk.Button({
10+
label: "Press me",
11+
margin_top: 6,
12+
css_classes: ["suggested-action"],
13+
});
14+
button.connect("clicked", () => {
15+
greet().catch(console.error);
16+
});
17+
box.append(button);
18+
19+
console.log("Welcome to Workbench!");
20+
21+
async function greet() {
22+
const dialog = new Adw.AlertDialog({
23+
body: "Hello World!",
24+
});
25+
26+
dialog.add_response("ok", "OK");
27+
28+
const response = await dialog.choose(workbench.window, null);
29+
console.log(response);
30+
}

Diff for: tsconfig.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"module": "ES2022",
4+
"moduleResolution": "Bundler",
5+
"target": "ES2022",
6+
"outDir": "out",
7+
"skipLibCheck": true,
8+
"lib": ["ES2022"]
9+
},
10+
"include": [
11+
"src/**/*.ts",
12+
"./workbench-types/index.d.ts",
13+
"./ambient/workbench.d.ts",
14+
]
15+
}

0 commit comments

Comments
 (0)