2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2018-05-07 11:11:50 +08:00
|
|
|
// 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.
|
2009-06-01 10:59:15 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2009-06-01 10:59:15 +08:00
|
|
|
#include "config.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "app/app.h"
|
2018-07-07 22:54:44 +08:00
|
|
|
#include "app/doc.h"
|
2013-11-11 05:27:11 +08:00
|
|
|
#include "app/modules/editors.h"
|
2014-12-28 22:06:11 +08:00
|
|
|
#include "app/pref/preferences.h"
|
2018-07-07 13:47:42 +08:00
|
|
|
#include "app/site.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/ui/color_bar.h"
|
2018-07-15 10:24:49 +08:00
|
|
|
#include "app/ui/doc_view.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/ui/editor/editor.h"
|
2015-05-10 06:55:33 +08:00
|
|
|
#include "app/ui/input_chain.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/ui/main_window.h"
|
2015-02-10 20:38:07 +08:00
|
|
|
#include "app/ui/preview_editor.h"
|
2015-04-07 13:29:33 +08:00
|
|
|
#include "app/ui/status_bar.h"
|
2017-03-27 00:33:12 +08:00
|
|
|
#include "app/ui/timeline/timeline.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/ui/workspace.h"
|
2015-04-03 02:43:50 +08:00
|
|
|
#include "app/ui/workspace_tabs.h"
|
2013-11-11 05:27:11 +08:00
|
|
|
#include "app/ui_context.h"
|
2011-03-23 08:11:25 +08:00
|
|
|
#include "base/mutex.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/sprite.h"
|
2009-06-01 10:59:15 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
namespace app {
|
2009-06-01 10:59:15 +08:00
|
|
|
|
2015-04-21 03:27:09 +08:00
|
|
|
UIContext* UIContext::m_instance = nullptr;
|
2013-01-21 05:40:37 +08:00
|
|
|
|
2009-10-09 09:34:06 +08:00
|
|
|
UIContext::UIContext()
|
2018-05-07 11:11:50 +08:00
|
|
|
: m_lastSelectedView(nullptr)
|
2009-06-01 10:59:15 +08:00
|
|
|
{
|
2016-09-14 02:02:00 +08:00
|
|
|
documents().add_observer(&Preferences::instance());
|
2014-09-09 10:08:06 +08:00
|
|
|
|
2010-08-04 10:33:44 +08:00
|
|
|
ASSERT(m_instance == NULL);
|
2009-10-09 09:34:06 +08:00
|
|
|
m_instance = this;
|
2009-06-01 10:59:15 +08:00
|
|
|
}
|
|
|
|
|
2009-10-09 09:34:06 +08:00
|
|
|
UIContext::~UIContext()
|
2009-06-01 10:59:15 +08:00
|
|
|
{
|
2010-08-04 10:33:44 +08:00
|
|
|
ASSERT(m_instance == this);
|
2009-10-09 09:34:06 +08:00
|
|
|
m_instance = NULL;
|
2014-07-29 11:53:24 +08:00
|
|
|
|
2016-09-14 02:02:00 +08:00
|
|
|
documents().remove_observer(&Preferences::instance());
|
2014-09-09 10:08:06 +08:00
|
|
|
|
2014-07-29 11:53:24 +08:00
|
|
|
// The context must be empty at this point. (It's to check if the UI
|
|
|
|
// is working correctly, i.e. closing all files when the user can
|
|
|
|
// take any action about it.)
|
|
|
|
ASSERT(documents().empty());
|
2009-06-01 10:59:15 +08:00
|
|
|
}
|
|
|
|
|
2015-05-19 04:04:31 +08:00
|
|
|
bool UIContext::isUIAvailable() const
|
2014-11-07 05:40:29 +08:00
|
|
|
{
|
|
|
|
return App::instance()->isGui();
|
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
DocView* UIContext::activeView() const
|
2013-01-21 05:40:37 +08:00
|
|
|
{
|
2015-05-19 04:04:31 +08:00
|
|
|
if (!isUIAvailable())
|
2015-10-27 04:51:32 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2016-04-23 00:19:06 +08:00
|
|
|
Workspace* workspace = App::instance()->workspace();
|
2015-10-27 04:51:32 +08:00
|
|
|
if (!workspace)
|
|
|
|
return nullptr;
|
2014-11-07 05:40:29 +08:00
|
|
|
|
2014-07-29 11:53:24 +08:00
|
|
|
WorkspaceView* view = workspace->activeView();
|
2018-07-15 10:24:49 +08:00
|
|
|
if (DocView* docView = dynamic_cast<DocView*>(view))
|
2013-03-28 08:19:35 +08:00
|
|
|
return docView;
|
|
|
|
else
|
2015-10-27 04:51:32 +08:00
|
|
|
return nullptr;
|
2013-01-21 05:40:37 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
void UIContext::setActiveView(DocView* docView)
|
2013-01-21 05:40:37 +08:00
|
|
|
{
|
2016-04-23 00:19:06 +08:00
|
|
|
MainWindow* mainWin = App::instance()->mainWindow();
|
2015-05-10 06:55:33 +08:00
|
|
|
|
2018-08-14 08:33:59 +08:00
|
|
|
// This can happen when the main window is being destroyed when we
|
|
|
|
// close the app, and the active view is changing because we are
|
|
|
|
// closing down every single tab.
|
|
|
|
if (!mainWin)
|
|
|
|
return;
|
|
|
|
|
2015-05-10 06:55:33 +08:00
|
|
|
// Prioritize workspace for user input.
|
2018-06-06 00:11:29 +08:00
|
|
|
App::instance()->inputChain().prioritize(mainWin->getWorkspace(), nullptr);
|
2015-05-10 06:55:33 +08:00
|
|
|
|
2015-04-07 03:12:28 +08:00
|
|
|
// Do nothing cases: 1) the view is already selected, or 2) the view
|
|
|
|
// is the a preview.
|
|
|
|
if (m_lastSelectedView == docView ||
|
|
|
|
(docView && docView->isPreview()))
|
2014-08-25 19:27:42 +08:00
|
|
|
return;
|
|
|
|
|
2015-02-10 20:38:07 +08:00
|
|
|
if (docView) {
|
|
|
|
mainWin->getTabsBar()->selectTab(docView);
|
2013-01-21 05:40:37 +08:00
|
|
|
|
2015-02-10 20:38:07 +08:00
|
|
|
if (mainWin->getWorkspace()->activeView() != docView)
|
|
|
|
mainWin->getWorkspace()->setActiveView(docView);
|
2013-03-28 08:19:35 +08:00
|
|
|
}
|
|
|
|
|
2016-02-13 12:33:43 +08:00
|
|
|
current_editor = (docView ? docView->editor(): nullptr);
|
2013-01-21 05:40:37 +08:00
|
|
|
|
|
|
|
if (current_editor)
|
|
|
|
current_editor->requestFocus();
|
|
|
|
|
2015-02-10 20:38:07 +08:00
|
|
|
mainWin->getPreviewEditor()->updateUsingEditor(current_editor);
|
|
|
|
mainWin->getTimeline()->updateUsingEditor(current_editor);
|
2013-02-21 06:54:00 +08:00
|
|
|
|
2013-03-12 07:29:45 +08:00
|
|
|
// Change the image-type of color bar.
|
|
|
|
ColorBar::instance()->setPixelFormat(app_get_current_pixel_format());
|
|
|
|
|
2013-02-21 06:54:00 +08:00
|
|
|
// Restore the palette of the selected document.
|
2013-03-12 07:29:45 +08:00
|
|
|
app_refresh_screen();
|
|
|
|
|
|
|
|
// Change the main frame title.
|
2014-09-03 11:39:15 +08:00
|
|
|
App::instance()->updateDisplayTitleBar();
|
2014-08-25 19:27:42 +08:00
|
|
|
|
|
|
|
m_lastSelectedView = docView;
|
2015-05-05 23:58:50 +08:00
|
|
|
|
|
|
|
// TODO all the calls to functions like updateUsingEditor(),
|
|
|
|
// setPixelFormat(), app_refresh_screen(), updateDisplayTitleBar()
|
|
|
|
// Can be replaced with a ContextObserver listening to the
|
|
|
|
// onActiveSiteChange() event.
|
|
|
|
notifyActiveSiteChanged();
|
2013-01-21 05:40:37 +08:00
|
|
|
}
|
|
|
|
|
2018-07-07 22:54:44 +08:00
|
|
|
void UIContext::onSetActiveDocument(Doc* document)
|
2015-04-21 03:27:09 +08:00
|
|
|
{
|
2018-05-07 11:11:50 +08:00
|
|
|
bool notify = (lastSelectedDoc() != document);
|
|
|
|
app::Context::onSetActiveDocument(document);
|
2015-04-21 03:27:09 +08:00
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
DocView* docView = getFirstDocView(document);
|
2015-05-05 23:58:50 +08:00
|
|
|
if (docView) { // The view can be null if we are in --batch mode
|
2015-04-21 03:27:09 +08:00
|
|
|
setActiveView(docView);
|
2015-05-05 23:58:50 +08:00
|
|
|
notify = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (notify)
|
|
|
|
notifyActiveSiteChanged();
|
2015-04-21 03:27:09 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
DocView* UIContext::getFirstDocView(Doc* document) const
|
2014-12-01 08:06:29 +08:00
|
|
|
{
|
2016-04-23 00:19:06 +08:00
|
|
|
Workspace* workspace = App::instance()->workspace();
|
|
|
|
if (!workspace) // Workspace (main window) can be null if we are in --batch mode
|
2015-05-01 04:02:57 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2015-02-23 08:18:53 +08:00
|
|
|
for (WorkspaceView* view : *workspace) {
|
2018-07-15 10:24:49 +08:00
|
|
|
if (DocView* docView = dynamic_cast<DocView*>(view)) {
|
2016-02-13 12:33:43 +08:00
|
|
|
if (docView->document() == document) {
|
2014-12-01 08:06:29 +08:00
|
|
|
return docView;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-01 04:02:57 +08:00
|
|
|
return nullptr;
|
2014-12-01 08:06:29 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
DocViews UIContext::getAllDocViews(Doc* document) const
|
2015-08-15 00:06:26 +08:00
|
|
|
{
|
2018-07-15 10:24:49 +08:00
|
|
|
DocViews docViews;
|
2019-03-19 09:31:31 +08:00
|
|
|
// The workspace can be nullptr when we are running in batch mode.
|
|
|
|
if (Workspace* workspace = App::instance()->workspace()) {
|
|
|
|
for (WorkspaceView* view : *workspace) {
|
|
|
|
if (DocView* docView = dynamic_cast<DocView*>(view)) {
|
|
|
|
if (docView->document() == document) {
|
|
|
|
docViews.push_back(docView);
|
|
|
|
}
|
2015-08-15 00:06:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return docViews;
|
|
|
|
}
|
|
|
|
|
2018-07-24 10:43:31 +08:00
|
|
|
Editors UIContext::getAllEditorsIncludingPreview(Doc* document) const
|
|
|
|
{
|
|
|
|
std::vector<Editor*> editors;
|
|
|
|
for (DocView* docView : getAllDocViews(document)) {
|
|
|
|
if (docView->editor())
|
|
|
|
editors.push_back(docView->editor());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MainWindow* mainWin = App::instance()->mainWindow()) {
|
|
|
|
PreviewEditorWindow* previewWin = mainWin->getPreviewEditor();
|
|
|
|
if (previewWin) {
|
|
|
|
Editor* miniEditor = previewWin->previewEditor();
|
|
|
|
if (miniEditor && miniEditor->document() == document)
|
|
|
|
editors.push_back(miniEditor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return editors;
|
|
|
|
}
|
|
|
|
|
2014-07-29 11:53:24 +08:00
|
|
|
Editor* UIContext::activeEditor()
|
2013-01-21 05:40:37 +08:00
|
|
|
{
|
2018-07-15 10:24:49 +08:00
|
|
|
DocView* view = activeView();
|
2014-07-29 11:53:24 +08:00
|
|
|
if (view)
|
2016-02-13 12:33:43 +08:00
|
|
|
return view->editor();
|
2013-01-21 05:40:37 +08:00
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-07-07 22:54:44 +08:00
|
|
|
void UIContext::onAddDocument(Doc* doc)
|
2009-06-01 10:59:15 +08:00
|
|
|
{
|
2018-05-07 11:11:50 +08:00
|
|
|
app::Context::onAddDocument(doc);
|
2009-06-01 10:59:15 +08:00
|
|
|
|
2013-12-09 07:19:32 +08:00
|
|
|
// We don't create views in batch mode.
|
|
|
|
if (!App::instance()->isGui())
|
|
|
|
return;
|
|
|
|
|
2013-01-21 05:40:37 +08:00
|
|
|
// Add a new view for this document
|
2018-07-15 10:24:49 +08:00
|
|
|
DocView* view = new DocView(
|
2018-05-07 11:11:50 +08:00
|
|
|
lastSelectedDoc(),
|
2018-07-15 10:24:49 +08:00
|
|
|
DocView::Normal,
|
2016-04-23 00:19:06 +08:00
|
|
|
App::instance()->mainWindow()->getPreviewEditor());
|
2013-01-21 05:40:37 +08:00
|
|
|
|
|
|
|
// Add a tab with the new view for the document
|
2016-04-23 00:19:06 +08:00
|
|
|
App::instance()->workspace()->addView(view);
|
2013-01-21 05:40:37 +08:00
|
|
|
|
|
|
|
setActiveView(view);
|
2016-02-13 12:33:43 +08:00
|
|
|
view->editor()->setDefaultScroll();
|
2009-06-01 10:59:15 +08:00
|
|
|
}
|
|
|
|
|
2018-07-07 22:54:44 +08:00
|
|
|
void UIContext::onRemoveDocument(Doc* doc)
|
2009-06-01 10:59:15 +08:00
|
|
|
{
|
2018-05-07 11:11:50 +08:00
|
|
|
app::Context::onRemoveDocument(doc);
|
2015-04-21 03:27:09 +08:00
|
|
|
|
2014-11-07 08:04:32 +08:00
|
|
|
// We don't destroy views in batch mode.
|
2015-05-19 04:04:31 +08:00
|
|
|
if (isUIAvailable()) {
|
2016-04-23 00:19:06 +08:00
|
|
|
Workspace* workspace = App::instance()->workspace();
|
2009-06-01 10:59:15 +08:00
|
|
|
|
2018-07-15 10:24:49 +08:00
|
|
|
for (DocView* docView : getAllDocViews(doc)) {
|
2015-04-07 12:21:31 +08:00
|
|
|
workspace->removeView(docView);
|
|
|
|
delete docView;
|
|
|
|
}
|
2013-03-28 08:19:35 +08:00
|
|
|
}
|
2009-06-01 10:59:15 +08:00
|
|
|
}
|
|
|
|
|
2015-04-21 03:27:09 +08:00
|
|
|
void UIContext::onGetActiveSite(Site* site) const
|
2009-06-01 10:59:15 +08:00
|
|
|
{
|
2018-07-15 10:24:49 +08:00
|
|
|
DocView* view = activeView();
|
2014-11-07 08:04:32 +08:00
|
|
|
if (view) {
|
2015-04-21 03:27:09 +08:00
|
|
|
view->getSite(site);
|
2016-06-14 01:50:43 +08:00
|
|
|
|
|
|
|
if (site->sprite()) {
|
|
|
|
// Selected layers
|
|
|
|
Timeline* timeline = App::instance()->timeline();
|
|
|
|
if (timeline &&
|
|
|
|
timeline->range().enabled()) {
|
2016-06-14 21:15:47 +08:00
|
|
|
switch (timeline->range().type()) {
|
2018-07-07 21:07:21 +08:00
|
|
|
case DocRange::kCels: site->focus(Site::InCels); break;
|
|
|
|
case DocRange::kFrames: site->focus(Site::InFrames); break;
|
|
|
|
case DocRange::kLayers: site->focus(Site::InLayers); break;
|
2016-06-14 21:15:47 +08:00
|
|
|
}
|
2016-06-14 01:50:43 +08:00
|
|
|
site->selectedLayers(timeline->selectedLayers());
|
2016-06-14 04:24:41 +08:00
|
|
|
site->selectedFrames(timeline->selectedFrames());
|
2016-06-14 01:50:43 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ColorBar* colorBar = ColorBar::instance();
|
|
|
|
if (colorBar &&
|
|
|
|
colorBar->getPaletteView()->getSelectedEntriesCount() > 0) {
|
2016-06-14 21:15:47 +08:00
|
|
|
site->focus(Site::InColorBar);
|
2016-06-14 01:50:43 +08:00
|
|
|
}
|
|
|
|
else {
|
2016-06-14 21:15:47 +08:00
|
|
|
site->focus(Site::InEditor);
|
2016-06-14 01:50:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-07 08:04:32 +08:00
|
|
|
}
|
2015-05-19 04:04:31 +08:00
|
|
|
else if (!isUIAvailable()) {
|
2018-05-07 11:11:50 +08:00
|
|
|
return app::Context::onGetActiveSite(site);
|
2014-11-07 08:04:32 +08:00
|
|
|
}
|
2009-06-01 10:59:15 +08:00
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|