forked from qt-creator/qt-creator
Lua: Add ability to create mode action
Change-Id: I8628dac240947173ddd8555442dbb1415b648c87 Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -4,8 +4,10 @@
|
|||||||
#include "../luaengine.h"
|
#include "../luaengine.h"
|
||||||
|
|
||||||
#include <coreplugin/actionmanager/actionmanager.h>
|
#include <coreplugin/actionmanager/actionmanager.h>
|
||||||
|
#include <coreplugin/modemanager.h>
|
||||||
|
|
||||||
using namespace Utils;
|
using namespace Utils;
|
||||||
|
using namespace Core;
|
||||||
|
|
||||||
namespace Lua::Internal {
|
namespace Lua::Internal {
|
||||||
|
|
||||||
@@ -24,6 +26,32 @@ void setupActionModule()
|
|||||||
"CA_NonConfigurable",
|
"CA_NonConfigurable",
|
||||||
Core::Command::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>()](
|
result["create"] = [parent = std::make_unique<QObject>()](
|
||||||
const std::string &actionId, const sol::table &options) mutable {
|
const std::string &actionId, const sol::table &options) mutable {
|
||||||
Core::ActionBuilder b(parent.get(), Id::fromString(QString::fromStdString(actionId)));
|
Core::ActionBuilder b(parent.get(), Id::fromString(QString::fromStdString(actionId)));
|
||||||
@@ -57,9 +85,18 @@ void setupActionModule()
|
|||||||
for (const auto &[_, v] : t)
|
for (const auto &[_, v] : t)
|
||||||
sequences.push_back(QKeySequence(v.as<QString>()));
|
sequences.push_back(QKeySequence(v.as<QString>()));
|
||||||
b.setDefaultKeySequences(sequences);
|
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
|
} else
|
||||||
throw std::runtime_error("Unknown key: " + key.toStdString());
|
throw std::runtime_error("Unknown key: " + key.toStdString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ScriptCommand{b.command(), b.contextAction()};
|
||||||
};
|
};
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@@ -24,11 +24,20 @@ action.CommandAttribute = {
|
|||||||
---@field commandDescription? string The description of the command.
|
---@field commandDescription? string The description of the command.
|
||||||
---@field defaultKeySequence? string The default key sequence for the action.
|
---@field defaultKeySequence? string The default key sequence for the action.
|
||||||
---@field defaultKeySequences? string[] The default key sequences 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.
|
---Creates a new Action.
|
||||||
---@param id string The id of the action.
|
---@param id string The id of the action.
|
||||||
---@param options ActionOptions
|
---@param options ActionOptions
|
||||||
|
---@return Command command The created command.
|
||||||
function action.create(id, options) end
|
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
|
return action
|
||||||
|
Reference in New Issue
Block a user