-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathLibrary.js
More file actions
114 lines (92 loc) · 2.99 KB
/
Library.js
File metadata and controls
114 lines (92 loc) · 2.99 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
import Gio from "gi://Gio";
import {
decode,
demos_dir,
getLanguage,
settings as global_settings,
quitOnLastWindowClose,
} from "../util.js";
import Window from "../window.js";
import resource from "./Library.blp" with { type: "uri" };
import { createSessionFromDemo } from "../sessions.js";
import EntryRow from "./EntryRow.js";
import illustration from "./library.svg";
import { build } from "../../troll/src/builder.js";
export default function Library({ application }) {
const objects = build(resource);
const { window, picture_illustration } = objects;
window.application = application;
picture_illustration.set_resource(illustration);
if (__DEV__) {
window.add_css_class("devel");
}
let last_selected;
window.connect("close-request", quitOnLastWindowClose);
const demos = getDemos();
demos.forEach((demo) => {
const widget = new EntryRow({ demo: demo });
if (demo.name === "Welcome") last_selected = widget;
widget.connect("activated", (_self, language) => {
last_selected = widget;
openDemo({
application,
demo_name: demo.name,
language,
}).catch(console.error);
});
objects[`library_${demo.category}`].add(widget);
});
const action_library = new Gio.SimpleAction({
name: "library",
parameter_type: null,
});
action_library.connect("activate", () => {
window.present();
last_selected?.grab_focus();
});
application.add_action(action_library);
application.set_accels_for_action("app.library", ["<Control><Shift>O"]);
}
const getDemos = (() => {
let demos;
return function getDemos() {
if (!demos) {
const file = demos_dir.get_child("index.json");
const [, data] = file.load_contents(null);
demos = JSON.parse(decode(data));
}
return demos;
};
})();
export function getDemo(name) {
const demos = getDemos();
return demos.find((demo) => demo.name === name);
}
async function openDemo({ application, demo_name, language }) {
const demo = getDemo(demo_name);
const session = createSessionFromDemo(demo);
if (language) {
session.settings.set_int("code-language", language.index);
global_settings.set_int("recent-code-language", language.index);
// If the user explictely requested to open the demo
// in a specific language then that's probably what they are interested in
// therefor override the demo default and force show the code panel
session.settings.set_boolean("show-code", true);
}
const { autorun, languages } = demo;
// Override the user preferred language if the demo doesn't support it
const lang = session.getCodeLanguage();
if (languages.length > 0 && !languages.includes(lang.id)) {
session.settings.set_int(
"code-language",
getLanguage(demo.languages[0]).index,
);
}
const { load, runCode } = Window({ application, session });
await load();
const code_language = session.getCodeLanguage();
const run = autorun && code_language.id === "javascript";
if (run) {
await runCode();
}
}