Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scripting app.command.Cut() and app.command.Paste() does not work in --batch mode (fix #4354) #4446

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Gasparoken
Copy link
Member

It fixes the original problem, but I need advice on this approach.

fix #4354

Copy link
Collaborator

@aseprite-bot aseprite-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: redundant boolean literal in ternary expression result [readability-simplify-boolean-expr]

Suggested change
App::instance()->isGui() ? true : false, // set native clipboard
App::instance()->isGui(), // set native clipboard

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just leave the true as it was (probably some batch scripts will want to copy some content to the native clipboard).

@@ -487,7 +504,9 @@

case ClipboardFormat::Image: {
// Get the image from the native clipboard.
if (!getImage(nullptr))
if (editor && !getImage(nullptr))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: implicit conversion 'Editor *' -> bool [readability-implicit-bool-conversion]

Suggested change
if (editor && !getImage(nullptr))
if ((editor != nullptr) && !getImage(nullptr))

@@ -487,7 +504,9 @@

case ClipboardFormat::Image: {
// Get the image from the native clipboard.
if (!getImage(nullptr))
if (editor && !getImage(nullptr))
return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: repeated branch in conditional chain [bugprone-branch-clone]

        return;
        ^
Additional context

src/app/util/clipboard.cpp:507: end of the original

        return;
              ^

src/app/util/clipboard.cpp:509: clone 1 starts here

        return;
        ^

Comment on lines +509 to 510
else if (!isImageAvailable())
return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use 'else' after 'return' [readability-else-after-return]

Suggested change
else if (!isImageAvailable())
return;
if (!isImageAvailable())
return;

Copy link
Collaborator

@aseprite-bot aseprite-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

@@ -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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: implicit conversion 'Editor *' -> bool [readability-implicit-bool-conversion]

Suggested change
if (Editor::activeEditor() && !getImage(nullptr))
if ((Editor::activeEditor() != nullptr) && !getImage(nullptr))

@Gasparoken
Copy link
Member Author

Ready for advice.

@aseprite aseprite deleted a comment from aseprite-bot May 28, 2024
Copy link
Member

@dacap dacap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments to complete the implementation.

Comment on lines +22 to +23
class Clipboard;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed because we are already including "app/util/clipboard.h".

@@ -30,12 +37,18 @@ CutCommand::CutCommand()

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ctx->isUIAvailable() when possible:

Suggested change
return App::instance()->isGui() ?
return ctx->isUIAvailable() ?

@@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this true? Or should we check that ctx->clipboard()? Should Context offer a default input chain?

Comment on lines +37 to +38
App::instance()->inputChain().canPaste(ctx) :
ctx->clipboard()->isImageAvailable();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same case as in cut, probably we can implement a default input chain for batch processing.

@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just leave the true as it was (probably some batch scripts will want to copy some content to the native clipboard).

Comment on lines +507 to +508
if (App::instance()->isGui() && !getImage(nullptr))
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change is not needed (?). Again a script could paste the native clipboard content if possible.

Comment on lines +827 to +833
bool Clipboard::isImageAvailable() const
{
return m_data->image &&
m_data->image->width() > 0 &&
m_data->image->height() > 0;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably this should go to some CliClipboardManager (which could implement an InputChain and be the default one) to handle all the cut/paste/copy logic for scripts (when UI and editors are not available). Not sure but I think it might be a possible design.

Comment on lines +397 to 401
if (!App::instance()->isGui())
return;
#ifdef ENABLE_UI
update_screen_for_document(writer.document());
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!App::instance()->isGui())
return;
#ifdef ENABLE_UI
update_screen_for_document(writer.document());
#endif
#ifdef ENABLE_UI
if (writer.context()->isUIAvailable())
update_screen_for_document(writer.document());
#endif

Comment on lines -38 to +46
App::instance()->inputChain().paste(ctx);
if (ctx->isUIAvailable())
App::instance()->inputChain().paste(ctx);
else if (ctx->clipboard())
ctx->clipboard()->paste(ctx, false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably we required a origin argument for the paste (at least initially) to paste the image in any position:

struct PasteParams : public NewParams {
  Param<gfx::Point> origin { this, gfx::Point(), "origin" };
};

class PasteCommand : public CommandWithNewParams<PasteParams> {
public:
  PasteCommand();
...

Comment on lines +367 to +381
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());
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this required? Didn't copyFromDocument() make the copy?

@dacap dacap assigned Gasparoken and unassigned dacap May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scripting app.command.Cut() and app.command.Paste() does not work in --batch mode
3 participants