| 1 | // Aseprite |
|---|---|
| 2 | // Copyright (C) 2001-2017 David Capello |
| 3 | // |
| 4 | // This program is distributed under the terms of |
| 5 | // the End-User License Agreement for Aseprite. |
| 6 | |
| 7 | #ifdef HAVE_CONFIG_H |
| 8 | #include "config.h" |
| 9 | #endif |
| 10 | |
| 11 | #include "app/app.h" |
| 12 | #include "app/commands/command.h" |
| 13 | #include "app/context.h" |
| 14 | #include "app/ui/main_window.h" |
| 15 | #include "app/ui/preview_editor.h" |
| 16 | |
| 17 | namespace app { |
| 18 | |
| 19 | class TogglePreviewCommand : public Command { |
| 20 | public: |
| 21 | TogglePreviewCommand(); |
| 22 | |
| 23 | protected: |
| 24 | bool onEnabled(Context* context) override; |
| 25 | bool onChecked(Context* context) override; |
| 26 | void onExecute(Context* context) override; |
| 27 | }; |
| 28 | |
| 29 | TogglePreviewCommand::TogglePreviewCommand() |
| 30 | : Command(CommandId::TogglePreview(), CmdUIOnlyFlag) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | bool TogglePreviewCommand::onEnabled(Context* context) |
| 35 | { |
| 36 | return context->checkFlags(ContextFlags::ActiveDocumentIsWritable | |
| 37 | ContextFlags::HasActiveSprite); |
| 38 | } |
| 39 | |
| 40 | bool TogglePreviewCommand::onChecked(Context* context) |
| 41 | { |
| 42 | MainWindow* mainWin = App::instance()->mainWindow(); |
| 43 | if (!mainWin) |
| 44 | return false; |
| 45 | |
| 46 | PreviewEditorWindow* previewWin = mainWin->getPreviewEditor(); |
| 47 | return (previewWin && previewWin->isVisible()); |
| 48 | } |
| 49 | |
| 50 | void TogglePreviewCommand::onExecute(Context* context) |
| 51 | { |
| 52 | PreviewEditorWindow* previewWin = |
| 53 | App::instance()->mainWindow()->getPreviewEditor(); |
| 54 | |
| 55 | bool state = previewWin->isPreviewEnabled(); |
| 56 | previewWin->setPreviewEnabled(!state); |
| 57 | } |
| 58 | |
| 59 | Command* CommandFactory::createTogglePreviewCommand() |
| 60 | { |
| 61 | return new TogglePreviewCommand; |
| 62 | } |
| 63 | |
| 64 | } // namespace app |
| 65 |