aseprite/src/app/commands/cmd_close_file.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

94 lines
2.2 KiB
C++
Raw Normal View History

2015-02-12 23:16:25 +08:00
// Aseprite
// Copyright (C) 2024 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
2015-02-12 23:16:25 +08:00
//
2016-08-27 04:02:58 +08:00
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/app.h"
#include "app/commands/command.h"
#include "app/commands/commands.h"
2018-07-15 10:24:49 +08:00
#include "app/ui/doc_view.h"
#include "app/ui/workspace.h"
#include "app/ui_context.h"
namespace app {
using namespace ui;
class CloseFileCommand : public Command {
public:
CloseFileCommand() : Command(CommandId::CloseFile()) {}
protected:
bool onEnabled(Context* context) override
{
Workspace* workspace = App::instance()->workspace();
if (!workspace) // Workspace (main window) can be null if we are in --batch mode
return false;
WorkspaceView* view = workspace->activeView();
return (view != nullptr);
}
void onExecute(Context* context) override
{
Workspace* workspace = App::instance()->workspace();
WorkspaceView* view = workspace->activeView();
if (view)
workspace->closeView(view, false);
}
};
class CloseAllFilesCommand : public Command {
public:
CloseAllFilesCommand() : Command(CommandId::CloseAllFiles()) { m_quitting = false; }
protected:
bool onEnabled(Context* context) override
{
// Null if we are in --batch mode
return App::instance()->workspace() != nullptr;
}
void onLoadParams(const Params& params) override { m_quitting = params.get_as<bool>("quitting"); }
void onExecute(Context* context) override
{
Workspace* workspace = App::instance()->workspace();
if (!workspace) // Workspace (main window) can be null if we are in --batch mode
return;
// Collect all document views
2018-07-15 10:24:49 +08:00
DocViews docViews;
for (auto view : *workspace) {
2018-07-15 10:24:49 +08:00
DocView* docView = dynamic_cast<DocView*>(view);
if (docView)
docViews.push_back(docView);
}
for (auto docView : docViews) {
if (!workspace->closeView(docView, m_quitting))
break;
}
}
private:
bool m_quitting;
};
Command* CommandFactory::createCloseFileCommand()
{
return new CloseFileCommand;
}
Command* CommandFactory::createCloseAllFilesCommand()
{
return new CloseAllFilesCommand;
}
} // namespace app