Add an experimental CommandResult for the execution of commands

This makes possible to return true or false after the execution of
each command app.command.CommandName() if it successes or fails.
Currently the Lua API documentation says something about this here:

  https://github.com/aseprite/api/blob/main/api/command/ReplaceColor.md#replacecolor

But this is not true, the function always returned nil. Now it will
return true or false.
This commit is contained in:
David Capello 2022-04-29 09:53:57 -03:00
parent cc7da16691
commit dfa357eb8d
5 changed files with 59 additions and 5 deletions

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2019-2021 Igara Studio S.A.
// Copyright (C) 2019-2022 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
@ -205,6 +205,7 @@ bool FilterManagerImpl::applyStep()
void FilterManagerImpl::apply()
{
CommandResult result;
bool cancelled = false;
begin();
@ -239,7 +240,15 @@ void FilterManagerImpl::apply()
position()));
}
}
result = CommandResult(CommandResult::kOk);
}
else {
result = CommandResult(CommandResult::kCanceled);
}
ASSERT(m_reader.context());
m_reader.context()->setCommandResult(result);
}
void FilterManagerImpl::applyToTarget()

View File

@ -12,7 +12,6 @@
#include "app/app.h"
#include "app/commands/filters/filter_manager_impl.h"
#include "app/console.h"
#include "app/context_access.h"
#include "app/i18n/strings.h"
#include "app/ini_file.h"
#include "app/modules/editors.h"

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018-2021 Igara Studio S.A.
// Copyright (C) 2018-2022 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
@ -143,6 +143,8 @@ void Context::executeCommand(Command* command, const Params& params)
if (!command)
return;
m_result.reset();
Console console;
LOG(VERBOSE, "CTXT: Executing command %s\n", command->id().c_str());
try {
@ -177,17 +179,23 @@ void Context::executeCommand(Command* command, const Params& params)
app_rebuild_documents_tabs();
}
catch (base::Exception& e) {
m_result = CommandResult(CommandResult::kError);
LOG(ERROR, "CTXT: Exception caught executing %s command\n%s\n",
command->id().c_str(), e.what());
Console::showException(e);
}
catch (std::exception& e) {
m_result = CommandResult(CommandResult::kError);
LOG(ERROR, "CTXT: std::exception caught executing %s command\n%s\n",
command->id().c_str(), e.what());
console.printf("An error ocurred executing the command.\n\nDetails:\n%s", e.what());
}
#ifdef NDEBUG
catch (...) {
m_result = CommandResult(CommandResult::kError);
LOG(ERROR, "CTXT: Unknown exception executing %s command\n",
command->id().c_str());
@ -200,6 +208,11 @@ void Context::executeCommand(Command* command, const Params& params)
#endif
}
void Context::setCommandResult(const CommandResult& result)
{
m_result = result;
}
void Context::onAddDocument(Doc* doc)
{
m_lastSelectedDoc = doc;

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018-2021 Igara Studio S.A.
// Copyright (C) 2018-2022 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
@ -36,6 +36,24 @@ namespace app {
class DocView;
class Preferences;
class CommandResult {
public:
enum Type {
kOk,
// Exception throw (e.g. cannot lock sprite)
kError,
// Canceled by user.
kCanceled,
};
CommandResult(Type type = Type::kOk) : m_type(type) { }
Type type() const { return m_type; }
void reset() { m_type = Type::kOk; }
private:
Type m_type;
};
class CommandPreconditionException : public base::Exception {
public:
CommandPreconditionException() throw()
@ -97,6 +115,9 @@ namespace app {
void executeCommandFromMenuOrShortcut(Command* command, const Params& params = Params());
virtual void executeCommand(Command* command, const Params& params = Params());
void setCommandResult(const CommandResult& result);
const CommandResult& commandResult() { return m_result; }
virtual DocView* getFirstDocView(Doc* document) const {
return nullptr;
}
@ -130,6 +151,9 @@ namespace app {
Doc* m_lastSelectedDoc;
mutable std::unique_ptr<Preferences> m_preferences;
// Result of the execution of a command.
CommandResult m_result;
DISABLE_COPYING(Context);
};

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2019-2022 Igara Studio S.A.
// Copyright (C) 2018 David Capello
//
// This program is distributed under the terms of
@ -52,6 +52,15 @@ int Command_call(lua_State* L)
}
ctx->executeCommand(command, params);
if (ctx->commandResult().type() == CommandResult::kOk) {
lua_pushboolean(L, true);
}
else {
// TODO rollback/cancel the whole current transaction?
// or just throw an luaL_error()?
lua_pushboolean(L, false);
}
return 1;
}