[aseprite] 90/134: Don't load a file if it's already open on drop files event

Tobias Hansen thansen at moszumanska.debian.org
Sat Mar 14 17:10:11 UTC 2015


This is an automated email from the git hooks/post-receive script.

thansen pushed a commit to branch master
in repository aseprite.

commit bc9f4b2c9b9b6f0ed5e76b43329df9e0a95ee435
Author: David Capello <davidcapello at gmail.com>
Date:   Sun Nov 30 21:06:29 2014 -0300

    Don't load a file if it's already open on drop files event
    
    This avoids to open a document two times on Mac OS X
    from the command line (the file is loaded when we
    process the command line, and by Finder, which send us
    an application:openFile: message).
---
 src/app/modules/gui.cpp | 22 ++++++++++++++++++----
 src/app/ui_context.cpp  | 18 ++++++++++++++++--
 src/app/ui_context.h    |  2 ++
 src/doc/documents.cpp   | 23 +++++++++++++++++------
 src/doc/documents.h     |  1 +
 5 files changed, 54 insertions(+), 12 deletions(-)

diff --git a/src/app/modules/gui.cpp b/src/app/modules/gui.cpp
index f6617f9..f3bfa50 100644
--- a/src/app/modules/gui.cpp
+++ b/src/app/modules/gui.cpp
@@ -435,10 +435,24 @@ bool CustomizedGuiManager::onProcessMessage(Message* msg)
           CommandsModule::instance()->getCommandByName(CommandId::OpenFile);
         Params params;
 
-        for (DropFilesMessage::Files::const_iterator
-               it = files.begin(); it != files.end(); ++it) {
-          params.set("filename", it->c_str());
-          UIContext::instance()->executeCommand(cmd_open_file, &params);
+        UIContext* ctx = UIContext::instance();
+
+        for (const auto& fn : files) {
+          // If the document is already open, select it.
+          Document* doc = static_cast<Document*>(ctx->documents().getByFileName(fn));
+          if (doc) {
+            DocumentView* docView = ctx->getFirstDocumentView(doc);
+            if (docView)
+              ctx->setActiveView(docView);
+            else {
+              ASSERT(false);    // Must be some DocumentView available
+            }
+          }
+          // Load the file
+          else {
+            params.set("filename", fn.c_str());
+            ctx->executeCommand(cmd_open_file, &params);
+          }
         }
       }
       break;
diff --git a/src/app/ui_context.cpp b/src/app/ui_context.cpp
index 13c46cd..630d306 100644
--- a/src/app/ui_context.cpp
+++ b/src/app/ui_context.cpp
@@ -125,8 +125,7 @@ size_t UIContext::countViewsOf(Document* document) const
   Workspace* workspace = App::instance()->getMainWindow()->getWorkspace();
   size_t counter = 0;
 
-  for (Workspace::iterator it=workspace->begin(); it != workspace->end(); ++it) {
-    WorkspaceView* view = *it;
+  for (auto view : *workspace) {
     if (DocumentView* docView = dynamic_cast<DocumentView*>(view)) {
       if (docView->getDocument() == document) {
         ++counter;
@@ -137,6 +136,21 @@ size_t UIContext::countViewsOf(Document* document) const
   return counter;
 }
 
+DocumentView* UIContext::getFirstDocumentView(Document* document) const
+{
+  Workspace* workspace = App::instance()->getMainWindow()->getWorkspace();
+
+  for (auto view : *workspace) {
+    if (DocumentView* docView = dynamic_cast<DocumentView*>(view)) {
+      if (docView->getDocument() == document) {
+        return docView;
+      }
+    }
+  }
+
+  return NULL;
+}
+
 Editor* UIContext::activeEditor()
 {
   DocumentView* view = activeView();
diff --git a/src/app/ui_context.h b/src/app/ui_context.h
index 5013c55..2ff543f 100644
--- a/src/app/ui_context.h
+++ b/src/app/ui_context.h
@@ -44,6 +44,8 @@ namespace app {
     // Returns the number of views that the given document has.
     size_t countViewsOf(Document* document) const;
 
+    DocumentView* getFirstDocumentView(Document* document) const;
+
     // Returns the current editor. It can be null.
     Editor* activeEditor();
 
diff --git a/src/doc/documents.cpp b/src/doc/documents.cpp
index 466748e..f167415 100644
--- a/src/doc/documents.cpp
+++ b/src/doc/documents.cpp
@@ -11,6 +11,7 @@
 #include "doc/documents.h"
 
 #include "base/mutex.h"
+#include "base/path.h"
 #include "doc/document.h"
 
 #include <algorithm>
@@ -87,18 +88,28 @@ void Documents::move(Document* doc, int index)
 
 Document* Documents::getById(ObjectId id) const
 {
-  for (const_iterator it = begin(), end = this->end(); it != end; ++it) {
-    if ((*it)->id() == id)
-      return *it;
+  for (const auto& doc : *this) {
+    if (doc->id() == id)
+      return doc;
   }
   return NULL;
 }
 
 Document* Documents::getByName(const std::string& name) const
 {
-  for (const_iterator it = begin(), end = this->end(); it != end; ++it) {
-    if ((*it)->name() == name)
-      return *it;
+  for (const auto& doc : *this) {
+    if (doc->name() == name)
+      return doc;
+  }
+  return NULL;
+}
+
+Document* Documents::getByFileName(const std::string& filename) const
+{
+  std::string fixfn = base::fix_path_separators(filename);
+  for (const auto& doc : *this) {
+    if (base::fix_path_separators(doc->filename()) == fixfn)
+      return doc;
   }
   return NULL;
 }
diff --git a/src/doc/documents.h b/src/doc/documents.h
index f589dd7..73cc168 100644
--- a/src/doc/documents.h
+++ b/src/doc/documents.h
@@ -56,6 +56,7 @@ namespace doc {
     Document* operator[](int index) const { return m_docs[index]; }
     Document* getById(ObjectId id) const;
     Document* getByName(const std::string& name) const;
+    Document* getByFileName(const std::string& filename) const;
 
   private:
     // Deletes all documents in the list (calling "delete" operation).

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/aseprite.git



More information about the Pkg-games-commits mailing list