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::CategoryPaths categories;
94
96 override
97 {
98 categories.init(
100 }
101
102 std::optional<Library::ProcessEntry> scanPath(std::string_view path) override
103 {
104 if(std::string_view{path}.ends_with(".ui.qml"))
105 return std::nullopt;
106
107 score::PathInfo pathinfo{path};
108 QFile file{QString::fromUtf8(
109 pathinfo.absoluteFilePath.data(), pathinfo.absoluteFilePath.size())};
110 if(!file.open(QIODevice::ReadOnly))
111 return std::nullopt;
112
113 auto data = file.readAll().trimmed();
114 auto matches = scoreImport.match(data);
115 if(!matches.hasMatch())
116 return std::nullopt;
117
119 pdata.prettyName = QString::fromUtf8(
120 pathinfo.completeBaseName.data(), pathinfo.completeBaseName.size());
122 pdata.customData = QString::fromUtf8(
123 pathinfo.absoluteFilePath.data(), pathinfo.absoluteFilePath.size());
124
126 pdata.key, categories(pathinfo), {std::move(pdata), {}}};
127 }
128};
129
130}
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 ProcessEntry.hpp:64
void init(std::string processName, const score::ApplicationContext &ctx)
GUI thread (setup). processName is the process's pretty name.
Definition LibraryInterface.cpp:32
Definition LibraryInterface.hpp:26
Definition ProcessesItemModel.hpp:38
Definition ProcessEntry.hpp:25
Definition ProcessEntry.hpp:40
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
Definition lib/score/tools/File.hpp:20