From 1b6bf5e6510c7f5c4ea663339b41928691dcdcfe Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Wed, 18 Sep 2024 14:07:59 +0200 Subject: [PATCH] Lua: Add ability to create mode action Change-Id: I8628dac240947173ddd8555442dbb1415b648c87 Reviewed-by: hjk --- src/plugins/lua/bindings/action.cpp | 37 +++++++++++++++++++++++++++++ src/plugins/lua/meta/action.lua | 11 ++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/plugins/lua/bindings/action.cpp b/src/plugins/lua/bindings/action.cpp index 27a42c65c94..fbf04121f25 100644 --- a/src/plugins/lua/bindings/action.cpp +++ b/src/plugins/lua/bindings/action.cpp @@ -4,8 +4,10 @@ #include "../luaengine.h" #include +#include 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( + "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()]( 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())); b.setDefaultKeySequences(sequences); + } else if (key == "asModeAction") { + if (v.is()) { + Core::ModeManager::addAction(b.commandAction(), v.as()); + } 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; diff --git a/src/plugins/lua/meta/action.lua b/src/plugins/lua/meta/action.lua index 48a1f49594b..39d4cf26ab4 100644 --- a/src/plugins/lua/meta/action.lua +++ b/src/plugins/lua/meta/action.lua @@ -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