Loading...
Searching...
No Matches
LibraryHandler.hpp
1#pragma once
2#include <JS/ConsolePanel.hpp>
3#include <JS/JSProcessModel.hpp>
4#include <Library/LibraryInterface.hpp>
5#include <Library/ProcessesItemModel.hpp>
6
7#include <QFile>
8#include <QFileInfo>
9namespace JS
10{
12 : public QObject
14{
15 SCORE_CONCRETE("6e72e377-efdd-4e3c-9900-922b618e7d70")
16
17public:
18 JS::PanelDelegate* panel{};
19
20 QSet<QString> acceptedFiles() const noexcept override { return {"mjs"}; }
21
22 bool add(const QString& path)
23 {
24 QFile f{path};
25 if(!f.open(QIODevice::ReadOnly))
26 return false;
27
28 if(!panel)
29 panel = &score::GUIAppContext().panel<JS::PanelDelegate>();
30
31 panel->importModule(path);
32 return true;
33 }
34
35 std::function<void()> asyncAddPath(std::string_view path) override
36 {
37 if(path.find("companion-bundled-modules") != std::string_view::npos)
38 return {};
39 if(path.find("node_modules") != std::string_view::npos)
40 return {};
41
42 // Everything else must happen on GUI thread (QML engine interaction)
43 QString qpath = QString::fromUtf8(path.data(), path.length());
44 return [this, qpath = std::move(qpath)]() {
45 add(qpath);
46 };
47 }
48
49 bool onDoubleClick(const QString& path, const score::DocumentContext& ctx) override
50 {
51 return add(path);
52 }
53};
54
56 : public QObject
58{
59 SCORE_CONCRETE("21f405da-a249-4e39-b405-9173aff11b26")
60
61 QSet<QString> acceptedFiles() const noexcept override { return {"js"}; }
62
63 bool onDoubleClick(const QString& path, const score::DocumentContext& ctx) override
64 {
65 QFile f{path};
66 if(!f.open(QIODevice::ReadOnly))
67 return false;
68
69 auto& p = ctx.app.panel<JS::PanelDelegate>();
70 if(QFileInfo{f}.suffix() == "mjs")
71 {
72 p.importModule(path);
73 }
74 else
75 {
76 auto data = f.readAll();
77 p.evaluate(data);
78 }
79 return true;
80 }
81};
82
83class LibraryHandler final
84 : public QObject
86{
87 SCORE_CONCRETE("5231ea8b-da66-4c6f-9e34-d9a79cbc494a")
88
89 QSet<QString> acceptedFiles() const noexcept override { return {"qml"}; }
90
91 static inline const QRegularExpression scoreImport{"import Score"};
92
93 Library::Subcategories categories;
94
96 override
97 {
98 // TODO relaunch whenever library path changes...
100 QModelIndex node = model.find(key);
101 if(node == QModelIndex{})
102 return;
103
104 categories.init(
105 Metadata<PrettyName_k, JS::ProcessModel>::get().toStdString(), node, ctx);
106 }
107
108 std::function<void()> asyncAddPath(std::string_view path) override
109 {
110 if(std::string_view{path}.ends_with(".ui.qml"))
111 return {};
112
113 QFileInfo fileinfo{QString::fromUtf8(path.data(), path.length())};
114 QFile file{fileinfo.absoluteFilePath()};
115 if(!file.open(QIODevice::ReadOnly))
116 return {};
117
118 auto data = file.readAll().trimmed();
119 auto matches = scoreImport.match(data);
120 if(!matches.hasMatch())
121 return {};
122
124 pdata.prettyName = fileinfo.completeBaseName();
126 pdata.customData = fileinfo.absoluteFilePath();
127
128 return [this, fileinfo, pdata = std::move(pdata)]() mutable {
129 categories.add(fileinfo, std::move(pdata));
130 };
131 }
132};
133
134}
Definition LibraryHandler.hpp:58
Definition LibraryHandler.hpp:86
Definition LibraryHandler.hpp:14
std::function< void()> asyncAddPath(std::string_view path) override
Definition LibraryHandler.hpp:35
Definition ConsolePanel.hpp:40
Definition LibraryInterface.hpp:24
Definition ProcessesItemModel.hpp:44
Definition ProcessesItemModel.hpp:33
Definition ProcessesItemModel.hpp:85
Static metadata implementation.
Definition lib/score/tools/Metadata.hpp:36
Definition ObjectMatches.hpp:6
Definition DocumentContext.hpp:18
Specializes ApplicationContext with the QMainWindow.
Definition GUIApplicationContext.hpp:15
T & panel() const
Access to a specific PanelDelegate.
Definition GUIApplicationContext.hpp:36