-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathwindow.js
More file actions
535 lines (449 loc) · 13.8 KB
/
window.js
File metadata and controls
535 lines (449 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
import Gtk from "gi://Gtk";
import GLib from "gi://GLib";
import GObject from "gi://GObject";
import Gio from "gi://Gio";
import Adw from "gi://Adw";
import Vte from "gi://Vte";
import * as xml from "./langs/xml/xml.js";
import { buildRuntimePath, languages } from "./util.js";
import Document from "./Document.js";
import PanelUI from "./PanelUI.js";
import PanelCode from "./PanelCode.js";
import PanelStyle from "./PanelStyle.js";
import Devtools from "./Devtools.js";
import { format as prettier } from "./lib/prettier.js";
import prettier_babel from "./lib/prettier-babel.js";
import prettier_postcss from "./lib/prettier-postcss.js";
import prettier_estree from "./lib/prettier-estree.js";
import Previewer from "./Previewer/Previewer.js";
import ValaCompiler from "./langs/vala/Compiler.js";
import RustCompiler from "./langs/rust/Compiler.js";
import ThemeSelector from "../troll/src/widgets/ThemeSelector.js";
import resource from "./window.blp";
import "./icons/re.sonny.Workbench-beaker.svg" with { type: "icon" };
import "./icons/re.sonny.Workbench-code-symbolic.svg" with { type: "icon" };
import "./icons/re.sonny.Workbench-placeholder-symbolic.svg" with { type: "icon" };
import "./icons/re.sonny.Workbench-preview-symbolic.svg" with { type: "icon" };
import "./icons/re.sonny.Workbench-ui-symbolic.svg" with { type: "icon" };
import "./icons/re.sonny.Workbench-screenshot-symbolic.svg" with { type: "icon" };
import "./widgets/Modal.js";
import "./widgets/CodeView.js";
import { deleteSession, saveSessionAsProject } from "./sessions.js";
import {
action_extensions,
isRustEnabled,
isValaEnabled,
} from "./Extensions/Extensions.js";
const style_manager = Adw.StyleManager.get_default();
export default function Window({ application, session }) {
const langs = Object.fromEntries(
languages.map((lang) => [lang.id, { ...lang }]),
);
const { settings } = session;
Vte.Terminal.new();
const builder = Gtk.Builder.new_from_resource(resource);
const window = builder.get_object("window");
if (__DEV__) {
window.add_css_class("devel");
}
window.set_application(application);
// Popover menu theme switcher
const button_menu = builder.get_object("button_menu");
const popover = button_menu.get_popover();
popover.add_child(new ThemeSelector(), "themeswitcher");
const output = builder.get_object("output");
const panel_preview = builder.get_object("panel_preview");
const panel_placeholder = builder.get_object("panel_placeholder");
const { term_console } = Devtools({ application, window, builder, settings });
const document_javascript = Document({
code_view: builder.get_object("code_view_javascript"),
lang: langs.javascript,
session,
});
langs.javascript.document = document_javascript;
const document_vala = Document({
code_view: builder.get_object("code_view_vala"),
lang: langs.vala,
session,
});
langs.vala.document = document_vala;
const document_rust = Document({
code_view: builder.get_object("code_view_rust"),
lang: langs.rust,
session,
});
langs.rust.document = document_rust;
const document_blueprint = Document({
code_view: builder.get_object("code_view_blueprint"),
lang: langs.blueprint,
session,
});
langs.blueprint.document = document_blueprint;
const document_xml = Document({
code_view: builder.get_object("code_view_xml"),
lang: langs.xml,
session,
});
langs.xml.document = document_xml;
const document_css = Document({
code_view: builder.get_object("code_view_css"),
lang: langs.css,
session,
});
langs.css.document = document_css;
const panel_ui = PanelUI({
application,
builder,
langs,
term_console,
document_xml,
document_blueprint,
settings,
});
PanelStyle({ builder, document_css, settings });
const previewer = Previewer({
output,
builder,
window,
application,
panel_ui,
term_console,
settings,
session,
});
const panel_code = PanelCode({
builder,
previewer,
document_vala,
document_javascript,
settings,
});
previewer.setPanelCode(panel_code);
const button_run = builder.get_object("button_run");
const button_preview = builder.get_object("button_preview");
const button_inspector = builder.get_object("button_inspector");
function updateStyle() {
// For Platform Tools
setGtk4PreferDark(style_manager.dark).catch(console.error);
}
updateStyle();
style_manager.connect("notify::dark", updateStyle);
settings.bind(
"show-preview",
button_preview,
"active",
Gio.SettingsBindFlags.DEFAULT,
);
button_preview.bind_property(
"active",
panel_preview,
"visible",
GObject.BindingFlags.SYNC_CREATE,
);
function updatePanel() {
panel_placeholder.visible = ![
"show-preview",
"show-ui",
"show-style",
"show-code",
].find((s) => settings.get_boolean(s));
}
updatePanel();
settings.connect("changed", updatePanel);
button_inspector.connect("clicked", () => {
previewer.openInspector().catch(console.error);
});
async function format(code_view, formatter) {
let code;
const { buffer } = code_view;
try {
code = await formatter(buffer.text.trim());
} catch (err) {
console.error(err);
return;
}
if (buffer.text === code) {
return;
}
const { cursor_position } = buffer;
code_view.replaceText(code, false);
buffer.place_cursor(buffer.get_iter_at_offset(cursor_position));
}
function formatRustCode(text) {
const rustfmtLauncher = Gio.SubprocessLauncher.new(
Gio.SubprocessFlags.STDIN_PIPE |
Gio.SubprocessFlags.STDOUT_PIPE |
Gio.SubprocessFlags.STDERR_PIPE,
);
const rustfmtProcess = rustfmtLauncher.spawnv([
"rustfmt",
"--quiet",
"--emit",
"stdout",
"--edition",
"2021",
]);
const [success, stdout, stderr] = rustfmtProcess.communicate_utf8(
text,
null,
);
if (!success || stderr !== "") {
console.error(`Error running rustfmt: ${stderr}`);
return text;
}
return stdout;
}
async function formatCode() {
if (panel_code.panel.visible) {
if (panel_code.language === "JavaScript") {
await format(langs.javascript.document.code_view, (text) => {
return prettier(text, {
parser: "babel",
plugins: [prettier_babel, prettier_estree],
});
});
} else if (panel_code.language === "Rust") {
await format(langs.rust.document.code_view, (text) => {
return formatRustCode(text);
});
}
}
if (builder.get_object("panel_style").visible) {
await format(langs.css.document.code_view, (text) => {
return prettier(text, {
parser: "css",
plugins: [prettier_postcss],
});
});
}
if (panel_ui.panel.visible) {
if (panel_ui.is_xml_selected()) {
await format(langs.xml.document.code_view, (text) => {
return xml.format(text, 2);
});
}
}
}
let compiler_vala = null;
let compiler_rust = null;
async function runCode({ format }) {
button_run.set_sensitive(false);
term_console.clear();
previewer.stop();
panel_ui.stop();
try {
await panel_ui.update();
if (format) {
await formatCode();
}
await compile();
} catch (err) {
// prettier xml errors are not instances of Error
if (err instanceof Error || err instanceof GLib.Error) {
console.error(err);
} else {
console.error(err);
}
}
previewer.start();
panel_ui.start();
button_run.set_sensitive(true);
term_console.scrollToEnd();
}
async function compile() {
const { language } = panel_code;
const lang = langs[language.toLowerCase()];
// Do nothing if there is no code to avoid compile errors
const text = lang.document.code_view.buffer.text.trim();
if (text === "") {
return;
}
if (language === "JavaScript") {
await previewer.update(true);
// We have to create a new file each time
// because gjs doesn't appear to use etag for module caching
// ?foo=Date.now() also does not work as expected
// TODO: File a bug
const path = buildRuntimePath(`workbench-${Date.now()}`);
const file_javascript = Gio.File.new_for_path(path);
await file_javascript.replace_contents_async(
new GLib.Bytes(text),
null,
false,
Gio.FileCreateFlags.NONE,
null,
);
let exports;
try {
exports = await import(`file://${file_javascript.get_path()}`);
} catch (err) {
await previewer.update(true);
throw err;
} finally {
file_javascript
.delete_async(GLib.PRIORITY_DEFAULT, null)
.catch(console.error);
}
previewer.setSymbols(exports);
} else if (language === "Vala") {
if (!isValaEnabled()) {
action_extensions.activate(null);
return;
}
compiler_vala = compiler_vala || ValaCompiler({ session });
const success = await compiler_vala.compile();
if (success) {
await previewer.useExternal();
if (await compiler_vala.run()) {
await previewer.open();
} else {
await previewer.useInternal();
}
}
} else if (language === "Rust") {
if (!isRustEnabled()) {
action_extensions.activate(null);
return;
}
compiler_rust = compiler_rust || RustCompiler({ session });
const success = await compiler_rust.compile();
if (success) {
await previewer.useExternal();
if (await compiler_rust.run()) {
await previewer.open();
} else {
await previewer.useInternal();
}
}
}
}
const action_run = new Gio.SimpleAction({
name: "run",
});
action_run.connect("activate", () => {
runCode({ format: true }).catch(console.error);
});
window.add_action(action_run);
application.set_accels_for_action("win.run", ["<Control>Return"]);
const action_format = new Gio.SimpleAction({
name: "format",
});
action_format.connect("activate", () => {
formatCode();
});
window.add_action(action_format);
application.set_accels_for_action("win.format", ["<Control><Shift>Return"]);
const action_close = new Gio.SimpleAction({
name: "close",
});
action_close.connect("activate", () => {
window.close();
});
window.add_action(action_close);
application.set_accels_for_action("win.close", ["<Control>W"]);
window.connect("close-request", () => {
onCloseSession({ session, window }).catch(console.error);
return true;
});
window.add_action(settings.create_action("safe-mode"));
window.add_action(settings.create_action("auto-preview"));
window.present();
const documents = Object.values(langs).map((lang) => lang.document);
async function load({ run }) {
panel_ui.stop();
previewer.stop();
documents.forEach((document) => document.stop());
await Promise.all([
document_javascript.load(),
document_rust.load(),
document_vala.load(),
document_blueprint.load(),
document_xml.load(),
document_css.load(),
]);
await previewer.useInternal();
if (run) {
await runCode({ format: false });
} else {
term_console.clear();
panel_ui.start();
await panel_ui.update();
previewer.start();
await previewer.update(true);
}
documents.forEach((document) => {
document.start();
});
term_console.scrollToEnd();
}
return { load, window };
}
async function setGtk4PreferDark(dark) {
const settings_path = GLib.build_filenamev([
GLib.get_user_config_dir(),
"gtk-4.0/settings.ini",
]);
GLib.mkdir_with_parents(GLib.path_get_dirname(settings_path), 0o777);
const settings = new GLib.KeyFile();
try {
settings.load_from_file(settings_path, GLib.KeyFileFlags.NONE);
} catch (err) {
if (err.code !== GLib.FileError.NOENT) throw err;
}
settings.set_boolean("Settings", "gtk-application-prefer-dark-theme", dark);
settings.save_to_file(settings_path);
}
async function onCloseSession({ session, window }) {
if (session.isProject()) {
window.destroy();
return;
}
if (!session.settings.get_boolean("edited")) {
await deleteSession(session);
window.destroy();
return;
}
const builder = Gtk.Builder.new_from_resource(resource);
const dialog = builder.get_object("message_dialog_save_project");
dialog.set_transient_for(window);
dialog.present();
let location;
const row_project_location = builder.get_object("row_project_location");
const button_location = builder.get_object("button_location");
row_project_location.add_suffix(button_location);
button_location.connect("clicked", () => {
selectLocation().catch(console.error);
});
const row_project_name = builder.get_object("row_project_name");
function updateSaveButton() {
if (!row_project_name.text) {
dialog.set_response_enabled("save", false);
return;
}
if (!location) {
dialog.set_response_enabled("save", false);
return;
}
dialog.set_response_enabled("save", true);
}
row_project_name.connect("notify::text", () => {
updateSaveButton();
});
async function selectLocation() {
const file_dialog = new Gtk.FileDialog();
location = await file_dialog.select_folder(window, null);
row_project_location.subtitle = location.get_basename();
updateSaveButton();
}
const response = await dialog.choose(null);
if (response === "cancel") return;
if (response === "discard") {
await deleteSession(session);
} else if (response === "save") {
const destination = location.get_child_for_display_name(
row_project_name.text,
);
await saveSessionAsProject(session, destination);
}
window.destroy();
}