From 44b2d060ee883de766ce36a7c1ef7a72fc92403c Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 25 Aug 2021 10:14:52 +0200
Subject: [PATCH] UI: Implement the close document for FakeVim

---
 src/Lib/AbstractDocument.cc       |  7 +++++++
 src/Lib/AbstractDocument.hh       |  2 ++
 src/UI/MainWindow.cc              | 18 +++++++++++++++++-
 src/UI/MainWindow.hh              |  3 +++
 src/UI/ScriptDocumentView.cc      |  8 ++++++--
 src/UI/ScriptViews/EditorProxy.cc |  2 +-
 6 files changed, 36 insertions(+), 4 deletions(-)
 create mode 100644 src/Lib/AbstractDocument.cc

diff --git a/src/Lib/AbstractDocument.cc b/src/Lib/AbstractDocument.cc
new file mode 100644
index 00000000..4cf89ed4
--- /dev/null
+++ b/src/Lib/AbstractDocument.cc
@@ -0,0 +1,7 @@
+#include "AbstractDocument.hh"
+
+bool
+Vivy::operator==(const AbstractDocument &a, const AbstractDocument &b) noexcept
+{
+    return a.getUuid() == b.getUuid();
+}
diff --git a/src/Lib/AbstractDocument.hh b/src/Lib/AbstractDocument.hh
index b561acfc..5a22f96a 100644
--- a/src/Lib/AbstractDocument.hh
+++ b/src/Lib/AbstractDocument.hh
@@ -66,6 +66,8 @@ public:
 signals:
     void documentChanged();
 };
+
+bool operator==(const AbstractDocument &a, const AbstractDocument &b) noexcept;
 }
 
 #endif // VIVY_ABSTRACT_DOCUMENT_H
diff --git a/src/UI/MainWindow.cc b/src/UI/MainWindow.cc
index b89262cd..fdd8b011 100644
--- a/src/UI/MainWindow.cc
+++ b/src/UI/MainWindow.cc
@@ -87,7 +87,8 @@ MainWindow::MainWindow() noexcept
     documents->setUsesScrollButtons(true);
     documents->setDocumentMode(true);
     documents->setTabBarAutoHide(true);
-    connect(documents, &QTabWidget::tabCloseRequested, this, &MainWindow::closeDocument);
+    connect(documents, &QTabWidget::tabCloseRequested, this,
+            [this](int index) noexcept -> void { closeDocument(index); });
     connect(documents, &QTabWidget::tabBarDoubleClicked, this, &MainWindow::openProperties);
     setCentralWidget(documents);
     centralWidget()->setContentsMargins(0, 0, 0, 0);
@@ -213,6 +214,21 @@ MainWindow::saveFileAs() noexcept
     }
 }
 
+void
+MainWindow::closeDocument(AbstractDocumentView *const view) noexcept
+{
+    if (view == nullptr || !view->getDocument())
+        return;
+
+    const int count = documents->count();
+    for (int index = 0; index < count; ++index) {
+        const AbstractDocumentView *const documentView = getTab(index);
+        const bool documentExists = documentView && documentView->getDocument();
+        if (documentExists && (*documentView->getDocument()) == (*view->getDocument()))
+            closeDocument(index);
+    }
+}
+
 void
 MainWindow::closeDocument(int index) noexcept
 {
diff --git a/src/UI/MainWindow.hh b/src/UI/MainWindow.hh
index 05d2292d..37d225a1 100644
--- a/src/UI/MainWindow.hh
+++ b/src/UI/MainWindow.hh
@@ -40,6 +40,9 @@ public:
         }
     }
 
+public slots:
+    void closeDocument(AbstractDocumentView *const) noexcept;
+
 private:
     void addTab(AbstractDocumentView *);
     AbstractDocumentView *getTab(const int) const noexcept;
diff --git a/src/UI/ScriptDocumentView.cc b/src/UI/ScriptDocumentView.cc
index 9fa80f5e..51f12c7b 100644
--- a/src/UI/ScriptDocumentView.cc
+++ b/src/UI/ScriptDocumentView.cc
@@ -25,9 +25,13 @@ ScriptDocumentView::ScriptDocumentView(std::shared_ptr<ScriptDocument> ptr, QWid
 
     // FakeVim
     {
-        handler = new FakeVimHandler(editor, this);
-        proxy   = EditorProxy::connectSignals(handler, editor);
+        MainWindow *const mw = vivyApp->getMainWindow();
+        handler              = new FakeVimHandler(editor, this);
+        proxy                = EditorProxy::connectSignals(handler, editor);
         connect(proxy, &EditorProxy::handleInput, handler, &FakeVimHandler::handleInput);
+        connect(proxy, &EditorProxy::requestQuit, this, [this, mw]() noexcept -> void {
+            mw->closeDocument(static_cast<AbstractDocumentView *>(this));
+        });
         // connect(proxy, &EditorProxy::requestSave, document, &AbstractDocument::save); // TODO
         // connect(proxy, &EditorProxy::requestSaveAndQuit, document, &AbstractDocument::save + &MainWindow::closeTab); // TODO
         // connect(proxy, &EditorProxy::requestQuit, document, &MainWindow::closeTab); // TODO
diff --git a/src/UI/ScriptViews/EditorProxy.cc b/src/UI/ScriptViews/EditorProxy.cc
index 0f297eca..a5934a3c 100644
--- a/src/UI/ScriptViews/EditorProxy.cc
+++ b/src/UI/ScriptViews/EditorProxy.cc
@@ -366,7 +366,7 @@ EditorProxy::cancel(const QString &fileName) noexcept
 void
 EditorProxy::invalidate() noexcept
 {
-    TODO(Use a Vivy thing here : MainWindow->closeTab(this) !)
+    emit requestQuit();
 }
 
 bool
-- 
GitLab