Lua: Add ability to create mode action

Change-Id: I8628dac240947173ddd8555442dbb1415b648c87
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Marcus Tillmanns
2024-09-18 14:07:59 +02:00
parent eeeaf61378
commit 1b6bf5e651
2 changed files with 47 additions and 1 deletions

View File

@@ -4,8 +4,10 @@
#include "../luaengine.h"
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/modemanager.h>
using namespace Utils;
using namespace Core;
namespace Lua::Internal {
@@ -24,6 +26,32 @@ void setupActionModule()
"CA_NonConfigurable",
Core::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); }),
"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);
}));
result["create"] = [parent = std::make_unique<QObject>()](
const std::string &actionId, const sol::table &options) mutable {
Core::ActionBuilder b(parent.get(), Id::fromString(QString::fromStdString(actionId)));
@@ -57,9 +85,18 @@ void setupActionModule()
for (const auto &[_, v] : t)
sequences.push_back(QKeySequence(v.as<QString>()));
b.setDefaultKeySequences(sequences);
} else if (key == "asModeAction") {
if (v.is<int>()) {
Core::ModeManager::addAction(b.commandAction(), v.as<int>());
} else {
throw std::runtime_error(
"asMode needs an integer argument for the priority");
}
} else
throw std::runtime_error("Unknown key: " + key.toStdString());
}
return ScriptCommand{b.command(), b.contextAction()};
};
return result;

View File

@@ -24,11 +24,20 @@ action.CommandAttribute = {
---@field commandDescription? string The description of the command.
---@field defaultKeySequence? string The default key sequence for the action.
---@field defaultKeySequences? string[] The default key sequences for the action.
local ActionOptions = {}
---@field asModeAction? integer Register the action as a mode action with the given priority.
ActionOptions = {}
---Creates a new Action.
---@param id string The id of the action.
---@param options ActionOptions
---@return Command command The created command.
function action.create(id, options) end
---@class Command
---@field enabled boolean Whether the command is enabled or not.
---@field text string The text of the command. Make sure to specify `commandAttributes = CommandAttribute.CA_UpdateText` in the options.
---@field tooltip string The tooltip of the command. Make sure to specify `commandAttributes = CommandAttribute.CA_UpdateText` in the options.
Command = {}
---return the module
return action