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
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "../luaengine.h"
#include "utils.h"
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/modemanager.h>
using namespace Utils;
using namespace Core;
namespace Lua::Internal {
void setupActionModule()
{
registerProvider("Action", [](sol::state_view lua) -> sol::object {
sol::table result = lua.create_table();
result.new_enum(
"CommandAttribute",
"CA_Hide",
Command::CA_Hide,
"CA_UpdateText",
Command::CA_UpdateText,
"CA_UpdateIcon",
Command::CA_UpdateIcon,
"CA_NonConfigurable",
Command::CA_NonConfigurable);
struct ScriptCommand
{
Command *m_cmd;
QAction *m_contextAction;
};
result.new_usertype<ScriptCommand>(
"Command",
sol::no_constructor,
"enabled",
sol::property(
[](ScriptCommand *cmd) { return cmd->m_contextAction->isEnabled(); },
[](ScriptCommand *cmd, bool enabled) { cmd->m_contextAction->setEnabled(enabled); }),
"keySequences",
sol::property([](ScriptCommand* cmd) -> QList<QKeySequence> {
return cmd->m_cmd->keySequences();
}),
"toolTip",
sol::property(
[](ScriptCommand *cmd) { return cmd->m_contextAction->toolTip(); },
[](ScriptCommand *cmd, const QString &toolTip) {
cmd->m_contextAction->setToolTip(toolTip);
}),
"text",
sol::property(
[](ScriptCommand *cmd) { return cmd->m_contextAction->text(); },
[](ScriptCommand *cmd, const QString &text) {
cmd->m_contextAction->setText(text);
}),
"icon",
sol::property([](ScriptCommand *cmd, const IconFilePathOrString &&icon) {
cmd->m_contextAction->setIcon(toIcon(icon)->icon());
}));
result["create"] = [parent = std::make_unique<QObject>()](
const std::string &actionId, const sol::table &options) mutable {
ActionBuilder b(parent.get(), Id::fromString(QString::fromStdString(actionId)));
for (const auto &[k, v] : options) {
QString key = k.as<QString>();
if (key == "context")
b.setContext(Id::fromString(v.as<QString>()));
else if (key == "onTrigger")
b.addOnTriggered([f = v.as<sol::main_function>()]() {
auto res = void_safe_call(f);
QTC_CHECK_RESULT(res);
});
else if (key == "text")
b.setText(v.as<QString>());
else if (key == "iconText")
b.setIconText(v.as<QString>());
else if (key == "toolTip")
b.setToolTip(v.as<QString>());
else if (key == "commandAttributes")
b.setCommandAttributes(Command::CommandAttributes::fromInt(v.as<int>()));
else if (key == "commandDescription")
b.setCommandDescription(v.as<QString>());
else if (key == "defaultKeySequence")
b.setDefaultKeySequence(QKeySequence(v.as<QString>()));
else if (key == "defaultKeySequences") {
sol::table t = v.as<sol::table>();
QList<QKeySequence> sequences;
sequences.reserve(t.size());
for (const auto &[_, v] : t)
sequences.push_back(QKeySequence(v.as<QString>()));
b.setDefaultKeySequences(sequences);
} else if (key == "asModeAction") {
if (v.is<int>()) {
ModeManager::addAction(b.commandAction(), v.as<int>());
} else {
throw std::runtime_error(
"asMode needs an integer argument for the priority");
}
} else if (key == "icon") {
b.setIcon(toIcon(v.as<IconFilePathOrString>())->icon());
} else if (key == "containers") {
v.as<sol::table>().for_each([&b](sol::object, sol::object value) {
if (value.is<sol::table>()) {
const sol::table t = value.as<sol::table>();
const auto containerId = t.get<std::string>("containerId");
const auto groupId = t.get_or<std::string>("groupId", {});
b.addToContainer(
Id::fromString(QString::fromStdString(containerId)),
Id::fromString(QString::fromStdString(groupId)));
} else if (value.is<QString>()) {
b.addToContainer(Id::fromString(value.as<QString>()));
}
});
} else
throw std::runtime_error("Unknown key: " + key.toStdString());
}
return ScriptCommand{b.command(), b.contextAction()};
};
result["trigger"] = [](const std::string &actionId) mutable {
Command *command = ActionManager::command(
Id::fromString(QString::fromStdString(actionId)));
if (!command)
throw std::runtime_error("Action not found: " + actionId);
if (!command->action())
throw std::runtime_error("Action not assigned: " + actionId);
if (!command->action()->isEnabled())
throw std::runtime_error("Action not enabled: " + actionId);
command->action()->trigger();
};
return result;
});
}
} // namespace Lua::Internal
|