Skip to content

Commit

Permalink
Fix scripting app.command.Cut() and app.command.Paste() does not work…
Browse files Browse the repository at this point in the history
… in --batch mode (fix aseprite#4354)
  • Loading branch information
dacap authored and Gasparoken committed May 3, 2024
1 parent 648ff61 commit 10c388f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
if: ${{ runner.os == 'Linux' || runner.os == 'macOS' }}
with:
key: ${{ matrix.os }}-${{ matrix.enable_ui }}-${{ matrix.build_type }}
- uses: turtlesec-no/get-ninja@main
- uses: aseprite/get-ninja@main
- uses: ilammy/msvc-dev-cmd@v1
if: runner.os == 'Windows'
- name: Workaround for windows-2022 and cmake 3.25.0
Expand Down
2 changes: 1 addition & 1 deletion laf
17 changes: 15 additions & 2 deletions src/app/commands/cmd_cut.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Aseprite
// Copyright (C) 2024 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello
//
// This program is distributed under the terms of
Expand All @@ -10,10 +11,16 @@

#include "app/app.h"
#include "app/commands/command.h"
#include "app/context_access.h"
#include "app/tx.h"
#include "app/ui/input_chain.h"
#include "app/util/clipboard.h"
#include "app/util/range_utils.h"

namespace app {

class Clipboard;

class CutCommand : public Command {
public:
CutCommand();
Expand All @@ -30,12 +37,18 @@ CutCommand::CutCommand()

bool CutCommand::onEnabled(Context* ctx)
{
return App::instance()->inputChain().canCut(ctx);
return App::instance()->isGui() ?
App::instance()->inputChain().canCut(ctx) : true;
}

void CutCommand::onExecute(Context* ctx)
{
App::instance()->inputChain().cut(ctx);
if (App::instance()->isGui())
App::instance()->inputChain().cut(ctx);
else if (ctx->clipboard()) {
ContextWriter writer(ctx);
ctx->clipboard()->cut(writer);
}
}

Command* CommandFactory::createCutCommand()
Expand Down
12 changes: 10 additions & 2 deletions src/app/commands/cmd_paste.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Aseprite
// Copyright (C) 2024 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello
//
// This program is distributed under the terms of
Expand All @@ -10,7 +11,9 @@

#include "app/app.h"
#include "app/commands/command.h"
#include "app/context.h"
#include "app/ui/input_chain.h"
#include "app/util/clipboard.h"

namespace app {

Expand All @@ -30,12 +33,17 @@ PasteCommand::PasteCommand()

bool PasteCommand::onEnabled(Context* ctx)
{
return App::instance()->inputChain().canPaste(ctx);
return ctx->isUIAvailable() ?
App::instance()->inputChain().canPaste(ctx) :
ctx->clipboard()->isImageAvailable();
}

void PasteCommand::onExecute(Context* ctx)
{
App::instance()->inputChain().paste(ctx);
if (ctx->isUIAvailable())
App::instance()->inputChain().paste(ctx);
else if (ctx->clipboard())
ctx->clipboard()->paste(ctx, false);
}

Command* CommandFactory::createPasteCommand()
Expand Down
30 changes: 28 additions & 2 deletions src/app/util/clipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ bool Clipboard::copyFromDocument(const Site& site, bool merged)
(mask ? new Mask(*mask): nullptr),
(pal ? new Palette(*pal): nullptr),
nullptr,
true, // set native clipboard
App::instance()->isGui() ? true : false, // set native clipboard
site.layer() && !site.layer()->isBackground());

return true;
Expand Down Expand Up @@ -364,6 +364,21 @@ void Clipboard::cut(ContextWriter& writer)
{
Tx tx(writer, "Cut");
Site site = writer.context()->activeSite();
if (!App::instance()->isGui()) {
if (const Doc* doc = static_cast<const Doc*>(site.document())) {
if (doc->sprite()) {
const Mask* mask = doc->mask();
const Palette* pal = doc->sprite()->palette(site.frame());
doc::Image* image(new_image_from_mask(site, true));
setData(image,
(mask ? new Mask(*mask): nullptr),
(pal ? new Palette(*pal): nullptr),
nullptr,
false,
site.layer() && !site.layer()->isBackground());
}
}
}
CelList cels;
if (site.range().enabled()) {
cels = get_unique_cels_to_edit_pixels(site.sprite(), site.range());
Expand All @@ -379,6 +394,8 @@ void Clipboard::cut(ContextWriter& writer)
tx.commit();
}
writer.document()->generateMaskBoundaries();
if (!App::instance()->isGui())
return;
#ifdef ENABLE_UI
update_screen_for_document(writer.document());
#endif
Expand Down Expand Up @@ -487,7 +504,9 @@ void Clipboard::paste(Context* ctx,

case ClipboardFormat::Image: {
// Get the image from the native clipboard.
if (!getImage(nullptr))
if (Editor::activeEditor() && !getImage(nullptr))
return;
else if (!isImageAvailable())
return;

ASSERT(m_data->image);
Expand Down Expand Up @@ -805,6 +824,13 @@ ImageRef Clipboard::getImage(Palette* palette)
return m_data->image;
}

bool Clipboard::isImageAvailable() const
{
return m_data->image &&
m_data->image->width() > 0 &&
m_data->image->height() > 0;
}

bool Clipboard::getImageSize(gfx::Size& size)
{
if (use_native_clipboard() && getNativeBitmapSize(&size))
Expand Down
3 changes: 3 additions & 0 deletions src/app/util/clipboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ namespace app {

doc::ImageRef getImage(doc::Palette* palette);

// Used to identify data available when the UI is not available
bool isImageAvailable() const;

// Returns true and fills the specified "size"" with the image's
// size in the clipboard, or return false in case that the clipboard
// doesn't contain an image at all.
Expand Down

0 comments on commit 10c388f

Please sign in to comment.