janela 0.14.0 → 0.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/README.md +10 -2
  2. package/api/index.d.ts +11 -1
  3. package/bin/janela.mjs +33 -2
  4. package/package.json +1 -1
  5. package/templates/index.html +224 -53
  6. package/templates/janela.conf.json +2 -2
  7. package/templates/react/files/index.html +1 -0
  8. package/templates/react/files/janela.conf.json +2 -2
  9. package/templates/react/files/src/App.tsx +154 -37
  10. package/templates/react/files/src/main.tsx +1 -0
  11. package/templates/react/files/src/styles.css +137 -0
  12. package/templates/react/files/src-host/main.ts +82 -0
  13. package/templates/solid/files/index.html +1 -0
  14. package/templates/solid/files/janela.conf.json +2 -2
  15. package/templates/solid/files/src/App.tsx +145 -35
  16. package/templates/solid/files/src/main.tsx +1 -0
  17. package/templates/solid/files/src/styles.css +137 -0
  18. package/templates/solid/files/src-host/main.ts +82 -0
  19. package/templates/svelte/files/index.html +1 -0
  20. package/templates/svelte/files/janela.conf.json +2 -2
  21. package/templates/svelte/files/src/App.svelte +116 -27
  22. package/templates/svelte/files/src/main.ts +1 -0
  23. package/templates/svelte/files/src/styles.css +137 -0
  24. package/templates/svelte/files/src-host/main.ts +82 -0
  25. package/templates/vue/files/index.html +1 -0
  26. package/templates/vue/files/janela.conf.json +2 -2
  27. package/templates/vue/files/src/App.vue +99 -20
  28. package/templates/vue/files/src/main.ts +1 -0
  29. package/templates/vue/files/src/styles.css +137 -0
  30. package/templates/vue/files/src-host/main.ts +82 -0
  31. package/templates/react/files/src/App.css +0 -3
  32. package/templates/solid/files/src/App.css +0 -3
@@ -7,41 +7,120 @@ import type { App } from "../src-host/main";
7
7
 
8
8
  const client = createClient<App>();
9
9
 
10
- const greeting = ref("…");
10
+ const greeting = ref("booting…");
11
11
  const a = ref(2);
12
12
  const b = ref(40);
13
- const sum = ref<number | null>(null);
13
+ const sum = ref("—");
14
+ const waited = ref("—");
15
+ const waiting = ref(false);
14
16
  const events = ref<string[]>([]);
17
+ const path = ref("janela.conf.json");
18
+ const title = ref("__NAME__ — renamed");
19
+ const fileOut = ref("—");
15
20
 
16
21
  // The event name is checked against the contract and `value` is inferred.
17
22
  // `on` returns a disposer.
18
- const off = client.on("added", (value) => events.value.unshift(`host emitted: ${value}`));
23
+ const off = client.on("added", (value) => events.value.unshift(`host emitted 'added': ${value}`));
19
24
  onUnmounted(off);
20
25
 
21
26
  onMounted(async () => {
22
27
  greeting.value = await client.invoke("greet", { name: "__NAME__" });
28
+ await client.invoke("log", "page loaded");
23
29
  });
24
30
 
25
31
  async function add() {
26
- sum.value = await client.invoke("add", { a: Number(a.value), b: Number(b.value) });
32
+ const [x, y] = [Number(a.value), Number(b.value)];
33
+ sum.value = `add(${x}, ${y}) → ${await client.invoke("add", { a: x, b: y })}`;
34
+ }
35
+
36
+ async function wait() {
37
+ waiting.value = true;
38
+ waited.value = "waiting… try add, it still answers";
39
+ waited.value = await client.invoke("wait", { ms: 2000 });
40
+ waiting.value = false;
41
+ }
42
+
43
+ async function read() {
44
+ const r = await client.invoke("readFile", { path: path.value });
45
+ fileOut.value = r.ok ? `${path.value} — ${r.length} chars\n\n${r.text.slice(0, 400)}` : `error: ${r.error}`;
46
+ }
47
+
48
+ // The native open dialog. The window keeps serving other commands while the
49
+ // user is deciding.
50
+ async function pick() {
51
+ const r = await client.invoke("openFile");
52
+ if (!r.ok) fileOut.value = `error: ${r.error}`;
53
+ else if (r.cancelled) fileOut.value = "cancelled";
54
+ else fileOut.value = `${r.path} — ${r.length} chars\n\n${r.text.slice(0, 400)}`;
27
55
  }
28
56
  </script>
29
57
 
30
58
  <template>
31
- <h1>{{ greeting }}</h1>
32
- <p>
33
- <input v-model="a" type="number" /> +
34
- <input v-model="b" type="number" />
35
- <button @click="add">add</button>
36
- <span v-if="sum !== null"> = {{ sum }}</span>
37
- </p>
38
- <ul>
39
- <li v-for="(e, i) in events" :key="i">{{ e }}</li>
40
- </ul>
41
- </template>
59
+ <main>
60
+ <header>
61
+ <div class="mark" aria-hidden="true">
62
+ <div class="bar"><i /><i /><i /></div>
63
+ <div class="pane" />
64
+ </div>
65
+ <h1>__NAME__</h1>
66
+ <p class="sub">TypeScript, compiled to a native binary. No Rust, no Node, no Electron.</p>
67
+ </header>
68
+
69
+ <p class="greeting">{{ greeting }}</p>
70
+
71
+ <div class="grid">
72
+ <section class="card">
73
+ <h2>Typed commands</h2>
74
+ <p class="why">
75
+ The name, the arguments and the result are all checked at compile time — no codegen,
76
+ because both sides are TypeScript.
77
+ </p>
78
+ <div class="row">
79
+ <input v-model="a" type="number" aria-label="first number" />
80
+ <span aria-hidden="true">+</span>
81
+ <input v-model="b" type="number" aria-label="second number" />
82
+ <button class="primary" @click="add">add</button>
83
+ </div>
84
+ <p class="result" :class="{ filled: sum !== '—' }">{{ sum }}</p>
85
+ <h3>Events from the host</h3>
86
+ <ul class="events">
87
+ <li v-for="(e, i) in events" :key="i">{{ e }}</li>
88
+ </ul>
89
+ </section>
42
90
 
43
- <style>
44
- body { font: 16px -apple-system, system-ui, sans-serif; padding: 2rem; }
45
- input { width: 5em; }
46
- ul { color: #666; font-size: 13px; }
47
- </style>
91
+ <section class="card">
92
+ <h2>Async without blocking</h2>
93
+ <p class="why">
94
+ The window keeps answering while this is pending: the dot keeps moving and
95
+ <strong>add</strong> still works.
96
+ </p>
97
+ <div class="row">
98
+ <button :disabled="waiting" @click="wait">wait 2s</button>
99
+ <span v-if="waiting"><span class="pulse" /></span>
100
+ </div>
101
+ <p class="result" :class="{ filled: waited !== '—' }">{{ waited }}</p>
102
+ </section>
103
+
104
+ <section class="card wide">
105
+ <h2>Files and the window</h2>
106
+ <p class="why">
107
+ Reads run on a worker thread, so a large file never freezes the window. The dialog is
108
+ the real native one.
109
+ </p>
110
+ <div class="row">
111
+ <input v-model="path" class="grow" type="text" aria-label="file to read" />
112
+ <button @click="read">read</button>
113
+ <button @click="pick">pick a file…</button>
114
+ <input v-model="title" class="grow" type="text" aria-label="window title" />
115
+ <button @click="client.invoke('setTitle', { title })">set title</button>
116
+ </div>
117
+ <pre class="result" :class="{ filled: fileOut !== '—' }">{{ fileOut }}</pre>
118
+ </section>
119
+ </div>
120
+
121
+ <footer>
122
+ <span>Edit <code>src/App.vue</code> for the page and <code>src-host/main.ts</code> for the commands.</span>
123
+ <button @click="client.invoke('quit')">Quit</button>
124
+ </footer>
125
+ </main>
126
+ </template>
@@ -1,3 +1,4 @@
1
+ import "./styles.css";
1
2
  import { createApp } from "vue";
2
3
  import App from "./App.vue";
3
4
 
@@ -0,0 +1,137 @@
1
+ /* The janela starter's look. Shared by every template so a project scaffolded
2
+ with --template vue looks the same as one scaffolded without a framework.
3
+ All of it is yours to replace.
4
+
5
+ The window mark in App is drawn in CSS rather than shipped as an image: a
6
+ janela frontend is embedded in the binary, so every asset has to be inlined
7
+ anyway, and a window needs no artwork. */
8
+
9
+ :root {
10
+ color-scheme: light dark;
11
+ --bg: #f6f6f7;
12
+ --card: #ffffff;
13
+ --text: #18181b;
14
+ --muted: #71717a;
15
+ --line: #e4e4e7;
16
+ --accent: #2563eb;
17
+ --accent-soft: #eff4ff;
18
+ --shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 8px 24px -12px rgb(0 0 0 / 0.12);
19
+ }
20
+ @media (prefers-color-scheme: dark) {
21
+ :root {
22
+ --bg: #17171a;
23
+ --card: #202024;
24
+ --text: #fafafa;
25
+ --muted: #a1a1aa;
26
+ --line: #2e2e34;
27
+ --accent: #60a5fa;
28
+ --accent-soft: #1d2433;
29
+ --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 8px 24px -12px rgb(0 0 0 / 0.5);
30
+ }
31
+ }
32
+
33
+ * { box-sizing: border-box; }
34
+ body {
35
+ margin: 0;
36
+ min-height: 100vh;
37
+ display: flex;
38
+ justify-content: center;
39
+ background: var(--bg);
40
+ color: var(--text);
41
+ font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
42
+ -webkit-font-smoothing: antialiased;
43
+ }
44
+ main { width: 100%; max-width: 760px; padding: 26px 22px 32px; }
45
+
46
+ /* --- header ------------------------------------------------------- */
47
+ header { text-align: center; margin-bottom: 20px; }
48
+ .mark {
49
+ width: 56px; height: 44px; margin: 0 auto 14px;
50
+ border: 2px solid var(--text); border-radius: 8px;
51
+ display: flex; flex-direction: column; overflow: hidden;
52
+ }
53
+ .mark .bar {
54
+ height: 13px; flex: none;
55
+ border-bottom: 2px solid var(--text);
56
+ display: flex; align-items: center; gap: 3px; padding: 0 4px;
57
+ }
58
+ .mark .bar i { width: 3px; height: 3px; border-radius: 50%; background: var(--text); }
59
+ .mark .pane { flex: 1; background: linear-gradient(135deg, var(--accent), transparent 70%); opacity: 0.85; }
60
+ h1 { margin: 0; font-size: 26px; letter-spacing: -0.02em; font-weight: 650; }
61
+ .sub { margin: 5px 0 0; color: var(--muted); font-size: 13.5px; }
62
+
63
+ /* The one line that proves the whole stack: this string was produced by
64
+ TypeScript compiled to machine code, and handed to the page. */
65
+ .greeting {
66
+ margin: 0 0 20px;
67
+ padding: 11px 16px;
68
+ border: 1px solid var(--line);
69
+ border-radius: 10px;
70
+ background: var(--accent-soft);
71
+ font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
72
+ text-align: center;
73
+ overflow-wrap: anywhere;
74
+ }
75
+
76
+ /* --- cards -------------------------------------------------------- */
77
+ /* auto-fit rather than a fixed column count: the same page is the iOS and
78
+ Android frontend, where it must read as a single column. */
79
+ .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 13px; }
80
+ .card {
81
+ background: var(--card);
82
+ border: 1px solid var(--line);
83
+ border-radius: 12px;
84
+ padding: 15px 17px;
85
+ box-shadow: var(--shadow);
86
+ }
87
+ .card h2 {
88
+ margin: 0 0 2px;
89
+ font-size: 13px; font-weight: 600;
90
+ letter-spacing: 0.04em; text-transform: uppercase;
91
+ color: var(--muted);
92
+ }
93
+ .card.wide { grid-column: 1 / -1; }
94
+ .card p.why { margin: 0 0 11px; color: var(--muted); font-size: 12.5px; line-height: 1.45; }
95
+ .card h3 {
96
+ margin: 16px 0 6px; font-size: 11px; font-weight: 600;
97
+ letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted);
98
+ }
99
+ .row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
100
+
101
+ input, button {
102
+ font: inherit;
103
+ border-radius: 8px;
104
+ border: 1px solid var(--line);
105
+ padding: 8px 12px;
106
+ background: var(--card);
107
+ color: var(--text);
108
+ transition: border-color 0.15s, background 0.15s;
109
+ }
110
+ input { min-width: 0; }
111
+ input[type="number"] { width: 5.5em; text-align: center; }
112
+ input.grow { flex: 1 1 12em; }
113
+ input:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
114
+ button { cursor: pointer; font-weight: 500; }
115
+ button:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
116
+ button:disabled { opacity: 0.5; cursor: default; }
117
+ button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
118
+ button.primary:hover:not(:disabled) { filter: brightness(1.08); color: #fff; }
119
+
120
+ .result { margin: 12px 0 0; font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--muted); }
121
+ .result.filled { color: var(--text); }
122
+ pre.result { white-space: pre-wrap; overflow-wrap: anywhere; max-height: 11em; overflow: auto; margin-top: 12px; }
123
+
124
+ /* A dot that keeps moving while a 2s command is pending. If it stalls,
125
+ the host has blocked the UI thread — which is the bug this proves is
126
+ absent. */
127
+ .pulse { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: pulse 1s ease-in-out infinite; }
128
+ @keyframes pulse { 0%, 100% { opacity: 0.25; transform: scale(0.8); } 50% { opacity: 1; transform: scale(1.15); } }
129
+ @media (prefers-reduced-motion: reduce) { .pulse { animation: none; opacity: 1; } }
130
+
131
+ ul.events { list-style: none; margin: 0; padding: 0; font: 13px/1.6 ui-monospace, SFMono-Regular, Menlo, monospace; }
132
+ ul.events li { padding: 3px 0; border-bottom: 1px dashed var(--line); }
133
+ ul.events li:last-child { border-bottom: 0; }
134
+ ul.events:empty::after { content: "nothing yet — press add"; color: var(--muted); font-family: inherit; }
135
+
136
+ footer { margin-top: 18px; display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: center; color: var(--muted); font-size: 13px; }
137
+ footer code { background: var(--card); border: 1px solid var(--line); border-radius: 5px; padding: 1px 5px; font-size: 12px; }
@@ -17,9 +17,29 @@ export type AppCommands = {
17
17
  greet: (args: { name: string }) => string;
18
18
  log: (args: string) => void;
19
19
  wait: (args: { ms: number }) => string;
20
+ readFile: (args: { path: string }) => ReadResult;
21
+ openFile: () => PickResult;
22
+ setTitle: (args: { title: string }) => void;
20
23
  quit: () => void;
21
24
  };
22
25
 
26
+ /**
27
+ * Results are declared as TOTAL records rather than unions of shapes: every
28
+ * field is always present, and `error` is null on success. A discriminated
29
+ * union would read better in isolation, but the page would then have to narrow
30
+ * before touching `text` — and the point of this template is to show the type
31
+ * edge working, not to teach narrowing.
32
+ */
33
+ export type ReadResult = { ok: boolean; error: string | null; length: number; text: string };
34
+ export type PickResult = {
35
+ ok: boolean;
36
+ error: string | null;
37
+ cancelled: boolean;
38
+ path: string;
39
+ length: number;
40
+ text: string;
41
+ };
42
+
23
43
  /** Every event this app emits, and what each one carries. */
24
44
  export type AppEvents = {
25
45
  added: number;
@@ -54,6 +74,68 @@ export function setup(app: App): void {
54
74
  });
55
75
  });
56
76
 
77
+ // File I/O without freezing the window: the read happens on a worker thread
78
+ // in the shim. Use this instead of node:fs readFileSync, which blocks the
79
+ // host loop — and with it the whole UI — until the syscall returns.
80
+ app.commandAsync("readFile", (args, resolve) => {
81
+ app.readFileAsync(args.path, (err, text) => {
82
+ // Annotated, not inferred: a bare `null` in a returned literal infers as
83
+ // `null | undefined`, which does not lift into the declared
84
+ // `string | null` (scriptc SC2002).
85
+ const answer: ReadResult =
86
+ err !== null
87
+ ? { ok: false, error: err, length: 0, text: "" }
88
+ : { ok: true, error: null, length: text.length, text: text };
89
+ resolve(answer);
90
+ });
91
+ });
92
+
93
+ // The native "open" dialog, paired with the reader above — picking a file is
94
+ // what makes readFileAsync useful. commandAsync is the right shape here: the
95
+ // page's promise stays parked while the user takes as long as they like, and
96
+ // the window carries on serving other calls meanwhile.
97
+ app.commandAsync("openFile", (_args, resolve) => {
98
+ app.openFileDialog(
99
+ { title: "Pick a file", filters: [{ name: "Text", extensions: ["txt", "md"] }] },
100
+ (paths, err) => {
101
+ if (err !== undefined) {
102
+ const failed: PickResult = {
103
+ ok: false, error: err, cancelled: false, path: "", length: 0, text: "",
104
+ };
105
+ resolve(failed);
106
+ return;
107
+ }
108
+ if (paths === null) {
109
+ const cancelled: PickResult = {
110
+ ok: true, error: null, cancelled: true, path: "", length: 0, text: "",
111
+ };
112
+ resolve(cancelled);
113
+ return;
114
+ }
115
+ app.readFileAsync(paths[0], (rerr, text) => {
116
+ const answer: PickResult =
117
+ rerr !== null
118
+ ? { ok: false, error: rerr, cancelled: false, path: paths[0], length: 0, text: "" }
119
+ : {
120
+ ok: true,
121
+ error: null,
122
+ cancelled: false,
123
+ path: paths[0],
124
+ length: text.length,
125
+ text: text,
126
+ };
127
+ resolve(answer);
128
+ });
129
+ },
130
+ );
131
+ });
132
+
133
+ // The window is yours to change at runtime, not just at startup.
134
+ app.command("setTitle", (args) => {
135
+ app.setTitle(args.title);
136
+ return null;
137
+ });
138
+
57
139
  app.command("quit", (_args) => {
58
140
  app.quit();
59
141
  return null;
@@ -1,3 +0,0 @@
1
- body { font: 16px -apple-system, system-ui, sans-serif; padding: 2rem; }
2
- input { width: 5em; }
3
- ul { color: #666; font-size: 13px; }
@@ -1,3 +0,0 @@
1
- body { font: 16px -apple-system, system-ui, sans-serif; padding: 2rem; }
2
- input { width: 5em; }
3
- ul { color: #666; font-size: 13px; }