From d329f3807526bdbcd3979a2ed9114f6ac496df1b Mon Sep 17 00:00:00 2001 From: David Capello Date: Thu, 18 May 2023 11:49:04 -0300 Subject: [PATCH] [lua] Add stopPropagation function to the beforecommand event Related to: https://github.com/aseprite/Attachment-System/issues/121 https://github.com/aseprite/Attachment-System/issues/127 --- src/app/script/events_class.cpp | 14 +++++++++++++- src/app/script/values.cpp | 2 ++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/app/script/events_class.cpp b/src/app/script/events_class.cpp index e84e35df9..62dcfe18e 100644 --- a/src/app/script/events_class.cpp +++ b/src/app/script/events_class.cpp @@ -145,6 +145,9 @@ private: std::vector m_listeners; }; +// Used in BeforeCommand +static bool s_stopPropagationFlag = false; + class AppEvents : public Events , private ContextObserver { public: @@ -232,8 +235,17 @@ private: } void onBeforeCommand(CommandExecutionEvent& ev) { + s_stopPropagationFlag = false; call(BeforeCommand, { { "name", ev.command()->id() }, - { "params", ev.params() } }); + { "params", ev.params() }, + { "stopPropagation", + (lua_CFunction) + [](lua_State*) -> int { + s_stopPropagationFlag = true; + return 0; + } } }); + if (s_stopPropagationFlag) + ev.cancel(); } void onAfterCommand(CommandExecutionEvent& ev) { diff --git a/src/app/script/values.cpp b/src/app/script/values.cpp index 65b0ae525..521ab8e08 100644 --- a/src/app/script/values.cpp +++ b/src/app/script/values.cpp @@ -168,6 +168,8 @@ void push_value_to_lua(lua_State* L, const std::any& value) { push_value_to_lua(L, *v); else if (auto v = std::any_cast(&value)) push_value_to_lua(L, *v); + else if (auto v = std::any_cast(&value)) + lua_pushcfunction(L, *v); else if (auto v = std::any_cast(&value)) push_value_to_lua(L, **v); else if (auto v = std::any_cast(&value))